Game Development Reference
In-Depth Information
to place the animal at the tile it just moved out from . We added a method called
StopMoving that accomplishes exactly that. In that method, we first have to calculate
what the position of the old tile is. We can do that by starting from the x and y
coordinates of the tile that we are currently moving into. These are passed along as
a parameter. If for example the penguin's velocity is the vector ( 5 , 0 ) (moving to the
right), we need to subtract one from the x coordinate to get the x coordinate of the
tile we are moving out of. If the penguin's velocity is ( 0 ,
5 ) (moving up), then we
need to add one to the y coordinate to get the y coordinate of the tile we are moving
out of. We can achieve this by normalizing the velocity vector and the subtracting it
from the x and y coordinates. This works because normalizing a vector results in a
vector of length one (unit length). Since we only move in either the x or y direction,
we will end up with a vector ( 1 , 0 ) in the first example, and ( 0 ,
1 ) in the second
example. Therefore, the position of the old tile is then calculated as follows:
GameObjectGrid tilefield = GameWorld.Find("tilefield") as GameObjectGrid;
velocity.Normalize();
Vector2 oldBlock = new Vector2(GetCurrentBlock().X, GetCurrentBlock().Y)
velocity;
Calculating the actual position of the penguin is then done as follows:
new Vector2(tilefield.CellWidth, tilefield.CellHeight);
position = oldBlock
And finally, we set the animal velocity to zero, so that it stays in its new position:
velocity = Vector2.Zero;
23.5 Dealing with Meeting Other Game Objects
What we still need to do is check if we collide with another game object, such as
another penguin or a shark. There are a few special types of animals:
multicolored penguins;
empty boxes;
seals.
We will add a few methods to the Animal class that determine if we are dealing with
these special cases. For example, we are dealing with a seal if the sheet index equals
seven, and it is not boxed:
public bool IsSeal()
{
return this .sprite.SheetIndex == 7 && ! this .boxed;
}
We are dealing with an empty box if the sheet index is 7 and it is boxed:
Search WWH ::




Custom Search