Top Banner
Bullets and Magazines Andrew Williams [email protected] http://www.bolton.ac.uk/staff/adw1
14

Bullets and Magazines Andrew Williams [email protected] .

Jan 20, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

Bullets and Magazines

Andrew [email protected]

http://www.bolton.ac.uk/staff/adw1

Page 2: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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?

Page 3: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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

Page 4: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

#include "AWSprite.h"// Slightly simplified for clarity....class AWBullet : public AWSprite {public:

// True if bullet has been fired but hasn't disappeared yetbool inUse;

// ConstructorAWBullet(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;

}}

};

Page 5: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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

Page 6: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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

Page 7: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

AWMagazine

● The number of bullets in a magazine determines how many shots can be on screen at once

● Examples:1 bullet 3 bullets 6 bullets 10 bullets 50 bullets

Page 8: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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

Page 9: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

Allocating a BulletAWBullet *blast;AWMagazine *laserblasts;

laserblasts = new AWMagazine(10, "blast.bmp", 1); // NOTE 1laserblasts->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 4blast->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);

}}

Page 10: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

Notes on the Example

● Note 1– Creating an AWMagazine creates a number of

AWBullets for you. ● You wouldn't normally create an AWBullet directly

Page 11: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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.

Page 12: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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

Page 13: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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

Page 14: Bullets and Magazines Andrew Williams A.Williams@bolton.ac.uk .

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 < laserblasts->size(); b++) {

blast = laserblasts->get(b);if(blast->inUse == false) continue;if(blast->bb_80_collision(alien)) {

alien->make_invisible();score += 10;

}}