Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bullets and Magazines Andrew Williams

Similar presentations


Presentation on theme: "Bullets and Magazines Andrew Williams"— Presentation transcript:

1 Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk http://www.bolton.ac.uk/staff/adw1

2 AWBullet ● Doesn't have to be a bullet as such: – Arrow – Missile – Bomb – Laser blast – Etc ● What is the distinguishing feature in the context of games development?

3 AWBullet ● The key thing about bullets is that they disappear – Once they've left the screen we don't really care about them – In general, in your side-scrolling shooter you don't want to accidentally destroy enemies that you've not seen yet ● Another thing to think about is that there may be a very large number of bullets – Think of shooting games where you hold the fire button down constantly

4 #include "AWSprite.h" // Slightly simplified for clarity.... class AWBullet : public AWSprite { public: // True if bullet has been fired but hasn't disappeared yet bool inUse; // Constructor AWBullet(char *bmpName, Uint32 hmf) : AWSprite(bmpName, hmf) { inUse = false; } void update_everything(Uint32 topLeftX, Uint32 topLeftY) { if(!inUse) return; if(is_on_screen(topLeftX, topLeftY)) { auto_move(); auto_animate(); auto_accelerate(); draw(); } else { set_auto_move(0); set_auto_animate(0); set_auto_accelerate(0); inUse = false; } };

5 AWBullet ● If in the course of a game, a player might fire 100000 laser blasts, we don't want to have to create 100000 AWSprites – That would be wasteful of space – And completely unnecessary ● we've already noted that we don't care about bullets once they've left the screen ● It's better to create a collection of bullets which we can re-use as appropriate

6 AWMagazine ● Magazine as in AK-47, not magazine as in Edge – Think of a magazine as a collection of bullets ● Each magazine can hold one type of bullet – So if your ship can fire laser blasts and missiles, you need two AWMagazines: laserblasts = new AWMagazine(10, “blast.bmp”, 1); missiles = new AWMagazine(6, “missile.bmp”, 4); ● The first parameter determines how many “bullets” there are in the magazine

7 AWMagazine ● The number of bullets in a magazine determines how many shots can be on screen at once ● Examples: 1 bullet 3 bullets 6 bullets10 bullets50 bullets

8 AWMagazine ● Some rules: – All bullets in a magazine look the same – Although they all use the same bitmap, there are n copies of the SDL_Surface for n bullets ● That is to say, AWMagazine is not space-efficient – Bullets in a magazine move, accelerate and can be animated, independently – You must use AWBullet *AWMagazine::allocate_a_bullet(); to obtain a new bullet when you want to fire

9 Allocating a Bullet AWBullet *blast; AWMagazine *laserblasts; laserblasts = new AWMagazine(10, "blast.bmp", 1); // NOTE 1 laserblasts->set_transparent_colour(0, 0, 0); // NOTE 2 /*... blah blah blah... * The event handler sets firing to true if * the player is trying to fire a bullet *... */ if(firing) { blast = laserblasts->allocate_a_bullet(); if(blast != NULL) { // NOTE 3 // NOTE 4 blast->set_world_position(ship->worldX+13, ship->worldY+14); blast->set_velocities(0.0f, -5.0f); blast->set_accelerations(0.0f, -0.1f); blast->set_auto_accelerate(20); blast->set_auto_move(20); }

10 Notes on the Example ● Note 1 – Creating an AWMagazine creates a number of AWBullets for you. ● You wouldn't normally create an AWBullet directly

11 Notes on the Example ● Note 2 – I have provided some convenience functions for doing something to all the AWBullets in an AWMagazine ● set_transparent_colour(..) – Because the sprite bitmap is the same, the transparent colour will normally be the same ● update_everything(..) – This goes through the list of bullets and updates the ones for which inUse is true. The others are ignored.

12 Notes on the Example ● Note 3 – If (blast==NULL) it means that there are no bullets available in the magazine ● In other words, all the bullets are currently inUse ● You must not attempt to use blast if it is NULL

13 Notes on the Example ● Note 4 – The characteristics of a (laser)blast are set on a per-bullet basis – The blast variable is not preserved, so you have to be a little careful how you use it – In general, it is best not to treat bullets as individuals after they have been fired

14 Collision Detection ● Remember, an AWBullet is just an AWSprite, so we can use all the collision-detection functions we have already provided: // See if we have hit anything.. for(unsigned b=0; b size(); b++) { blast = laserblasts->get(b); if(blast->inUse == false) continue; if(blast->bb_80_collision(alien)) { alien->make_invisible(); score += 10; }


Download ppt "Bullets and Magazines Andrew Williams"

Similar presentations


Ads by Google