Class EntityModel
- All Implemented Interfaces:
Configuration,Cloneable,Comparable<EntityModel>
- Direct Known Subclasses:
BarrierModel,SceneryModel,SpriteModel,TextModel
GameController.onOutOfBounds(event.OutOfBoundsEvent[]) call-back.
All bounded entities will disappear once they have left the viewing area
(unless the entity is in wrapped mode). Wrapped-mode entities will not be subject to the
out-of-bounds call-back by the framework. Wrapped-mode entities will reappear at
the edge across from the edge it exits. An entity can be dynamically set to
wrapped or bounded with the setWrappedMode(boolean)
method. Entities have a specific (x,y) location on screen. They have x and y velocities and
acceleration as well. The GameController uses the velocity and acceleration values to
update the instantaneous (x,y) location of all entities that are active and enlisted.
The hit-box for entities are defined about the body-center of each entity. The collision bounds for an unbound entity is the intersection of the viewing area of the entire canvas and the collision bounds defined for that entity.
An unbounded entity crossing over a screen boundary will have only the trailing part of its hit-box participate in collisions until its body-center exits the viewing area. An entity entering the viewing area across a boundary will have the leading area of the collision rectangle active once its body center has entered the viewing area.
The EntityModel primarily plays out a role that is defined by the controller, changing its state based on
control commands from the GameController. The EntityModel can send an
unsolicited message to the GameController with the
notifyController(String) method.
- Author:
- Krish Pillai
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface common.Configuration
Configuration.Direction -
Field Summary
Fields inherited from interface common.Configuration
AUDIO_DIR, DEFAULT_FRAME_RATE, DRAWABLE_DIR, GAMELOGGER, setViewCallChain -
Constructor Summary
ConstructorsConstructorDescriptionEntityModel(float x, float y) Entity located at (x, y).EntityModel(int x, int y) Entity located at (x, y).EntityModel(int x, int y, int layer) Entity located at x, y. -
Method Summary
Modifier and TypeMethodDescriptionclone()booleancollidesWith(EntityModel that) Checks if this entity is in collision with the specified entity.intCompares two entities on the basis of their age.voiddispose()Sets disposed flag to true causing game controller to proceed with removal of the entity.booleandoubleGets the bearing of the entity in degrees.doubleReturns the bearing of the entity in radians.floatThe Coefficient of Restitution (COR), is the ratio of the final to initial relative velocity between two objects after they collide.Return the current collision bounds for this entity.Returns a rectangle defining the absolute area of the screen location of the entity.intReturns the height of the hit-box, the collision bounds for this entity.intReturns the width of the hit-box, the collision bounds for this entity.intgetID()Returns the ID number of this object.intgetLayer()Returns the layer defined for this entity for rendering purposes.floatgetMass()Return the mass of the entity.getName()The name of the entity can be a unique identifier or a tag.floatReturns the scale factor along the x-axis.floatReturns the scale factor along the y-axis.floatgetSpeed()Returns the scalar speed of this entity sqrt(xv^2 + yv^2)intReturns the time-to-live setting of this entity.intgetX()Returns the x coordinate of the body center of this entity.floatReturns the acceleration along the x axis.floatReturns the x coordinate of the body center of this entity.floatReturns the x-velocity of the entityintgetY()Returns the y coordinate of the body center of this entity.floatReturns the y component of the entities acceleration.floatReturns the y coordinate of the body center of this entity.floatReturns the velocity component along the Y axis.inthashCode()Uses the layer and ID of the entity to compute a hash code.booleanisActive()Returns the state of the entity.booleanIndicates the state of the visual representation.booleanReturns the disposed state of the entity.booleanisGhost()Return the collidable state of the entity.booleanReturns the state of the collision bounds display.booleanRenders entity on screen if true.booleanReturns state of view, wrapped or normal.voidpoachState(EntityModel that) Deep-copies the state of this entity into the argument entityvoidResizes collisions bounds to match the width and height of the view.voidClears out the state of the entity.voidsetActive(boolean active) Sets the state of the entity.voidsetAnimation(boolean animate) Sets view iterator state.voidsetCoefficientOfRestitution(float coefficientOfRestitution) COR normally ranges from 0 to 1 where 1 would be a perfectly elastic collision.voidsetCollisionBounds(int width, int height) This method defines the dimensions of the hit-box for this entity.voidsetCollisionBounds(Dimension bounds) Set collision bounds independent of width and height of the sprite.voidsetGhost(boolean ghost) Sets the collidable state for this entity.voidsetLayer(int layer) Sets the layer for this entity.voidsetLocation(float x, float y) Sets x and y coordinates on screenvoidsetMass(float mass) Sets the mass of the entity.voidSets a name for this entity.voidsetScale(float scale) The scaling factor to be applied to the view associated with this entity.voidsetScale(float scaleX, float scaleY) The scaling factor to be applied to the view associated with this entity.voidsetShowBounds(boolean show) Sets the state of the collision bounds display.voidsetTimeToLive(int ttl) Set the number of frames after which this entity will be removed from screen.voidsetVelocityDegrees(float speed, float bearing) East is 0 degrees, South is 90 degrees, West is +/-180, North is -90.voidsetVelocityRadians(float speed, float bearing) East is 0 radians, South is PI/2, North is -PI/2, West is +/-PIvoidsetVisible(boolean visible) Setting an entity visible will make it appear on screen.voidsetWrappedMode(boolean wrapped) Sets the EntityView to wrap around screen boundaries.voidsetXAcceleration(float xAcceleration) Setter for x component of the accelerationvoidsetXLocation(float xLocation) Sets the x coordinate of the body center of this entity.voidsetXVelocity(float xVelocity) Sets the x axis velocity of the entity.voidsetYAcceleration(float yAcceleration) Sets the y component of the entities acceleration.voidsetYLocation(float yLocation) Sets the y coordinate of the body center of this entity.voidsetYVelocity(float yVelocity) Sets the velocity of the entity align the vertical axis.toString()String representation of the entity showing unique ID, location and collision bounds.
-
Constructor Details
-
EntityModel
public EntityModel(float x, float y) Entity located at (x, y). The arguments are screen coordinates with the top-left as the origin (Swing convention).- Parameters:
x- The horizontal displacement from the left edge of the canvas in pixelsy- The vertical displacement from the top edge of the canvas in pixels- See Also:
-
EntityModel
public EntityModel(int x, int y) Entity located at (x, y). The arguments are screen coordinates with the top-left as the origin (Swing convention).- Parameters:
x- The horizontal displacement from the left edge of the canvas in pixelsy- The vertical displacement from the top edge of the canvas in pixels- See Also:
-
EntityModel
public EntityModel(int x, int y, int layer) Entity located at x, y. The arguments are screen coordinates with the top-left as the origin.- Parameters:
x- The horizontal displacement from the left edge of the canvas in pixelsy- The vertical displacement from the top edge of the canvas in pixelslayer- The layer on which this entity is rendered. Higher layers occlude lower layers, zero being the lowest.
-
-
Method Details
-
getXLocation
public float getXLocation()Returns the x coordinate of the body center of this entity.- Returns:
- x X location as float
-
getX
public int getX()Returns the x coordinate of the body center of this entity.- Returns:
- x pixel location as integer
-
setXLocation
public void setXLocation(float xLocation) Sets the x coordinate of the body center of this entity.- Parameters:
xLocation- X coordinate of body center
-
getYLocation
public float getYLocation()Returns the y coordinate of the body center of this entity.- Returns:
- y Y location of body center
-
getY
public int getY()Returns the y coordinate of the body center of this entity.- Returns:
- y pixel location of body center as integer
-
setYLocation
public void setYLocation(float yLocation) Sets the y coordinate of the body center of this entity.- Parameters:
yLocation- The y coordinate
-
setLocation
public void setLocation(float x, float y) Sets x and y coordinates on screen- Parameters:
x- the x locationy- the y location
-
getLayer
public int getLayer()Returns the layer defined for this entity for rendering purposes. Higher layer numbers are painted later and will overlay lower layers.- Returns:
- layer The layer (Z-axis)
-
setLayer
public void setLayer(int layer) Sets the layer for this entity. Layer denotes the Z-axis of the view. Lower layers numbers are occluded by higher layers. Used only for rendering and does not affect collisions.- Parameters:
layer- The layer
-
getXVelocity
public float getXVelocity()Returns the x-velocity of the entity- Returns:
- xVelocity Velocity of the entity along the x axis
-
setXVelocity
public void setXVelocity(float xVelocity) Sets the x axis velocity of the entity.- Parameters:
xVelocity- The velocity along the x axis
-
getYVelocity
public float getYVelocity()Returns the velocity component along the Y axis. Positive values are directed downwards on screen. Negative values are directed to the top of the screen. The origin of the screen coordinate system is the top-left corner.- Returns:
- yVelocity The velocity component along the y axis
-
setYVelocity
public void setYVelocity(float yVelocity) Sets the velocity of the entity align the vertical axis. Positive values point downwards on screen.. The origin is the top-left corner of the canvas.- Parameters:
yVelocity- The velocity component along the y axis
-
setXAcceleration
public void setXAcceleration(float xAcceleration) Setter for x component of the acceleration- Parameters:
xAcceleration- Acceleration along X axis. Positive direction is right wards.
-
setYAcceleration
public void setYAcceleration(float yAcceleration) Sets the y component of the entities acceleration. Top of screen is positive, and bottom is negative.- Parameters:
yAcceleration- Acceleration along y axis. South is negative.
-
getXAcceleration
public float getXAcceleration()Returns the acceleration along the x axis. Positive direction is right wards.- Returns:
- xAcceleration Acceleration along the x axis
-
getYAcceleration
public float getYAcceleration()Returns the y component of the entities acceleration. Negative axis is downwards.- Returns:
- yAcceleration Acceleration along the y axis
-
isActive
public boolean isActive()Returns the state of the entity. Active entities take part in theupdateParameters(long)call. Passive models do not get this call-back invoked.- Returns:
- state Active state of the entity
-
getBearingDegrees
public double getBearingDegrees()Gets the bearing of the entity in degrees. The top-left of the screen is the origin. 0 East, +-180 West, -90 North, and +90 South- Returns:
- bearing the direction in which the entity is headed
-
getBearingRadians
public double getBearingRadians()Returns the bearing of the entity in radians. zero is 3 O'Clock PI/2 is 6 O'Clock +-PI is 9 O'Clock -PI/2 is 12 O'Clock- Returns:
- bearing the heading direction of the entity
-
setActive
public void setActive(boolean active) Sets the state of the entity. Active entities take part in theupdateParameters(long)call. Inactive (passive) models do not get this call-back invoked.- Parameters:
active- State of the entity
-
setAnimation
public void setAnimation(boolean animate) Sets view iterator state.- Parameters:
animate- true or false
-
isAnimated
public boolean isAnimated()Indicates the state of the visual representation. If animated, the view iterator will advance through the frames defined in the image representing this entity.- Returns:
-
setCollisionBounds
public void setCollisionBounds(int width, int height) This method defines the dimensions of the hit-box for this entity. The hit-box is centered about the entity body-center. The hit-box can refer to a smaller area than that presented by the view for this entity.- Parameters:
width- The width of the hit-boxheight- The height of the hit-box
-
getHitboxWidth
public int getHitboxWidth()Returns the width of the hit-box, the collision bounds for this entity.- Returns:
- width Width of the hit-box
-
setVelocityDegrees
public void setVelocityDegrees(float speed, float bearing) East is 0 degrees, South is 90 degrees, West is +/-180, North is -90.- Parameters:
speed- the scalar speed of the entitybearing- in degrees
-
setVelocityRadians
public void setVelocityRadians(float speed, float bearing) East is 0 radians, South is PI/2, North is -PI/2, West is +/-PI- Parameters:
speed- the scalar speed of the entity sqrt(xv^2 + yv^2)bearing- direction in radians
-
getHitboxHeight
public int getHitboxHeight()Returns the height of the hit-box, the collision bounds for this entity.- Returns:
- height The height of the hit-box
-
isGhost
public boolean isGhost()Return the collidable state of the entity. Ghosts do not take part in collisions.- Returns:
- state The ghost setting for this entity
-
setGhost
public void setGhost(boolean ghost) Sets the collidable state for this entity. An entity that is set to 'Ghost' will not take part in collision, but will update its location for each frame.- Parameters:
ghost- True or False (True means entity is a ghost)
-
getCoefficientOfRestitution
public float getCoefficientOfRestitution()The Coefficient of Restitution (COR), is the ratio of the final to initial relative velocity between two objects after they collide. It normally ranges from 0 to 1 where 1 would be a perfectly elastic collision. A perfectly inelastic collision has a coefficient of 0.- Returns:
- cor The Coefficient of Restitution (COR)
-
setCoefficientOfRestitution
public void setCoefficientOfRestitution(float coefficientOfRestitution) COR normally ranges from 0 to 1 where 1 would be a perfectly elastic collision. A perfectly inelastic collision has a coefficient of 0.- Parameters:
coefficientOfRestitution- COR
-
getMass
public float getMass()Return the mass of the entity. The mass will factor into momentum computations for Physics.- Returns:
- mass
- See Also:
-
setMass
public void setMass(float mass) Sets the mass of the entity. The mass will factor into momentum computations for Physics.- Parameters:
mass- The mass of the entity (default is 1.0f)
-
setScale
public void setScale(float scale) The scaling factor to be applied to the view associated with this entity. The scale factor specified will be applied to the width and height of the existing view preserving the existing aspect ratio. The scale factor should be non-zero and positive.- Parameters:
scale- x and y scale factor- Throws:
IllegalArgumentException- A scale of zero or negative is illegal (run-time)- See Also:
-
setScale
public void setScale(float scaleX, float scaleY) The scaling factor to be applied to the view associated with this entity. The scale factor specified will be applied to the width and height of the existing view. Aspect ratio of the original view will change. Scale factor should be positive and non-zero.- Parameters:
scaleX- Horizontal scale factorscaleY- Vertical scale factor- Throws:
IllegalArgumentException- A scale of zero or negative is illegal (run-time)- See Also:
-
getScaleX
public float getScaleX()Returns the scale factor along the x-axis. Cannot be zero or negative.- Returns:
- scaleX Scale factor along the x-axis (horizontal)
-
getScaleY
public float getScaleY()Returns the scale factor along the y-axis. Cannot be zero or negative.- Returns:
- scaleY Scale factor along the y-axis (vertical)
-
getSpeed
public float getSpeed()Returns the scalar speed of this entity sqrt(xv^2 + yv^2)- Returns:
- speed the scalar speed
-
getID
public int getID()Returns the ID number of this object. The ID number is a sequence number indicating the order of appearance (construction) of this entity. The sequence number gets incremented every time a new entity is created and is unique to the instance.- Returns:
- ID The id number of this entity instance
-
isVisible
public boolean isVisible()Renders entity on screen if true. Invisible entities will take part in collisions and out-of-bound events based on ghost and active settings.- Returns:
- state Visible if true
-
poachState
Deep-copies the state of this entity into the argument entity- Parameters:
that- the entity to be initialized
-
setVisible
public void setVisible(boolean visible) Setting an entity visible will make it appear on screen. Invisible entities will be subject to parameter updates (unless made inactive) and subject to collisions (unless turned into a ghost)- Parameters:
visible- True if visible- See Also:
-
resetCollisionBounds
public void resetCollisionBounds()Resizes collisions bounds to match the width and height of the view. -
setCollisionBounds
Set collision bounds independent of width and height of the sprite. Sprites with protuberances may need collision bounds that are smaller hit-box than the hit-box bounding the entire sprite.- Parameters:
bounds- The width and height of the hit-box
-
getCollisionBounds
Return the current collision bounds for this entity. The entity location (x,y) is not available through this call. UsegetCollisionRange()to get the absolute screen area containing the entity.- Returns:
- dim The dimensions (width and height) of the hit-box
- See Also:
-
getCollisionRange
Returns a rectangle defining the absolute area of the screen location of the entity. The rectangle contains the top left corner of the area rect occupied by the entity, and the dimensions of the collision bounds defined for the entity. The rectangle is centered about the location (body-center) of the entity.- rect.x = top left corner x coordinate
- rect.y = top left corner y coordinate
- rect.width = width of the hit box
- rect.height = height of the hit box
- Returns:
- range The collision range of the entity
-
isShowBounds
public boolean isShowBounds()Returns the state of the collision bounds display. Setting this to true will display a hit-box around the entity. This feature is useful for debugging purposes.- Returns:
- state true if the hit-box is to be rendered.
-
setShowBounds
public void setShowBounds(boolean show) Sets the state of the collision bounds display. Setting this to true will display a hit-box around the entity. This feature is useful for debugging purposes.- Parameters:
show- true to turn on the hit-box rendering
-
setTimeToLive
public void setTimeToLive(int ttl) Set the number of frames after which this entity will be removed from screen. TTL (time-to-live) is decremented every frame if it is greater than zero.dispose()is invoked by the controller when ttl becomes zero.- Parameters:
ttl- non-negative time-to-live of this entity
-
getTimeToLive
public int getTimeToLive()Returns the time-to-live setting of this entity.- Returns:
- the time-to-live
-
isDisposed
public boolean isDisposed()Returns the disposed state of the entity.- Returns:
- state Disposed (true) or not (false)
-
dispose
public void dispose()Sets disposed flag to true causing game controller to proceed with removal of the entity. The game controller will deactivate and delist the . Set this flag to remove the entity. Entities once disposed are not usable unless enlisted again. Disposing an entity generates a "disposed : ID" message to the controller, which can be used for game specific cleanup operations. -
setWrappedMode
public void setWrappedMode(boolean wrapped) Sets the EntityView to wrap around screen boundaries. The collision bounds for wrapped entities will be based on the location of the body center of the entity.- Parameters:
wrapped- boolean indicating wrapped state- See Also:
-
EntityView.setWrappedView(boolean)GameController.onOutOfBounds(event.OutOfBoundsEvent[])
-
isWrappedMode
public boolean isWrappedMode()Returns state of view, wrapped or normal. Wrapped view wraps around screen edges and are unbounded. Unbounded entities are not subject to out-of-bounds checks by the controller.- Returns:
- state Wrapped or not
- See Also:
-
setWrappedMode(boolean)GameController.onOutOfBounds(event.OutOfBoundsEvent[])
-
getName
The name of the entity can be a unique identifier or a tag.- Returns:
- the name
-
setName
Sets a name for this entity. Can be used by the controller for identification purposes.- Parameters:
name- the name to set
-
clone
-
resetEntityState
public void resetEntityState()Clears out the state of the entity. -
toString
String representation of the entity showing unique ID, location and collision bounds. -
hashCode
public int hashCode()Uses the layer and ID of the entity to compute a hash code. The ID is a serial number in order of creation starting with zero and counts up with each instantiation. -
equals
-
collidesWith
Checks if this entity is in collision with the specified entity. Returns true if the hit-boxes of this entity intersects that hit-box of the other.- Parameters:
that- The other entity model- Returns:
- true or false
-
compareTo
Compares two entities on the basis of their age. The ID number is used as the order. The ID number counts up each time an entity is created, and indicates the order in which the entity was created.- Specified by:
compareToin interfaceComparable<EntityModel>
-