Package gameobjects
Class GameObject
java.lang.Object
gameobjects.GameObject
- Direct Known Subclasses:
AnimatedGameobject
,EgoObject
,FallingStar
,RectObject
,TextObject
The class
GameObject
represents a (possibly animated) object appearing in a level of the
game. It is therefore attached to an instance of the class Playground
. A GameObject has
at least the following properties:
- 2D screen position
- 2D speed
- a name that is unique within a certain
Playground
- a reference to the
Playground
object it belongs to
- a reference to an instance of
ObjectController
that handles the movement logic of the object
- a (circular) radius for simple collision checking. This may be handled differently in
subclasses
draw(Graphics2D)
method. It is this method that must be redefined if a
new appearance should be realized. For introducing new behavior, it is sufficient to supply a
different ObjectController
instance when constructing a GameObject.-
Field Summary
-
Constructor Summary
ConstructorDescriptionGameObject
(String id, Playground playground, double x, double y, double vx, double vy) Constructor to initialize a GameObject, respectively set the currentPlayground
instance this GameObject belongs to.GameObject
(String id, Playground playground, ObjectController controller, double x, double y, double vx, double vy) Constructor to initialize a GameObject, respectively set the current Playground instance this GameObject belongs to. -
Method Summary
Modifier and TypeMethodDescriptionSets the artist to be used for drawing the object onto visible canvas area.void
saves the collider in the internal list of Colliders to be used for this GameObject.Sets the controller to use for this GameObject's logical behavior.boolean
collisionDetection
(GameObject other) Collision detection implemented by iteration through the own list ofCollider
and calling theirCollider.collidesWith(Collider)
method to check collision with the given parameter instance of otherGameObject
.void
draw
(Graphics2D g) Draws the object in its current state.generates and sets collider(s) for this GameObject.double
getId()
return the unique object ID.Access to object controller.getObjectFlag
(String flag) getOrCreateObjectFlag
(String flag, Object createValue) double
getVX()
gets the screen X speed in pixels per frame.double
getVY()
gets the screen Y speed in pixels per frame.double
getX()
gets the screen X position.double
getY()
gets the screen Y position.boolean
isActive()
setActive
(boolean flag) void
sets colliders.void
setComponentProperty
(String comp, String property, Object value) calls via reflection a method of a component if this GameObjects instance and provides the given value as String parameter.void
setObjectController
(ObjectController controller) Sets a new object controller (replaces any former one).void
setObjectFlag
(String flag, Object value) void
setPlayground
(Playground playground) void
setVX
(double vx) set screen x speed in pixel per framevoid
setVY
(double vy) set screen y speed in pixel per frame.void
setX
(double x) set screen x position.void
setY
(double y) set screen y position.void
triggers this GameObjects own controller (if set) to update the object.
-
Field Details
-
RADIUS
public static final int RADIUS- See Also:
-
RECTANGLE
public static final int RECTANGLE- See Also:
-
MASK
public static final int MASK- See Also:
-
id
-
scol
-
-
Constructor Details
-
GameObject
Constructor to initialize a GameObject, respectively set the currentPlayground
instance this GameObject belongs to.- Parameters:
id
- unique ID for this GameObject (should not be null or empty String)playground
- the Playground the GameObject belongs to (should not be null)x
- initial screen position in direction horizontal (positive value including zero)y
- initial screen position in direction vertical (positive value including zero)vx
- initial speed (velocity) in direction horizontal (can be negative, zero, positive)vy
- initial speed (velocity) in direction horizontal (can be negative, zero, positive)
-
GameObject
public GameObject(String id, Playground playground, ObjectController controller, double x, double y, double vx, double vy) Constructor to initialize a GameObject, respectively set the current Playground instance this GameObject belongs to.- Parameters:
id
- unique ID for this GameObject (should not be null or empty String)playground
- the Playground the GameObject belongs to (should not be null)controller
- controller instance to be used for this GameObject (can be null)x
- initial screen position in direction horizontal (positive value including zero)y
- initial screen position in direction vertical (positive value including zero)vx
- initial speed (velocity) in direction horizontal (can be negative, zero, positive)vy
- initial speed (velocity) in direction horizontal (can be negative, zero, positive)
-
-
Method Details
-
setColliders
sets colliders.- Parameters:
l
- LinkedList of Colliders.
-
generateColliders
generates and sets collider(s) for this GameObject. This implementation does nothing. Intended to be overridden by subclasses.- Returns:
- instance of this GameObject (this).
-
addController
Sets the controller to use for this GameObject's logical behavior.- Parameters:
c
- instance to be used.- Returns:
- the current instance (this).
-
addArtist
Sets the artist to be used for drawing the object onto visible canvas area.- Parameters:
a
- instance to be used for callingArtist.draw(Graphics2D)
.- Returns:
- the current instance (this).
-
addCollider
saves the collider in the internal list of Colliders to be used for this GameObject.- Parameters:
c
- instance to be added to internal list
-
getPlayground
-
setPlayground
-
setComponentProperty
calls via reflection a method of a component if this GameObjects instance and provides the given value as String parameter.- Parameters:
comp
- class name of GameObject component. Currently only "controller" is supported, otherwise nothing happens.property
- method name of the component to call.value
- argument to pass to the method as String parameter.
-
setObjectFlag
-
getObjectFlag
-
getOrCreateObjectFlag
-
isActive
public boolean isActive() -
setActive
-
getId
return the unique object ID.- Returns:
- unique object ID
-
getX
public double getX()gets the screen X position.- Returns:
- screen x position
-
getY
public double getY()gets the screen Y position.- Returns:
- screen Y position
-
getVX
public double getVX()gets the screen X speed in pixels per frame.- Returns:
- screen x speed
-
getVY
public double getVY()gets the screen Y speed in pixels per frame.- Returns:
- screen y speed
-
setX
public void setX(double x) set screen x position.- Parameters:
x
- new position
-
setY
public void setY(double y) set screen y position.- Parameters:
y
- new position
-
setVX
public void setVX(double vx) set screen x speed in pixel per frame- Parameters:
vx
- new x speed
-
setVY
public void setVY(double vy) set screen y speed in pixel per frame.- Parameters:
vy
- new y speed.
-
setObjectController
Sets a new object controller (replaces any former one).- Parameters:
controller
- An instance ofObjectController
or one of its subclasses.
-
getObjectController
Access to object controller.- Returns:
- the controller for this object.
-
getGameTime
public double getGameTime() -
collisionDetection
Collision detection implemented by iteration through the own list ofCollider
and calling theirCollider.collidesWith(Collider)
method to check collision with the given parameter instance of otherGameObject
.- Parameters:
other
- instance of the other GameObject to check collision with- Returns:
- true if collision is detected, false otherwise
-
updateObject
public void updateObject()triggers this GameObjects own controller (if set) to update the object.- See Also:
-
controller
-
draw
Draws the object in its current state. Is called by the game engine, should NOT be called otherwise.- Parameters:
g
- object that has all the necessary drawing functionalities
-