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

Warfare Unleashed – Implementing Gameplay

Concerning collision response, if there are many cases to consider, the dispatching could be done using a two-dimensional lookup table. The categories of both partners would serve as indices, and the table entries are function objects that implement the collision response for a concrete collider pair.

As nice as these optimizations sound, there is a price to pay—the implementation becomes more complicated. A decent amount of book keeping is required to keep everything synchronous, for example, the grid. Each time an entity moves, it might move to another cell, so we have to keep track of it. Newly created entities must be inserted, and destroyed entities must be removed from the right cell.

In conclusion, such optimizations are not only nice to have, but a bare necessity when the world and the number of entities grow. However, the implied book-keeping overhead does not pay off for smaller scenarios, which is a reason why we kept things simple in our game.

An interacting world

A lot of game logic has been implemented in the different entities, now we look at functionality that is defined in the World class. You have already seen the collision in the last section.

Cleaning everything up

During the game, entities are destroyed in battle, and have to be removed from the scene graph. We do not remove them instantly. Once in a frame, we iterate through the scene graph, check which nodes have been destroyed, and detach them from their parents. To find out whether a node has been destroyed, we write the virtual function SceneNode::isDestroyed(). By default, it returns false. A derived entity may specify a condition under which it returns true. Usually, this will be the case when the hitpoints are zero or less (that is, the entity is destroyed).

bool Entity::isDestroyed() const

{

return mHitpoints <= 0;

}

In addition, we add a virtual function that checks if a scene node should be removed from the scene graph. By default, this is true as soon as the node is destroyed.

bool SceneNode::isMarkedForRemoval() const

{

return isDestroyed();

}

[ 180 ]

www.it-ebooks.info

Chapter 7

However, this need not always be the case. Imagine an entity that has been destroyed, but still needs to reside for some time in the world, in order to drop a pickup, show an explosion animation, or similar. While isDestroyed() tells whether entities are logically dead and therefore, don't interact with the world anymore, isMarkedForRemoval() tells whether the scene node can be removed from the scene graph. The Aircraft class itself delays removal after destruction, to let enemies drop their pickups in the update() function. There, a special flag determines the return value.

bool Aircraft::isMarkedForRemoval() const

{

return mIsMarkedForRemoval;

}

The removal is performed by the following method. In the first part, std::remove_ if() rearranges the children container, so that all active nodes are at the beginning, and the ones to remove at the end. The call to erase() actually destroys these SceneNode::Ptr objects. In the second part, the function is recursively called for all child nodes. std::mem_fn() creates a function object which returns true, if and only if, the member function passed as argument returns true.

void SceneNode::removeWrecks()

{

auto wreckfieldBegin = std::remove_if(mChildren.begin(), mChildren.end(), std::mem_fn(&SceneNode::isMarkedForRemoval)); mChildren.erase(wreckfieldBegin, mChildren.end());

std::for_each(mChildren.begin(), mChildren.end(), std::mem_fn(&SceneNode::removeWrecks));

}

This function can now be called in World::update(), and we automatically get rid of all nodes that request their removal.

Out of view, out of the world

Most entities that leave the current view become meaningless. Launched projectiles that have missed their enemy unwaveringly follow their path in the endless void.

Enemies that fly past the screen continue to fly, although the player will never see them again, which can be costly performance-wise.

[ 181 ]

www.it-ebooks.info

Warfare Unleashed – Implementing Gameplay

In order to reduce the amount of unnecessary entities, especially having our collision algorithm in mind, we want to remove entities that are located outside the view. Remember that getBattlefieldBounds() returns sf::FloatRect, which is slightly bigger than getViewBounds(). It also contains the area beyond the view, inside which the enemies spawn. We create a command that destroys all entities, of which the bounding rectangle doesn't intersect with the battlefield's bounding rectangle

(that is, they are outside).

void World::destroyEntitiesOutsideView()

{

Command command;

command.category = Category::Projectile

| Category::EnemyAircraft; command.action = derivedAction<Entity>( [this] (Entity& e, sf::Time)

{

if (!getBattlefieldBounds()

.intersects(e.getBoundingRect()))

e.destroy();

});

mCommandQueue.push(command);

}

The final update

A lot of new logic code has found its way into the World class; the different functions are invoked from World::update(), which currently looks as follows. The function names are self-explanatory.

void World::update(sf::Time dt)

{

mWorldView.move(0.f, mScrollSpeed * dt.asSeconds()); mPlayerAircraft->setVelocity(0.f, 0.f);

destroyEntitiesOutsideView();

guideMissiles();

while (!mCommandQueue.isEmpty()) mSceneGraph.onCommand(mCommandQueue.pop(), dt);

adaptPlayerVelocity();

[ 182 ]

www.it-ebooks.info

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