This is the kind of thing I would do; Just paste it and try it, you can move around and use space to shoot:
#include "DarkGDK.h"
#define HitObjects 100 // 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 100 // 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
int ReloadTimer; // This is the timer alowing the player to shoot
int CurrentBullet; // This is the bullet cycle
class cBullet // The Bullet Class
{
public:
int Object, Life; // The Bullets Object and its Life Timer
bool Exist; // This checks if the bullet is active to run its code
void Shoot()
{
Object = BulletObjects; // Sets the specific object range
while (dbObjectExist(Object)) Object++; // Looks for an open slot
dbMakeObjectSphere(Object,20); // This makes the bullet
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 Kill() // Destroys the bullet
{
dbDeleteObject(Object); //Delete the object
Exist = false; // Now it no longer exists
}
void 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
}
}
};
cBullet 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
}
}
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 (dbSpaceKey() == 1) ShootBullet(); // SpaceBar will shoot a bullet;
dbSync ( );
}
}