Class Entity
java.lang.Object
model.Entity
- All Implemented Interfaces:
EventListener,EntityEventListener
- Direct Known Subclasses:
AnimatedEntity,BorderEntity,ShipEntity
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 Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected booleanprotected booleanCheck allowed movement directionsprotected booleanprotected StateThe current state of the entitystatic final Stringstatic final intstatic final intstatic final doubleThe dying rate of life left before we are considered deadprotected doubleThe current speed of this entity horizontally (pixels/sec)protected doubleThe current speed of this entity vertically (pixels/sec)The sprites to use in DYING stateprotected booleanFlag to indicate a state just changedstatic final doubleThe percentage of life left before we are considered deadprotected doubleThe 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.protected ClipThe sound the entity is makingprotected SpriteThe sprite that represents this entity at any given timeprotected doubleThe current x location of this entityprotected doubleThe current y location of this entity -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidAdd a listener to the listener listbooleancollidesWith(Entity other) Check if this entity collides with another.protected RectangleDefine the area of the Entity to use for collision detection.voidNo Action by defaultvoidDraw this entity to the graphics context providedvoidSet the state of the entity to DEADvoidNo Action by defaultboolean************************* Usual Overrides*******************************voidInform listeners that a Collision involving this entity has occurredprotected voidInform listeners that this entity has no life left ie its deadprotected voidInform listeners that a this entity has moved******************* Setters and GettersdoubleGet the horizontal speed of this entitydoublegetSound()doubleGet the vertical speed of this entityintgetX()Get the x location of this entityintgetY()Get the y location of this entityinthashCode()voidmove(long delta) Request that this entity updateState itself based on a certain amount of time (in milliseconds) passing.voidmoveTo(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 gamevoidRemove a listener from the listener listfinal voidsetCurrentState(State newState) voidsetDyingSpritesList(List<String> dyingSpritesList) voidsetHorizontalMovement(double dx) Set the horizontal speed of this entityvoidsetLifeLeft(double lifeLeft) voidvoidsetSpritevoidsetVerticalMovement(double dy) Set the vertical speed of this entitytoString()voidupdateState(long delta) Request that this entity updates its state based on a certain amount of time (in milliseconds) passing.
-
Field Details
-
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_TOLERANCEThe percentage of life left before we are considered dead- See Also:
-
DEFAULT_DEATH_RATE
public static final double DEFAULT_DEATH_RATEThe dying rate of life left before we are considered dead- See Also:
-
lifeLeft
protected double lifeLeftThe 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 xThe current x location of this entity -
y
protected double yThe current y location of this entity -
sprite
The sprite that represents this entity at any given time -
dx
protected double dxThe current speed of this entity horizontally (pixels/sec) -
dy
protected double dyThe current speed of this entity vertically (pixels/sec) -
currentState
The current state of the entity -
dyingSpritesList
-
sound
The sound the entity is making -
justEnteredState
protected boolean justEnteredStateFlag to indicate a state just changed -
canMoveRight
protected boolean canMoveRightCheck allowed movement directions -
canMoveLeft
protected boolean canMoveLeft -
canMoveUp
protected boolean canMoveUp -
canMoveDown
protected boolean canMoveDown
-
-
Constructor Details
-
Entity
public Entity()************************* constructors ********************************* -
Entity
Construct a entity based on a sprite image and a location.- Parameters:
refToSpriteImageFile- The reference to the image to be displayed for this entityx- The initial x location of this entityy- The initial y location of this entity
-
-
Method Details
-
getSound
-
setSound
-
getDyingSpritesList
-
setDyingSpritesList
-
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
-
setCurrentState
-
setSprite
-
equals
-
hashCode
-
toString
-
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
Draw this entity to the graphics context provided- Parameters:
g- The graphics context on which to draw
-
collisionArea
Define the area of the Entity to use for collision detection. This default uses the location and size of sprite.- Returns:
-
collidesWith
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 ordinatey- y ordinate
-
getSprite
-
removeEntityEventListener
Remove a listener from the listener list- Parameters:
l- The listener to be removed from the listener list
-
addEntityEventListener
Add a listener to the listener list- Parameters:
l- The listener to be added to the listener list
-
fireCollisionHasOccured
Inform listeners that a Collision involving this entity has occurred- Parameters:
e- The details of the collision
-
fireEntityMoved
Inform listeners that a this entity has moved- Parameters:
e- The details of the updateState
-
fireEntityDied
Inform listeners that this entity has no life left ie its dead- Parameters:
e- The details of the death
-
collisionHasOccured
No Action by default- Specified by:
collisionHasOccuredin interfaceEntityEventListener- Parameters:
e-
-
entityMoved
No Action by default- Specified by:
entityMovedin interfaceEntityEventListener- Parameters:
e-
-
entityDied
Set the state of the entity to DEAD- Specified by:
entityDiedin interfaceEntityEventListener- Parameters:
e-
-