Game Development Reference
In-Depth Information
We define the shield's radius and initialize its position and bounding sphere according to the
constructor parameters. That's all there is to it!
The Shot Class
The Shot class is equally simple. It derives from DynamicGameObject3D , as it is actually moving.
Listing 12-7 shows the code.
Listing 12-7. Shot.java, the Shot Class
package com.badlogic.androidgames.androidinvaders;
import com.badlogic.androidgames.framework.DynamicGameObject3D;
public class Shot extends DynamicGameObject3D {
static float SHOT_VELOCITY = 10f;
static float SHOT_RADIUS = 0.1f;
public Shot( float x, float y, float z, float velocityZ) {
super (x, y, z, SHOT_RADIUS );
velocity.z = velocityZ;
}
public void update( float deltaTime) {
position.z += velocity.z * deltaTime;
bounds.center.set(position);
}
}
Here, we define some constants, namely the shot velocity and its radius. The constructor takes
a shot's initial position, as well as its velocity on the z axis. Wait, didn't we just define the velocity
as a constant? Yes, but that would let our shot travel only in the direction of the positive z axis.
That's fine for shots fired by the invaders, but the shots from the ship must travel in the opposite
direction. When we create a shot (outside of this class), we know in which direction the shot
should travel.
The update() method just does the usual point-mass physics. There is no acceleration involved,
and thus we only need to add the constant velocity, multiplied by the delta time, to the shot's
position. The crucial part is that we also update the position of the bounding sphere's center
in accordance with the shot's position. Otherwise, the bounding sphere would not move with
the shot.
The Ship Class
The Ship class is responsible for updating the ship's position, keeping it within the bounds of
the game field, and keeping track of the state it is in. It can be either alive or exploding. In both
cases, we keep track of the amount of time the ship has been in this state. We can then use the
state time to do animations, for example, just as we did in Super Jumper and its WorldRenderer
class. The ship will get its current velocity from the outside, based on the user input, either
with accelerometer readings, as we did for Bob, or based on a constant, depending on what
 
Search WWH ::




Custom Search