JAdventure - Kurs zur Softwareentwicklung
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

1.4.2 Entities

LEVEL

Aus den Beschreibungen entnehmen wir erst einmal, dass ein Level die folgenden Attribute haben soll

  • String name
  • int width
  • int height
  • List<GameObject> childs

Damit lässt sich relativ einfach und schnell eine Klasse Level schreiben:

/**
 * JAdventure Level
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Level {

    /**
     * Name of the Level.
     */
    private String name;

    /**
     * Width of the level.
     */
    private int width;

    /**
     * Height of the level.
     */
    private int height;

    /**
     * Game objects inside the level.
     */
    private final List<GameObject> objects = new ArrayList<>();

    public void removeObject(@NonNull GameObject obj) {
        objects.remove(obj);
    }

    public void addObject(@NonNull GameObject obj, int x, int y) {
        objects.add(obj);
        obj.setPosition(new Position(x, y, this));
    }
}

GameObject

Die Klasse GameObject bekommt die Attribute

  • String name
  • Size size
  • Position position
/**
 * An Object inside the JAdventure game.
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode
public class GameObject {

    /**
     * Name of the Object.
     */
    private String name;

    /**
     * Size of the Object.
     */
    private Size size;

    /**
     * Position of the Object.
     */
    private Position position;
}

Size

Dir Größe eines Objekts soll entweder über eine rechteckige Größe oder eine runde Größe definiert werden. Damit erhalten wir die folgenden Klassen.

Size

/**
 * Size of an GameObject
 */
public abstract class Size { }

CircleSize

/**
 * Size of a circular object.
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode(callSuper=true)
public class CircleSize extends Size {
    /**
     * Radius of the circle around the object location.
     */
    private int radius;
}

RectangleSize

/**
 * Rectangle Size
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode(callSuper=true)
public class RectangleSize extends Size {

    /**
     * Width of the object.
     */
    int width;

    /**
     * Height of the object.
     */
    int height;
}