Main game loop
Code:
void __fastcall TForm1::GameLoop(TObject*, bool &done)
{
int b,temp;
//checks if you've killed everything
if (dead_count == 21)
{
Application->MessageBoxA("You won!","Well done, you stopped the invasion", MB_OK);
Application->Terminate();
}
//Key presses are acted upon here
if (keys.right == true)
{
User->MoveRight(); //right key pressed, move right
}
if (keys.left == true)
{
User->MoveLeft(); //left key pressed, move left
}
if (keys.up == true)
{
User->MoveUp(); //up key pressed. move up
}
if (keys.down == true)
{
User->MoveDown(); // down key pressed. move down
}
// fires a regular laser shot
if (keys.laser == true)
{
//dynamicly allocates memory on the heap; User(the ship) is passed to the constructor
//so the x1 && y1 coords are the same as the users
laser.push_back(new User_Laser(User));
//keys.laser = false; //un-presses the space bar so we can't have insane firing speeds =)
}
// fires a regular missile shot
if (keys.missile == true)
{
//dynamicly allocates memory on the heap; User(the ship) is passed to the constructor
//so the x1 && y1 coords are the same as the users
missile.push_back(new User_missile(User));
keys.missile = false;//un-presses the control key so we can't have insane firing speeds =)
}
//deletes any unused lasers, then moves the rest
EraseObsoleteLasers(laser,Baddy);
EraseObsoleteMissiles(missile, Baddy);
//draws to the form. See the "Draw" file for further info
Draw(BackBuffer, User, Baddy, ClientRect, BackGround, laser, missile, Health, bdy_laser, bdyDraw, FirstMissile , FirstLaser);
done = false;
}
functions in header file,
Code:
#include <vector.h>
#include "fire.h"
#include "Unit1.h"
using namespace std;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void EraseObsoleteLasers(std::vector<User_Laser*>& laserVect, Enemy *Baddy[3][7])
{
std::vector<User_Laser*>::iterator currentLaser = laserVect.begin();
int b;
while (currentLaser < laserVect.end())
{
if ((*currentLaser) == 0 || (*currentLaser)->Obsolete)
{
User_Laser* laserToDelete = (*currentLaser);
currentLaser = laserVect.erase(currentLaser);
delete laserToDelete;
}
else
{
++b;
(*currentLaser)->regShot();
(*currentLaser)->Collision(Baddy, laserVect, b); //check for collision between the laser and *a* baddy
++currentLaser;
}
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void EraseObsoleteMissiles(std::vector<User_missile*>& missileVect, Enemy *Baddy[3][7])
{
std::vector<User_missile*>::iterator currentMissile = missileVect.begin();
int b;
while (currentMissile != missileVect.end())
{
if ((*currentMissile)->Obsolete)
currentMissile = missileVect.erase(currentMissile);
else
{
++b;
(*currentMissile)->regShot();
//check for collision between the laser and *a* baddy
(*currentMissile)->Collision(Baddy, missileVect, b);
++currentMissile;
}
}
}