Download iPhone Game Development

Transcript
Special Tiles
Although impassable tile collisions can simply be resolved as soon as they are detected,
other special types of tiles need extra game logic. To handle these situations, we can
send the entity a message when it collides with a certain type of tile. This allows us to
write code to allow entities to handle special tiles in their own way.
If a hippopotamus is walking on land and collides with a water tile, it can move to a
swimming state and keep going. In contrast, a cougar might decide it doesn’t want to
get wet and the AI can use the message to turn the cougar around and find another path.
Entity-to-Entity
Entity-to-world collision is fairly straightforward. Your only goal is to keep entities from
entering impassable tiles, and notifying them when they’re on special tiles such as water.
Entity-to-entity collision, however, is more complex.
With entity-to-entity collision, there are many results from different types of collisions.
If an animal touches another animal, we could simply choose not to let the animals
pass through each other, or we could ignore the situation if we want to allow animals
to walk over each other. Or if they are two different types of animals, they might attack
each other.
When collision resolution takes many different forms due to game design, the line
between physics code and game logic code gets blurred. Similar to special tiles, we can
handle this through special messages alerting entities of events so that the entities
themselves can handle the resolution logic.
However, we might also want to resolve these collisions strictly at the GameState level.
A single entity knows only as much about the world as we give it access to. However,
the GameState envelops the entire world, including all of the entities, the level, and all
of the game logic code.
To clarify, consider the following example. For the first level, we want to determine
when one of the emu chicks has walked onto the nest at the top of the level. To represent
the nest, we will have a special kind of entity called a trigger area. A trigger area has a
bounding box for determining physical collisions just like a regular entity, but it doesn’t
necessarily have a graphical representation. Because the nest is already represented in
the tiles of the level background, we don’t need to draw anything for this trigger area.
Instead, we merely perform entity-to-entity collision between emu chicks and the nest
trigger area.
However, when we detect this collision, we want to remove the emu entity from the
level. Simply sending a message to the entity won’t allow it to remove itself. We need
the resolution to happen at the GameState level.
In addition, not all entities require collision detection against each other. Using the
previous example, although an emu chick should be tested for collision against the nest
126 | Chapter 4: 2D Game Engine