Class GameElement

java.lang.Object
rekit.logic.gameelements.GameElement
All Implemented Interfaces:
Collidable
Direct Known Subclasses:
BackgroundElement, Entity, Inanimate, Particle

public abstract class GameElement extends Object implements Collidable

Abstract class that represents an Element that is a visual part of a IScene, eg. a level.

Its situation in the level is defined by the attributes pos (=position), vel (=velocity) and size.

To implement a GameElement, note these important steps:

  • Use super constructor GameElement(Vec startPos, Vec vel, Vec size, Team team)
  • Implement factory method create(Vec startPos, String[] modifiers) for dynamic creation
  • Implement template method logicLoop() to add custom logic.
  • Implement template method internalRender(Field field) to add custom visualization.
Author:
Angelo Aracri, Dominik Fuchss
  • Field Details

    • visible

      protected boolean visible
      Flag that is used to signal if the GameElement is supposed to be rendered or not.
    • deleteMe

      protected boolean deleteMe
      Flag that is used to signal if the GameElement is supposed to be deleted or not.
    • team

      protected Team team
      The Team the GameElement is in that mainly specifies behavior upon collision.
    • deltaTime

      protected long deltaTime
      The latest deltaTime in logicLoop().
  • Constructor Details

    • GameElement

      protected GameElement(Team team)
      Prototype constructor. Use the constructor GameElement(Vec startPos, Vec vel, Vec size, Team team) for regular use and extending.
      Parameters:
      team - the Team of the GameElement
    • GameElement

      protected GameElement(Vec startPos, Vec vel, Vec size, Team team)
      Standard constructor that should be used in every extending class.
      Parameters:
      startPos - the absolute Vec that points to the GameElements initial position.
      vel - the Vec that represents the initial velocity of the GameElement
      size - the Vec that represents the size of the GameElement
      team - the Team that is used for determining if two GameElements can damage each other, etc.
  • Method Details

    • logicLoop

      public void logicLoop()

      Template method that will be called periodically after being added to a IScene.

      Should be overwritten in sub classes for implementing custom logic, physics emulation, time-based actions, ...

    • render

      public final void render(GameGrid f)

      Method that will be called periodically after being added to a IScene.

      Its only task is to call the template method internalRender(Field f) if the GameElement is supposed to be rendered.

      Parameters:
      f - the GameGrid that represents the games field and supplies primitive drawing operations.
    • internalRender

      protected void internalRender(GameGrid f)

      Template method that will be called periodically after being added to a IScene. Must be as performant as possible to keep the FPS low, since it will be called in every render-loop.

      Should be overwritten in sub classes for custom visualization using the GameGrid.

      Parameters:
      f - the GameGrid that represents the games field and supplies primitive drawing operations.
    • create

      public GameElement create(Vec startPos, String... options)

      Stub Factory method that every GameElement must implement in order to being able to be instantiated dynamically by the level creation.

      A startPos must be supplied to position the new GameElement right.

      Also, an array of modifiers that the extending GameElement can define and use is supplied. These are used make level creation able to determine the GameElements behavior. Note: the modifiers are not checked in syntax, so it must be thoroughly checked.

      Parameters:
      startPos - the initial position of the new GameElement.
      options - optional parameters that may determine additional behavior of the GameElement.
      Returns:
      the newly created GameElement.
    • isVisible

      protected boolean isVisible()
      Returns if the GameElement is currently supposed to be rendered or not.
      Returns:
      true if the GameElement should be rendered, false otherwise.
    • getPos

      public final Vec getPos()

      Getter for the GameElements absolute position.

      Note: the position points to the center of the GameElement.

      Returns:
      the current position of the GameElement.
    • setPos

      public final void setPos(Vec value)

      Setter for the GameElements absolute position.

      Note: the position points to the center of the GameElement.

      Parameters:
      value - the new position the GameElement is supposed to have.
    • getVel

      public final Vec getVel()

      Getter for the GameElements velocity.

      Returns:
      the current velocity of the GameElement.
    • setVel

      public final void setVel(Vec newVel)

      Setter for the GameElements velocity.

      Parameters:
      newVel - the new velocity the GameElement is supposed to have.
    • getSize

      public final Vec getSize()

      Getter for the GameElements size.

      Returns:
      the current size of the GameElement.
    • setSize

      public void setSize(Vec size)
      Set the size of the GameElement.
      Parameters:
      size - the size
    • reactToCollision

      public void reactToCollision(GameElement element, Direction dir)
      Description copied from interface: Collidable

      Notification that this GameElement has been collided with from another GameElement element from the given direction dir.

      Can be overwritten for defining an optional reaction that the GameElement performs on the other GameElement and/or itself upon collision.

      Specified by:
      reactToCollision in interface Collidable
      Parameters:
      element - the GameElement that collided with this GameElement
      dir - the Direction this GameElement has been collided from.
    • getFrame

      public Frame getFrame()
      Returns a Frame that represents the collision box the GameElement will be using. Is dependent of the GameElements position and size.
      Returns:
      the GameElements collision box
    • setScene

      public final void setScene(ILevelScene value)
      Setter for the IScene this GameElement belongs to.
      Parameters:
      value - the new IScene the GameElement is in.
    • getScene

      public final ILevelScene getScene()
      Get the current scene.
      Returns:
      the current scene
    • getTeam

      public final Team getTeam()
      Getter for the GameElements Team that mainly specifies behavior upon collision.
      Returns:
      the GameElements Team
    • getZ

      public final byte getZ()

      Getter for an imaginary z-position of the GameElement.

      Is used to determine the rendering order of every GameElement.

      Returns:
      a number that represents the GameElements rendering-order
    • getZHint

      public Integer getZHint()
      Get a hint (for Team.Range.normalize(int) for positioning this GameElement via getZ().
      Returns:
      null if no hint, otherwise the expected position
    • destroy

      public void destroy()

      Method that, upon call, signals that this GameElement is supposed to be deleted in the next logicLoop of the IScene.

      Note: The underlying GameElement might go through several collisions and be rendered multiple times before it is actually deleted.

    • getDeleteMe

      public final boolean getDeleteMe()
      Getter for the flag that specifies if the GameElement is waiting for deletion.
      Returns:
      true if the GameElement is supposed to be deleted, false otherwise.
    • init

      public void init()
      (Re-)Initialize the GameElement.
    • checkCollision

      public final void checkCollision(GameElement e2)
      Check for collision with another element and invoke necessary methods.
      Parameters:
      e2 - the other element
    • killBoost

      public void killBoost()
      Behavior after this GameElement hit an enemy
    • isAddableToGroup

      public boolean isAddableToGroup()
      This method indicates whether this entity can be added to it's group.
      Returns:
      true if addable to a group, false otherwise