#include <CGame.h>
Collaboration diagram for CGame:

Public Types | |
| enum | TGameBonus { EBonusLife, EBonusPowerup } |
| Types of bonus that can be collected. More... | |
Public Methods | |
| ~CGame () | |
| closes the status screen and delete member objects. More... | |
| void | Play () |
| Main game loop. More... | |
| void | AddBadGuy (TInt16 aType, const TInt16 aArgs[]) |
| Add a bad guy to the bad guy array. More... | |
| void | AddBadGuy (const TBadGuy &aBadGuy) |
| Add a bad guy to the bad guy array. More... | |
| void | Bonus (TGameBonus aBonus) |
| Collect bonuses. More... | |
| void | AddScore (TInt aScore) |
| Give the player some points. More... | |
| void | DamageBadGuys (TInt16 aId, TInt16 aHealth) |
| Damage bad guys with correct id. More... | |
| TInt16 | FirstExplosionSprite () |
| First bad guy explosion sprite. More... | |
| TInt16 | LastExplosionSprite () |
| Last bad guy explosion sprite. More... | |
| TInt16 | MapSpeed () |
| Speed map is scrolling at. More... | |
Static Public Methods | |
| CGame * | NewL (TGameData &aData) |
| Standard leave safe construction. More... | |
Private Methods | |
| CGame (TGameData &aData) | |
| constructor. More... | |
| void | ConstructL () |
| Second phase construtor. More... | |
| void | Collisions () |
| Work out what hit what. More... | |
| void | RemoveEveryThing () |
| Erase all sprites. More... | |
| void | DrawEveryThing () |
| Draw all sprites. More... | |
| void | MoveThings () |
| Move bullets and bad guys. More... | |
| void | HandleKeys () |
| Handle movement and action keys. More... | |
| void | Shoot () |
| Shoot. More... | |
Private Attributes | |
| TGameData & | iData |
| Game data, lives, levels, key presses etc. More... | |
| TPlayersShip * | iPlayersShip |
| Players ship. More... | |
| TBadGuy * | iBadGuys |
| Array of bad guys. More... | |
| TBullet * | iBullets |
| Array of players bullets. More... | |
| TInt | iLastFired |
| Last bullet that was fired. More... | |
| CMap * | iMap |
| Offscreen map. More... | |
| RStatus | iStatus |
| Status screen. More... | |
| TInt | iPowerups |
| Number of weapon powerups collected. More... | |
This owns the game objects (map, badguys, bullets, status and players ship) then Play() loops. Each loops checks the keys pressed, moves things, plots everything, copies the offscreen map to the screen, removes everything, checks collisions and scrolls the map. It does this until the player dies, quits or completes the level. The thread within which this runs is suspended when the game is paused so this code knows nothing about pausing.
Definition at line 40 of file CGame.h.
|
|
Types of bonus that can be collected.
Definition at line 43 of file CGame.h.
00043 {
00044 EBonusLife,
00045 EBonusPowerup
00046 };
|
|
|
closes the status screen and delete member objects.
Definition at line 56 of file CGame.cpp. References RStatus::Close(), iBadGuys, iBullets, iMap, iPlayersShip, and iStatus.
|
|
|
constructor.
Definition at line 51 of file CGame.cpp. Referenced by NewL().
00051 : iData(aData), iLastFired(0) { 00052 00053 }; |
|
|
Standard leave safe construction.
Definition at line 41 of file CGame.cpp. References CGame(), and ConstructL(). Referenced by CGameFramework::GameThread().
00041 {
00042 CGame *self= new (ELeave) CGame(aData);
00043 CleanupStack::PushL(self);
00044 self->ConstructL();
00045 CleanupStack::Pop(self);
00046 return(self);
00047 }
|
|
|
Main game loop. This loop runs while the player hasn't died, finished the level, or quit. The player dies when players ship health has reached zero, and the explosion animation has finished. The player finishes the level when the map reaches the end and the player flies off the right hand side of the screen. The game quits if the TGameData::EQuit bit is set in TGameData::iKeys The loop consists of:
Definition at line 140 of file CGame.cpp. References CMap::BlitAsm(), Collisions(), TPlayersShip::Dead(), TPlayersShip::Dieing(), RStatus::Draw(), DrawEveryThing(), TSprite32x24::EHitLandScape, TGameData::EQuit, CMap::Finished(), HandleKeys(), TPlayersShip::Health(), TSprite32x24::Hit(), iData, TGameData::iForceRedraw, TGameData::iKeys, TGameData::iLevel, TGameData::iLevelCompleted, TGameData::iLives, iMap, iPlayersShip, iPowerups, TGameData::iScore, TLevelData::iShipPowerups, TLevelData::iShipStartHealth, iStatus, TSprite32x24::iX, MoveThings(), RemoveEveryThing(), CMap::ScrollMap(), TPlayersShip::SetHealth(), and RStatus::Update(). Referenced by CGameFramework::GameThread().
00140 {
00141 TUint lasttick=0;
00142
00143 while(!(iData.iKeys & TGameData::EQuit)) {
00144 // handle key presses
00145 HandleKeys();
00146
00147 // move bullets and bad guys
00148 MoveThings();
00149 // plot the ship/bullets & badguys into offscreen buffer
00150 DrawEveryThing();
00151
00152 // make sure each loop takes at least 2 ticks
00153 TUint tick = User::TickCount();
00154 while((tick-lasttick)<2)
00155 tick = User::TickCount();
00156 lasttick=tick;
00157
00158 // copy the offscreen buffer to the screen
00159 iMap->BlitAsm();
00160
00161 // remove all the sprites we ploted in reverse order
00162 RemoveEveryThing();
00163
00164 // handle bad guy collisions
00165 Collisions();
00166
00167 // crash if player hit the landscape
00168 if(iPlayersShip->Hit()==TSprite32x24::EHitLandScape)
00169 iPlayersShip->SetHealth(0);
00170
00171
00172 // Handle crashing ship
00173 if(iPlayersShip->Dead()!=EFalse) {
00174 iData.iLives--;
00175 return;
00176 }
00177
00178 // handle end of level (when map has finished, and player flies
00179 // off the right hand side of the screen)
00180 if(iMap->Finished()!=EFalse && iPlayersShip->iX==304 &&
00181 iPlayersShip->Dieing()==EFalse) {
00182 iData.iLevelCompleted=ETrue;
00183 // update level data so that next level can start with same
00184 // health / powerups
00185 iData.iLevel.iShipPowerups=iPowerups;
00186 iData.iLevel.iShipStartHealth=iPlayersShip->Health();
00187 return;
00188 }
00189
00190
00191 // plot status window
00192 if(iData.iForceRedraw==EFalse) {
00193 iStatus.Update(iData.iScore,iData.iLives,iPlayersShip->Health());
00194 } else {
00195 iStatus.Draw(iData.iScore,iData.iLives,iPlayersShip->Health());
00196 iData.iForceRedraw=EFalse;
00197 }
00198
00199 // scroll the offset screen map, plot another line of map sprites
00200 // if nessesary.
00201 iMap->ScrollMap();
00202 }
00203 };
|
|
||||||||||||
|
Add a bad guy to the bad guy array. If an empty slot is available create a new bad guy with these settings.
Definition at line 406 of file CGame.cpp. References iBadGuys, TBadGuy::Init(), KMaxBadGuys, and TSprite32x24::Used(). Referenced by TBadGuy::PathMove(), and CMap::ProcessMapLine().
00406 {
00407 TInt empty=0;
00408 for(;empty<KMaxBadGuys && iBadGuys[empty].Used()!=EFalse;empty++) {};
00409 if(empty>=KMaxBadGuys) return;
00410 iBadGuys[empty].Init(aType,aArgs);
00411 };
|
|
|
Add a bad guy to the bad guy array. If an empty slot is available create a new bad guy with these settings.
Definition at line 390 of file CGame.cpp. References iBadGuys, KMaxBadGuys, and TSprite32x24::Used().
00390 {
00391 TInt empty=0;
00392 for(;empty<KMaxBadGuys && iBadGuys[empty].Used()!=EFalse;empty++) {};
00393 if(empty>=KMaxBadGuys) return;
00394 iBadGuys[empty]=aBadGuy;
00395 }
|
|
|
Collect bonuses. This is called to add an extra life, or powerup a bullet when the players ship collides with a bonus sprite.
Definition at line 213 of file CGame.cpp. References EBonusLife, EBonusPowerup, TLevelData::iBulletPower1_StartSprite, TLevelData::iBulletPower2_StartSprite, TLevelData::iBulletPower3_StartSprite, TLevelData::iBulletPower4_StartSprite, iBullets, iData, TGameData::iLevel, TGameData::iLives, iPowerups, KMaxBullets, TBullet::Set(), and TBullet::Strength(). Referenced by ConstructL(), and TBadGuy::HitShip().
00213 {
00214 switch(aBonus) {
00215 case EBonusLife:
00216 iData.iLives++;
00217 break;
00218
00219 case EBonusPowerup: {
00220 // remember the number of these so that if the player completes
00221 // the level they can be carried to the next level
00222 iPowerups++;
00223 // find the bullet with the least power and increase
00224 // its power by one, changing its sprite at the same time.
00225 // if all bullets are at full strength nothing happens.
00226 TInt16 minPower=iBullets[0].Strength();
00227 TInt powerUp=0;
00228 for(TInt i=1;i<KMaxBullets;i++) {
00229 const TInt16 strength=iBullets[i].Strength();
00230 if(strength < minPower) {
00231 powerUp=i;
00232 minPower = strength;
00233 }
00234 }
00235 switch(minPower) {
00236 case 0:
00237 iBullets[powerUp].Set(powerUp+iData.iLevel.iBulletPower1_StartSprite,1);
00238 break;
00239 case 1:
00240 iBullets[powerUp].Set(powerUp+iData.iLevel.iBulletPower2_StartSprite,2);
00241 break;
00242 case 2:
00243 iBullets[powerUp].Set(powerUp+iData.iLevel.iBulletPower3_StartSprite,3);
00244 break;
00245 case 3:
00246 iBullets[powerUp].Set(powerUp+iData.iLevel.iBulletPower4_StartSprite,4);
00247 break;
00248 }
00249 }
00250 break;
00251
00252 }
00253 }
|
|
|
Give the player some points.
Definition at line 275 of file CGame.cpp. References iData, and TGameData::iScore. Referenced by TBadGuy::Die().
|
|
||||||||||||
|
Damage bad guys with correct id.
Definition at line 285 of file CGame.cpp. References TBadGuy::Damage(), iBadGuys, TBadGuy::Id(), KMaxBadGuys, and TSprite32x24::Used(). Referenced by TBadGuy::HitBullet(), and TBadGuy::HitShip().
|
|
|
First bad guy explosion sprite.
Definition at line 261 of file CGame.cpp. References TLevelData::iBadGuyDeathStartSprite, iData, and TGameData::iLevel. Referenced by TBadGuy::Move().
00261 {
00262 return iData.iLevel.iBadGuyDeathStartSprite;
00263 }
|
|
|
Last bad guy explosion sprite.
Definition at line 270 of file CGame.cpp. References TLevelData::iBadGuyDeathEndSprite, iData, and TGameData::iLevel. Referenced by TBadGuy::Move().
00270 {
00271 return iData.iLevel.iBadGuyDeathEndSprite;
00272 }
|
|
|
Speed map is scrolling at. The map has only two speeds, 2 words a second (4 pixels) or stopped; Definition at line 475 of file CGame.cpp. References CMap::Finished(), and iMap. Referenced by TBadGuy::Move().
|
|
|
Second phase construtor. Creates status screen, creates players ship, creates array of slots for bad guys, creates array of bullets and sets them up as strength 1 Definition at line 72 of file CGame.cpp. References Bonus(), EBonusPowerup, TLevelData::iBackgrounds, iBadGuys, TLevelData::iBulletPower1_StartSprite, iBullets, iData, TGameData::iLevel, iMap, TLevelData::iMapData, iPlayersShip, TLevelData::iShipDeathEndSprite, TLevelData::iShipDeathStartSprite, TLevelData::iShipPowerups, TLevelData::iShipSprite, TLevelData::iShipStartHealth, TLevelData::iShipStartXPos, TLevelData::iShipStartYPos, TLevelData::iStatus, iStatus, KMaxBadGuys, KMaxBullets, CMap::NewL(), RStatus::Open(), and TBullet::Set(). Referenced by NewL().
00072 {
00073 // create the status screen area
00074 User::LeaveIfError(iStatus.Open(iData.iLevel.iStatus));
00075
00076 // create the players ship as defined by the level data
00077 iPlayersShip = new (ELeave) TPlayersShip(iData.iLevel.iShipStartXPos,
00078 iData.iLevel.iShipStartYPos,
00079 iData.iLevel.iShipStartHealth,
00080 iData.iLevel.iShipSprite,
00081 iData.iLevel.iShipDeathStartSprite,
00082 iData.iLevel.iShipDeathEndSprite);
00083
00084 // create slots for KMaxBadGuy bad guys
00085 // (TBadGuy is used for bonuses and sheilds as well as bad guys)
00086 iBadGuys= new (ELeave) TBadGuy[KMaxBadGuys];
00087
00088 // set up players bullets at power one
00089 iBullets= new (ELeave) TBullet[KMaxBullets];
00090 for(TInt i=0;i<KMaxBullets;i++) {
00091 iBullets[i].Set(iData.iLevel.iBulletPower1_StartSprite+i,1);
00092 }
00093
00094 TInt16 count=iData.iLevel.iShipPowerups;
00095 while(count--) {
00096 Bonus(EBonusPowerup);
00097 }
00098
00099 // create the offscreen buffer, and plot the first screen full of
00100 // the map
00101 iMap=CMap::NewL(*this,iData.iLevel.iBackgrounds,iData.iLevel.iMapData);
00102 // CMap may calls back into CGame to add bad guys, if there
00103 // are any bad guys in the first 11 lines of the map this will
00104 // happen during construction, so CGame should be valid when
00105 // constructing iMap,
00106 };
|
|
|
Work out what hit what. Check the TSprite32x24::Hit for each of the bad guys to see what is was plotted over. Call the HitShip() or HitBullet() members as nessesary. Definition at line 300 of file CGame.cpp. References TSprite32x24::EHitBullet1, TSprite32x24::EHitBullet8, TSprite32x24::EHitShip, TSprite32x24::Hit(), TBadGuy::HitBullet(), TBadGuy::HitShip(), iBadGuys, iBullets, iPlayersShip, KMaxBadGuys, TSprite32x24::THit, and TSprite32x24::Used(). Referenced by Play().
00300 {
00301 for(TInt i=KMaxBadGuys-1;i>=0;--i) {
00302 if(iBadGuys[i].Used()!=EFalse) {
00303 TSprite32x24::THit what=iBadGuys[i].Hit();
00304
00305 if(what==TSprite32x24::EHitShip) {
00306 // badguy has hit player
00307 iBadGuys[i].HitShip(*iPlayersShip,*this);
00308
00309 } else if(what >=TSprite32x24::EHitBullet1 &&
00310 what <=TSprite32x24::EHitBullet8) {
00311 // badguy has hit a bullet
00312
00313 TInt bulletNo = what - TSprite32x24::EHitBullet1;
00314 iBadGuys[i].HitBullet(iBullets[bulletNo],*this);
00315 }
00316 }
00317 }
00318 }
|
|
|
Erase all sprites. Put back the saved backgrounds to erase the sprites. This is done in the reverse of the order they were plotted so that the background is restored correctly Definition at line 326 of file CGame.cpp. References iBadGuys, iBullets, iPlayersShip, KMaxBadGuys, KMaxBullets, TSprite32x24::Remove(), and TSprite32x24::Used(). Referenced by Play().
00326 {
00327 TInt i;
00328 for(i=KMaxBadGuys-1;i>=0;--i) {
00329 if(iBadGuys[i].Used()!=EFalse)
00330 iBadGuys[i].Remove();
00331 }
00332 for(i=KMaxBullets-1;i>=0;--i) {
00333 if(iBullets[i].Used()!=EFalse)
00334 iBullets[i].Remove();
00335 }
00336 iPlayersShip->Remove();
00337 }
|
|
|
Draw all sprites. Draw the badguys, bullets and players ship. The order is players ship and bullets first so that when the bad guys are plotted the collision information for the bad guy will include either bullets or the players ship if the badguy's position intersects with a bullet or the player. Definition at line 349 of file CGame.cpp. References iBadGuys, iBullets, iData, TGameData::iLevel, iMap, iPlayersShip, TLevelData::iSprites, KMaxBadGuys, KMaxBullets, TSprite32x24::Plot(), and TSprite32x24::Used(). Referenced by Play().
00349 {
00350 const TUint16 *sprites=iData.iLevel.iSprites;
00351
00352 iPlayersShip->Plot(*iMap,sprites);
00353
00354 TInt i;
00355 for(i=0;i<KMaxBullets;++i) {
00356 if(iBullets[i].Used()!=EFalse)
00357 iBullets[i].Plot(*iMap,sprites);
00358 }
00359
00360 for(i=0;i<KMaxBadGuys;++i) {
00361 if(iBadGuys[i].Used()!=EFalse)
00362 iBadGuys[i].Plot(*iMap,sprites);
00363 }
00364 }
|
|
|
Move bullets and bad guys.
Definition at line 368 of file CGame.cpp. References iBadGuys, iBullets, iData, TGameData::iLevel, TLevelData::iPaths, iPlayersShip, TSprite32x24::iX, TSprite32x24::iY, KMaxBadGuys, KMaxBullets, TBadGuy::Move(), TBullet::Move(), and TSprite32x24::Used(). Referenced by Play().
00368 {
00369 for(TInt k=0;k<KMaxBullets;k++) {
00370 if(iBullets[k].Used()!=EFalse) {
00371 iBullets[k].Move();
00372 }
00373 }
00374
00375 for(TInt i=0;i<KMaxBadGuys;++i) {
00376 if(iBadGuys[i].Used()!=EFalse) {
00377 iBadGuys[i].Move(iPlayersShip->iX,iPlayersShip->iY,iData.iLevel.iPaths,*this);
00378 }
00379 }
00380 };
|
|
|
Handle movement and action keys. Moves the players ship and fires the ships guns. It will allow the player to move while dieing but not to shoot while dieing. Definition at line 421 of file CGame.cpp. References TPlayersShip::Dieing(), TGameData::EAction3, TGameData::EDown, TGameData::ELeft, TGameData::ERight, TGameData::EUp, CMap::Finished(), iData, TGameData::iKeys, iMap, iPlayersShip, TPlayersShip::MoveBy(), and Shoot(). Referenced by Play().
00421 {
00422 //movement
00423 TInt x=0;
00424 TInt y=0;
00425 if(iData.iKeys & TGameData::EUp) y-=6;
00426 if(iData.iKeys & TGameData::EDown) y+=6;
00427 if(iData.iKeys & TGameData::ELeft) x-=6;
00428 if(iData.iKeys & TGameData::ERight) x+=6;
00429 iPlayersShip->MoveBy(x,y,iMap->Finished());
00430
00431
00432 // shooting
00433 // can't shoot while dieing,
00434 if(iData.iKeys & TGameData::EAction3 && iPlayersShip->Dieing()==EFalse) {
00435 // de-bounce fire key
00436 iData.iKeys = iData.iKeys & ~TGameData::EAction3;
00437 Shoot();
00438 }
00439 }
|
|
|
Shoot. Make one of the bullet slots live. The function tries to rotate the bullet slots so that if one is a greater power than the others it is used once every KMaxBullets shots. Definition at line 448 of file CGame.cpp. References iBullets, iLastFired, iPlayersShip, TSprite32x24::iX, TSprite32x24::iY, KMaxBullets, TBullet::Shoot(), and TSprite32x24::Used(). Referenced by HandleKeys().
00448 {
00449 // add another bullet if there is a free slot
00450 // try and rotate bullet slots,
00451 TInt i;
00452 for(i=iLastFired+1;i<KMaxBullets;i++) {
00453 if(iBullets[i].Used()==EFalse) {
00454 iLastFired=i;
00455 iBullets[i].Shoot(iPlayersShip->iX+12,iPlayersShip->iY);
00456 return;
00457 }
00458 }
00459 for(i=0;i<=iLastFired && i<KMaxBullets;i++) {
00460 if(iBullets[i].Used()==EFalse) {
00461 iLastFired=i;
00462 iBullets[i].Shoot(iPlayersShip->iX+12,iPlayersShip->iY);
00463 return;
00464 }
00465 }
00466 }
|
|
|
Game data, lives, levels, key presses etc.
Definition at line 75 of file CGame.h. Referenced by AddScore(), Bonus(), ConstructL(), DrawEveryThing(), FirstExplosionSprite(), HandleKeys(), LastExplosionSprite(), MoveThings(), and Play(). |
|
|
Players ship.
Definition at line 76 of file CGame.h. Referenced by Collisions(), ConstructL(), DrawEveryThing(), HandleKeys(), MoveThings(), Play(), RemoveEveryThing(), Shoot(), and ~CGame(). |
|
|
Array of bad guys.
Definition at line 77 of file CGame.h. Referenced by AddBadGuy(), Collisions(), ConstructL(), DamageBadGuys(), DrawEveryThing(), MoveThings(), RemoveEveryThing(), and ~CGame(). |
|
|
Array of players bullets.
Definition at line 78 of file CGame.h. Referenced by Bonus(), Collisions(), ConstructL(), DrawEveryThing(), MoveThings(), RemoveEveryThing(), Shoot(), and ~CGame(). |
|
|
Last bullet that was fired.
Definition at line 79 of file CGame.h. Referenced by Shoot(). |
|
|
Offscreen map.
Definition at line 80 of file CGame.h. Referenced by ConstructL(), DrawEveryThing(), HandleKeys(), MapSpeed(), Play(), and ~CGame(). |
|
|
Status screen.
Definition at line 81 of file CGame.h. Referenced by ConstructL(), Play(), and ~CGame(). |
|
|
Number of weapon powerups collected.
|