org.jtgl.gaming
Class Sprite

java.lang.Object
  extended by org.jtgl.gaming.Surface
      extended by org.jtgl.gaming.Sprite

public class Sprite
extends Surface

Sprite class

Author:
Manuel Polo (manuel_polo at yahoo dot es)

Field Summary
static int COLLISION_BOTTOM
          Constant signalling collision with bottom side of lastCollidedRect.
static int COLLISION_BOTTOM_LEFT
          Constant signalling collision with a bottom left corner side of lastCollidedRect.
static int COLLISION_BOTTOM_RIGHT
          Constant signalling collision with a bottom left corner side of lastCollidedRect.
static int COLLISION_LEFT
          Constant signalling collision with left side of lastCollidedRect.
static int COLLISION_RIGHT
          Constant signalling collision with right side of lastCollidedRect.
static int COLLISION_TOP
          Constant signalling collision with top side of lastCollidedRect.
static int COLLISION_TOP_LEFT
          Constant signalling collision with a top left corner side of lastCollidedRect.
static int COLLISION_TOP_RIGHT
          Constant signalling collision with a top left corner side of lastCollidedRect.
 boolean debugCollisionRect
           
 
Constructor Summary
Sprite(ImageMatrix matrix)
          Creates an animated Sprite using a ImageMatrix
Sprite(int width, int height)
          Creates a Sprite surface (Use only for subclasses)
Sprite(JTGLImage staticImage)
          Creates a non animated Sprite using the provided image
Sprite(JTGLImage image, int frameWidth, int frameHeight)
          Creates an animated Sprite using the frames from provided image
Sprite(Sprite sprite)
          Creates a Sprite from another one
 
Method Summary
 void action(long currentTimeMs)
          Performs a timely action by calling performTimedAction.
 void animate(long currentTimeMs)
           
protected  boolean checkTileCollision(TiledSurface tiledSurface, int col, int row)
          Checks where this Sprite collides with surrounding tiles.
 boolean collidesAtWith(int tx, int ty, int x, int y, int width, int height)
           
 boolean collidesAtWith(int tx, int ty, JTGLRect rect)
           
 boolean collidesAtWith(int tx, int ty, Sprite sprite)
           
 boolean collidesAtWith(int tx, int ty, Surface surface)
           
 boolean collidesAtWith(int x, int y, TiledSurface tiledSurface)
           
 boolean collidesAtWithBounds(int tx, int ty, int x, int y, int width, int height)
           
 boolean collidesAtWithBounds(int tx, int ty, JTGLRect rect)
           
 boolean collidesAtWithBounds(int tx, int ty, Sprite sprite)
           
 boolean collidesAtWithBounds(int tx, int ty, Surface surface)
           
 boolean collidesWith(int x, int y, int width, int height)
           
 boolean collidesWith(int x, int y, TiledSurface tiledSurface, boolean saveCollidedRect)
          Test if this Sprite collides at location (x,y) with a non-empty cell from a TiledSurface instance
 boolean collidesWith(JTGLRect rect)
           
 boolean collidesWith(Sprite sprite)
           
 boolean collidesWith(Surface surface)
           
 boolean collidesWith(TiledSurface tiledSurface)
           
 boolean collidesWith(TiledSurface tiledSurface, boolean saveCollidedRect)
           
 boolean collidesWithBounds(int x, int y, int width, int height)
           
 boolean collidesWithBounds(JTGLRect rect)
           
 boolean collidesWithBounds(Sprite sprite)
           
 boolean collidesWithBounds(Surface surface)
           
 void draw(JTGLGraphics g)
           
 JTGLRect getAbsCollisionRectangle()
           
 long getActionDelay()
           
 TimeTrigger getActionTrigger()
           
 long getAnimationDelay()
           
 TimeTrigger getAnimationTrigger()
           
 JTGLRect getCollisionRectangle()
           
 int getFrame()
           
 int getFrameSequenceLength()
           
 int getLastCollidedCol()
           
 JTGLRect getLastCollidedRect()
           
 int getLastCollidedRow()
           
 int getLastCollisionLocation()
          Detects where occurred last collision within a Rect bounds (border limit only!)
 int getMoveX()
           
 int getMoveY()
           
 int getRawFrameCount()
           
 int getSpeed()
           
 void incSpeed(int amount)
           
 boolean isActionEnabled()
           
 boolean isAnimationEnabled()
           
 boolean isLocationValid()
          Default position validation function.
 void move()
          Moves this sprite by moveX,moveY.
 boolean moveRefTo(int x, int y, boolean doValidate)
          Moves the sprite toward the specified position.
 void nextFrame()
           
protected  void performTimedAction(long currentTimeMs)
          Perform actions every actionDelay delay By default moves this sprite by moveX,moveY.
protected  void performTimedAnimation(long currentTimeMs)
           
 boolean preCollidesWith(TiledSurface tiledSurface)
           
 boolean preCollidesWithBounds(int x, int y, int width, int height)
           
 boolean preCollidesWithBounds(JTGLRect rect)
           
 boolean preCollidesWithBounds(Sprite sprite)
           
 boolean preCollidesWithBounds(Surface surface)
           
 void prevFrame()
           
 void setActionDelay(long delay)
           
 void setActionEnabled(boolean enabled)
           
 void setAnimationDelay(long delay)
           
 void setAnimationEnabled(boolean enabled)
           
 void setCollisionRectangle(int x, int y, int width, int height)
           
 void setFrame(int sequenceIndex)
           
 void setInitSequence()
           
 void setMove(int moveX, int moveY)
           
 void setSequence(int[] newSequence)
           
 void setSpeed(int speed)
           
 
Methods inherited from class org.jtgl.gaming.Surface
getAbsRefLocation, getBounds, getCenterLocation, getHeight, getLocation, getRefLocation, getWidth, getX, getY, isVisible, move, setAbsRefLocation, setLocation, setRefLocation, setVisible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COLLISION_TOP

public static final int COLLISION_TOP
Constant signalling collision with top side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_BOTTOM

public static final int COLLISION_BOTTOM
Constant signalling collision with bottom side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_LEFT

public static final int COLLISION_LEFT
Constant signalling collision with left side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_RIGHT

public static final int COLLISION_RIGHT
Constant signalling collision with right side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_TOP_LEFT

public static final int COLLISION_TOP_LEFT
Constant signalling collision with a top left corner side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_TOP_RIGHT

public static final int COLLISION_TOP_RIGHT
Constant signalling collision with a top left corner side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_BOTTOM_LEFT

public static final int COLLISION_BOTTOM_LEFT
Constant signalling collision with a bottom left corner side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

COLLISION_BOTTOM_RIGHT

public static final int COLLISION_BOTTOM_RIGHT
Constant signalling collision with a bottom left corner side of lastCollidedRect.

See Also:
lastCollidedRect, getLastCollidedRect, getLastCollisionLocation, Constant Field Values

debugCollisionRect

public boolean debugCollisionRect
Constructor Detail

Sprite

public Sprite(int width,
              int height)
Creates a Sprite surface (Use only for subclasses)


Sprite

public Sprite(JTGLImage staticImage)
Creates a non animated Sprite using the provided image


Sprite

public Sprite(ImageMatrix matrix)
Creates an animated Sprite using a ImageMatrix


Sprite

public Sprite(JTGLImage image,
              int frameWidth,
              int frameHeight)
Creates an animated Sprite using the frames from provided image


Sprite

public Sprite(Sprite sprite)
Creates a Sprite from another one

Method Detail

setSpeed

public final void setSpeed(int speed)

getSpeed

public final int getSpeed()

incSpeed

public final void incSpeed(int amount)

setMove

public final void setMove(int moveX,
                          int moveY)

getMoveX

public final int getMoveX()

getMoveY

public final int getMoveY()

setActionDelay

public void setActionDelay(long delay)

getActionDelay

public long getActionDelay()

setActionEnabled

public void setActionEnabled(boolean enabled)

isActionEnabled

public boolean isActionEnabled()

getActionTrigger

public TimeTrigger getActionTrigger()

action

public void action(long currentTimeMs)
Performs a timely action by calling performTimedAction.


performTimedAction

protected void performTimedAction(long currentTimeMs)
Perform actions every actionDelay delay By default moves this sprite by moveX,moveY. Subclases may override this method in order to perform any desired action(s).


move

public final void move()
Moves this sprite by moveX,moveY.


setAnimationDelay

public void setAnimationDelay(long delay)

getAnimationDelay

public long getAnimationDelay()

setAnimationEnabled

public void setAnimationEnabled(boolean enabled)

isAnimationEnabled

public boolean isAnimationEnabled()

getAnimationTrigger

public TimeTrigger getAnimationTrigger()

animate

public void animate(long currentTimeMs)

performTimedAnimation

protected void performTimedAnimation(long currentTimeMs)

setSequence

public void setSequence(int[] newSequence)

setInitSequence

public void setInitSequence()

setFrame

public void setFrame(int sequenceIndex)

getFrame

public final int getFrame()

getRawFrameCount

public int getRawFrameCount()

getFrameSequenceLength

public int getFrameSequenceLength()

nextFrame

public void nextFrame()

prevFrame

public void prevFrame()

setCollisionRectangle

public void setCollisionRectangle(int x,
                                  int y,
                                  int width,
                                  int height)

getCollisionRectangle

public JTGLRect getCollisionRectangle()

getAbsCollisionRectangle

public JTGLRect getAbsCollisionRectangle()

getLastCollidedRect

public JTGLRect getLastCollidedRect()

getLastCollidedCol

public int getLastCollidedCol()

getLastCollidedRow

public int getLastCollidedRow()

getLastCollisionLocation

public int getLastCollisionLocation()
Detects where occurred last collision within a Rect bounds (border limit only!)


collidesWith

public boolean collidesWith(int x,
                            int y,
                            int width,
                            int height)

collidesAtWith

public boolean collidesAtWith(int tx,
                              int ty,
                              int x,
                              int y,
                              int width,
                              int height)

collidesWith

public boolean collidesWith(JTGLRect rect)

collidesAtWith

public boolean collidesAtWith(int tx,
                              int ty,
                              JTGLRect rect)

collidesWith

public boolean collidesWith(Surface surface)

collidesAtWith

public boolean collidesAtWith(int tx,
                              int ty,
                              Surface surface)

collidesWith

public boolean collidesWith(Sprite sprite)

collidesAtWith

public boolean collidesAtWith(int tx,
                              int ty,
                              Sprite sprite)

collidesWith

public boolean collidesWith(TiledSurface tiledSurface)

collidesWith

public boolean collidesWith(TiledSurface tiledSurface,
                            boolean saveCollidedRect)

preCollidesWith

public boolean preCollidesWith(TiledSurface tiledSurface)

collidesAtWith

public boolean collidesAtWith(int x,
                              int y,
                              TiledSurface tiledSurface)

collidesWith

public boolean collidesWith(int x,
                            int y,
                            TiledSurface tiledSurface,
                            boolean saveCollidedRect)
Test if this Sprite collides at location (x,y) with a non-empty cell from a TiledSurface instance


checkTileCollision

protected boolean checkTileCollision(TiledSurface tiledSurface,
                                     int col,
                                     int row)
Checks where this Sprite collides with surrounding tiles. This function is called from all collision checker methods that involve a TiledSurface. By default checks if surrounding tiles are non empty, but may be subclassed to check for other tiles.


preCollidesWithBounds

public boolean preCollidesWithBounds(int x,
                                     int y,
                                     int width,
                                     int height)

collidesWithBounds

public boolean collidesWithBounds(int x,
                                  int y,
                                  int width,
                                  int height)

collidesAtWithBounds

public boolean collidesAtWithBounds(int tx,
                                    int ty,
                                    int x,
                                    int y,
                                    int width,
                                    int height)

preCollidesWithBounds

public boolean preCollidesWithBounds(JTGLRect rect)

collidesWithBounds

public boolean collidesWithBounds(JTGLRect rect)

collidesAtWithBounds

public boolean collidesAtWithBounds(int tx,
                                    int ty,
                                    JTGLRect rect)

preCollidesWithBounds

public boolean preCollidesWithBounds(Surface surface)

collidesWithBounds

public boolean collidesWithBounds(Surface surface)

collidesAtWithBounds

public boolean collidesAtWithBounds(int tx,
                                    int ty,
                                    Surface surface)

preCollidesWithBounds

public boolean preCollidesWithBounds(Sprite sprite)

collidesAtWithBounds

public boolean collidesAtWithBounds(int tx,
                                    int ty,
                                    Sprite sprite)

collidesWithBounds

public boolean collidesWithBounds(Sprite sprite)

isLocationValid

public boolean isLocationValid()
Default position validation function.
This function can be overloaded to define your own sprite position validation or collision detection.
The overloaded function may use the sprite data members and should update the sprite position centerX and centerY if needed.

Returns:
true if the (centerX,centerY) is a valid position, false if it's not and the position has been corrected.
NOTE: false returns will stop the movements of the moveRefTo function.
See Also:
moveRefTo(int, int, boolean)

moveRefTo

public boolean moveRefTo(int x,
                         int y,
                         boolean doValidate)
Moves the sprite toward the specified position.
Code taken from SuperWaba Game Lib (http://www.superwaba.org)

Parameters:
x - position.
y - position.
doValidate - if true the position is validated which means that the isLocationValid() function is called.
NOTE: a false return of isLocationValid() will stop the moveRefTo function.
Returns:
true if the defined position has been set
See Also:
isLocationValid()

draw

public void draw(JTGLGraphics g)
Specified by:
draw in class Surface