Here, this might help you. I am in the early stages of an RPG, wrote this up last night. Sorry, didn't comment very much, but the code is fairly straitforward. The code is also pretty sloppy, was rapidly prototyping the engine.
Anyways, enough talk, do ahead and download it and feel free to ask any questions.
EDIT - WASD to move, hold-rightclick to rotate/raise/lower camera using mouse. It's pretty intuitive.
Source Files:
classes.h
/*
classes.h - description here
*/
// Definitions
#define PLAYER 1
int FREEOBJECT = 2;
struct vector2 // 2d vector
{
float x, y;
};
struct vector3 // 3d vector
{
float x, y, z;
};
struct vector4 //4d vector
{
float x, y, z, v;
};
int findFree( )
{
FREEOBJECT++;
return FREEOBJECT;
}
class player
{
public:
int spawn( float fX, float fY, float fZ )
{
// spawn player and set basic attributes
spawned = true;
height = 5;
position.x = fX;
position.y = fY;
position.z = fZ;
// make player model
// dbLoadObject( "media/3d/characters/dwarf1.x", PLAYER );
// dbScaleObject( PLAYER, 35, 35, 35 );
dbMakeObjectCube( PLAYER, 1 );
dbPositionObject( PLAYER, position.x, position.y + 0.5, position.z );
return 0;
}
int rotate( float fX, float fY, float fZ )
{
angle.x = fX;
angle.y = fY;
angle.z = fZ;
dbRotateObject( PLAYER, angle.x, angle.y, angle.z );
return 0;
}
int control( )
{
// show mouse
dbShowMouse( );
// reset animation
animstate = 'z';
// rightclick
if( dbMouseClick( ) == 2 || dbMouseClick( ) == 3 )
{
// rotate player
// rotate( 0, dbWrapValue( angle.y + ( dbMouseMoveX( ) / 2 ) ), 0 );
if( dbMouseX( ) > oldMouseX )
angle.y = angle.y + 8;
if( dbMouseX( ) < oldMouseX )
angle.y = angle.y - 8;
oldMouseX = dbMouseX( );
// set camera height
if( height <= 10 && height >= 0 )
{
if( dbMouseY( ) > oldMouseY )
height = height + .8;
if( dbMouseY( ) < oldMouseY )
height = height - .8;
}
oldMouseY = dbMouseY( );
if( height <= 0 )
height = .01;
if( height >= 10 )
height = 9.99;
if( dbKeyState( 30 ) )
{
animstate = 'b';
position.x = dbNewXValue( position.x, angle.y - 90, .1 );
position.z = dbNewZValue( position.z, angle.y - 90, .1 );
}
if( dbKeyState( 32 ) )
{
animstate = 'b';
position.x = dbNewXValue( position.x, angle.y + 90, .1 );
position.z = dbNewZValue( position.z, angle.y + 90, .1 );
}
}
// control forward/backwards
if( dbKeyState( 17 ) )
{
animstate = 'a';
position.x = dbNewXValue( position.x, angle.y, .2 );
position.z = dbNewZValue( position.z, angle.y, .2 );
}
if( dbKeyState( 31 ) )
{
animstate = 'b';
position.x = dbNewXValue( position.x, angle.y, -.1 );
position.z = dbNewZValue( position.z, angle.y, -.1 );
}
// rotate
if( dbMouseClick( ) != 2 )
{
if( dbKeyState( 30 ) )
{
animstate = 'b';
angle.y = dbWrapValue( angle.y - 5 );
}
if( dbKeyState( 32 ) )
{
animstate = 'b';
angle.y = dbWrapValue( angle.y + 5 );
}
}
// move, rotate, and animate player
dbPositionObject( PLAYER, position.x, position.y + 0.5, position.z );
dbRotateObject( PLAYER, angle.x, angle.y, angle.z );
animate( );
// success!
return 0;
}
int animate( )
{
switch( animstate )
{
case 'a':
dbSetObjectSpeed( PLAYER, 80 );
dbLoopObject( PLAYER, 1, 13 );
oldanimstate = animstate;
return 0;
case 'b':
dbSetObjectSpeed( PLAYER, 40 );
dbLoopObject( PLAYER, 1, 13 );
oldanimstate = animstate;
return 0;
default:
dbSetObjectSpeed( PLAYER, 40 );
dbLoopObject( PLAYER, 292, 360 );
oldanimstate = animstate;
return 0;
}
}
int camera( )
{
dbSetCameraToFollow( position.x, position.y, position.z, angle.y, 8, height, 6, 0 );
dbPointCamera( position.x, position.y, position.z );
return 0;
}
bool despawn( )
{
// despawn player/delete model
spawned = false;
dbDeleteObject( PLAYER );
return spawned;
}
bool isSpawned( )
{
return spawned;
}
private:
// general stuff
bool spawned;
// camera/movement stuff
int mouseX;
int mouseY;
float oldMouseY;
float oldMouseX;
float height;
// animations
char animstate;
char oldanimstate;
// vector 3s
vector3 position;
vector3 angle;
// game information
int pyrolvl;
int hydrolvl;
int terralvl;
int zephyrlvl;
};
class object
{
public:
int spawn( int fType, float fX, float fY, float fZ )
{
// assign number
int num = findFree( );
// spawn whatever type the object is
switch( fType )
{
case '1':
dbLoadObject( "media/3d/objects/barrel.x", num );
dbScaleObject( num, 2, 2, 2 );
dbPositionObject( num, fX, fY + 1, fZ );
return 0;
case '2':
dbLoadObject( "media/3d/objects/chest.x", num );
dbScaleObject( num, 4, 4, 4 );
dbPositionObject( num, fX, fY + 0.3, fZ );
return 0;
case '100':
dbLoadObject( "media/3d/objects/Item Shop.x", num );
dbScaleObject( num, 30, 30, 30 );
dbPositionObject( num, fX, fY, fZ );
return 0;
case '101':
dbLoadObject( "media/3d/objects/Armour_ Shop.x", num );
dbScaleObject( num, 30, 30, 30 );
dbPositionObject( num, fX, fY, fZ );
return 0;
default:
dbMakeObjectCube( num, 1 );
dbPositionObject( num, fX, fY + 0.5, fZ );
return 0;
}
}
private:
// position stuff
vector3 position;
vector3 angle;
};
class monster
{
public:
int spawn( int fType, float fX, float fY, float fZ )
{
// assign number
int num = findFree( );
// load enemy type
switch( fType )
{
case '1': // mean dwarf!
dbLoadObject( "media/3d/characters/dwarf2.x", num );
dbScaleObject( num, 35, 35, 35 );
dbPositionObject( num, fX, fY, fZ );
return 0;
default:
dbMakeObjectCube( num, 1 );
dbPositionObject( num, fX, fY + 0.5, fZ );
return 0;
}
}
private:
// game info stuff
bool hostile;
// position crap
vector3 position;
vector3 angle;
};
main.cpp
/*
Element RPG
by Zach Turnlund
05 - 26 - 2008
*/
// DarkGDK header
#include "DarkGDK.h"
// Other headers
#include "classes.h"
void DarkGDK ( void )
{
// setup sync and video
dbSyncOn ( );
dbSyncRate ( 30 );
dbSetDisplayMode( 800, 600, 32 );
dbColorBackdrop( 0 );
dbSetAmbientLight( 100 );
// test matrix
dbMakeMatrix( 1, 10, 10, 10, 10 );
// create test player
player mainPlayer;
mainPlayer.spawn( 5, 0, 5 );
// main loop
while ( LoopGDK ( ) )
{
if( mainPlayer.isSpawned( ) == true )
{
// control player
mainPlayer.control( );
mainPlayer.camera( );
// break spawn
if( dbKeyState( 18 ) )
mainPlayer.despawn( );
}
// syncs it up!
dbSync ( );
}
// return back to windows
return;
}
Tux is my guildmaster.