The Shot Class

The shot class is equally simplistic. It derives from DynamicGameObject3D, as it is actually moving. Listing 12-7 shows you the code.

Listing 12-7. Shot.java, the Shot Class package com.badlogic.androidgames.droidinvaders;

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);

We again 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 in the direction of the positive z-axis only. That's OK for shots fired by the invaders, but the shots of our ship must travel in the opposite direction. When we create a shot (outside of this class) we know which direction the shot should travel in. So the shot has its velocity set by its creator.

The update() method just does the usual point-mass physics. We don't have any acceleration involved and thus 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 along with our shot.

0 0

Post a comment