The Alien Sprite
AlienSprite
implements the basic behavior of an alien sprite and is subclassed to create the AlienAStarSprite
and AlienQuadSprite
classes. AlienSprite
is a subclass of TiledSprite
.
Alien behavior can best be understood by considering the statechart in Figure 13-15.
The plan move state is entered by the WorldDisplay
object, notifying the alien that the player has moved. This gives it the opportunity to recalculate its current direction or destination, but the precise algorithm will vary from one AlienSprite
subclass to another.
The other activity is the usual update/draw cycle driven by the animation loop in AlienTilesPanel
. The alien tries to hit the player while in the attack state. A successful hit is reported to the WorldDisplay
object, and the alien stays where it is. Otherwise, the alien updates its position, in the hope of getting closer to the player. In the draw state, the sprite's tile coordinates are mapped to a pixel location and the sprite's image is rendered.
Figure 13-15. Alien statechart
Responding to a player's movement is sprite-specific, so playerHasMoved()
is empty in AlienSprite
:
public void playerHasMoved(Point playerLoc) { }
PlayerLoc
contains the current tile coordinates for the PlayerSprite
object.
Updating the AlienSprite
The attack, stationary, and move states are encapsulated in update()
:
// globals private final static int UPDATE_FREQ = 30; ...
Get Killer Game Programming in Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.