|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjgame.JGObject
Superclass for game objects, override to define animated game objects. When an object is created, it is automatically registered with the currently running engine. The object will become active only after the frame ends. The object is managed by the engine, which will display it and call the move and hit methods when appropriate. Call remove() to remove the object. It will be removed after the frame ends. Use isAlive() to see if the object has been removed or not.
Each object corresponds to one image. The object's appearance can be changed using setImage or any of the animation functions. If you want multi-image objects, use multiple objects and co-reference them using regular references or using JGEngine's getObject(). You can also define your own paint() method to generate any appearance you want.
Objects have a pointer to the engine by which they are managed (eng). This can be used to call the various useful methods in the engine. Alternatively, the objects can be made inner classes of your JGEngine class, so that they have direct access to all JGEngine methods.
The object remembers some of the state of the previous frame (in particular the previous position and bounding boxes), so that corrective action can be taken after something special happened (such as bumping into a wall).
Objects have a direction and speed built in. After their move() method finishes, their x and y position are incremented with the given speed/direction. Speed may be used as absolute value (leave the direction at 1), or as a value relative to the direction (the movement is speed*dir).
Objects can be suspended, which may be useful for having them sleep until they come into view. When suspended, they are invisible, and their move, hit, and paint methods are not called. They can still be counted by countObjects and removed by removeObjects, if you choose to do so. By default, objects resume operation when they enter the view. This can be disabled by means of the resume_in_view setting. An object that has suspend_off_view enabled will be suspended immediately at creation when it is off view.
Field Summary | |
int |
colid
Collision ID |
JGEngine |
eng
You can use this to call methods in the object's engine. |
static int |
expire_never
Expiry value: never expire. |
static int |
expire_off_pf
Expiry value: expire when off playfield. |
static int |
expire_off_view
Expiry value: expire when out of view. |
int |
expiry
Number of move() steps before object removes itself, -1 (default) is never; -2 means expire when off-playfield, -3 means expire when off-view, -4 means suspend when off-view, -5 means suspend when off-view and expire when off-playfield. |
boolean |
resume_in_view
If true, object will automatically start() when it is suspended and in view. |
static int |
suspend_off_view
Expiry value: suspend when out of view. |
static int |
suspend_off_view_expire_off_pf
Expiry value: suspend when out of view and expire when out of playfield. |
double |
x
Object position |
int |
xdir
Object direction, is multiplied with speed; default=1 |
double |
xspeed
Object speed; default=0 |
double |
y
Object position |
int |
ydir
Object direction, is multiplied with speed; default=1 |
double |
yspeed
Object speed; default=0 |
Constructor Summary | |
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname)
Create object. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
double xspeed,
double yspeed)
Create object with given absolute speed. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
double xspeed,
double yspeed,
int expiry)
Create object with given absolute speed and expiry. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int expiry)
Create object with given expiry. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int xdir,
int ydir,
double xspeed,
double yspeed,
int expiry)
Create object with given direction/speed, expiry. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int tilebbox_x,
int tilebbox_y,
int tilebbox_width,
int tilebbox_height)
Create object with given tile bbox. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int tilebbox_x,
int tilebbox_y,
int tilebbox_width,
int tilebbox_height,
double xspeed,
double yspeed)
Create object with given tile bbox and absolute speed. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int tilebbox_x,
int tilebbox_y,
int tilebbox_width,
int tilebbox_height,
double xspeed,
double yspeed,
int expiry)
Create object with given tile bbox, absolute speed, expiry. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int tilebbox_x,
int tilebbox_y,
int tilebbox_width,
int tilebbox_height,
int expiry)
Create object with given tile bbox and expiry. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int tilebbox_x,
int tilebbox_y,
int tilebbox_width,
int tilebbox_height,
int xdir,
int ydir,
double xspeed,
double yspeed,
int expiry)
Create object with given tile bbox, direction/speed, expiry. |
Method Summary | |
static boolean |
and(int value,
int mask)
A Boolean AND shorthand to use for collision; returns (value&mask) != 0. |
int |
checkBGCollision(double xofs,
double yofs)
Get OR of Tile Cids of the object's current tile bbox at the current position, when offset by the given offset. |
int |
checkBGCollision(java.awt.Rectangle r)
Check collision of tiles within given rectangle, return the OR of all cids found. |
void |
clearAnim()
Clear the animation, the object's current image will remain. |
void |
clearBBox()
Clear bbox definition so that we use the image bbox again. |
void |
clearTileBBox()
Clear tile bbox definition so that we use the regular bbox again. |
void |
dbgPrint(java.lang.String msg)
Print a message to the debug channel, using the object ID as source |
java.lang.String |
getAnimId()
Get the ID of the currently running animation. |
java.awt.Rectangle |
getBBox()
Get object collision bounding box in pixels. |
java.awt.Point |
getCenterTile()
Get the top left center tile of the object (that is, the x and y of getCenterTiles()). |
java.awt.Rectangle |
getCenterTiles()
Get the tile indices spanning the tiles that the object has the most overlap with. |
java.lang.String |
getGraphic()
Get object's current animation ID, or image ID if not defined. |
java.awt.Rectangle |
getImageBBox()
Get collision bounding box of object's image (same as object's default bbox, note that the offset is (0,0) here). |
java.awt.Rectangle |
getLastBBox()
Get object collision bounding box in pixels of previous frame. |
java.awt.Rectangle |
getLastCenterTiles()
|
java.awt.Rectangle |
getLastTileBBox()
Get tile collision bounding box of previous frame. |
java.awt.Rectangle |
getLastTiles()
Get the tile index coordinates of the object's previous tile bbox. |
double |
getLastX()
Get x position of previous frame. |
double |
getLastY()
Get y position of previous frame. |
java.lang.String |
getName()
Get object's ID |
java.awt.Rectangle |
getTileBBox()
Get tile collision bounding box in pixels. |
java.awt.Rectangle |
getTiles()
Get the tile index coordinates of all the tiles that the object's tile bbox overlaps with. |
java.awt.Point |
getTopLeftTile()
Get the topleftmost tile of the object. |
void |
hit_bg(int tilecid)
Override to handle tile collision; default is do nothing. |
void |
hit_bg(int tilecid,
int tx,
int ty)
Override to handle tile collision; default is do nothing. |
void |
hit_bg(int tilecid,
int tx,
int ty,
int txsize,
int tysize)
Override to handle tile collision; default is do nothing. |
void |
hit(JGObject obj)
Override to handle collision; default is do nothing. |
boolean |
isAlive()
Check if object is still active, or has already been removed. |
boolean |
isBottomAligned(double margin)
Returns true if the bottom of the object's tile bbox is within margin of being tile grid aligned. |
boolean |
isInView(int marginx,
int marginy)
Margin is the margin beyond which the object is considered off-view. |
boolean |
isLeftAligned(double margin)
Returns true if the left of the object's tile bbox is within margin of being tile grid aligned. |
boolean |
isOnPF(int marginx,
int marginy)
Margin is the margin beyond which the object is considered off the playfield. |
boolean |
isOnScreen(int marginx,
int marginy)
Deprecated. Use isOnPF and isInView according to your situation. |
boolean |
isRightAligned(double margin)
Returns true if the right of the object's tile bbox is within margin of being tile grid aligned. |
boolean |
isSuspended()
Check if object is suspended. |
boolean |
isTopAligned(double margin)
Returns true if the top of the object's tile bbox is within margin of being tile grid aligned. |
boolean |
isXAligned()
Returns true if x is distance xspeed-epsilon away from being grid aligned. |
boolean |
isXAligned(double margin)
Returns true if x is within margin of being tile grid aligned. |
boolean |
isYAligned()
Returns true if y is distance yspeed-epsilon away from being grid aligned. |
boolean |
isYAligned(double margin)
Returns true if y is within margin of being tile grid aligned. |
void |
move()
Override to implement automatic move; default is do nothing. |
void |
paint()
Override to define custom paint actions. |
void |
remove()
Mark object for removal, ignore if already removed. |
void |
resetAnim()
Reset the animation's state to the start state. |
void |
resetAnim(java.lang.String anim_id)
Always set the animation to the given default animation definition, resetting any changes or updates made to the animation. |
void |
resume()
Resume from suspended state, if suspended. |
void |
setAnim(java.lang.String anim_id)
Set the animation to the given default animation definition, or leave it as it was if the anim_id is unchanged. |
void |
setAnimPingpong(boolean pingpong)
|
void |
setAnimSpeed(double speed)
Set animation speed; speed may be less than 0, indicating that animation should go backwards. |
void |
setBBox(int x,
int y,
int width,
int height)
Set bbox definition to override the image bbox. |
void |
setDir(int xdir,
int ydir)
Set direction. |
void |
setDirSpeed(int xdir,
int ydir,
double speed)
Set speed and direction in one go. |
void |
setDirSpeed(int xdir,
int ydir,
double xspeed,
double yspeed)
Set speed and direction in one go. |
void |
setGraphic(java.lang.String gfxname)
Set ID of animation or image to display. |
void |
setImage(java.lang.String imgname)
Set ID of image to display; clear animation. |
void |
setPos(double x,
double y)
|
void |
setResumeMode(boolean resume_in_view)
Set resume condition. |
void |
setSpeed(double speed)
Set relative speed; the value is copied into xspeed,yspeed. |
void |
setSpeed(double xspeed,
double yspeed)
Set relative speed; the values are copied into xspeed,yspeed. |
void |
setSpeedAbs(double xspeed,
double yspeed)
Set absolute speed. |
void |
setTileBBox(int x,
int y,
int width,
int height)
|
void |
snapBBoxToGrid(double gridsnapx,
double gridsnapy,
boolean snap_right,
boolean snap_bottom)
Snap an object's tile bbox corner to grid; floats are rounded down. |
void |
snapToGrid()
Snap object to grid using the default gridsnap margin of (xspeed-epsilon, yspeed-epsilon), corresponding to the default is...Aligned margin. |
void |
snapToGrid(double gridsnapx,
double gridsnapy)
Snap object to grid. |
void |
startAnim()
|
void |
stopAnim()
|
void |
suspend()
Suspend object until either resume is called or, if resume_in_view is true, when it comes into view. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int expire_never
public static final int expire_off_pf
public static final int expire_off_view
public static final int suspend_off_view
public static final int suspend_off_view_expire_off_pf
public double x
public double y
public double xspeed
public double yspeed
public int xdir
public int ydir
public int colid
public int expiry
public boolean resume_in_view
public JGEngine eng
Constructor Detail |
public JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int expiry)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int tilebbox_x, int tilebbox_y, int tilebbox_width, int tilebbox_height)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int tilebbox_x, int tilebbox_y, int tilebbox_width, int tilebbox_height, int expiry)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, double xspeed, double yspeed)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, double xspeed, double yspeed, int expiry)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int tilebbox_x, int tilebbox_y, int tilebbox_width, int tilebbox_height, double xspeed, double yspeed)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int tilebbox_x, int tilebbox_y, int tilebbox_width, int tilebbox_height, double xspeed, double yspeed, int expiry)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int xdir, int ydir, double xspeed, double yspeed, int expiry)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int tilebbox_x, int tilebbox_y, int tilebbox_width, int tilebbox_height, int xdir, int ydir, double xspeed, double yspeed, int expiry)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imageMethod Detail |
public void dbgPrint(java.lang.String msg)
public java.lang.String getName()
public void clearTileBBox()
public void setTileBBox(int x, int y, int width, int height)
public void setBBox(int x, int y, int width, int height)
public void clearBBox()
public void setPos(double x, double y)
public void setSpeedAbs(double xspeed, double yspeed)
public void setDirSpeed(int xdir, int ydir, double xspeed, double yspeed)
public void setDirSpeed(int xdir, int ydir, double speed)
public void setSpeed(double xspeed, double yspeed)
public void setSpeed(double speed)
public void setDir(int xdir, int ydir)
public void setGraphic(java.lang.String gfxname)
public void setImage(java.lang.String imgname)
public java.lang.String getGraphic()
public void setAnim(java.lang.String anim_id)
public void resetAnim(java.lang.String anim_id)
public void clearAnim()
public java.lang.String getAnimId()
public void setAnimSpeed(double speed)
public void setAnimPingpong(boolean pingpong)
public void startAnim()
public void stopAnim()
public void resetAnim()
public java.awt.Rectangle getBBox()
public java.awt.Rectangle getLastBBox()
public java.awt.Rectangle getTileBBox()
public java.awt.Rectangle getLastTileBBox()
public java.awt.Rectangle getImageBBox()
public double getLastX()
public double getLastY()
public void snapToGrid()
public void snapToGrid(double gridsnapx, double gridsnapy)
gridsnapx
- margin below which to snap, 0.0 is no snapgridsnapy
- margin below which to snap, 0.0 is no snappublic void snapBBoxToGrid(double gridsnapx, double gridsnapy, boolean snap_right, boolean snap_bottom)
snap_right
- snap the right hand side of the tile bboxsnap_bottom
- snap the bottom of the tile bboxpublic java.awt.Rectangle getTiles()
public java.awt.Rectangle getLastTiles()
public java.awt.Rectangle getCenterTiles()
public java.awt.Rectangle getLastCenterTiles()
public java.awt.Point getCenterTile()
public java.awt.Point getTopLeftTile()
public boolean isXAligned()
public boolean isYAligned()
public boolean isXAligned(double margin)
public boolean isYAligned(double margin)
public boolean isLeftAligned(double margin)
public boolean isTopAligned(double margin)
public boolean isRightAligned(double margin)
public boolean isBottomAligned(double margin)
public int checkBGCollision(java.awt.Rectangle r)
public int checkBGCollision(double xofs, double yofs)
public boolean isOnScreen(int marginx, int marginy)
marginx
- pixel margin, may be negativemarginy
- pixel margin, may be negativepublic boolean isInView(int marginx, int marginy)
marginx
- pixel margin, may be negativemarginy
- pixel margin, may be negativepublic boolean isOnPF(int marginx, int marginy)
marginx
- pixel margin, may be negativemarginy
- pixel margin, may be negativepublic static boolean and(int value, int mask)
public void suspend()
public void resume()
public boolean isSuspended()
public void setResumeMode(boolean resume_in_view)
resume_in_view
- resume when in viewpublic boolean isAlive()
public void remove()
public void move()
public void hit(JGObject obj)
public void hit_bg(int tilecid)
public void hit_bg(int tilecid, int tx, int ty, int txsize, int tysize)
public void hit_bg(int tilecid, int tx, int ty)
public void paint()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |