|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjgame.JGObject
public class 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). The object speed is automatically multiplied by the game speed (which has default 1.0).
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. Also, they will not expire. 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 |
JGEngineInterface |
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. |
double |
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. |
static double |
gamespeed
The engine's gamespeed setting, stored in a local variable for extra speed. |
boolean |
is_suspended
Indicates if object is suspended. |
static int |
pfheight
The engine's pfWidth/pfHeight, stored in a local variable for extra speed. |
static int |
pfwidth
The engine's pfWidth/pfHeight, stored in a local variable for extra speed. |
static boolean |
pfwrapx
The engine's pfWrap settings, stored in a local variable for extra speed. |
static boolean |
pfwrapy
The engine's pfWrap settings, stored in a local variable for extra speed. |
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. |
static int |
tileheight
The engine's tileWidth and tileHeight, stored in a local variable for extra speed. |
static int |
tilewidth
The engine's tileWidth and tileHeight, stored in a local variable for extra speed. |
static int |
viewheight
The engine's viewWidth and viewHeight, stored in a local variable for extra speed. |
static int |
viewwidth
The engine's viewWidth and viewHeight, stored in a local variable for extra speed. |
static int |
viewxofs
The engine's viewX/YOfs, stored in a local variable for extra speed. |
static int |
viewyofs
The engine's viewX/YOfs, stored in a local variable for extra speed. |
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, old style. |
|
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, old style. |
|
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 expiry,
double xspeed,
double yspeed)
Create object with given absolute speed, expiry, new style. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int expiry,
double xspeed,
double yspeed,
int xdir,
int ydir)
Create object with given direction/speed, expiry, new style. |
|
JGObject(java.lang.String name,
boolean unique_id,
double x,
double y,
int collisionid,
java.lang.String gfxname,
int expiry,
double xspeed,
double yspeed,
int xdir,
int ydir,
JGRectangle tilebbox)
Create object with given tile bbox, direction/speed, expiry, new style. |
|
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, old style. |
|
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, old style. |
|
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, old style. |
|
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, old style. |
|
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, old style. |
|
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, old style. |
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(JGRectangle r)
Check collision of tiles within given rectangle, return the OR of all cids found. |
int |
checkCollision(int cid,
double xofs,
double yofs)
Check collision of this object with other objects, when the object position would be offset by xofs,yofs. |
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 |
void |
destroy()
Override to implement object disposal code. |
void |
frameFinished()
Signal that a new frame has just been updated; make snapshot of object state. |
java.lang.String |
getAnimId()
Get the ID of the currently running animation. |
JGRectangle |
getBBox()
Get object collision bounding box in pixels. |
boolean |
getBBox(JGRectangle bbox_copy)
Copy object collision bounding box in pixels into bbox_copy. |
JGPoint |
getCenterTile()
Get the top left center tile of the object (that is, the x and y of getCenterTiles()). |
JGRectangle |
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. |
JGRectangle |
getImageBBox()
Get collision bounding box of object's image (same as object's default bbox, note that the offset is (0,0) here). |
JGRectangle |
getImageBBoxConst()
Get collision bounding box of object's image (same as object's default bbox, note that the offset is (0,0) here). |
java.lang.String |
getImageName()
Get name of current image. |
double |
getLastX()
Get x position of previous frame. |
double |
getLastY()
Get y position of previous frame. |
java.lang.String |
getName()
Get object's ID |
JGRectangle |
getTileBBox()
Get tile collision bounding box in pixels. |
boolean |
getTileBBox(JGRectangle bbox_copy)
Get tile collision bounding box in pixels and store it in bbox_copy. |
JGRectangle |
getTiles()
Get the tile index coordinates of all the tiles that the object's tile bbox overlaps with. |
JGPoint |
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 |
isAligned()
Returns true if both isXAligned() and isYAligned() are true. |
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 |
moduloPos()
Modulo x/y position according to wrap settings. |
void |
move()
Override to implement automatic move; default is do nothing. |
void |
paint()
Override to define custom paint actions. |
double |
random(double min,
double max)
|
double |
random(double min,
double max,
double interval)
|
int |
random(int min,
int max,
int interval)
|
void |
remove()
Mark object for removal, ignore if already removed. |
void |
removeDone()
Signal to object that remove is done, don't call directly. |
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. |
static boolean |
setEngine(JGEngineInterface engine)
Set the engine to connect to when a new object is created. |
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*gamespeed-epsilon, yspeed*gamespeed-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. |
void |
updateAnimation(double gamespeed)
Do automatic animation. |
static void |
updateEngineSettings()
Called automatically by the engine to signal changes to pfWrap, gameSpeed, pfWidth/Height, viewX/YOfs. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static int viewwidth
public static int viewheight
public static int tilewidth
public static int tileheight
public static boolean pfwrapx
public static boolean pfwrapy
public static double gamespeed
public static int pfwidth
public static int pfheight
public static int viewxofs
public static int viewyofs
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 double expiry
public boolean resume_in_view
public boolean is_suspended
public JGEngineInterface 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 imagepublic JGObject(java.lang.String name, boolean unique_id, double x, double y, int collisionid, java.lang.String gfxname, int expiry, 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 expiry, double xspeed, double yspeed, int xdir, int ydir)
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, double xspeed, double yspeed, int xdir, int ydir, JGRectangle tilebbox)
unique_id
- append name with unique ID if unique_id setgfxname
- id of animation or image, null = no imageMethod Detail |
---|
public static boolean setEngine(JGEngineInterface engine)
engine
- pass null to indicate the engine has exited.
public static void updateEngineSettings()
public void dbgPrint(java.lang.String msg)
public java.lang.String getName()
public java.lang.String getImageName()
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 boolean getBBox(JGRectangle bbox_copy)
public JGRectangle getBBox()
public boolean getTileBBox(JGRectangle bbox_copy)
public JGRectangle getTileBBox()
public JGRectangle getImageBBox()
public JGRectangle getImageBBoxConst()
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 JGRectangle getTiles()
public JGRectangle getCenterTiles()
public JGPoint getCenterTile()
public JGPoint getTopLeftTile()
public boolean isAligned()
isXAligned()
,
isYAligned()
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 checkCollision(int cid, double xofs, double yofs)
cid
- cid mask of objects to consider, 0=anypublic int checkBGCollision(JGRectangle 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 double random(double min, double max)
public double random(double min, double max, double interval)
public int random(int min, int max, int interval)
public void updateAnimation(double gamespeed)
public void frameFinished()
public void moduloPos()
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 final void removeDone()
public void remove()
public void destroy()
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 |