I created a Shooting_Bullet class to work with my main:
Shooting.h:
#pragma once
#ifndef SHOOTING_H
#define SHOOTING_H
class Shooting_Bullets // The Bullet Class
{
public:
int ReloadTimer; // This is the timer alowing the player to shoot
int CurrentBullet; // This is the bullet cycle
int BulletID;
int Object, Life; // The Bullets Object and its Life Timer
bool Exist; // This checks if the bullet is active to run its code
Shooting_Bullets(int objID, char* BulletName);
Shooting_Bullets(void); // Default constructor
~Shooting_Bullets();
Shooting_Bullets(const Shooting_Bullets& copy);
void Shoot();
void Kill();
void Run();
};
void ShootBullet();
void RunBullets();
#endif
Shooting.cpp :
#include "Main.h"
#include "DarkGDK.h"
#include "SC_Collision.h"
#include "Shooting.h"
#include "Player.h"
#define HitObjects 20 // All Objects that the bullet would collide with must be bigger than this
#define BulletObjects 1000 // This is the range from where the code looks for free slots to create new bullets
#define MaxBullets 30 // This is the Max number of bullets to cycle, this effects memory
#define ShootDelay 10 // This is the amount of time between shooting
#define BulletSpeed 10 // This is the speed the bullet travels
#define BulletLife 1000 // The bullet will be killed after this time to prevent it from moving around aimlesly
Shooting_Bullets :: Shooting_Bullets(int objID, char * BulletName)
{
dbLoadObject(BulletName,objID);
objID=BulletID;
}
Shooting_Bullets :: Shooting_Bullets()
{
}
Shooting_Bullets :: ~Shooting_Bullets()
{
dbDeleteObject(BulletID);
}
Shooting_Bullets::Shooting_Bullets(const Shooting_Bullets& copy)
{
this->BulletID = copy.BulletID;
this->Exist = copy.Exist;
this->CurrentBullet= copy.CurrentBullet;
//this->Kill = copy.Kill();
this->Life = copy.Life;
this->Object = copy.Object;
this->ReloadTimer = copy.ReloadTimer;
//this->Run = copy.Run();
//this->Shoot = copy.Shoot();
//this->~Shooting_Bullets = copy.~Shooting_Bullets();
}
void Shooting_Bullets :: Shoot()
{
Object = BulletObjects; // Sets the specific object range
while (dbObjectExist(Object)) Object++; // Looks for an open slot
dbMakeObjectSphere(Object,20); // This makes the bullet
//dbLoadObject
dbPositionObject(Object, dbCameraPositionX(), dbCameraPositionY(), dbCameraPositionZ()); //Sets the Position
dbSetObjectToCameraOrientation (Object); //Sets the orientation
Exist = true; // Now it exists
Life = BulletLife; //Sets its life Span
}
void Shooting_Bullets :: Kill() // Destroys the bullet
{
dbDeleteObject(Object); //Delete the object
Exist = false; // Now it no longer exists
}
void Shooting_Bullets :: Run() // The Bullet's Run code
{
if (Exist == true) // It wil only run if it exists
{
Life--; // Life Span Timer
if (Life <= 0) Kill(); // Destroys it when life is over
dbMoveObject(Object, BulletSpeed); // Moves it at the traveling speed
int Collide = dbObjectCollision(Object,0); // Checks if it hits anything
if (Collide > HitObjects)
{
Kill(); // Destroys it when it hits a wall
}
}
}
int ReloadTimer; // This is the timer alowing the player to shoot
int CurrentBullet; // This is the bullet cycle
Shooting_Bullets Bullet[MaxBullets];// Makes the actual bullet array
void RunBullets() // This will run all the bullets
{
ReloadTimer--; // Counts down the player usage timer
for (int runner = 0; runner < MaxBullets; runner++)
{
Bullet[runner]Run();
}
}
void ShootBullet()
{
if (ReloadTimer <=0)
{
if (CurrentBullet >= MaxBullets) CurrentBullet = 0; // Makes sure that we stay in the cycle
Bullet[CurrentBullet].Shoot(); //Shoots it
CurrentBullet++; //Next Bullet
ReloadTimer = ShootDelay; // Resets Timer
}
}
main.cpp:
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbAutoCamOff(); // Very important
dbMakeObjectBox(120,100,200,100); // Makes a thing to shoot, id is bigger than HitObjects so it will stop bullets
dbPositionObject(120,0,0,500);
while ( LoopGDK ( ) )
{
dbControlCameraUsingArrowKeys ( 0, 2.0f, 2.0f ); // Some Camera Movement
/*RunBullets(); */// Our bullet runcode
if ( dbMouseClick() == 1 )
{
ShootBullet();
for( int i=10002; i <= 10040; i++ )
{
dbMakeObjectPlain( i,3,3 );
//texture object i,7
dbSetObjectLight( i,0 );
dbHideObject( i );
}
int counter = 10002;
int igroup = 0;
char group [ 32 ] = "all groups";
int collide = 0;
int toggletext = 0;
//get our collision vector
float oldx = dbCameraPositionX();
float oldy = dbCameraPositionY();
float oldz = dbCameraPositionZ();
dbMoveCamera( 2000 );
float x = dbCameraPositionX();
float y = dbCameraPositionY();
float z = dbCameraPositionZ();
dbMoveCamera( -2000 );
collide = SC_RayCastGroup( igroup, oldx,oldy,oldz, x,y,z, 0 );
if ( collide > 0 )
{
//get the collision point
float newx = SC_GetStaticCollisionX() ;
float newy = SC_GetStaticCollisionY();
float newz = SC_GetStaticCollisionZ();
//get collision normal
float normx = SC_GetCollisionNormalX();
float normy = SC_GetCollisionNormalY();
float normz = SC_GetCollisionNormalZ();
//position and point a marker in the right direction
dbPositionObject( counter, newx + normx/10.0f, newy + normy/10.0f, newz + normz/10.0f );
dbPointObject( counter, newx + normx, newy + normy, newz + normz );
dbShowObject( counter );
counter++;
if ( counter > 10040 ) counter = 10002;
}
}
/////////////
char str [ 128 ];
sprintf_s( str, 128, "FPS: %d", dbScreenFPS( ) ); dbText( 0,80,str );
sprintf_s( str, 128, "Touching Ground: %d", ground ); dbText( 0,100,str );
}
}
dbSync ( );
}
The bullets appear, but disappear on the second without moving or going anywhere, is something stopping them?