Class Entity

java.lang.Object
model.Entity
All Implemented Interfaces:
EventListener, EntityEventListener
Direct Known Subclasses:
AnimatedEntity, BorderEntity, ShipEntity

public abstract class Entity extends Object implements EntityEventListener
An entity represents any element that appears in the game. The entity is responsible for responding to collisions and movement based on a set of properties defined either by a subclass. You cannot create an Entity object (the class is abstract). You must extend Entity and provide particular features for your entity. Example of this are AlienEntity, ShipEntity and ShotEntity. Note that doubles are used for positions. This may seem strange given that pixels locations are integers. However, using double means that an entity can updateState a partial pixel. It doesn't of course mean that they will be display half way through a pixel but allows us not lose accuracy as we calculate the updateState.
  • Field Details

    • DEF_REF_TO_SPRITE_IMAGE_FILE

      public static final String DEF_REF_TO_SPRITE_IMAGE_FILE
    • DEF_X

      public static final int DEF_X
      See Also:
    • DEF_Y

      public static final int DEF_Y
      See Also:
    • LIFE_LEFT_TOLERANCE

      public static final double LIFE_LEFT_TOLERANCE
      The percentage of life left before we are considered dead
      See Also:
    • DEFAULT_DEATH_RATE

      public static final double DEFAULT_DEATH_RATE
      The dying rate of life left before we are considered dead
      See Also:
    • lifeLeft

      protected double lifeLeft
      The amount of life the entity has left as a fraction of 1.0 Full life is 1.0 Lost half its life then the value would be 0.5 and so on.
    • x

      protected double x
      The current x location of this entity
    • y

      protected double y
      The current y location of this entity
    • sprite

      protected Sprite sprite
      The sprite that represents this entity at any given time
    • dx

      protected double dx
      The current speed of this entity horizontally (pixels/sec)
    • dy

      protected double dy
      The current speed of this entity vertically (pixels/sec)
    • currentState

      protected State currentState
      The current state of the entity
    • dyingSpritesList

      protected List<String> dyingSpritesList
      The sprites to use in DYING state
    • sound

      protected Clip sound
      The sound the entity is making
    • justEnteredState

      protected boolean justEnteredState
      Flag to indicate a state just changed
    • canMoveRight

      protected boolean canMoveRight
      Check allowed movement directions
    • canMoveLeft

      protected boolean canMoveLeft
    • canMoveUp

      protected boolean canMoveUp
    • canMoveDown

      protected boolean canMoveDown
  • Constructor Details

    • Entity

      public Entity()
      ************************* constructors *********************************
    • Entity

      public Entity(String refToSpriteImageFile, int x, int y)
      Construct a entity based on a sprite image and a location.
      Parameters:
      refToSpriteImageFile - The reference to the image to be displayed for this entity
      x - The initial x location of this entity
      y - The initial y location of this entity
  • Method Details

    • getSound

      public Clip getSound()
    • setSound

      public void setSound(Clip sound)
    • getDyingSpritesList

      public List<String> getDyingSpritesList()
      ******************* Setters and Getters
      Returns:
    • setDyingSpritesList

      public void setDyingSpritesList(List<String> dyingSpritesList)
    • setHorizontalMovement

      public void setHorizontalMovement(double dx)
      Set the horizontal speed of this entity
      Parameters:
      dx - The horizontal speed of this entity (pixels/sec)
    • setVerticalMovement

      public void setVerticalMovement(double dy)
      Set the vertical speed of this entity
      Parameters:
      dy - vertical speed
    • getHorizontalMovement

      public double getHorizontalMovement()
      Get the horizontal speed of this entity
      Returns:
      The horizontal speed of this entity (pixels/sec)
    • getVerticalMovement

      public double getVerticalMovement()
      Get the vertical speed of this entity
      Returns:
      The vertical speed of this entity (pixels/sec)
    • getX

      public int getX()
      Get the x location of this entity
      Returns:
      The x location of this entity
    • getY

      public int getY()
      Get the y location of this entity
      Returns:
      The y location of this entity
    • getLifeLeft

      public double getLifeLeft()
      Returns:
      The amount of life this entity has left (1.0 indcated full life)
    • setLifeLeft

      public void setLifeLeft(double lifeLeft)
      Parameters:
      lifeLeft - set the lifeleft the entity
    • getCurrentState

      public State getCurrentState()
    • setCurrentState

      public final void setCurrentState(State newState)
    • setSprite

      public void setSprite(String ref)
      setSprite
      Parameters:
      ref - String
    • equals

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

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

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

      public void updateState(long delta)
      Request that this entity updates its state based on a certain amount of time (in milliseconds) passing. The current state will be used to determine what changes in data are needed (eg sprite, lifeleft etc)
      Parameters:
      delta - The amount of time that has passed in milliseconds
    • move

      public void move(long delta)
      Request that this entity updateState itself based on a certain amount of time (in milliseconds) passing.
      Parameters:
      delta - The amount of time that has passed in milliseconds
    • draw

      public void draw(Graphics g)
      Draw this entity to the graphics context provided
      Parameters:
      g - The graphics context on which to draw
    • collisionArea

      protected Rectangle collisionArea()
      Define the area of the Entity to use for collision detection. This default uses the location and size of sprite.
      Returns:
    • collidesWith

      public boolean collidesWith(Entity other)
      Check if this entity collides with another.
      Parameters:
      other - The other entity to check collision against
      Returns:
      True if the entities collide with each other. Note cannot collide with yourself!
    • moveTo

      public void moveTo(int x, int y)
      Request that this entity move itself to a particular location immediately Be careful with this one as it could jump borders setup by the game
      Parameters:
      x - x ordinate
      y - y ordinate
    • getSprite

      public Sprite getSprite()
    • removeEntityEventListener

      public void removeEntityEventListener(EntityEventListener l)
      Remove a listener from the listener list
      Parameters:
      l - The listener to be removed from the listener list
    • addEntityEventListener

      public final void addEntityEventListener(EntityEventListener l)
      Add a listener to the listener list
      Parameters:
      l - The listener to be added to the listener list
    • fireCollisionHasOccured

      public void fireCollisionHasOccured(EntityHitEvent e)
      Inform listeners that a Collision involving this entity has occurred
      Parameters:
      e - The details of the collision
    • fireEntityMoved

      protected void fireEntityMoved(EntityMoveEvent e)
      Inform listeners that a this entity has moved
      Parameters:
      e - The details of the updateState
    • fireEntityDied

      protected void fireEntityDied(EntityDiedEvent e)
      Inform listeners that this entity has no life left ie its dead
      Parameters:
      e - The details of the death
    • collisionHasOccured

      public void collisionHasOccured(EntityHitEvent e)
      No Action by default
      Specified by:
      collisionHasOccured in interface EntityEventListener
      Parameters:
      e -
    • entityMoved

      public void entityMoved(EntityMoveEvent e)
      No Action by default
      Specified by:
      entityMoved in interface EntityEventListener
      Parameters:
      e -
    • entityDied

      public void entityDied(EntityDiedEvent e)
      Set the state of the entity to DEAD
      Specified by:
      entityDied in interface EntityEventListener
      Parameters:
      e -