Package role

Class EntityModel

java.lang.Object
role.EntityModel
All Implemented Interfaces:
Configuration, Cloneable, Comparable<EntityModel>
Direct Known Subclasses:
BarrierModel, SceneryModel, SpriteModel, TextModel

public abstract class EntityModel extends Object implements Configuration, Cloneable, Comparable<EntityModel>
The entity model is the base representational class for all entities. This abstract class contains its physical attributes, the dynamics of the entity, as well as a view of the entity. An entity may be bounded or unbounded. Bounded entities will be subjected to the 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

  • Constructor Summary

    Constructors
    Constructor
    Description
    EntityModel(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 Type
    Method
    Description
     
    boolean
    Checks if this entity is in collision with the specified entity.
    int
    Compares two entities on the basis of their age.
    void
    Sets disposed flag to true causing game controller to proceed with removal of the entity.
    boolean
     
    double
    Gets the bearing of the entity in degrees.
    double
    Returns the bearing of the entity in radians.
    float
    The 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.
    int
    Returns the height of the hit-box, the collision bounds for this entity.
    int
    Returns the width of the hit-box, the collision bounds for this entity.
    int
    Returns the ID number of this object.
    int
    Returns the layer defined for this entity for rendering purposes.
    float
    Return the mass of the entity.
    The name of the entity can be a unique identifier or a tag.
    float
    Returns the scale factor along the x-axis.
    float
    Returns the scale factor along the y-axis.
    float
    Returns the scalar speed of this entity sqrt(xv^2 + yv^2)
    int
    Returns the time-to-live setting of this entity.
    int
    Returns the x coordinate of the body center of this entity.
    float
    Returns the acceleration along the x axis.
    float
    Returns the x coordinate of the body center of this entity.
    float
    Returns the x-velocity of the entity
    int
    Returns the y coordinate of the body center of this entity.
    float
    Returns the y component of the entities acceleration.
    float
    Returns the y coordinate of the body center of this entity.
    float
    Returns the velocity component along the Y axis.
    int
    Uses the layer and ID of the entity to compute a hash code.
    boolean
    Returns the state of the entity.
    boolean
    Indicates the state of the visual representation.
    boolean
    Returns the disposed state of the entity.
    boolean
    Return the collidable state of the entity.
    boolean
    Returns the state of the collision bounds display.
    boolean
    Renders entity on screen if true.
    boolean
    Returns state of view, wrapped or normal.
    void
    Deep-copies the state of this entity into the argument entity
    void
    Resizes collisions bounds to match the width and height of the view.
    void
    Clears out the state of the entity.
    void
    setActive(boolean active)
    Sets the state of the entity.
    void
    setAnimation(boolean animate)
    Sets view iterator state.
    void
    setCoefficientOfRestitution(float coefficientOfRestitution)
    COR normally ranges from 0 to 1 where 1 would be a perfectly elastic collision.
    void
    setCollisionBounds(int width, int height)
    This method defines the dimensions of the hit-box for this entity.
    void
    Set collision bounds independent of width and height of the sprite.
    void
    setGhost(boolean ghost)
    Sets the collidable state for this entity.
    void
    setLayer(int layer)
    Sets the layer for this entity.
    void
    setLocation(float x, float y)
    Sets x and y coordinates on screen
    void
    setMass(float mass)
    Sets the mass of the entity.
    void
    Sets a name for this entity.
    void
    setScale(float scale)
    The scaling factor to be applied to the view associated with this entity.
    void
    setScale(float scaleX, float scaleY)
    The scaling factor to be applied to the view associated with this entity.
    void
    setShowBounds(boolean show)
    Sets the state of the collision bounds display.
    void
    setTimeToLive(int ttl)
    Set the number of frames after which this entity will be removed from screen.
    void
    setVelocityDegrees(float speed, float bearing)
    East is 0 degrees, South is 90 degrees, West is +/-180, North is -90.
    void
    setVelocityRadians(float speed, float bearing)
    East is 0 radians, South is PI/2, North is -PI/2, West is +/-PI
    void
    setVisible(boolean visible)
    Setting an entity visible will make it appear on screen.
    void
    setWrappedMode(boolean wrapped)
    Sets the EntityView to wrap around screen boundaries.
    void
    setXAcceleration(float xAcceleration)
    Setter for x component of the acceleration
    void
    setXLocation(float xLocation)
    Sets the x coordinate of the body center of this entity.
    void
    setXVelocity(float xVelocity)
    Sets the x axis velocity of the entity.
    void
    setYAcceleration(float yAcceleration)
    Sets the y component of the entities acceleration.
    void
    setYLocation(float yLocation)
    Sets the y coordinate of the body center of this entity.
    void
    setYVelocity(float yVelocity)
    Sets the velocity of the entity align the vertical axis.
    String representation of the entity showing unique ID, location and collision bounds.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • 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 pixels
      y - 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 pixels
      y - 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 pixels
      y - The vertical displacement from the top edge of the canvas in pixels
      layer - 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 location
      y - 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 the updateParameters(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 the updateParameters(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-box
      height - 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 entity
      bearing - 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 factor
      scaleY - 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

      public void poachState(EntityModel that)
      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.
      See Also:
    • setCollisionBounds

      public void setCollisionBounds(Dimension bounds)
      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

      public Dimension getCollisionBounds()
      Return the current collision bounds for this entity. The entity location (x,y) is not available through this call. Use getCollisionRange() to get the absolute screen area containing the entity.
      Returns:
      dim The dimensions (width and height) of the hit-box
      See Also:
    • getCollisionRange

      public Rectangle 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:
    • 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:
    • getName

      public String getName()
      The name of the entity can be a unique identifier or a tag.
      Returns:
      the name
    • setName

      public void setName(String name)
      Sets a name for this entity. Can be used by the controller for identification purposes.
      Parameters:
      name - the name to set
    • clone

      public Object clone()
    • resetEntityState

      public void resetEntityState()
      Clears out the state of the entity.
    • toString

      public String toString()
      String representation of the entity showing unique ID, location and collision bounds.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the entity
    • 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.
      Overrides:
      hashCode in class Object
      Returns:
      code The computed hash code.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • collidesWith

      public boolean collidesWith(EntityModel that)
      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

      public int compareTo(EntityModel o)
      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:
      compareTo in interface Comparable<EntityModel>