Class EntityService

java.lang.Object
com.deco2800.game.entities.EntityService
Direct Known Subclasses:
NpcService, StructureService

public class EntityService extends Object
Provides a global access point for entities to register themselves. This allows for iterating over entities to perform updates each loop. All game entities should be registered here. Avoid adding additional state here! Global access is often the easy but incorrect answer to sharing data.
  • Field Details

  • Constructor Details

    • EntityService

      public EntityService()
  • Method Details

    • register

      public void register(Entity entity)
      Register a new entity with the entity service. The entity will be created and start updating.
      Parameters:
      entity - new entity.
    • registerNamed

      public void registerNamed(String name, Entity entity)
      Registers an entity with a name so it can be found later
      Parameters:
      name - the name to register it as (must be unique or will overwrite)
      entity - the entity to register
    • setCurrentWorldStep

      public void setCurrentWorldStep(Boolean step)
      act as mutex lock to stop concurrent crashes with physics engine
      Parameters:
      step - if physics engine is updating
    • getCurrentWorldStep

      public boolean getCurrentWorldStep()
      Returns:
      if physics engine is updating
    • getToDestroyEntities

      public ArrayList<Entity> getToDestroyEntities()
      Returns:
      list of all entities to destroy
    • setDestroyEntire

      public void setDestroyEntire(ArrayList<Entity> newEntities)
    • addToDestroyEntities

      public void addToDestroyEntities(Entity e)
      Parameters:
      e - entity to destroy
    • getNamedEntity

      public Entity getNamedEntity(String name)
      Returns a registered named entity
      Parameters:
      name - the name the entity was registered as
      Returns:
      the registered entity or null
    • unregisterNamed

      public void unregisterNamed(String key)
    • getLastEntity

      public Entity getLastEntity()
      Returns the last registered entity
      Returns:
      the last registered entity or null
    • getAllNamedEntities

      public Map<String,Entity> getAllNamedEntities()
      Returns all registered entities
      Returns:
      all registered entities or null
    • unregister

      public void unregister(Entity entity)
      Unregister an entity with the entity service. The entity will be removed and stop updating.
      Parameters:
      entity - entity to be removed.
    • removeNamedEntity

      public void removeNamedEntity(String name, Entity entity)
    • update

      public void update()
      Update all registered entities. Should only be called from the main game loop.
    • dispose

      public void dispose()
      Dispose all entities.
    • getEntities

      public Collection<Entity> getEntities()
      Returns:
      Collection of entities stored on the map
    • getEnemyEntities

      public Collection<Entity> getEnemyEntities()
      Returns:
      Collection of entities stored on the map. This must be done for looping within components
    • addEntity

      public void addEntity(Entity newEntity)
      Adds a new entity to hashtable
      Parameters:
      newEntity - new entity to be added to the environment
    • findClosestEntity

      public Entity findClosestEntity(int x, int y)
      Finds the closet entity based off euclidean distance from a given x,y point
      Parameters:
      x - cell cord
      y - cell cord
      Returns:
      Entity closet
    • findClosestEnemy

      public Entity findClosestEnemy(int x, int y)
      Finds the closet entity based off euclidean distance from a given x,y point
      Parameters:
      x - cell cord
      y - cell cord
      Returns:
      Entity closet