Game Development Reference
In-Depth Information
onscreen buttons are being pressed. Additionally, the ship will keep track of the number of lives
it has, and offer us a way to tell it that it has been killed. Listing 12-8 shows the code.
Listing 12-8. Ship.java, the Ship Class
package com.badlogic.androidgames.androidinvaders;
import com.badlogic.androidgames.framework.DynamicGameObject3D;
public class Ship extends DynamicGameObject3D {
static float SHIP_VELOCITY = 20f;
static int SHIP_ALIVE = 0;
static int SHIP_EXPLODING = 1;
static float SHIP_EXPLOSION_TIME = 1.6f;
static float SHIP_RADIUS = 0.5f;
We start off with some constants to define the maximum ship velocity, two states (alive and
exploding), the amount of time it takes the ship to explode fully, and the ship's bounding sphere
radius. Also, we let the class derive from DynamicGameObject3D , since it has a position and
bounding sphere, as well as a velocity. The acceleration vector stored in a DynamicGameObject3D
will again be unused.
int lives;
int state;
float stateTime = 0;
Next we have the members, consisting of two integers, to keep track of the number of lives the
ship has and its state (either SHIP_ALIVE or SHIP_EXPLODING ). The last member keeps track of
how many seconds the ship has been in its current state.
public Ship( float x, float y, float z) {
super (x, y, z, SHIP_RADIUS );
lives = 3;
state = SHIP_ALIVE ;
}
The constructor performs the usual super class constructor call and initializes some of the
members. The ship will have a total of three lives.
public void update( float deltaTime, float accelY) {
if (state == SHIP_ALIVE ) {
velocity.set(accelY / 10 * SHIP_VELOCITY , 0, 0);
position.add(velocity.x * deltaTime, 0, 0);
if (position.x < World. WORLD_MIN_X )
position.x = World. WORLD_MIN_X ;
if (position.x > World. WORLD_MAX_X )
position.x = World. WORLD_MAX_X ;
bounds.center.set(position);
} else {
if (stateTime >= SHIP_EXPLOSION_TIME ) {
lives--;
Search WWH ::




Custom Search