Class BasicEntity
- java.lang.Object
-
- de.unistuttgart.informatik.fius.icge.simulation.entity.BasicEntity
-
- All Implemented Interfaces:
Entity
- Direct Known Subclasses:
MovableEntity
public abstract class BasicEntity extends Object implements Entity
A basic implementation ofEntity
- Author:
- Tim Neumann
-
-
Field Summary
Fields Modifier and Type Field Description protected CompletableFuture<Void>
endOfLastEnqueuedOperation
The completable future representing the completion of the last enqueued operation.
-
Constructor Summary
Constructors Constructor Description BasicEntity()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
enqueueToPerformNewOperation(CompletableFuture<Void> endOfNewOperation)
Wait for the last enqueued long running operation to finish before allowing the new Operation to perform.Drawable
getDrawInformation()
protected Playfield
getPlayfield()
Get the playfield of this entity.Position
getPosition()
protected Simulation
getSimulation()
Get the simulation of this entity.protected abstract String
getTextureHandle()
Get the texture handle, with which to get the texture for this entity.protected abstract int
getZPosition()
Get the z position of this entity.void
initOnPlayfield(Playfield playfield)
Method to initialize this entity after being added to the playfield.boolean
isOnPlayfield()
Check whether this entity is on a playfieldvoid
sleep(int ticks)
Prevent this entity from performing any long running operation forticks
simulation ticks.String
toString()
-
-
-
Field Detail
-
endOfLastEnqueuedOperation
protected CompletableFuture<Void> endOfLastEnqueuedOperation
The completable future representing the completion of the last enqueued operation.
-
-
Method Detail
-
getPosition
@InspectionAttribute(readOnly=true) public Position getPosition()
- Specified by:
getPosition
in interfaceEntity
- Returns:
- the position of this entity
- Throws:
EntityNotOnFieldException
- if this entity is not on a playfield
-
getTextureHandle
protected abstract String getTextureHandle()
Get the texture handle, with which to get the texture for this entity.- Returns:
- the texture handle for the texture of this entity
-
getZPosition
protected abstract int getZPosition()
Get the z position of this entity.The z position is used to order entities on the same field.
- Returns:
- the z position of this entity.
-
getDrawInformation
public Drawable getDrawInformation()
- Specified by:
getDrawInformation
in interfaceEntity
- Returns:
- the information required to draw this entity; must not be null
- Throws:
EntityNotOnFieldException
- if this entity is not on a playfield
-
initOnPlayfield
public void initOnPlayfield(Playfield playfield)
Description copied from interface:Entity
Method to initialize this entity after being added to the playfield.This method should not be called from anywhere other than the playfield implementation.
This method needs to be called by the playfield directly before adding the entity to the field.
- Specified by:
initOnPlayfield
in interfaceEntity
- Parameters:
playfield
- The playfield this entity was added to; must not be null
-
isOnPlayfield
public boolean isOnPlayfield()
Check whether this entity is on a playfield- Returns:
- true if and only if this entity is on a playfield
-
getPlayfield
protected Playfield getPlayfield()
Get the playfield of this entity.- Returns:
- the playfield
- Throws:
EntityNotOnFieldException
- if this entity is not on a playfield
-
getSimulation
protected Simulation getSimulation()
Get the simulation of this entity.- Returns:
- the simulation
- Throws:
EntityNotOnFieldException
- if this entity is not on a playfieldIllegalStateException
- if the playfield of this entity is not part of any simulation
-
sleep
public void sleep(int ticks)
Prevent this entity from performing any long running operation forticks
simulation ticks.Only operations that take
>= 1
clock ticks to execute will be affected by this sleep.- Parameters:
ticks
- numberof simulation ticks to pause; must be> 0
- Throws:
IllegalArgumentException
- if ticks is<= 0
-
enqueueToPerformNewOperation
protected void enqueueToPerformNewOperation(CompletableFuture<Void> endOfNewOperation)
Wait for the last enqueued long running operation to finish before allowing the new Operation to perform.Use this method only if you know what you are doing!
Due to the possibility of using an entity from multiple threads, it would be possible for an entity to have multiple long running operations (e.g. turning, walking) at once. To make sure that does not happen it is necessary to only allow one thread per entity to schedule an operation via the simulation clock. This method helps with that.
Call this method before scheduling an operation with the simulation clock.
This method synchronizes on the
operationQueueLock
to make sure that only one thread can pass.The field
endOfLastEnqueuedOperation
keeps track of the operation that is currently performed by this entity.- Parameters:
endOfNewOperation
- The completable future representing the operation to be performed (must be completed when the operation is completed)
-
-