That depends if the MoveableObject
class also takes care of the actual movement of the object (i.e. update its position according to the speed
and direction
members) or just holds the speed
and direction
members so that the engine module of the game will use it to calculate the object's position.
In the latter case I would change the name of the MoveableObject
class to something more appropriate and stick with the aggregation. Otherwise an inheritance will be more appropriate for movable objects. That way the moving logic will be implemented once and the sub classes can take care only for their own specific need.
To explain further if the MoveableObject
is just a data container you can create an interface :
public Interface Moveable {
public MoveableObject getMovementData();
}
and each moveable class will look like :
public class Vampire implements Moveable {
.
.
private MoveableObject movementData; //
.
.
public MoveableObject getMovementData() {
return movementData;
}
}
and in your game engine hold an array of all moving objects (Type moveable) and in the update method get the movement data and process it:
public Class GameEngine {
.
.
private ArrayList<Moveable> movableObjects = .....
.
.
public void gameUpdate() {
for (Moveable moveableObj : movableObjects) {
MoveableObject movementData = moveableObj.getMovementData();
// Process and update as required using the given data
}
}