Ok I am new here and to GDK in general, i studied C++ for a year so i have a little knowledge and understanding of the code.
So far i have the skeleton for my game using some graphics bought from this site(support the developers!)i will add buildings and vehicals once i have got the basics done.
Now onto what's got me, a health bar for the player and a ammo counter, so yeah any help on these 2 subjects would be great and any idea's on improving my code also would be great, i am always looking to learn thanks for your time....
#include "DarkGDK.h"
//include string functionallity
#include <string.h>
//Constant Declarations
//Matrix Constants
const int TheGround = 1;
const int width = 5000;
const int depth = 5000;
//Images
const int grass = 1;
const int clouds = 2;
const int RedCross = 3;
//Object Constants
const int crosshair = 1;
const int sky = 2;
const int gun = 3;
const int healthbar = 4;
const int enemy = 6;
const int PlayerBullet = 20;
//animation constants
const int IdleStart = 0;
const int IdleStop = 24;
const int ImpactStart = 25;
const int ImpactStop = 34;
const int AttackStart = 35;
const int AttackStop = 59;
const int DieStart = 60;
const int DieStop = 109;
//sound constants
const int PlayerGun = 1;
const int BodyHit = 2;
const int scream = 3;
//bullet constants
const int BulletStep = 20;
const int BulletRange = 1000;
//Function Prototypes
void SetUpScreen();
void LoadImages();
void CreateMatrix();
void LoadObjects();
void LoadSounds();
void MoveCamera(float &,float &, float, float &, float &);
void DisplayEnemyHealth(int);
void ControlPlayerBullet(int &, int &, int &, int &, int &);
void CheckForHit(int &, int &, int &, int &);
void ResetPlayerBullet(int &, int &);
// the main entry point for the application is this function
void DarkGDK ( void )
{
//declare camera movement variables
float CamXAng = 0, CamYAng = 0, CamZAng = 0;
float xPos = 2200, zPos = 2000;
//player health variable
int health = 100;
//declare player bullet variables
int BulletLive = 0;
int ObjectHit = 0;
int DistanceTravelled = 0;
int ammo = 200;
int EnemyHealth = 5;
int EnemyFacing = 0;
//call our user defined functions
SetUpScreen();
LoadImages();
CreateMatrix();
LoadObjects();
LoadSounds();
//position camera in the center of the landscape
dbPositionCamera(xPos,25,zPos);
dbPointCamera(2300,25,12300);
// our main loop
while ( LoopGDK ( ) )
{
//get new camera position
MoveCamera(CamXAng,CamYAng,CamZAng,xPos,zPos);
//slowly rotate the sky
dbYRotateObject(sky,dbObjectAngleY(sky)+0.05);
DisplayEnemyHealth(EnemyHealth);
ControlPlayerBullet(ammo, BulletLive, DistanceTravelled, ObjectHit, EnemyHealth);
//if enemy alive and not playing animation, play idle animation
if(EnemyHealth>0 && dbObjectPlaying(enemy)==0)
{
dbSetObjectSpeed(enemy,10);
dbLoopObject(enemy,IdleStart,IdleStop);
}//end if
//update the screen
dbSync();
}//end while
// and now everything is ready to return back to Windows
return;
}
//===============================
//* Function Declarations *
//===============================
void SetUpScreen()
{
//turn on sync rate and set maximum to 60 fps
dbSetDisplayMode(800,600,32);
dbSetWindowPosition(50,50);
dbHideMouse();
dbSetTextFont("Tahoma");
dbSetTextSize(20);
dbInk(dbRGB(255,255,255),0);
//display loading message
dbCenterText(dbScreenWidth()/2,dbScreenHeight()/2, " Loading Scene....");
dbSync();
dbSyncOn();
dbSyncRate(60);
dbSetTextSize(16);
}//end function set up screen
void LoadImages()
{
dbLoadImage("grass08.bmp",grass);
dbLoadImage("Bsky_2.bmp",clouds);
dbLoadImage("CrossHair.bmp",RedCross);
}//end function load images
void LoadObjects()
{
//create, texture, position and rotate sky
dbMakeObjectSphere(sky,-5500);
dbPositionObject(sky,2500,-10,2500);
dbTextureObject(sky,clouds);
dbXRotateObject(sky,180);
dbYRotateObject(sky,180);
dbFixObjectPivot(sky);
//turn ambient lighting off for sky
dbSetObjectAmbient(sky,0);
//create and texture a plain object for the crosshair
dbMakeObjectPlain(crosshair,10,10);
dbPositionObject(crosshair,0,0,100);
dbTextureObject(crosshair,RedCross);
//keep the crosshair locked to the screen
dbLockObjectOn(crosshair);
//make the crosshair transparent - black pixels not drawn
dbSetObjectTransparency(crosshair,1);
//load,scale and position lock on gun object
dbLoadObject("mp5\\H-mp5-static.x",gun);
dbScaleObject(gun,5000,5000,5000);
dbPositionObject(gun,10,-13,20);
dbLockObjectOn(gun);
//load,scale and position enemy object
dbLoadObject("thug\\L-thug-Idle.x",enemy);
dbAppendObject("thug\\L-thug-Impact.x",enemy,ImpactStart);
dbAppendObject("thug\\L-thug-Attack1.x",enemy,AttackStart);
dbAppendObject("thug\\L-thug-Die.x",enemy,DieStart);
dbPositionObject(enemy,2225,0,2500);
//scale thug to 17* its original size
dbScaleObject(enemy,1700,1700,1700);
//set the animation play speed
dbSetObjectSpeed(enemy,10);
dbLoopObject(enemy,IdleStart,IdleStop);
//make bullet object
dbMakeObjectSphere(PlayerBullet,1);
dbColorObject(PlayerBullet,dbRGB(0,0,0));
dbHideObject(PlayerBullet);
//ambient light affects the amount of shadowing on 3d objects
dbSetAmbientLight(30);
//set initial camera position
dbSetCameraRange(1,6000);
}//end function load objects
void CreateMatrix()
{
//make matrix 5000*5000 pixels and 20*20 tiles
dbMakeMatrix(TheGround,width,depth,20,20);
//texture with grass
dbPrepareMatrixTexture(TheGround,grass,1,1);
dbSetMatrix(TheGround,0,0,1,2,1,0,1);
dbPositionMatrix(TheGround,0,0,0);
dbUpdateMatrix(TheGround);
}//end function create matrix
void MoveCamera( float &CamXAng, float &CamYAng, float CamZAng, float &xPos, float &zPos)
{
//declare char pointer to hold player input from dbInKey()
char * pKeyPress;
//fix mouse pointer to center of screen
dbPositionMouse(dbScreenWidth()/2,dbScreenHeight()/2);
//use the mouse to alter the camera angles, divide by 3 to decrease movement
CamXAng = CamXAng+(dbMouseMoveY()/3);
CamYAng = CamYAng+(dbMouseMoveX()/3);
//restrict "Head" movement to +/- 45 degrees
if(CamXAng>0 && CamXAng>45)
{
CamXAng = 45;
}//end if
if(CamXAng<0 && CamXAng<-45)
{
CamXAng = -45;
}//end if
//rotate camera to new position
dbRotateCamera(CamXAng,CamYAng,CamZAng);
//use dbInKey() to get which key user has pressed
pKeyPress = dbInKey();
//move forward if "w" pressed
if(strcmp(pKeyPress,"w")==0)
{
xPos = dbNewXValue(xPos,CamYAng,1);
zPos = dbNewZValue(zPos,CamYAng,1);
}//end if
//move backward if "s" pressed
if(strcmp(pKeyPress,"s")==0)
{
xPos = dbNewXValue(xPos,CamYAng,-1);
zPos = dbNewZValue(zPos,CamYAng,-1);
}//end if
//straffe (move 90 degrees) left if "a" is pressed
if(strcmp(pKeyPress,"a")==0)
{
//get new x and z positions based on y angle
xPos = dbNewXValue(xPos,CamYAng-90,0.5);
zPos = dbNewZValue(zPos,CamYAng-90,0.5);
}//end if
//straffe (move 90 degrees) right if "d" is pressed
if(strcmp(pKeyPress,"d")==0)
{
//get new x and z positions based on y angle
xPos = dbNewXValue(xPos,CamYAng+90,0.5);
zPos = dbNewZValue(zPos,CamYAng+90,0.5);
}//end if
//use updated x and z values to position camera
dbPositionCamera(xPos,25,zPos);
}//end function move camera
void LoadSounds()
{
dbLoadSound("Sounds\\machinegun.wav",PlayerGun);
dbSetSoundVolume(PlayerGun,85);
dbLoadSound("Sounds\\uh.wav",BodyHit);
dbSetSoundVolume(BodyHit,96);
dbLoadSound("Sounds\\scream1.wav",scream);
dbSetSoundVolume(scream,80);
}//end function load sounds
void DisplayEnemyHealth(int EnemyHealth)
{
char EnemyHealthStr[30]="Enemy Health: ";
strcat(EnemyHealthStr,dbStr(EnemyHealth));
dbInk(dbRGB(255,255,255),0);
dbText(50,20,EnemyHealthStr);
}//end function display enemy health
void ControlPlayerBullet(int &ammo, int &BulletLive, int &DistanceTravelled, int &ObjectHit, int &EnemyHealth)
{
if(ammo>0) //if player has ammo
{
//if left mouse click and no live bullet position and rotate bullet object to camera
//set bullet live to 1 and dec ammo by 1
if(dbMouseClick()==1 && BulletLive==0)
{
dbPositionObject(PlayerBullet,dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ());
dbSetObjectToCameraOrientation(PlayerBullet);
dbShowObject(PlayerBullet);
BulletLive = 1;
ammo -= 1;
if(dbSoundPlaying(PlayerGun)==0);
{//if gun fire sound is not playing then play it
dbPlaySound(PlayerGun);
}//end if
}//end if
if(BulletLive==1)
{//if bullet alive move 29 units
DistanceTravelled += BulletStep;
if(DistanceTravelled<BulletRange)
{
dbMoveObject(PlayerBullet,BulletStep);
CheckForHit(ObjectHit, EnemyHealth, DistanceTravelled, BulletLive);
}
else
{
ResetPlayerBullet(BulletLive, DistanceTravelled);
ObjectHit = 0;
}//end else
}//end if
}//end if
else
{
dbText(50,40,"No ammo left");
}//end else
}//end function control player bullet
void CheckForHit(int &ObjectHit, int &EnemyHealth, int &DistanceTravelled, int &BulletLive)
{
if(dbObjectPositionY(PlayerBullet)<0)
{//if bullet "hits" the ground
ResetPlayerBullet(BulletLive, DistanceTravelled);
ObjectHit = 1;
}//end if
if(dbObjectCollision(PlayerBullet,0)>2)
{// if any object hit by bullet
ObjectHit = dbObjectCollision(PlayerBullet,0);
if(ObjectHit==enemy && EnemyHealth>0)
{//if object hit is enemy and has been hit <5 times
//play impact animation and sound, decrement health
dbPlayObject(enemy, ImpactStart, ImpactStop);
dbPlaySound(BodyHit);
EnemyHealth -= 1;
if(EnemyHealth<=0)
{// if enemy has no health remaining, play die animation and sound
dbSetObjectSpeed(enemy,55);
dbPlayObject(enemy, DieStart, DieStop);
dbPlaySound(scream);
}//end if
}//end if
ResetPlayerBullet(BulletLive, DistanceTravelled);
}//end if
}//end function check for hit
void ResetPlayerBullet(int &BulletLive, int &DistanceTravelled)
{
dbHideObject(PlayerBullet);
BulletLive = 0;
DistanceTravelled = 0;
}//end function reset player bullet