Package Green
Class Actor
java.lang.Object
Green.Actor
The base class for all objects in the library. It supports movement, rotation, opacity, custom sprites, custom frame-by-frame drawing, and render order.
It's position system is from top-left to bottom-right.
- Author:
- Zacchary Dempsey-Plante
-
Constructor Summary
ConstructorsConstructorDescriptionActor(float x, float y, int w, int h)
Creates the new actor at a position with a defined width and height.Creates the new actor at a position with a sprite.Creates the new actor at a position with a sprite scaled byscaleMultiplier
.Creates the new actor at a position with a sprite scaled byscaleMultiplier
, resized according toresizeFormat
.Creates the new actor at a position with a sprite stretched to fit the defined width and height.Creates the new actor at a position with a sprite, resized according toresizeFormat
to fit the defined width and height. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
act(float deltaTime)
Called once per frame.void
addedToWorld(World world)
void
draw()
Renders theActor
to the screen.boolean
int
Retrieves the height of theActor
.getImage()
getIntersectingObjects(Class<A> type)
getNeighbours(Class<A> type, float range)
getObjectsAtOffset(float oX, float oY, Class<A> type)
Retrieves a list of all objects matchingtype
within 1 unit of an offset.getObjectsAtOffset(float oX, float oY, Class<A> type, float range)
Retrieves a list of all objects matchingtype
withinrange
of an offset.getObjectsInRange(Class<A> type, float range)
<A extends Actor>
AgetOneIntersectingObject(Class<A> type)
<A extends Actor>
AgetOneObjectAtOffset(float oX, float oY, Class<A> type)
Retrieves the first object matchingtype
within 1 unit of an offset.<A extends Actor>
AgetOneObjectAtOffset(float oX, float oY, Class<A> type, float range)
Retrieves the first object matchingtype
withinrange
of an offset.<A extends Actor>
AgetOneObjectInRange(Class<A> type, float range)
float
Retrieves the opacity value (in the range 0 - 255) of theActor
.int
Retrieves the image resize format of theActor
.float
Retrieves the rotation value (in degrees) of theActor
.getUuid()
int
getWidth()
Retrieves the width of theActor
.getWorld()
static <W extends World>
WgetWorldOfType(Class<W> type)
float
getX()
Retrieves the X-axis position of theActor
.float
getY()
Retrieves the X-axis position of theActor
.float
getZ()
Retrieves the Z-axis position of theActor
.int
hashCode()
boolean
intersects(Actor actor)
boolean
isAtEdge()
boolean
isMouseButtonDownHere(int mouseButton)
Retrieves whether a specific mouse button is currently down on thisActor
.boolean
isMouseButtonDownThisFrameHere(int mouseButton)
Retrieves whether a specific mouse button was pressed this frame on thisActor
.boolean
isMouseButtonUpThisFrameHere(int mouseButton)
Retrieves whether a specific mouse button was released this frame on thisActor
.boolean
Retrieves whether the mouse is currently scrolling on thisActor
.void
move(float amount)
Moves theActor
amount
units in the direction of the actor's rotation.void
moveGlobal(float x, float y)
boolean
pointInBounds(float pX, float pY)
void
removedFromWorld(World world)
void
setDimensions(int w, int h)
Sets the width and height of theActor
.void
setHeight(int h)
Sets the height of theActor
.void
Sets the image of theActor
.void
setLocation(float x, float y)
Sets the X-axis and Y-axis positions of theActor
.void
setOpacity(float opacity)
Sets the opacity value (in the range 0 - 255) of theActor
.void
setResizeFormat(int format)
Sets the image resize format of theActor
.void
setRotation(float rotation)
Sets the rotation of theActor
in degrees.void
setWidth(int w)
Sets the width of theActor
.void
setX(float x)
Sets the X-axis position of theActor
.void
setY(float y)
Sets the Y-axis position of theActor
.void
setZ(float z)
Sets the Z-axis position of theActor
.toString()
void
turn(float degrees)
Rotates theActor
degrees
amount in degrees.void
turnTowards(float x, float y)
void
turnTowards(Actor obj)
-
Constructor Details
-
Actor
Creates the new actor at a position with a sprite.- Parameters:
x
- The X-axis position to start the actor at.y
- The Y-axis position to start the actor at.image
- The image to use with the actor.
-
Actor
public Actor(float x, float y, int w, int h)Creates the new actor at a position with a defined width and height.- Parameters:
x
- The X-axis position to start the actor at.y
- The Y-axis position to start the actor at.w
- The width to create the actor with.h
- The height to create the actor with.
-
Actor
Creates the new actor at a position with a sprite stretched to fit the defined width and height.- Parameters:
x
- The X-axis position to start the actor at.y
- The Y-axis position to start the actor at.image
- The image to use with the actor.w
- The width to create the actor with.h
- The height to create the actor with.
-
Actor
Creates the new actor at a position with a sprite scaled byscaleMultiplier
.- Parameters:
x
- The X-axis position to start the actor at.y
- The Y-axis position to start the actor at.image
- The image to use with the actor.scaleMultiplier
- The multiplier to apply toimage
's dimensions.
-
Actor
Creates the new actor at a position with a sprite, resized according toresizeFormat
to fit the defined width and height.- Parameters:
x
- The X-axis position to start the actor at.y
- The Y-axis position to start the actor at.image
- The image to use with the actor.w
- The width to create the actor with.h
- The height to create the actor with.resizeFormat
- The image resize format to use. It must be eitherGreen.BILINEAR
,Green.NEAREST_NEIGHBOR
, orGreen.TILE
.- Throws:
UnknownResizeFormatException
- Thrown when an unknown resize format is supplied.
-
Actor
Creates the new actor at a position with a sprite scaled byscaleMultiplier
, resized according toresizeFormat
.- Parameters:
x
- The X-axis position to start the actor at.y
- The Y-axis position to start the actor at.image
- The image to use with the actor.scaleMultiplier
- The multiplier to apply toimage
's dimensions.resizeFormat
- The image resize format to use. It must be eitherGreen.BILINEAR
,Green.NEAREST_NEIGHBOR
, orGreen.TILE
.- Throws:
UnknownResizeFormatException
- Thrown when an unknown resize format is supplied.
-
-
Method Details
-
hashCode
public int hashCode() -
equals
-
toString
-
getUuid
Retrieves theUUID
of theActor
. This value is unique to eachActor
instance, and cannot be modified.- Returns:
- The unique instance ID.
-
getX
public final float getX()Retrieves the X-axis position of theActor
.- Returns:
- The X-axis position.
-
getY
public final float getY()Retrieves the X-axis position of theActor
.- Returns:
- The Y-axis position.
-
getZ
public final float getZ()Retrieves the Z-axis position of theActor
. This is only used to determine render order.- Returns:
- The Z-axis position.
-
getRotation
public final float getRotation()Retrieves the rotation value (in degrees) of theActor
.- Returns:
- The rotation in degrees.
-
getWidth
public final int getWidth()Retrieves the width of theActor
.- Returns:
- The width.
-
getHeight
public final int getHeight()Retrieves the height of theActor
.- Returns:
- The height.
-
getSourceImage
- Returns:
- The original associated image.
-
getImage
- Returns:
- The associated image.
-
getOpacity
public final float getOpacity()Retrieves the opacity value (in the range 0 - 255) of theActor
.- Returns:
- The opacity value in range 0 - 255.
-
getResizeFormat
public final int getResizeFormat()Retrieves the image resize format of theActor
.- Returns:
- The image resize format - either
Green.BILINEAR
,Green.NEAREST_NEIGHBOR
, orGreen.TILE
.
-
getWorld
- Returns:
- The world this
Actor
is a part of, that is currently loaded.
-
getWorldOfType
-
setX
public final void setX(float x)Sets the X-axis position of theActor
.- Parameters:
x
- The X-axis position to set.
-
setY
public final void setY(float y)Sets the Y-axis position of theActor
.- Parameters:
y
- The Y-axis position to set.
-
setZ
public final void setZ(float z)Sets the Z-axis position of theActor
. This is only used to determine render order.- Parameters:
z
- The Z-axis position to set.
-
setLocation
public final void setLocation(float x, float y)Sets the X-axis and Y-axis positions of theActor
.- Parameters:
x
- The X-axis position to set.y
- The Y-axis position to set.
-
setRotation
public final void setRotation(float rotation)Sets the rotation of theActor
in degrees.- Parameters:
rotation
- The rotation value to set in degrees.
-
setWidth
public final void setWidth(int w)Sets the width of theActor
.- Parameters:
w
- The width value to set.
-
setHeight
public final void setHeight(int h)Sets the height of theActor
.- Parameters:
h
- The height value to set.
-
setDimensions
public final void setDimensions(int w, int h)Sets the width and height of theActor
.- Parameters:
w
- The width value to set.h
- The height value to set.
-
setImage
Sets the image of theActor
.- Parameters:
image
- ThePImage
to set.
-
setOpacity
public final void setOpacity(float opacity)Sets the opacity value (in the range 0 - 255) of theActor
.- Parameters:
opacity
- The opacity value to set, in range 0 - 255.
-
setResizeFormat
public final void setResizeFormat(int format)Sets the image resize format of theActor
.- Parameters:
format
- The image resize format to set. It must be eitherGreen.BILINEAR
,Green.NEAREST_NEIGHBOR
, orGreen.TILE
.- Throws:
UnknownResizeFormatException
- Thrown when an unknown resize format is supplied.
-
move
public final void move(float amount)Moves theActor
amount
units in the direction of the actor's rotation.- Parameters:
amount
- Number of units to move.
-
moveGlobal
public final void moveGlobal(float x, float y)- Parameters:
x
- Number of units to move in the X-axis.y
- Number of units to move in the Y-axis.
-
turn
public final void turn(float degrees)Rotates theActor
degrees
amount in degrees.- Parameters:
degrees
- The amount to turn in degrees.
-
isAtEdge
- Returns:
- Whether the
Actor
is at the edge of it'sWorld
. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
intersects
-
pointInBounds
public final boolean pointInBounds(float pX, float pY)- Parameters:
pX
- The X-axis coordinate of the point to check.pY
- The Y-axis coordinate of the point to check.- Returns:
- Whether the point is within the bounds of the
Actor
.
-
getIntersectingObjects
public final <A extends Actor> List<A> getIntersectingObjects(Class<A> type) throws NoWorldExceptionRetrieves a list of all objects matchingtype
in theWorld
that intersect with theActor
, using basic rect comparison.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
type
- The type of otherActor
to compare against.- Returns:
- The list of intersecting objects matching
type
. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getOneIntersectingObject
Retrieves the first object matchingtype
in theWorld
that intersects with theActor
, using basic rect comparison.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
type
- The type of otherActor
to compare against.- Returns:
- The first intersecting object matching
type
. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getObjectsAtOffset
public final <A extends Actor> List<A> getObjectsAtOffset(float oX, float oY, Class<A> type, float range) throws NoWorldExceptionRetrieves a list of all objects matchingtype
withinrange
of an offset.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
oX
- The X-axis value of the offset.oY
- The Y-axis value of the offset.type
- The type ofActor
to match.range
- The maximum distance anActor
can be from the offset point to match.- Returns:
- A list of all objects in the
World
matchingtype
at (oX
,oY
) from theActor
's position. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getObjectsAtOffset
public final <A extends Actor> List<A> getObjectsAtOffset(float oX, float oY, Class<A> type) throws NoWorldExceptionRetrieves a list of all objects matchingtype
within 1 unit of an offset.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
oX
- The X-axis value of the offset.oY
- The Y-axis value of the offset.type
- The type ofActor
to match.- Returns:
- A list of all objects in the
World
matchingtype
at (oX
,oY
) from theActor
's position. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getOneObjectAtOffset
public final <A extends Actor> A getOneObjectAtOffset(float oX, float oY, Class<A> type, float range) throws NoWorldExceptionRetrieves the first object matchingtype
withinrange
of an offset.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
oX
- The X-axis value of the offset.oY
- The Y-axis value of the offset.type
- The type ofActor
to match.range
- The maximum distance anActor
can be from the offset point to match.- Returns:
- The first object in the
World
matchingtype
at (oX
,oY
) from theActor
's position. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getOneObjectAtOffset
public final <A extends Actor> A getOneObjectAtOffset(float oX, float oY, Class<A> type) throws NoWorldExceptionRetrieves the first object matchingtype
within 1 unit of an offset.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
oX
- The X-axis value of the offset.oY
- The Y-axis value of the offset.type
- The type ofActor
to match.- Returns:
- The first object in the
World
matchingtype
at (oX
,oY
) from theActor
's position. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getObjectsInRange
public final <A extends Actor> List<A> getObjectsInRange(Class<A> type, float range) throws NoWorldException- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
type
- The type ofActor
to match.range
- The maximum distance anActor
can be from the offset point to match.- Returns:
- A list of all objects in the
World
matchingtype
withinrange
of theActor
. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getOneObjectInRange
public final <A extends Actor> A getOneObjectInRange(Class<A> type, float range) throws NoWorldException- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
type
- The type ofActor
to match.range
- The maximum distance anActor
can be from the offset point to match.- Returns:
- The first object in the
World
matchingtype
withinrange
of theActor
. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
getNeighbours
public final <A extends Actor> List<A> getNeighbours(Class<A> type, float range) throws NoWorldExceptionRetrieves a list of all objects in theWorld
matchingtype
withinrange
of theActor
. Due to the difference in how this library and Greenfoot work, this is just an alias togetObjectsInRange(Class, float)
.- Type Parameters:
A
- The type ofActor
to return if possible, as defined bytype
.- Parameters:
type
- The type ofActor
to match.range
- The maximum distance anActor
can be from the offset point to match.- Returns:
- A list of all objects in the
World
matchingtype
withinrange
of theActor
. - Throws:
NoWorldException
- Thrown when the method is called and theActor
is not part of aWorld
.
-
turnTowards
public final void turnTowards(float x, float y)Rotates theActor
to face it's right side towards the position (x
,y
). Does nothing if the position provided is the position of theActor
.- Parameters:
x
- The X-axis position to turn towards.y
- The Y-axis position to turn towards.
-
turnTowards
- Parameters:
obj
- The otherActor
to turn towards.
-
isMouseButtonDownHere
public final boolean isMouseButtonDownHere(int mouseButton)Retrieves whether a specific mouse button is currently down on thisActor
.- Parameters:
mouseButton
- The mouse button to check for - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
.- Returns:
- Whether
mouseButton
is down.
-
isMouseButtonDownThisFrameHere
public final boolean isMouseButtonDownThisFrameHere(int mouseButton)Retrieves whether a specific mouse button was pressed this frame on thisActor
.- Parameters:
mouseButton
- The mouse button to check for - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
.- Returns:
- Whether
mouseButton
was pressed in this frame.
-
isMouseButtonUpThisFrameHere
public final boolean isMouseButtonUpThisFrameHere(int mouseButton)Retrieves whether a specific mouse button was released this frame on thisActor
.- Parameters:
mouseButton
- The mouse button to check for - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
.- Returns:
- Whether
mouseButton
was released in this frame.
-
isMouseScrollingHere
public final boolean isMouseScrollingHere()Retrieves whether the mouse is currently scrolling on thisActor
.- Returns:
- Whether the mouse is currently scrolling.
-
draw
public void draw() -
addedToWorld
-
removedFromWorld
-
act
public abstract void act(float deltaTime)Called once per frame. This is likely where most work with theActor
will be done.- Parameters:
deltaTime
- The time, in seconds, since the last time this method was called.
-