game
Class Piece

java.lang.Object
  extended by game.pubsub.BasePublisher<PieceEvent>
      extended by game.Piece
All Implemented Interfaces:
Publisher<PieceEvent>
Direct Known Subclasses:
Ball, Boundary, Brick, Paddle

public class Piece
extends BasePublisher<PieceEvent>

The Piece class is used to represent various components in the game, including bricks, balls, the paddle and the boundaries of the game. It provides methods to move a piece and if this piece interacts with another. It also supports adjustment to its size and to its "life status".

Author:
Jon Turner, adapted from original by Ron Cytron

Field Summary
protected  BoundingBox bb
           
 
Constructor Summary
Piece(BoundingBox bb)
           
 
Method Summary
 boolean contains(lab4.Point p)
          A subclass can override this method to produce more precise intersection calculation.
 void die()
          Causes the piece to die.
 void dieSome(double factor)
          Reduces the life status of the piece by a specified factor and informs all game components that are subscribers of this piece.
 BoundingBox getBB()
           
 lab4.Point getCenter()
           
 lab4.Vector getDirection()
           
 int getHeight()
           
 double getStatus()
           
 int getWidth()
           
 boolean intersects(Piece other)
          The intersects method is used to determine when two pieces have come into contact with one another.
 boolean isDead()
           
 java.lang.Iterable<lab4.Point> points()
          Returns a list of points that are inside the piece.
 void setBB(BoundingBox bb)
          Set the bounding box for the piece.
 void setCenter(lab4.Point p)
          Changes the location of the point and informs subscribers of the change in location.
 void shrink(double factor)
          Informs subscribers that the piece has shrunk.
 
Methods inherited from class game.pubsub.BasePublisher
addSubscriber, notifySubscribers, removeSubscriber
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bb

protected BoundingBox bb
Constructor Detail

Piece

public Piece(BoundingBox bb)
Method Detail

getWidth

public int getWidth()
Returns:
the width of the piece

getHeight

public int getHeight()
Returns:
the height of the piece

getCenter

public lab4.Point getCenter()
Returns:
the center of the piece

getBB

public BoundingBox getBB()
Returns:
the bounding box for the piece

setBB

public void setBB(BoundingBox bb)
Set the bounding box for the piece.

Parameters:
bb - is the new bounding box

getStatus

public double getStatus()
Returns:
the "life status" of the piece

isDead

public boolean isDead()
Returns:
true if the piece is dead

getDirection

public lab4.Vector getDirection()
Returns:
the direction in which the piece last moved; more precisely, the vector difference between the last two center points.

intersects

public boolean intersects(Piece other)
The intersects method is used to determine when two pieces have come into contact with one another. It does this by first comparing the piece's bounding boxes (which is fast) and then for pieces whose bounding boxes intersect, it compares the sets of points with integer coordinates that are inside the pieces. This part, it can be slow if both pieces are really large.

Parameters:
other - the second piece which is checked against this for intersection
Returns:
true if the two pieces intersect, else false.

contains

public boolean contains(lab4.Point p)
A subclass can override this method to produce more precise intersection calculation.

Parameters:
p - is point to be tested for containment
Returns:
true if p is a point inside the piece

points

public java.lang.Iterable<lab4.Point> points()
Returns a list of points that are inside the piece. It does this by checking which points in the bounding box are actually contained in the piece.

Returns:
list of interior points

die

public void die()
Causes the piece to die.


dieSome

public void dieSome(double factor)
Reduces the life status of the piece by a specified factor and informs all game components that are subscribers of this piece.

Parameters:
factor - that is multiplied by the life status; ignored if less than 0 or greater than 1

setCenter

public void setCenter(lab4.Point p)
Changes the location of the point and informs subscribers of the change in location.

Parameters:
p - new location for the center of the piece

shrink

public void shrink(double factor)
Informs subscribers that the piece has shrunk. Subclasses are expected to provide their own shrink class to effect the change in size, then invoke the method in the superclass.

Parameters:
factor - by which the size is changed