simulation_MVC_Basic
Class ShapeSprite

java.lang.Object
  extended by simulation_MVC_Basic.ShapeSprite
All Implemented Interfaces:
Sprite
Direct Known Subclasses:
ProjectileSprite

public class ShapeSprite
extends java.lang.Object
implements Sprite

This class encapsulates a sprite which is an image (as determined by the File that is passed to the constructor). The sprite's initial position is randomly chosen. The sprite will be drawn partially when it encounters the bottom and right boundaries, but not the top and left boundaries. This sprite has a maximum displacement at each frame in each of the horizontal and vertical dimensions. Each sprite has a associated trajectory, given by x and y displacement values. The sprite knows how to draw itself, given a Graphics2D instance. The sprite is alive until it is hit, then it dies and is no longer shown in the world.

Author:
mb

Field Summary
protected  int heightOfDrawingRegion
           
protected  int horizontalDelta
           
protected  java.awt.Image img
           
protected  java.awt.Dimension imgDim
           
static java.util.logging.Logger logger
           
protected  int MAX_DIM
           
protected  int verticalDelta
           
protected  int widthOfDrawingRegion
           
 
Constructor Summary
ShapeSprite(int widthOfDrawingRegion, int heightOfDrawingRegion, java.io.File file, int targetValue)
          Creates a sprite within a game world with the specified dimensions.
 
Method Summary
 void bounceAway()
          Causes this sprite to move one unit in the opposite direction of movement biases (defined for the random movements)
 void draw(java.awt.Graphics2D g)
          The method draws this sprite upon the passed graphics instance.
 void explode()
          Causes this sprite to explode.
protected  boolean getAliveState()
          Return this sprite's state with respect to whether it is alive or not.
 java.awt.Point getAnchor()
          Returns the anchor point of this sprite
 java.awt.geom.Rectangle2D getBounds2D()
          This method returns a rectangular box that completely encloses this sprite.
 java.awt.Dimension getDimension()
          Returns the dimension of this sprite
 void getNudgedRandomly()
          Causes the sprite to get nudged in a random direction.
 int getPointsValue()
          Returns the points value that is associated with hitting this sprite with a projectile
 boolean isCollided(ShapeSprite sprite2)
          Returns a boolean value that indicates whether the passed sprite intersects with this sprite.
 boolean isCollided(Sprite s)
          Determines whether the passed sprite collides with this sprite
 boolean isHit()
          Returns whether this sprite is in the state of having been hit.
 void moveAlongTrajectory()
          Causes the sprite to move along its defined trajectory, at its defined magnitude.
 void moveToCentre()
          Causes the sprite to be relocated to the centre of the game world.
protected  void setAliveState(boolean aliveState)
          Mutates this sprite's state with respect to whether it is alive or not.
 void setAnchor(java.awt.Point newAnchor)
          Set the anchor point of this sprite to the passed value
 java.lang.String toString()
          Generates a string representation of this sprite
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

public static java.util.logging.Logger logger

widthOfDrawingRegion

protected int widthOfDrawingRegion

heightOfDrawingRegion

protected int heightOfDrawingRegion

img

protected java.awt.Image img

MAX_DIM

protected int MAX_DIM

imgDim

protected java.awt.Dimension imgDim

horizontalDelta

protected int horizontalDelta

verticalDelta

protected int verticalDelta
Constructor Detail

ShapeSprite

public ShapeSprite(int widthOfDrawingRegion,
                   int heightOfDrawingRegion,
                   java.io.File file,
                   int targetValue)
Creates a sprite within a game world with the specified dimensions. The sprite's appearance is determined by the image in the passed file. When this sprite is hit, its point value is given by the parameter targetValue

Parameters:
widthOfDrawingRegion - as described above
heightOfDrawingRegion - as described above
file - as described above
targetValue - as described above
Method Detail

draw

public void draw(java.awt.Graphics2D g)
The method draws this sprite upon the passed graphics instance.

Specified by:
draw in interface Sprite
Parameters:
g -

isHit

public boolean isHit()
Returns whether this sprite is in the state of having been hit.

Returns:
as described aboev

explode

public void explode()
Causes this sprite to explode. Presently the explosion is very anti-climatic (it just disappears). Mutates this sprite's state to reflect that it has been hit.


setAliveState

protected void setAliveState(boolean aliveState)
Mutates this sprite's state with respect to whether it is alive or not.


getAliveState

protected boolean getAliveState()
Return this sprite's state with respect to whether it is alive or not.


getNudgedRandomly

public void getNudgedRandomly()
Causes the sprite to get nudged in a random direction. There is a direction bias (left/right, up/down). When the sprite encounters a world boundary, the direction bias will toggle. The distance is randomly chosen, but with a defined maximum displacement


moveAlongTrajectory

public void moveAlongTrajectory()
Causes the sprite to move along its defined trajectory, at its defined magnitude. When the sprite encounters a world boundary, the trajectory will deflect off the encountered boundary. direction bias will toggle.


moveToCentre

public void moveToCentre()
Causes the sprite to be relocated to the centre of the game world.


getDimension

public java.awt.Dimension getDimension()
Returns the dimension of this sprite

Returns:
as described above

getAnchor

public java.awt.Point getAnchor()
Returns the anchor point of this sprite

Returns:
as described above

setAnchor

public void setAnchor(java.awt.Point newAnchor)
Set the anchor point of this sprite to the passed value


isCollided

public boolean isCollided(ShapeSprite sprite2)
Returns a boolean value that indicates whether the passed sprite intersects with this sprite.

Parameters:
sprite2 - the other sprite
Returns:
as described above

getBounds2D

public java.awt.geom.Rectangle2D getBounds2D()
Description copied from interface: Sprite
This method returns a rectangular box that completely encloses this sprite. The bounding box can be assumed to be the smallest bounding box that is possible.

Specified by:
getBounds2D in interface Sprite
Returns:
as described above

isCollided

public boolean isCollided(Sprite s)
Determines whether the passed sprite collides with this sprite

Parameters:
s - as described above
Returns:
as described above

bounceAway

public void bounceAway()
Causes this sprite to move one unit in the opposite direction of movement biases (defined for the random movements)


toString

public java.lang.String toString()
Generates a string representation of this sprite

Overrides:
toString in class java.lang.Object

getPointsValue

public int getPointsValue()
Description copied from interface: Sprite
Returns the points value that is associated with hitting this sprite with a projectile

Specified by:
getPointsValue in interface Sprite
Returns:
as described above