Game Development Reference
In-Depth Information
To store the type, the state, and the state time of the
Platform
instance, we need three
members. These get initialized in the constructor based on the type of the
Platform
, which is a
parameter of the constructor, along with the platform center's position.
public void
update(
float
deltaTime) {
if
(type ==
PLATFORM
_
TYPE
_
MOVING
) {
position.add(velocity.x * deltaTime, 0);
bounds.lowerLeft.set(position).sub(
PLATFORM
_
WIDTH
/ 2,
PLATFORM
_
HEIGHT
/ 2);
if
if(position.x <
PLATFORM
_
WIDTH
/ 2) {
velocity.x = -velocity.x;
position.x =
PLATFORM
_
WIDTH
/ 2;
}
if
if(position.x > World.
WORLD
_
WIDTH
-
PLATFORM
_
WIDTH
/ 2) {
velocity.x = -velocity.x;
position.x = World.
WORLD
_
WIDTH
-
PLATFORM
_
WIDTH
/ 2;
}
}
stateTime += deltaTime;
}
The
update()
method will move the platform and check for the out-of-world condition, acting
accordingly by inverting the velocity vector. This is exactly the same thing we did in the
Squirrel.update()
method. We also update the state time at the end of the method.
public void
pulverize() {
state = PLATFORM_STATE_PULVERIZING;
stateTime = 0;
velocity.x = 0;
}
}
The final method of this class is called
pulverize()
. It switches the state from
PLATFORM_STATE_
NORMAL
to
PLATFORM_STATE_PULVERIZING
and resets the state time and velocity. This means
that moving platforms will stop moving. The method will be called if the
World
class detects a
collision between Bob and the
Platform
, and it decides to pulverize the
Platform
based on a
random number. We'll talk about that in a bit. First we need to talk about Bob.
The Bob Class
The
Bob
class is shown in Listing 9-14.
Listing 9-14. Bob.java
package
com.badlogic.androidgames.jumper;
import
com.badlogic.androidgames.framework.DynamicGameObject;
public class
Bob
extends
DynamicGameObject{
public static final int
BOB
_
STATE
_
JUMP
= 0;