Game Development Reference
In-Depth Information
public bool
IsEmptyBox()
{
return this
.sprite.SheetIndex == 7 &&
this
.boxed;
}
Finally, we are dealing with a multicolored penguin if the sheet index is 6 and we
are not boxed:
public bool
IsMultiColoredPenguin()
{
return this
.sprite.SheetIndex == 6 && !
this
.boxed;
}
First, we will check if there is a shark at the tile we are moving into. For that, we
retrieve the level and we use the
FindSharkAtPosition
method from the
Level
class to
find out if there is a shark:
Level level = Root
as
Level;
GameObject s = level.FindSharkAtPosition(target);
if
(s !=
null
&& s.Visible)
{
// handle the shark interaction
}
If we encounter a shark, we are eaten and the shark leaves the playing field with
a belly full of penguin. In the game, this means that the penguin will stop moving
(forever) and both the shark and the penguin will become invisible (insert Jaws
music score here). The following lines of code achieve this:
s.Visible =
false
;
this
.Visible =
false
;
this
.stopMoving(x, y);
The next thing to check is if there is another penguin or a seal. For that we use the
FindAnimalAtPosition
method from the
Level
class. We retrieve the animal as follows:
Animal a = level.FindAnimalAtPosition(target);
If the method returns
null
, or the animal is not visible, we do not have to do anything
and we can return from the method:
if
(a ==
null
|| !a.Visible)
return
;
The first case we will solve is if we are colliding with an empty box. If that is the
case, we move the animal inside the box by setting the sheet index of the box to the
sheet index of this animal, and we make this animal invisible: