Game Development Reference
In-Depth Information
Listing 12-9. Invader.java, the Invader Class
package com.badlogic.androidgames.androidinvaders;
import com.badlogic.androidgames.framework.DynamicGameObject3D;
public class Invader extends DynamicGameObject3D {
static final int INVADER_ALIVE = 0;
static final int INVADER_DEAD = 1;
static final float INVADER_EXPLOSION_TIME = 1.6f;
static final float INVADER_RADIUS = 0.75f;
static final float INVADER_VELOCITY = 1;
static final int MOVE_LEFT = 0;
static final int MOVE_DOWN = 1;
static final int MOVE_RIGHT = 2;
We start with some constants that define the state of an invader, the duration of its explosion,
its radius, and its default velocity, followed by three constants that allow us to keep track of the
direction in which the invader is currently moving.
int state = INVADER_ALIVE ;
float stateTime = 0;
int move = MOVE_LEFT ;
boolean wasLastStateLeft = true ;
float movedDistance = World. WORLD_MAX_X / 2;
We keep track of an invader's state, state time, movement direction, and movement distance,
which should initially be set to half the playing field width. We also keep track of whether the
last horizontal movement was to the left or not. This allows us to decide in which direction the
invader should go once it has finished its vertical movement on the z axis.
public Invader( float x, float y, float z) {
super (x, y, z, INVADER_RADIUS );
}
The constructor performs the usual setup of the invader's position and bounding ship, via the
super class constructor.
public void update( float deltaTime, float speedMultiplier) {
if (state == INVADER_ALIVE ) {
movedDistance += deltaTime * INVADER_VELOCITY * speedMultiplier;
if (move == MOVE_LEFT ) {
position.x -= deltaTime * INVADER_VELOCITY * speedMultiplier;
if (movedDistance > World. WORLD_MAX_X ) {
move = MOVE_DOWN ;
movedDistance = 0;
wasLastStateLeft = true ;
}
}
if (move == MOVE_RIGHT ) {
position.x += deltaTime * INVADER_VELOCITY * speedMultiplier;
Search WWH ::




Custom Search