Class BasicEntity

  • All Implemented Interfaces:
    Entity
    Direct Known Subclasses:
    MovableEntity

    public abstract class BasicEntity
    extends Object
    implements Entity
    A basic implementation of Entity
    Author:
    Tim Neumann
    • Field Detail

      • endOfLastEnqueuedOperation

        protected CompletableFuture<Void> endOfLastEnqueuedOperation
        The completable future representing the completion of the last enqueued operation.
    • Constructor Detail

      • BasicEntity

        public BasicEntity()
    • Method Detail

      • 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.
      • 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 interface Entity
        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
      • sleep

        public void sleep​(int ticks)
        Prevent this entity from performing any long running operation for ticks 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)