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

Warfare Unleashed – Implementing Gameplay

Creating enemies

Enemies are other instances of the Aircraft class. They appear at the top of the screen and move downwards, until they fly past the bottom of the screen. Most properties are the same for the player and enemies, so we only explain the new aircraft functionality.

Movement patterns

By default, enemies fly downwards in a straight line. But it would be nice if different enemies moved differently, giving the feeling of a very basic artificial intelligence

(AI). Thus, we introduce specific movement patterns. Such a pattern can be described as a sequence of directions to which the enemy airplane heads. A direction consists of an angle and a distance.

struct Direction

 

{

 

 

Direction(float angle, float distance);

float

angle;

float

distance;

};

 

Our data table for aircraft gets a new entry for the sequence of directions as shown in following code:

struct AircraftData

{

int

hitpoints;

float

speed;

Textures::ID

texture;

std::vector<Direction>

directions;

};

Let's implement a zigzag movement pattern for the Raptor plane. First, it steers for 80 units in 45 degrees direction. Then, the angle changes to -45 degrees, and the plane traverses 160 units back. Last, it moves again 80 units in +45 degrees direction, until it arrives at its original x position.

data[Aircraft::Raptor].directions.push_back(Direction( 45, 80)); data[Aircraft::Raptor].directions.push_back(Direction(-45, 160)); data[Aircraft::Raptor].directions.push_back(Direction( 45, 80));

For the Avenger plane, we use a slightly more complex pattern: it is essentially a zigzag, but between the two diagonal movements, the plane moves straight for 50 units.

[ 156 ]

www.it-ebooks.info

Chapter 7

data[Aircraft::Avenger].directions.push_back(Direction(+45, 50)); data[Aircraft::Avenger].directions.push_back(Direction( 0, 50)); data[Aircraft::Avenger].directions.push_back(Direction(-45, 100)); data[Aircraft::Avenger].directions.push_back(Direction( 0, 50)); data[Aircraft::Avenger].directions.push_back(Direction(+45, 50));

The following figure shows the sequence of directions for both planes; the Raptor plane is located on the left, Avenger on the right:

This way of defining movement is very simple, yet it enables a lot of possibilities. You can let the planes fly in any direction (also sideward or backwards); you can even approximate curves when using small intervals.

Now, we look at the logic we have to implement to follow these movement patterns. To the Aircraft class, we add two member variables: mTravelledDistance, which denotes the distance already travelled for each direction, and mDirectionIndex, to know which direction the plane is currently taking.

First, we retrieve the aircraft's movement pattern and store it as a reference to const named directions. We only proceed if there are movement patterns for the current type (otherwise the plane flies straight down).

void Aircraft::updateMovementPattern(sf::Time dt)

{

const std::vector<Direction>& directions = Table[mType].directions;

if (!directions.empty())

{

[ 157 ]

www.it-ebooks.info

Warfare Unleashed – Implementing Gameplay

Second, we check if the current direction has already been passed by the plane (that is, the travelled distance is higher than the direction's distance). If so, the index is advanced to the next direction. The modulo operator allows a cycle; after finishing the last direction, the plane begins again with the first one.

float distanceToTravel

= directions[mDirectionIndex].distance;

if (mTravelledDistance > distanceToTravel)

{

mDirectionIndex

= (mDirectionIndex + 1) % directions.size(); mTravelledDistance = 0.f;

}

Now, we have to get a velocity vector out of the angle. First, we turn the angle by 90 degrees (by default, 0 degrees points to the right), but since our planes fly

downwards, we work in a rotated coordinate system, such that we can use a minus to toggle between left/right. We also have to convert degrees to radians, using our function toRadian().

The velocity's x component is computed using the cosine of the angle multiplied with the maximal speed; analogue for the y component, where the sine is used. Eventually, the travelled distance is updated:

float radians

= toRadian(directions[mDirectionIndex].angle + 90.f); float vx = getMaxSpeed() * std::cos(radians);

float vy = getMaxSpeed() * std::sin(radians); setVelocity(vx, vy);

mTravelledDistance += getMaxSpeed() * dt.asSeconds();

}

}

Note that if the distance to travel is no multiple of the aircraft speed, the plane will fly further than intended. This error is usually small, because there are many logic frames per second, and hardly

noticeable, since each enemy will only be in the view for a short time.

Spawning enemies

It would be good if enemies were initially inactive, and the world created them as soon as they come closer to the player. By doing so, we do not need to process enemies that are relevant in the distant future; the scene graph can concentrate on updating and drawing active enemies.

[ 158 ]

www.it-ebooks.info

Chapter 7

We create a structure nested inside the World class that represents a spawn point for an enemy.

struct SpawnPoint

{

SpawnPoint(Aircraft::Type type, float x, float y);

Aircraft::Type

type;

float

x;

float

y;

};

A member variable World::mEnemySpawnPoints of type std::vector<SpawnPoint> holds all future spawn points. As soon as an enemy position enters the battlefield, the corresponding enemy is created and inserted to the scene graph, and the spawn point is removed.

The World class member function getBattlefieldBounds(), returns sf::FloatRect to the battlefield area, similar to getViewBounds(). The battlefield area extends the view area by a small rectangle at the top, inside which new enemies spawn before they enter the view. If an enemy's y coordinate lies below the battlefield's top member, the enemy will be created at its spawn point. Since enemies face downwards, they are rotated by 180 degrees.

void World::spawnEnemies()

{

while (!mEnemySpawnPoints.empty() && mEnemySpawnPoints.back().y

> getBattlefieldBounds().top)

{

SpawnPoint spawn = mEnemySpawnPoints.back();

std::unique_ptr<Aircraft> enemy(

new Aircraft(spawn.type, mTextures, mFonts)); enemy->setPosition(spawn.x, spawn.y); enemy->setRotation(180.f);

mSceneLayers[Air]->attachChild(std::move(enemy));

mEnemySpawnPoints.pop_back();

}

}

[ 159 ]

www.it-ebooks.info

Warfare Unleashed – Implementing Gameplay

Now, let's insert the spawn points. addEnemy() effectively calls mEnemySpawnPoints.push_back(), and interprets the passed coordinates relative to the player's spawn position. After inserting all spawn points, we sort them by their y coordinates. By doing so, spawnEnemies() needs to check only the elements at the end of the sequence instead of iterating through it every time.

void World::addEnemies()

{

addEnemy(Aircraft::Raptor, 0.f, 500.f); addEnemy(Aircraft::Avenger, -70.f, 1400.f);

...

std::sort(mEnemySpawnPoints.begin(), mEnemySpawnPoints.end(), [] (SpawnPoint lhs, SpawnPoint rhs)

{

return lhs.y < rhs.y;

});

}

Here is an example of the player facing four Avenger enemies. Above each, you see how many hitpoints it has left.

[ 160 ]

www.it-ebooks.info

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