Package role

Class SpriteModel

All Implemented Interfaces:
Configuration, Cloneable, Comparable<EntityModel>

public abstract class SpriteModel extends EntityModel
This class represents the Sprite based model. It creates and sets up a view that is defined for entities represented by sprites.
Author:
Krish Pillai
  • Constructor Details

    • SpriteModel

      public SpriteModel()
      No argument constructor. The entity will be located at the top left corner of the canvas.
    • SpriteModel

      public SpriteModel(int x, int y)
      The instantiated entity will be placed at the location (x,y).
      Parameters:
      x - The x coordinate of the body center
      y - The y coordinate of the body center
    • SpriteModel

      public SpriteModel(int x, int y, int layer)
      The instantiated entity will be placed at the location (x,y) and on the layer (z-axis) specified. Lower layers are obscured by higher layer numbers.
      Parameters:
      x - The x coordinate of the body center
      y - The y coordinate of the body center
      layer - the z layer on which the entity appears
  • Method Details

    • setView

      public void setView(Color color)
      Creates and sets up a colored view for this entity. The appearance is not backed by a real sprite. Should be invoked from onCreate(). Sets color to the specified color. This can be used for creating basic entities of a specific color.
      Parameters:
      color - The color to be applied to this basic entity ((10 x 10 pixel dot))
      See Also:
      • GameController.enlistEntities()
      • EntityModel.setAppearance()
    • setView

      public void setView(String resource)
      Set view based on a drawable sprite sheet. The file should be located in res/drawable. File name should have the proper format specified by the regex "^[a-zA-Z0-9]+-[0-9]+.png$"
      Parameters:
      resource - File containing the drawable
      See Also:
      • GameController.enlistEntities()
      • EntityModel.setAppearance()
    • setFlipped

      public void setFlipped(boolean flip)
      Flips the view of this entity horizontally (lateral inversion)
      Parameters:
      flip - laterally inverts sprite if set to true
    • isFlipped

      public boolean isFlipped()
      Returns the state of the view. True indicates lateral inversion of the view.
      Returns:
      true or false indicating if the image is flipped horizontally or not
    • setView

      public void setView(String resource, int persistence)
      Creates and sets up view with the image specified. The current scale factor is used. Persistence value indicates the number of frames to sustain a specific image from the sprite sheet. This is a way to correlate the animation rate of the sprite with the actual velocity of the entity. File name should have the proper format specified by the regex "^[a-zA-Z0-9]+-[0-9]+.png$"
      Parameters:
      resource - The name of the file (located in res/drawable)
      persistence - Frames to sustain view
    • setView

      public void setView(String resource, float scale, int persistence)
      Creates and sets up view with the image specified. The current scale factor is used. Persistence value indicates the number of frames to sustain a specific image from the sprite sheet. This is a way to correlate the animation rate of the sprite with the actual velocity of the entity. File name should have the proper format specified by the regex "^[a-zA-Z0-9]+-[0-9]+.png$"
      Parameters:
      resource - The name of the file (located in res/drawable)
      scale - Scale factor for the image
      persistence - Frames to sustain view
    • setView

      public void setView(String resource, float scaleX, float scaleY, int persistence)
      Creates and sets up view with the image specified. The current scale factor is used. Persistence value indicates the number of frames to sustain a specific image from the sprite sheet. This is a way to correlate the animation rate of the sprite with the actual velocity of the entity. File name should have the proper format specified by the regex "^[a-zA-Z0-9]+-[0-9]+.png$"
      Parameters:
      resource - The name of the file (located in res/drawable)
      scaleX - Horizontal scale factor for the image
      scaleY - Vertical scale factor for the image
      persistence - Frames to sustain view
    • setView

      public void setView(BufferedImage image)
      Creates and sets up view with the image specified. Should be invoked from onCreate().
      Parameters:
      image - The buffered image to be rendered
      See Also:
      • GameController.enlistEntities()
      • EntityModel.setAppearance()
    • setView

      public void setView(BufferedImage image, float scale, int persistence)
      Creates and sets up view with the image specified. The image is scaled and updated based on persistence value. Should be invoked from onCreate().
      Parameters:
      image - The buffered image to be rendered
      scale - Scale factor along x and y axes
      persistence - Frames to sustain view
      See Also:
      • GameController.enlistEntities()
      • EntityModel.setAppearance()
    • modifySprite

      public void modifySprite(String resource)
      Changes current sprite to the specified sprite.
      Parameters:
      resource - the sprite sheet
    • setAnimate

      public void setAnimate(boolean animated)
      Keeps entity active, but stops the sprite from rolling. The updateParameters() method on the entity will be invoked. The Sprite iterator goes inactive until the animated state is set true.
      Parameters:
      animated - true or false
    • isAnimated

      public boolean isAnimated()
      True state (default) indicates that the sprite is animated. False indicated the sprite iterator has been disabled.
      Overrides:
      isAnimated in class EntityModel
      Returns:
      the state of sprite animation
    • getRotation

      public float getRotation()
      Returns the rotation angle (Clockwise) set for this view. Angle is specified in degrees. The anchor point is the body center of the entity.
      Returns:
      theta the angle of rotation in degrees
      See Also:
    • setRotationAngle

      public void setRotationAngle(float theta)
      The angle in degrees (Clockwise rotation). The anchor point is the object center. Sets the angle by which the sprite should be rotated before rendering.
      Parameters:
      theta - The angle in degrees
      See Also:
    • modifySprite

      public void modifySprite(String resource, float scale, int persistence)
      Changes the sprite for this entity
      Parameters:
      resource - the sprite sheet
      scale - the magnification to be applied
      persistence - the frame count for which each image in the sprite sheet should be displayed
    • modifySprite

      public void modifySprite(BufferedImage image)
      Changes the sprite for this entity
      Parameters:
      image - the new image to be used for this entity
    • modifySprite

      public void modifySprite(BufferedImage image, float scale, int persistence)
      Changes the sprite for this entity
      Parameters:
      image - the new image to be used for this entity
      scale - the magnification to be applied
      persistence - the frame count for which each image in the sprite sheet should be displayed
    • toString

      public String toString()
      Description copied from class: EntityModel
      String representation of the entity showing unique ID, location and collision bounds.
      Overrides:
      toString in class EntityModel
      Returns:
      a string representation of the entity