Package gameobjects

Class GameObject

java.lang.Object
gameobjects.GameObject
Direct Known Subclasses:
AnimatedGameobject, EgoObject, FallingStar, RectObject, TextObject

public abstract class GameObject extends Object
The class GameObject represents a (possibly animated) object appearing in a level of the game. It is therefore attached to an instance of the class Playground. A GameObject has at least the following properties:
  • 2D screen position
  • 2D speed
  • a name that is unique within a certain Playground
  • a reference to the Playground object it belongs to
  • a reference to an instance of ObjectController that handles the movement logic of the object
  • a (circular) radius for simple collision checking. This may be handled differently in subclasses
The main task of GameObject, or its subclasses, is to draw the object on the screen, which is handles by the draw(Graphics2D) method. It is this method that must be redefined if a new appearance should be realized. For introducing new behavior, it is sufficient to supply a different ObjectController instance when constructing a GameObject.
  • Field Details

  • Constructor Details

    • GameObject

      public GameObject(String id, Playground playground, double x, double y, double vx, double vy)
      Constructor to initialize a GameObject, respectively set the current Playground instance this GameObject belongs to.
      Parameters:
      id - unique ID for this GameObject (should not be null or empty String)
      playground - the Playground the GameObject belongs to (should not be null)
      x - initial screen position in direction horizontal (positive value including zero)
      y - initial screen position in direction vertical (positive value including zero)
      vx - initial speed (velocity) in direction horizontal (can be negative, zero, positive)
      vy - initial speed (velocity) in direction horizontal (can be negative, zero, positive)
    • GameObject

      public GameObject(String id, Playground playground, ObjectController controller, double x, double y, double vx, double vy)
      Constructor to initialize a GameObject, respectively set the current Playground instance this GameObject belongs to.
      Parameters:
      id - unique ID for this GameObject (should not be null or empty String)
      playground - the Playground the GameObject belongs to (should not be null)
      controller - controller instance to be used for this GameObject (can be null)
      x - initial screen position in direction horizontal (positive value including zero)
      y - initial screen position in direction vertical (positive value including zero)
      vx - initial speed (velocity) in direction horizontal (can be negative, zero, positive)
      vy - initial speed (velocity) in direction horizontal (can be negative, zero, positive)
  • Method Details

    • setColliders

      public void setColliders(LinkedList<Collider> l)
      sets colliders.
      Parameters:
      l - LinkedList of Colliders.
    • generateColliders

      public GameObject generateColliders()
      generates and sets collider(s) for this GameObject. This implementation does nothing. Intended to be overridden by subclasses.
      Returns:
      instance of this GameObject (this).
    • addController

      public GameObject addController(ObjectController c)
      Sets the controller to use for this GameObject's logical behavior.
      Parameters:
      c - instance to be used.
      Returns:
      the current instance (this).
    • addArtist

      public GameObject addArtist(Artist a)
      Sets the artist to be used for drawing the object onto visible canvas area.
      Parameters:
      a - instance to be used for calling Artist.draw(Graphics2D).
      Returns:
      the current instance (this).
    • addCollider

      public void addCollider(Collider c)
      saves the collider in the internal list of Colliders to be used for this GameObject.
      Parameters:
      c - instance to be added to internal list
    • getPlayground

      public Playground getPlayground()
    • setPlayground

      public void setPlayground(Playground playground)
    • setComponentProperty

      public void setComponentProperty(String comp, String property, Object value)
      calls via reflection a method of a component if this GameObjects instance and provides the given value as String parameter.
      Parameters:
      comp - class name of GameObject component. Currently only "controller" is supported, otherwise nothing happens.
      property - method name of the component to call.
      value - argument to pass to the method as String parameter.
    • setObjectFlag

      public void setObjectFlag(String flag, Object value)
    • getObjectFlag

      public Object getObjectFlag(String flag)
    • getOrCreateObjectFlag

      public Object getOrCreateObjectFlag(String flag, Object createValue)
    • isActive

      public boolean isActive()
    • setActive

      public GameObject setActive(boolean flag)
    • getId

      public String getId()
      return the unique object ID.
      Returns:
      unique object ID
    • getX

      public double getX()
      gets the screen X position.
      Returns:
      screen x position
    • getY

      public double getY()
      gets the screen Y position.
      Returns:
      screen Y position
    • getVX

      public double getVX()
      gets the screen X speed in pixels per frame.
      Returns:
      screen x speed
    • getVY

      public double getVY()
      gets the screen Y speed in pixels per frame.
      Returns:
      screen y speed
    • setX

      public void setX(double x)
      set screen x position.
      Parameters:
      x - new position
    • setY

      public void setY(double y)
      set screen y position.
      Parameters:
      y - new position
    • setVX

      public void setVX(double vx)
      set screen x speed in pixel per frame
      Parameters:
      vx - new x speed
    • setVY

      public void setVY(double vy)
      set screen y speed in pixel per frame.
      Parameters:
      vy - new y speed.
    • setObjectController

      public void setObjectController(ObjectController controller)
      Sets a new object controller (replaces any former one).
      Parameters:
      controller - An instance of ObjectController or one of its subclasses.
    • getObjectController

      public ObjectController getObjectController()
      Access to object controller.
      Returns:
      the controller for this object.
    • getGameTime

      public double getGameTime()
    • collisionDetection

      public boolean collisionDetection(GameObject other)
      Collision detection implemented by iteration through the own list of Collider and calling their Collider.collidesWith(Collider) method to check collision with the given parameter instance of other GameObject.
      Parameters:
      other - instance of the other GameObject to check collision with
      Returns:
      true if collision is detected, false otherwise
    • updateObject

      public void updateObject()
      triggers this GameObjects own controller (if set) to update the object.
      See Also:
      • controller
    • draw

      public void draw(Graphics2D g)
      Draws the object in its current state. Is called by the game engine, should NOT be called otherwise.
      Parameters:
      g - object that has all the necessary drawing functionalities