Game Development Reference
In-Depth Information
First you check whether there is a shark at the tile the animal is moving into. To do that, you retrieve
the level and use the findSharkAtPosition method from the Level class to find out whether
there is a shark:
var s = this.root.findSharkAtPosition(target);
if (s !== null && s.visible) {
// handle the shark interaction
}
The findSharkAtPosition method is straightforward; have a look at the method in the example code
belonging to this chapter. If a penguin encounters a shark, the penguin is eaten and the shark leaves
the playing field with a full belly. In the game, this means the penguin stops moving (forever) and
both the shark and the penguin become invisible. The following lines of code achieve this:
s.visible = false;
this.visible = false;
this.stopMoving();
The next thing to check is whether there is another penguin or a seal. To do that, you use the
findAnimalAtPosition method from the Level class. You retrieve the animal as follows:
var a = this.root.findAnimalAtPosition(target);
If the method returns null or the animal isn't visible, you don't have to do anything, and you can
return from the method:
if (a === null || !a.visible)
return;
The first case you solve is if the penguin is colliding with a seal. In that case, the penguin doesn't
have to do anything—it simply stops moving:
if (a.isSeal())
this.stopMoving();
The next case is if the animal collides with an empty box. If that is the case, you move the animal
inside the box by setting the sheet index of the box to the sheet index of the animal, and you make
the animal invisible:
else if (a.isEmptyBox()) {
this.visible = false;
a.sheetIndex = this.sheetIndex;
}
If the sheet index of animal a is the same as the sheet index of the penguin, or either one of the
penguins is multicolored, you have a valid pair of penguins and make both penguins invisible:
else if (a.sheetIndex === this.sheetIndex || this.isMulticolor() || a.isMulticolor()) {
a.visible = false;
this.visible = false;
}
 
Search WWH ::




Custom Search