Class Entity

java.lang.Object
com.csse3200.game.entities.Entity
All Implemented Interfaces:
com.badlogic.gdx.utils.Json.Serializable

public class Entity extends Object implements com.badlogic.gdx.utils.Json.Serializable
Core entity class. Entities exist in the game and are updated each frame. All entities have a position and scale, but have no default behaviour. Components should be added to an entity to give it specific behaviour. This class should not be inherited or modified directly.

Example use:

 Entity player = new Entity()
         .addComponent(new RenderComponent())
         .addComponent(new PlayerControllerComponent());
 ServiceLocator.getEntityService().register(player);
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Add a component to the entity.
    void
    Create the entity and start running.
    void
    Dispose of the entity.
    void
    Perform an early update on all components.
    boolean
     
    com.badlogic.gdx.math.Vector2
    Get the entity's center position
    <T extends Component>
    T
    Get a component of type T on the entity.
    Get the event handler attached to this entity.
    int
    This entity's unique ID.
    com.badlogic.gdx.math.Vector2
    Get the entity's game position.
    com.badlogic.gdx.math.Vector2
    Get the entity's scale.
    Gets the type of entity
    int
     
    void
    read(com.badlogic.gdx.utils.Json json, com.badlogic.gdx.utils.JsonValue jsonMap)
    Reads the json file and creates the entities based on the information in the json file
    void
    readItem(com.badlogic.gdx.utils.Json json, com.badlogic.gdx.utils.JsonValue jsonMap)
    Reads the item entity from the json file
    void
    readTractor(com.badlogic.gdx.utils.JsonValue jsonMap)
     
    void
    scaleHeight(float y)
    Set the entity's height and scale the width to maintain aspect ratio.
    void
    scaleWidth(float x)
    Set the entity's width and scale the height to maintain aspect ratio.
    void
    setCenterPosition(com.badlogic.gdx.math.Vector2 position)
     
    void
    setEnabled(boolean enabled)
    Enable or disable an entity.
    void
    setPosition(float x, float y)
    Set the entity's game position.
    void
    setPosition(com.badlogic.gdx.math.Vector2 position)
    Set the entity's game position.
    void
    setPosition(com.badlogic.gdx.math.Vector2 position, boolean notify)
    Set the entity's game position and optionally notifies listeners.
    void
    setScale(float x, float y)
    Set the entity's scale.
    void
    setScale(com.badlogic.gdx.math.Vector2 scale)
    Set the entity's scale.
    void
    togglePauseAnimations(boolean pausePlayer)
     
     
    void
    Perform an update on all components.
    void
    write(com.badlogic.gdx.utils.Json json)
    Writes to the json info about entities.
    void
    writeItem(com.badlogic.gdx.utils.Json json)
    Writes the item to the json file

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Entity

      public Entity()
    • Entity

      public Entity(EntityType type)
  • Method Details

    • setEnabled

      public void setEnabled(boolean enabled)
      Enable or disable an entity. Disabled entities do not run update() or earlyUpdate() on their components, but can still be disposed.
      Parameters:
      enabled - true for enable, false for disable.
    • getPosition

      public com.badlogic.gdx.math.Vector2 getPosition()
      Get the entity's game position.
      Returns:
      position
    • setPosition

      public void setPosition(com.badlogic.gdx.math.Vector2 position)
      Set the entity's game position.
      Parameters:
      position - new position.
    • setPosition

      public void setPosition(float x, float y)
      Set the entity's game position.
      Parameters:
      x - new x position
      y - new y position
    • setPosition

      public void setPosition(com.badlogic.gdx.math.Vector2 position, boolean notify)
      Set the entity's game position and optionally notifies listeners.
      Parameters:
      position - new position.
      notify - true to notify (default), false otherwise
    • getScale

      public com.badlogic.gdx.math.Vector2 getScale()
      Get the entity's scale. Used for rendering and physics bounding box calculations.
      Returns:
      Scale in x and y directions. 1 = 1 metre.
    • setScale

      public void setScale(com.badlogic.gdx.math.Vector2 scale)
      Set the entity's scale.
      Parameters:
      scale - new scale in metres
    • setScale

      public void setScale(float x, float y)
      Set the entity's scale.
      Parameters:
      x - width in metres
      y - height in metres
    • scaleWidth

      public void scaleWidth(float x)
      Set the entity's width and scale the height to maintain aspect ratio.
      Parameters:
      x - width in metres
    • scaleHeight

      public void scaleHeight(float y)
      Set the entity's height and scale the width to maintain aspect ratio.
      Parameters:
      y - height in metres
    • getCenterPosition

      public com.badlogic.gdx.math.Vector2 getCenterPosition()
      Get the entity's center position
      Returns:
      center position
    • setCenterPosition

      public void setCenterPosition(com.badlogic.gdx.math.Vector2 position)
    • getComponent

      public <T extends Component> T getComponent(Class<T> type)
      Get a component of type T on the entity.
      Type Parameters:
      T - The component type, e.g. RenderComponent
      Parameters:
      type - The component class, e.g. RenderComponent.class
      Returns:
      The entity component, or null if nonexistent.
    • addComponent

      public Entity addComponent(Component component)
      Add a component to the entity. Can only be called before the entity is registered in the world.
      Parameters:
      component - The component to add. Only one component of a type can be added to an entity.
      Returns:
      Itself
    • dispose

      public void dispose()
      Dispose of the entity. This will dispose of all components on this entity.
    • create

      public void create()
      Create the entity and start running. This is called when the entity is registered in the world, and should not be called manually.
    • earlyUpdate

      public void earlyUpdate()
      Perform an early update on all components. This is called by the entity service and should not be called manually.
    • update

      public void update()
      Perform an update on all components. This is called by the entity service and should not be called manually.
    • togglePauseAnimations

      public void togglePauseAnimations(boolean pausePlayer)
    • getId

      public int getId()
      This entity's unique ID. Used for equality checks
      Returns:
      unique ID
    • getEvents

      public EventHandler getEvents()
      Get the event handler attached to this entity. Can be used to trigger events from an attached component, or listen to events from a component.
      Returns:
      entity's event handler
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • write

      public void write(com.badlogic.gdx.utils.Json json)
      Writes to the json info about entities. Writes the entities x,y coordinates ALso loops through the entities associated components and writes information to the json about the component. note each component should have a override write function
      Specified by:
      write in interface com.badlogic.gdx.utils.Json.Serializable
      Parameters:
      json - which is a valid Json that is written to
    • writeItem

      public void writeItem(com.badlogic.gdx.utils.Json json)
      Writes the item to the json file
      Parameters:
      json - which is a valid Json that is written to
    • readItem

      public void readItem(com.badlogic.gdx.utils.Json json, com.badlogic.gdx.utils.JsonValue jsonMap)
      Reads the item entity from the json file
      Parameters:
      json - which is a valid Json that is read from
      jsonMap - which is a valid JsonValue that is read from
    • read

      public void read(com.badlogic.gdx.utils.Json json, com.badlogic.gdx.utils.JsonValue jsonMap)
      Reads the json file and creates the entities based on the information in the json file
      Specified by:
      read in interface com.badlogic.gdx.utils.Json.Serializable
      Parameters:
      json - which is a valid Json that is read from
      jsonMap - which is a valid JsonValue that is read from
    • getType

      public EntityType getType()
      Gets the type of entity
      Returns:
      the type of entity from EntityType enum
    • readTractor

      public void readTractor(com.badlogic.gdx.utils.JsonValue jsonMap)