Frage

I'm building a basic game from a brief, and have been given some code to look at. All characters in the game are game objects, some are moveable ones, and some aren't. The moveable ones have a position and direction.

I have been given a MoveableObject class (below). Moveable GameObjects instantiate this class in their own constructor. I have not been guaranteed this code is right, so I'm trying to grasp the theory behind it.

public class MoveableObject {
    int speed;
    String direction;

    public MoveableObject(){
        speed = 0;
        direction = "unknown";
    }

    public MoveableObject(int iSpeed, String sDirection){
        speed = iSpeed;
        direction = sDirection;
    }

and all the moveable characters create an instance of this class in a constructor. For example, from a vampire class.

public class Vampire
    public Vampire(){
            MoveableObject thisObject = new MoveableObject(30, "South-East");
        }
}

To my knowledge, this is known as aggregation. I thought this was to be used in a HAS-A relationship, and not an IS-A. Since a character IS-A moveable object, should I not use inheritance or an interface? I also don't understand why you need to create an instance of a MoveableObject, if each character either is or isn't moveable, surely, you should give them the ability to be moveable or not (through inheritance or interface) and then instantiate the character?

Thanks in advance.

War es hilfreich?

Lösung

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
       }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top