Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SFML Game Development.pdf
Скачиваний:
209
Добавлен:
28.03.2016
Размер:
4.19 Mб
Скачать

Warfare Unleashed – Implementing Gameplay

mActiveEnemies.clear();

}

That's it, now we are able to fire and forget!

The result looks as follows:

Picking up some goodies

Now we have implemented enemies and projectiles. But even if the player shot enemy airplanes down, and had exciting battles, he wouldn't remark that his success changes anything. You want to give the player the feeling that he is progressing in the game. Usual for this game genre are power-ups that the enemies drop when they are killed. So let's go ahead and implement that in our game.

Now this is the same story as with the projectile. Most of the things we need have already been implemented; therefore, this will be quite easy to add. What we want is only an entity that, when the player touches it, applies an effect to the player and disappears. Not much work with our current framework.

[ 170 ]

www.it-ebooks.info

Chapter 7

class Pickup : public Entity

{

public:

enum Type

{

HealthRefill,

MissileRefill,

FireSpread,

FireRate,

TypeCount

};

 

public:

 

 

Pickup(Type type,

 

const TextureHolder& textures);

virtual unsigned int

getCategory() const;

virtual sf::FloatRect

getBoundingRect() const;

void

apply(Aircraft& player) const;

protected:

 

virtual void

drawCurrent(sf::RenderTarget& target,

 

sf::RenderStates states) const;

private:

 

Type

mType;

sf::Sprite

mSprite;

};

So, let's start looking at a few interesting parts. As usual, we have a data table, create a sprite and center it, so the constructor looks just as you would expect it. Let's investigate the apply() function, and how the data table is created. In

apply(), a function object stored in the table is invoked with player as argument. The initializePickupData() function initializes the function objects, using std::bind() that redirects to the Aircraft member functions.

void Pickup::apply(Aircraft& player) const

{

Table[mType].action(player);

}

[ 171 ]

www.it-ebooks.info

Warfare Unleashed – Implementing Gameplay

std::vector<PickupData> initializePickupData()

{

std::vector<PickupData> data(Pickup::TypeCount);

data[Pickup::HealthRefill].texture = Textures::HealthRefill; data[Pickup::HealthRefill].action

= std::bind(&Aircraft::repair, _1, 25);

data[Pickup::MissileRefill].texture = Textures::MissileRefill; data[Pickup::MissileRefill].action

= std::bind(&Aircraft::collectMissiles, _1, 3);

data[Pickup::FireSpread].texture = Textures::FireSpread; data[Pickup::FireSpread].action

= std::bind(&Aircraft::increaseSpread, _1);

data[Pickup::FireRate].texture = Textures::FireRate; data[Pickup::FireRate].action

= std::bind(&Aircraft::increaseFireRate, _1);

return data;

}

The pickups call already defined functions on the player aircraft that let us modify its state. These functions may repair it, refill it with missiles, or improve its firepower.

It's nice when things just work out of the box.

That's how the scene looks when two pickups (health and fire rate) are floating in the air. You may notice that the player's Eagle plane shoots two bullets at once, which is the result of a previously collected fire spread pickup.

[ 172 ]

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]