Game Development Reference
In-Depth Information
public static final int
BOB
_
STATE
_
FALL
= 1;
public static final int
BOB
_
STATE
_
HIT
= 2;
public static final float
BOB
_
JUMP
_
VELOCITY
= 11;
public static final float
BOB
_
MOVE
_
VELOCITY
= 20;
public static final float
BOB
_
WIDTH
= 0.8f;
public static final float
BOB
_
HEIGHT
= 0.8f;
We start with a couple of constants again. Bob can be in one of three states: jumping upward,
falling downward, or being hit. He also has a vertical jump velocity, which is only applied on the
y axis, and a horizontal move velocity, which is only applied on the x axis. The final two
constants define Bob's width and height in the world. Of course, we also have to store Bob's
state and state time.
int
state;
float
stateTime;
public
Bob(
float
x,
float
y) {
super
(x, y,
BOB
_
WIDTH
,
BOB
_
HEIGHT
);
state =
BOB
_
STATE
_
FALL
;
stateTime = 0;
}
The constructor just calls the superclass's constructor so that Bob's center position and
bounding shape are initialized correctly, and then initializes the
state
and
stateTime
member
variables.
public void
update(
float
deltaTime) {
velocity.add(World.
gravity
.x * deltaTime, World.
gravity
.y * deltaTime);
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
bounds.lowerLeft.set(position).sub(bounds.width / 2, bounds.height / 2);
if
if(velocity.y > 0 && state !=
BOB
_
STATE
_
HIT
) {
if
(state !=
BOB
_
STATE
_
JUMP
) {
state =
BOB
_
STATE
_
JUMP
;
stateTime = 0;
}
}
if
if(velocity.y < 0 && state !=
BOB
_
STATE
_
HIT
) {
if
(state !=
BOB
_
STATE
_
FALL
) {
state =
BOB
_
STATE
_
FALL
;
stateTime = 0;
}
}
if
if(position.x < 0)
position.x = World.
WORLD
_
WIDTH
;
if
if(position.x > World.
WORLD
_
WIDTH
)
position.x = 0;
stateTime += deltaTime;
}