Genuts API

com.genuts.gameui
Class SpriteCollisionManager

java.lang.Object
  extended bycom.genuts.gameui.SpriteCollisionManager
All Implemented Interfaces:
CollisionManager

public class SpriteCollisionManager
extends java.lang.Object
implements CollisionManager

The sprite collision manager checks collisions in the general case for sprites.

The collision bounding box used for collision checking is the sprite bounding box without collision offsets on each side.

To know if a sprite is in collision with borders of the playfield, it uses the sprite collision bounding box, and to know if a sprite is in collision with another one, the manager calls checkCollision(Sprite) of each sprite. Then if a collision is detected, the collision manager calls fireCollisionEvent(Sprite, Sprite) to dispatch the collision.

The method getSpriteAt(int, int) uses the collision bounding box to optimise the process.

The collision order in a cell is given by sprites' ID; more the ID is high more the priority of the collision is high. For sprites with the same ID, the last validated sprite will have the priority.

See Also:
Sprite, PlayField.fireCollisionEvent(Sprite, Sprite)

Constructor Summary
SpriteCollisionManager(int step)
          Initializes this collision manager with a collision step.
 
Method Summary
 void addSprite(Sprite sprite)
          Adds a sprite in the check list of this collision manager.
 void checkAllCollisions()
          Checks collisions of sprites with all other sprites in the playfield, and with the edges of the playfield.
 void checkCollision(Sprite sprite)
          Checks collision of the given sprite with all other sprites in the playfield, and with the edges of the playfield.
 void drawDebug(java.awt.Graphics g)
          Draws debug informations for this collision manager.
 int getCollisionStep()
          Returns the collision step check of this collision manager.
 PlayField getPlayfield()
          Returns the playfield associated with this collision manager.
 Sprite getSpriteAt(int x, int y)
          Locates the first Sprite that contains the x,y position.
 Sprite getSpriteAtCell(int x, int y)
          Locates the first Sprite which is contained in cell x,y of the array of sprites.
 void invalidateSprite(Sprite sprite)
          Performs actions when a sprite is invalidate.
 void playfieldSizeChanged()
          Informs that the size of the playfield has changed.
 boolean preCheckCollision(Sprite sprite)
          Checks pre-collision of the given sprite with all other sprites in this playfield, and with the edges of this playfield.
 void removeSprite(Sprite sprite)
          Removes a sprite from the check list of this collision manager.
 void reset()
          Resets cache datas for this CollisionManager.
 void setCollisionStep(int step)
          Sets the collision step check of this collision manager.
 void setPlayfield(PlayField playfield)
          Sets the playfield associated with this collision manager.
 void validateSprite(Sprite sprite)
          Performs actions when a sprite is validate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpriteCollisionManager

public SpriteCollisionManager(int step)
Initializes this collision manager with a collision step.

More the step is small, more the collision checking is quicker, but more memory needed is important and more movement of a sprite is slower.

Parameters:
step - Step for collision check.
Method Detail

getCollisionStep

public int getCollisionStep()
Returns the collision step check of this collision manager.
More the step is small, more the collision checking is quicker, but more memory need is important and more movement of a sprite is slower.

Returns:
the collision step of this collision manager.

setCollisionStep

public void setCollisionStep(int step)
Sets the collision step check of this collision manager.

More the step is small, more the collision checking is quicker, but more memory need is important and more movement of a sprite is slower.

Parameters:
step - The collision step for this collision manager.

getPlayfield

public PlayField getPlayfield()
Description copied from interface: CollisionManager
Returns the playfield associated with this collision manager.

Specified by:
getPlayfield in interface CollisionManager
Returns:
The playfield associated with this collision manager.

setPlayfield

public void setPlayfield(PlayField playfield)
Description copied from interface: CollisionManager
Sets the playfield associated with this collision manager.

Specified by:
setPlayfield in interface CollisionManager
Parameters:
playfield - The playfield associated with this collision manager.

playfieldSizeChanged

public void playfieldSizeChanged()
Informs that the size of the playfield has changed.

Be careful with sprites which are out the new size of the playfield, they are simply ignored, and on there next move, a collision will be generated.

Specified by:
playfieldSizeChanged in interface CollisionManager

reset

public void reset()
Description copied from interface: CollisionManager
Resets cache datas for this CollisionManager.

Specified by:
reset in interface CollisionManager

addSprite

public void addSprite(Sprite sprite)
Description copied from interface: CollisionManager
Adds a sprite in the check list of this collision manager.

Generally, this is called by the playfield when a new sprite is added.

Specified by:
addSprite in interface CollisionManager
Parameters:
sprite - Sprite to add

removeSprite

public void removeSprite(Sprite sprite)
Description copied from interface: CollisionManager
Removes a sprite from the check list of this collision manager.

Generally, this is called by the playfield when a sprite is removed.

Specified by:
removeSprite in interface CollisionManager
Parameters:
sprite - Sprite to remove

invalidateSprite

public void invalidateSprite(Sprite sprite)
Description copied from interface: CollisionManager
Performs actions when a sprite is invalidate.

Generally, this is called by a sprite when Sprite.invalidate() is called.

Specified by:
invalidateSprite in interface CollisionManager
Parameters:
sprite - Sprite to invalidate.
See Also:
Sprite.invalidate()

validateSprite

public void validateSprite(Sprite sprite)
Description copied from interface: CollisionManager
Performs actions when a sprite is validate.

Generally, this is called by a sprite when Sprite.validate() is called.

Specified by:
validateSprite in interface CollisionManager
Parameters:
sprite - Sprite to validate.
See Also:
Sprite.validate()

getSpriteAt

public Sprite getSpriteAt(int x,
                          int y)
Locates the first Sprite that contains the x,y position. The top-most child sprite is returned in the case where there is overlap in the sprites. This is determined by finding the sprite with the highest ID that claims to contain the given point via Sprite.isSpriteAt().

Parameters:
x - X coordinate to check
y - Y coordinate to check
Returns:
First sprite object containing the specified point, or null if there is no sprite.
See Also:
Sprite.isSpriteAt(int, int)

getSpriteAtCell

public Sprite getSpriteAtCell(int x,
                              int y)
Locates the first Sprite which is contained in cell x,y of the array of sprites.
The top-most child sprite is returned in the case where there is overlap in the sprites. This is determined by finding the sprite closest to the index 0.

The upper left cell's coordinates are (0, 0).

Parameters:
x - X coordinate of the cell to check
y - Y coordinate of the cell to check
Returns:
First sprite object contained in the specified cell, or null if there is no sprite.

preCheckCollision

public boolean preCheckCollision(Sprite sprite)
Description copied from interface: CollisionManager
Checks pre-collision of the given sprite with all other sprites in this playfield, and with the edges of this playfield.

If one or more pre-collisions are detected, firePreCollisionEvent(Sprite, Sprite) is called.

Specified by:
preCheckCollision in interface CollisionManager
Parameters:
sprite - Sprite to check
Returns:
true to confirm the state of the sprite, false otherwise.
See Also:
CollisionManager.checkCollision(Sprite), PlayField.firePreCollisionEvent(Sprite, Sprite)

checkCollision

public void checkCollision(Sprite sprite)
Description copied from interface: CollisionManager
Checks collision of the given sprite with all other sprites in the playfield, and with the edges of the playfield. If one or more collisions are detected, fireCollisionEvent(Sprite, Sprite) is called.

Specified by:
checkCollision in interface CollisionManager
Parameters:
sprite - Sprite to check
See Also:
PlayField.fireCollisionEvent(Sprite, Sprite)

checkAllCollisions

public void checkAllCollisions()
Description copied from interface: CollisionManager
Checks collisions of sprites with all other sprites in the playfield, and with the edges of the playfield. If one or more collisions are detected, fireCollisionEvent(Sprite, Sprite) is called.

Specified by:
checkAllCollisions in interface CollisionManager
See Also:
PlayField.fireCollisionEvent(Sprite, Sprite)

drawDebug

public void drawDebug(java.awt.Graphics g)
Description copied from interface: CollisionManager
Draws debug informations for this collision manager.

Specified by:
drawDebug in interface CollisionManager
Parameters:
g - The graphics context to use for painting.

Genuts API

Genuts API