main.cpp
#include "DarkGDK.h"
#include "SC_Collision_v3.h"
#include "setup.h"
#include "camera.h"
#include "player.h"
#include "collision.h"
#include "enemies.h"
void dbSetShadowShadingOn ( int iID, int iMesh, int iRange, int iUseShader );
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
//dbSetWindowOff();
dbSetAmbientLight(30);
SC_Start();
float camY;
initial();
dbSetShadowShadingOn ( 2, -1, 1000.0f, 1 );
position_camera();
position_player();
setup_objects();
//create_enemies();
for (int i=0;i<20;i++)
{
dbMakeObjectCube(i,7);
dbPositionObject(i,dbRND(100),25,dbRND(100));
}
while ( LoopGDK ( ) )
{
camY=(dbCameraAngleY()*dbCameraAngleY())/2;
//enemy_ai();
mouse_control();
move_camera();
update_camera();
test_collision();
create_collision();
SC_UpdateObject(2);
SC_UpdateObject(1);
dbText(0,100,dbStr(dbScanCode()));
char str [ 128 ];
sprintf_s( str, 128, "mouse X: %d", dbMouseX() ); dbText( 0,60,str );
sprintf_s( str, 128, "mouse Y: %d", dbMouseY() ); dbText( 0,80,str );
sprintf_s( str, 128, "w pressed: %d", dbKeyState(17) ); dbText( 0,100,str );
sprintf_s( str, 128, "colliding: %d", collide ); dbText( 0,120,str );
sprintf_s( str, 128, "on ground: %d", ground ); dbText( 0,150,str );
// update the screen
dbSync ( );
}
// return back to windows
return;
}
setup.h
int fCameraAngleY = 1.0f;
int fCameraAngleX=1.0f;
int a=0;
void initial()
{
dbLoadObject("Media//Levels//Room1.x",1);
dbPositionObject(1,0,0,0);
dbScaleObject(1,20,20,20);
dbLoadObject("Media//player//Robot3redone.x",2);
dbRotateObject(2,dbObjectAngleX(2)+90,0,0);
}
void mouse_control()
{
/*fCameraAngleY = dbWrapValue ( fCameraAngleY + dbMouseMoveY ( ) * 0.5f );
fCameraAngleY = dbWrapValue ( fCameraAngleY + dbMouseMoveX ( ) * 0.5f );
dbYRotateCamera ( fCameraAngleY );
dbYRotateObject (2, fCameraAngleY ); */
/*if (dbMouseClick()==1)
{
dbYRotateObject(2, dbMouseY());
}*/
}
canera.h
float camX=dbCameraPositionX ( );
float camY=dbCameraPositionY ( );
float camZ=dbCameraPositionZ ( );
void position_camera()
{
dbXRotateCamera ( 60 );
}
void update_camera()
{
//dbPositionCamera(camX,camY+70,camZ+(-10));
dbPositionCamera(dbObjectPositionX(2),dbObjectPositionY(2)+180,dbObjectPositionZ(2)-50);
}
void move_camera()
{
if ( dbKeyState( 17 ) == 1 )//w
{
dbMoveObjectUp(2,0.8);
camZ=camZ+0.4;
}
if ( dbKeyState( 31 ) == 1 )//s
{
dbMoveObjectUp(2,-0.8);
camZ=camZ-0.4;
}
if ( dbKeyState( 32 ) == 1 )//d
{
//dbMoveObjectRight(2,0.8);
dbYRotateObject(2,dbObjectAngleY(2)+2);
camX=camX+0.4;
}
if ( dbKeyState( 30 ) == 1 )//a
{
//dbMoveObjectLeft(2,0.8);
dbYRotateObject(2,dbObjectAngleY(2)-2);
camX=camX-0.4;
}
if (dbKeyState(17)==1 && dbKeyState(32) ==1 && dbCameraAngleY() < 30 )
{
dbYRotateCamera(dbCameraAngleY()+0.6);
}
if (dbKeyState(17)==1 && dbKeyState(30) ==1 && dbCameraAngleY() > -30 )
{
dbYRotateCamera(dbCameraAngleY()-0.6);
}
if ((dbScanCode()==17 || dbScanCode()==31) && dbObjectPlaying(2)==false)
{
dbPlayObject(2);
dbLoopObject(2);
}
if ((dbScanCode()!=17 && dbScanCode() !=31) && dbObjectPlaying(2)==true)
{
dbStopObject(2);
}
}
player.h
int PlayerX = dbObjectPositionX(2);
int PlayerY = dbObjectPositionY(2);
int PlayerZ = dbObjectPositionZ(2);
void position_player()
{
dbPositionObject(2,PlayerX,0,PlayerZ);
}
collision.h
int collide ;
float vx = 0;
float vy = 0;
float vz = 0;
float gravity = -0.1f;
float slope = 0.5f;
int ground = 1;
int jumptimer = 0;
float player_radius = 10.0f;
int PlayerSpeedForward = 0/*10*/;
int PlayerSpeedRight = 0/*10*/;
int PlayerSpeedLeft = 0/*(-10)*/;
int PlayerSpeedBack = 0/*(-10)*/;
void setup_objects()
{
SC_SetupObject(1,1,0);
SC_SetupObject(2,2,0);
}
void test_collision()
{
if (SC_ObjectCollision(1,2)==1)
{
//collide = 1;
}
else
{
//collide = 0;
}
}
void create_collision()
{
float oldx = dbObjectPositionX(2);
float oldy = dbObjectPositionY(2);
float oldz = dbObjectPositionZ(2);
//apply gravity, and user changes to movement
float angy = dbObjectAngleY(2);
vx = 0;
vz = 0;
//if player is jumping or falling then apply e'normal' gravity
//if not attempt to keep the player stuck to the floor
if ( vy == 0 && jumptimer == 0 ) vy = vy + 10*gravity;
else vy = vy + gravity;
if (dbKeyState(32) == 1 ) {
/*vx = vx + dbCos(angy)+PlayerSpeedRight; vz = vz - dbSin(angy);*/ }
if (dbKeyState(30) == 1 )
{ //left -
/*vx = vx - dbCos(angy)+PlayerSpeedLeft; vz = vz + dbSin(angy);*/ }
if (dbKeyState(31) == 1 ) {
// back -
/*vx = vx - dbSin(angy); vz = vz - dbCos(angy)+PlayerSpeedBack;*/ }
if (dbKeyState(17) == 1 ) { //forward
/*vx = vx + dbSin(angy); vz = vz + dbCos(angy)+PlayerSpeedForward;*/ }
//right = +10 x
//left = -10 x
//back = -10 y
//forward = +10 y
//only jump if on ground, and a certain time after last jump
if ( ground == 1 )
{
if ( dbSpaceKey() == 1 && jumptimer == 0 )
{
vy = vy + 3.0f;
jumptimer = 20;
}
}
//this would be the player's final position without collision
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
collide = SC_SphereCastGroup( 1, oldx,oldy,oldz, oldx,oldy+vy,oldz, player_radius,0 );
if ( collide > 0 )
{
//how flat is this ground
float ny = SC_GetCollisionNormalY();
if ( dbAbs(ny) > slope )
{
//FLAT, stick
oldy = SC_GetStaticCollisionY();
}
else
{
//STEEP, slide
x = x - oldx; z = z - oldz;
oldx = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
oldz = SC_GetCollisionSlideZ();
x = x + oldx; z = z + oldz;
}
//ny#<0 means the player has hit a ceiling rather than a floor
if ( ny > slope )
{
//only on ground if standing on flat ground
ground = 1;
vy = 0;
}
else
{
ground = 0;
//if player has hit a flat ceiling then stop vy# movement
if ( ny < -slope ) vy = gravity;
}
}
else
{
//nothing below player, not on ground, add vertical speed to player
oldy = oldy + vy;
ground = 0;
}
if ( ground == 1 && jumptimer > 0 ) jumptimer--;
collide = SC_SphereSlideGroup( 1, oldx,oldy,oldz, x,oldy,z, player_radius,0 );
if ( collide > 0 )
{
x = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
z = SC_GetCollisionSlideZ();
vx = 0;
vz = 0;
}
dbPositionObject(2,x,oldy,z );
}
I know it's a bit messy just now..