## Game Object Dynamic GameObject and Cannon

Let's start with the static case, or base case, in Listing 8-7. Listing 8-7. GameObject.java, a Static Game Object with a Position and Bounds package com.badlogic.androidgames.gamedev2d;

public class GameObject {

public final Vector2 position; public final Rectangle bounds;

public GameObject(float x, float y, float width, float height) { this.position = new Vector2(x,y);

this.bounds = new Rectangle(x-width/2, y-height/2, width, height);

Every object in our game has a position that coincides with its center. Additionally we let each object have a single bounding shape—a rectangle in this case. In our constructor we set the position and bounding rectangle (which is centered around the center of the object) according to the parameters.

For dynamic objects, that is, objects which move, we also need to keep track of their velocity and acceleration (if they're actually accelerated by themselves—e.g., via an engine or thruster). Listing 8-8 shows the code for the DynamicGameObject class.

Listing 8-8. DynamicGameObject.java: Extending the GameObject with a Velocity and Acceleration Vector package com.badlogic.androidgames.gamedev2d;

public class DynamicGameObject extends GameObject { public final Vector2 velocity; public final Vector2 accel;

public DynamicGameObject(float x, float y, float width, float height) { super(x, y, width, height); velocity = new Vector2();

We just extend the GameObject class to inherit the position and bounds members. Additionally we create vectors for the velocity and acceleration. A new dynamic game object will have zero velocity and acceleration after it has been initialized.

In our cannonball example we have the cannon, the cannonball, and the targets. The cannonball is a DynamicGameObject, as it moves according to our simple physics model. The targets are static and can be implemented by using the standard GameObject. The cannon itself can also be implemented via the GameObject class. We will derrive a Cannon class from the GameObject class and add a field storing the cannon's current angle. Listing 8-9 shows the code.

Listing 8-9. Cannon.java: Extending the GameObject with an Angle package com.badlogic.androidgames.gamedev2d;

public class Cannon extends GameObject { public float angle;

public Cannon(float x, float y, float width, float height) { super(x, y, width, height); angle = 0;

That nicely encapsulates all the data needed to represent an object in our cannon world. Every time we need a special kind of object, like the cannon, we can simply derive from GameObject if it is a static object, or DynamicGameObject if it has a velocity and acceleration.

NOTE: The overuse of inheritance can lead to severe headaches and very ugly code architecture. Do not use it just for the sake of using it. The simple class hierarchy just used is OK, but we shouldn't let it go a lot deeper (e.g., by extending Cannon). There are alternative representations of game objects that do away with all inheritance by composition. For our purposes, simple inheritance is more than enough, though. If you are interested in other representations, search for "composites" or "mixins" on the Web.

0 0