Game Development Reference
In-Depth Information
float
side = world.bob.velocity.x < 0? -1: 1;
batcher.drawSprite(world.bob.position.x, world.bob.position.y, side * 1, 1, keyFrame);
}
The method
renderBob()
is responsible for rendering Bob. Based on Bob's state and state
time, we select a keyframe out of the total of five keyframes we have for Bob (see Figure
9-9
earlier in the chapter). Based on Bob's velocity's x component, we also determine which side
Bob is facing. Based on that, we multiply by either 1 or -1 to flip the texture region accordingly.
Remember, we only have keyframes for a Bob looking to the right. Note also that we don't
use
BOB_WIDTH
or
BOB_HEIGHT
to specify the size of the rectangle we draw for Bob. Those sizes
are the sizes of the bounding shapes, which are not necessarily the sizes of the rectangles we
render. Instead we use our 1×1-meter-to-32×32-pixel mapping. That's something we'll do for
all sprite rendering; we'll either use a 1×1 rectangle (Bob, coins, squirrels, springs), a 2×0.5
rectangle (platforms), or a 2×2 rectangle (castle).
private void
renderPlatforms() {
int
len = world.platforms.size();
for
(
int
i = 0; i < len; i++) {
Platform platform = world.platforms.get(i);
TextureRegion keyFrame = Assets.
platform
;
if
if(platform.state == Platform.
PLATFORM
_
STATE
_
PULVERIZING
) {
keyFrame = Assets.
brakingPlatform
.getKeyFrame(platform.stateTime,
Animation.
ANIMATION
_
NONLOOPING
);
}
batcher.drawSprite(platform.position.x, platform.position.y,
2, 0.5f, keyFrame);
}
}
The method
renderPlatforms()
loops through all the platforms in the world and selects a
TextureRegion
based on the platform's state. A platform can be either pulverized or not
pulverized. In the latter case, we simply use the first keyframe; in the former case, we fetch a
keyframe from the pulverization animation based on the platform's state time.
private void
renderItems() {
int
len = world.springs.size();
for
(
int
i = 0; i < len; i++) {
Spring spring = world.springs.get(i);
batcher.drawSprite(spring.position.x, spring.position.y, 1, 1, Assets.
spring
);
}
len = world.coins.size();
for
(
int
i = 0; i < len; i++) {
Coin coin = world.coins.get(i);
TextureRegion keyFrame = Assets.
coinAnim
.getKeyFrame(coin.stateTime,
Animation.
ANIMATION
_
LOOPING
);
batcher.drawSprite(coin.position.x, coin.position.y, 1, 1, keyFrame);
}
}