Package com.deco2800.game.entities
Class EntityService
java.lang.Object
com.deco2800.game.entities.EntityService
- Direct Known Subclasses:
NpcService
,StructureService
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a new entity to hashtablevoid
void
dispose()
Dispose all entities.findClosestEnemy
(int x, int y) Finds the closet entity based off euclidean distance from a given x,y pointfindClosestEntity
(int x, int y) Finds the closet entity based off euclidean distance from a given x,y pointReturns all registered entitiesboolean
Returns the last registered entitygetNamedEntity
(String name) Returns a registered named entityvoid
Register a new entity with the entity service.void
registerNamed
(String name, Entity entity) Registers an entity with a name so it can be found latervoid
removeNamedEntity
(String name, Entity entity) void
setCurrentWorldStep
(Boolean step) act as mutex lock to stop concurrent crashes with physics enginevoid
setDestroyEntire
(ArrayList<Entity> newEntities) void
unregister
(Entity entity) Unregister an entity with the entity service.void
unregisterNamed
(String key) void
update()
Update all registered entities.
-
Field Details
-
toDestroyEntities
-
-
Constructor Details
-
EntityService
public EntityService()
-
-
Method Details
-
register
Register a new entity with the entity service. The entity will be created and start updating.- Parameters:
entity
- new entity.
-
registerNamed
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
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
- Returns:
- list of all entities to destroy
-
setDestroyEntire
-
addToDestroyEntities
- Parameters:
e
- entity to destroy
-
getNamedEntity
Returns a registered named entity- Parameters:
name
- the name the entity was registered as- Returns:
- the registered entity or null
-
unregisterNamed
-
getLastEntity
Returns the last registered entity- Returns:
- the last registered entity or null
-
getAllNamedEntities
Returns all registered entities- Returns:
- all registered entities or null
-
unregister
Unregister an entity with the entity service. The entity will be removed and stop updating.- Parameters:
entity
- entity to be removed.
-
removeNamedEntity
-
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
- Returns:
- Collection of entities stored on the map
-
getEnemyEntities
- Returns:
- Collection of entities stored on the map. This must be done for looping within components
-
addEntity
Adds a new entity to hashtable- Parameters:
newEntity
- new entity to be added to the environment
-
findClosestEntity
Finds the closet entity based off euclidean distance from a given x,y point- Parameters:
x
- cell cordy
- cell cord- Returns:
- Entity closet
-
findClosestEnemy
Finds the closet entity based off euclidean distance from a given x,y point- Parameters:
x
- cell cordy
- cell cord- Returns:
- Entity closet
-