Package Green

Class Actor

java.lang.Object
Green.Actor

public abstract class Actor extends Object
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

    Constructors
    Constructor
    Description
    Actor​(float x, float y, int w, int h)
    Creates the new actor at a position with a defined width and height.
    Actor​(float x, float y, PImage image)
    Creates the new actor at a position with a sprite.
    Actor​(float x, float y, PImage image, float scaleMultiplier)
    Creates the new actor at a position with a sprite scaled by scaleMultiplier.
    Actor​(float x, float y, PImage image, float scaleMultiplier, int resizeFormat)
    Creates the new actor at a position with a sprite scaled by scaleMultiplier, resized according to resizeFormat.
    Actor​(float x, float y, PImage image, int w, int h)
    Creates the new actor at a position with a sprite stretched to fit the defined width and height.
    Actor​(float x, float y, PImage image, int w, int h, int resizeFormat)
    Creates the new actor at a position with a sprite, resized according to resizeFormat to fit the defined width and height.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    act​(float deltaTime)
    Called once per frame.
    void
    addedToWorld​(World world)
    Called when the Actor is added to any World.
    void
    Renders the Actor to the screen.
    boolean
    equals​(Object obj)
     
    int
    Retrieves the height of the Actor.
    Retrieves the PImage associated with the Actor.
    <A extends Actor>
    List<A>
    Retrieves a list of all objects matching type in the World that intersect with the Actor, using basic rect comparison.
    <A extends Actor>
    List<A>
    getNeighbours​(Class<A> type, float range)
    Retrieves a list of all objects in the World matching type within range of the Actor.
    <A extends Actor>
    List<A>
    getObjectsAtOffset​(float oX, float oY, Class<A> type)
    Retrieves a list of all objects matching type within 1 unit of an offset.
    <A extends Actor>
    List<A>
    getObjectsAtOffset​(float oX, float oY, Class<A> type, float range)
    Retrieves a list of all objects matching type within range of an offset.
    <A extends Actor>
    List<A>
    getObjectsInRange​(Class<A> type, float range)
    Retrieves a list of all objects in the World matching type within range of the Actor.
    <A extends Actor>
    A
    Retrieves the first object matching type in the World that intersects with the Actor, using basic rect comparison.
    <A extends Actor>
    A
    getOneObjectAtOffset​(float oX, float oY, Class<A> type)
    Retrieves the first object matching type within 1 unit of an offset.
    <A extends Actor>
    A
    getOneObjectAtOffset​(float oX, float oY, Class<A> type, float range)
    Retrieves the first object matching type within range of an offset.
    <A extends Actor>
    A
    getOneObjectInRange​(Class<A> type, float range)
    Retrieves the first object in the World matching type within range of the Actor.
    float
    Retrieves the opacity value (in the range 0 - 255) of the Actor.
    int
    Retrieves the image resize format of the Actor.
    float
    Retrieves the rotation value (in degrees) of the Actor.
    Retrieves the original PImage (before any scaling has been applied) associated with the Actor.
    Retrieves the UUID of the Actor.
    int
    Retrieves the width of the Actor.
    Retrieves the current World this Actor is in, or null otherwise.
    static <W extends World>
    W
    getWorldOfType​(Class<W> type)
    Retrieves the current World this Actor is in as type if possible, or null otherwise.
    float
    Retrieves the X-axis position of the Actor.
    float
    Retrieves the X-axis position of the Actor.
    float
    Retrieves the Z-axis position of the Actor.
    int
     
    boolean
    intersects​(Actor actor)
    Checks to see whether the Actor intersects another Actor, using basic rect comparison.
    boolean
    Checks to see if the Actor is at the edge of the World it is currently in.
    boolean
    isMouseButtonDownHere​(int mouseButton)
    Retrieves whether a specific mouse button is currently down on this Actor.
    boolean
    isMouseButtonDownThisFrameHere​(int mouseButton)
    Retrieves whether a specific mouse button was pressed this frame on this Actor.
    boolean
    isMouseButtonUpThisFrameHere​(int mouseButton)
    Retrieves whether a specific mouse button was released this frame on this Actor.
    boolean
    Retrieves whether the mouse is currently scrolling on this Actor.
    void
    move​(float amount)
    Moves the Actor amount units in the direction of the actor's rotation.
    void
    moveGlobal​(float x, float y)
    Moves the Actor x units on the X-axis and y units on the Y-axis.
    boolean
    pointInBounds​(float pX, float pY)
    Checks to see if a point defined by pX and pY is within the Actor's rotated rect.
    void
    Called when the Actor is removed from any World.
    void
    setDimensions​(int w, int h)
    Sets the width and height of the Actor.
    void
    setHeight​(int h)
    Sets the height of the Actor.
    void
    setImage​(PImage image)
    Sets the image of the Actor.
    void
    setLocation​(float x, float y)
    Sets the X-axis and Y-axis positions of the Actor.
    void
    setOpacity​(float opacity)
    Sets the opacity value (in the range 0 - 255) of the Actor.
    void
    setResizeFormat​(int format)
    Sets the image resize format of the Actor.
    void
    setRotation​(float rotation)
    Sets the rotation of the Actor in degrees.
    void
    setWidth​(int w)
    Sets the width of the Actor.
    void
    setX​(float x)
    Sets the X-axis position of the Actor.
    void
    setY​(float y)
    Sets the Y-axis position of the Actor.
    void
    setZ​(float z)
    Sets the Z-axis position of the Actor.
     
    void
    turn​(float degrees)
    Rotates the Actor degrees amount in degrees.
    void
    turnTowards​(float x, float y)
    Rotates the Actor to face it's right side towards the position (x, y).
    void
    Rotates the Actor to face it's right side towards another Actor.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Actor

      public Actor(float x, float y, PImage image)
      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

      public Actor(float x, float y, PImage image, int w, int h)
      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

      public Actor(float x, float y, PImage image, float scaleMultiplier)
      Creates the new actor at a position with a sprite scaled by scaleMultiplier.
      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 to image's dimensions.
    • Actor

      public Actor(float x, float y, PImage image, int w, int h, int resizeFormat)
      Creates the new actor at a position with a sprite, resized according to resizeFormat 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 either Green.BILINEAR, Green.NEAREST_NEIGHBOR, or Green.TILE.
      Throws:
      UnknownResizeFormatException - Thrown when an unknown resize format is supplied.
    • Actor

      public Actor(float x, float y, PImage image, float scaleMultiplier, int resizeFormat)
      Creates the new actor at a position with a sprite scaled by scaleMultiplier, resized according to resizeFormat.
      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 to image's dimensions.
      resizeFormat - The image resize format to use. It must be either Green.BILINEAR, Green.NEAREST_NEIGHBOR, or Green.TILE.
      Throws:
      UnknownResizeFormatException - Thrown when an unknown resize format is supplied.
  • Method Details

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getUuid

      public final UUID getUuid()
      Retrieves the UUID of the Actor. This value is unique to each Actor instance, and cannot be modified.
      Returns:
      The unique instance ID.
    • getX

      public final float getX()
      Retrieves the X-axis position of the Actor.
      Returns:
      The X-axis position.
    • getY

      public final float getY()
      Retrieves the X-axis position of the Actor.
      Returns:
      The Y-axis position.
    • getZ

      public final float getZ()
      Retrieves the Z-axis position of the Actor. 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 the Actor.
      Returns:
      The rotation in degrees.
    • getWidth

      public final int getWidth()
      Retrieves the width of the Actor.
      Returns:
      The width.
    • getHeight

      public final int getHeight()
      Retrieves the height of the Actor.
      Returns:
      The height.
    • getSourceImage

      public final PImage getSourceImage()
      Retrieves the original PImage (before any scaling has been applied) associated with the Actor.
      Returns:
      The original associated image.
    • getImage

      public final PImage getImage()
      Retrieves the PImage associated with the Actor.
      Returns:
      The associated image.
    • getOpacity

      public final float getOpacity()
      Retrieves the opacity value (in the range 0 - 255) of the Actor.
      Returns:
      The opacity value in range 0 - 255.
    • getResizeFormat

      public final int getResizeFormat()
      Retrieves the image resize format of the Actor.
      Returns:
      The image resize format - either Green.BILINEAR, Green.NEAREST_NEIGHBOR, or Green.TILE.
    • getWorld

      public final World getWorld()
      Retrieves the current World this Actor is in, or null otherwise.
      Returns:
      The world this Actor is a part of, that is currently loaded.
    • getWorldOfType

      public static <W extends World> W getWorldOfType(Class<W> type)
      Retrieves the current World this Actor is in as type if possible, or null otherwise.
      Type Parameters:
      W - The type of World to return if possible, as defined by type.
      Parameters:
      type - The type of World to return if possible.
      Returns:
      The world this Actor is a part of that is currently loaded.
    • setX

      public final void setX(float x)
      Sets the X-axis position of the Actor.
      Parameters:
      x - The X-axis position to set.
    • setY

      public final void setY(float y)
      Sets the Y-axis position of the Actor.
      Parameters:
      y - The Y-axis position to set.
    • setZ

      public final void setZ(float z)
      Sets the Z-axis position of the Actor. 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 the Actor.
      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 the Actor in degrees.
      Parameters:
      rotation - The rotation value to set in degrees.
    • setWidth

      public final void setWidth(int w)
      Sets the width of the Actor.
      Parameters:
      w - The width value to set.
    • setHeight

      public final void setHeight(int h)
      Sets the height of the Actor.
      Parameters:
      h - The height value to set.
    • setDimensions

      public final void setDimensions(int w, int h)
      Sets the width and height of the Actor.
      Parameters:
      w - The width value to set.
      h - The height value to set.
    • setImage

      public final void setImage(PImage image)
      Sets the image of the Actor.
      Parameters:
      image - The PImage to set.
    • setOpacity

      public final void setOpacity(float opacity)
      Sets the opacity value (in the range 0 - 255) of the Actor.
      Parameters:
      opacity - The opacity value to set, in range 0 - 255.
    • setResizeFormat

      public final void setResizeFormat(int format)
      Sets the image resize format of the Actor.
      Parameters:
      format - The image resize format to set. It must be either Green.BILINEAR, Green.NEAREST_NEIGHBOR, or Green.TILE.
      Throws:
      UnknownResizeFormatException - Thrown when an unknown resize format is supplied.
    • move

      public final void move(float amount)
      Moves the Actor 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)
      Moves the Actor x units on the X-axis and y units on the Y-axis.
      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 the Actor degrees amount in degrees.
      Parameters:
      degrees - The amount to turn in degrees.
    • isAtEdge

      public final boolean isAtEdge() throws NoWorldException
      Checks to see if the Actor is at the edge of the World it is currently in.
      Returns:
      Whether the Actor is at the edge of it's World.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • intersects

      public final boolean intersects(Actor actor)
      Checks to see whether the Actor intersects another Actor, using basic rect comparison.
      Parameters:
      actor - The other Actor to check against.
      Returns:
      Whether this Actor intersects the other, or false if the same Actor is supplied.
    • pointInBounds

      public final boolean pointInBounds(float pX, float pY)
      Checks to see if a point defined by pX and pY is within the Actor's rotated rect.
      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 NoWorldException
      Retrieves a list of all objects matching type in the World that intersect with the Actor, using basic rect comparison.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      type - The type of other Actor to compare against.
      Returns:
      The list of intersecting objects matching type.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getOneIntersectingObject

      public final <A extends Actor> A getOneIntersectingObject(Class<A> type) throws NoWorldException
      Retrieves the first object matching type in the World that intersects with the Actor, using basic rect comparison.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      type - The type of other Actor to compare against.
      Returns:
      The first intersecting object matching type.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getObjectsAtOffset

      public final <A extends Actor> List<A> getObjectsAtOffset(float oX, float oY, Class<A> type, float range) throws NoWorldException
      Retrieves a list of all objects matching type within range of an offset.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      oX - The X-axis value of the offset.
      oY - The Y-axis value of the offset.
      type - The type of Actor to match.
      range - The maximum distance an Actor can be from the offset point to match.
      Returns:
      A list of all objects in the World matching type at (oX, oY) from the Actor's position.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getObjectsAtOffset

      public final <A extends Actor> List<A> getObjectsAtOffset(float oX, float oY, Class<A> type) throws NoWorldException
      Retrieves a list of all objects matching type within 1 unit of an offset.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      oX - The X-axis value of the offset.
      oY - The Y-axis value of the offset.
      type - The type of Actor to match.
      Returns:
      A list of all objects in the World matching type at (oX, oY) from the Actor's position.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getOneObjectAtOffset

      public final <A extends Actor> A getOneObjectAtOffset(float oX, float oY, Class<A> type, float range) throws NoWorldException
      Retrieves the first object matching type within range of an offset.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      oX - The X-axis value of the offset.
      oY - The Y-axis value of the offset.
      type - The type of Actor to match.
      range - The maximum distance an Actor can be from the offset point to match.
      Returns:
      The first object in the World matching type at (oX, oY) from the Actor's position.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getOneObjectAtOffset

      public final <A extends Actor> A getOneObjectAtOffset(float oX, float oY, Class<A> type) throws NoWorldException
      Retrieves the first object matching type within 1 unit of an offset.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      oX - The X-axis value of the offset.
      oY - The Y-axis value of the offset.
      type - The type of Actor to match.
      Returns:
      The first object in the World matching type at (oX, oY) from the Actor's position.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getObjectsInRange

      public final <A extends Actor> List<A> getObjectsInRange(Class<A> type, float range) throws NoWorldException
      Retrieves a list of all objects in the World matching type within range of the Actor.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      type - The type of Actor to match.
      range - The maximum distance an Actor can be from the offset point to match.
      Returns:
      A list of all objects in the World matching type within range of the Actor.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getOneObjectInRange

      public final <A extends Actor> A getOneObjectInRange(Class<A> type, float range) throws NoWorldException
      Retrieves the first object in the World matching type within range of the Actor.
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      type - The type of Actor to match.
      range - The maximum distance an Actor can be from the offset point to match.
      Returns:
      The first object in the World matching type within range of the Actor.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • getNeighbours

      public final <A extends Actor> List<A> getNeighbours(Class<A> type, float range) throws NoWorldException
      Retrieves a list of all objects in the World matching type within range of the Actor. Due to the difference in how this library and Greenfoot work, this is just an alias to getObjectsInRange(Class, float).
      Type Parameters:
      A - The type of Actor to return if possible, as defined by type.
      Parameters:
      type - The type of Actor to match.
      range - The maximum distance an Actor can be from the offset point to match.
      Returns:
      A list of all objects in the World matching type within range of the Actor.
      Throws:
      NoWorldException - Thrown when the method is called and the Actor is not part of a World.
    • turnTowards

      public final void turnTowards(float x, float y)
      Rotates the Actor to face it's right side towards the position (x, y). Does nothing if the position provided is the position of the Actor.
      Parameters:
      x - The X-axis position to turn towards.
      y - The Y-axis position to turn towards.
    • turnTowards

      public final void turnTowards(Actor obj)
      Rotates the Actor to face it's right side towards another Actor.
      Parameters:
      obj - The other Actor to turn towards.
    • isMouseButtonDownHere

      public final boolean isMouseButtonDownHere(int mouseButton)
      Retrieves whether a specific mouse button is currently down on this Actor.
      Parameters:
      mouseButton - The mouse button to check for - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT.
      Returns:
      Whether mouseButton is down.
    • isMouseButtonDownThisFrameHere

      public final boolean isMouseButtonDownThisFrameHere(int mouseButton)
      Retrieves whether a specific mouse button was pressed this frame on this Actor.
      Parameters:
      mouseButton - The mouse button to check for - either PConstants.LEFT, PConstants.CENTER, or PConstants.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 this Actor.
      Parameters:
      mouseButton - The mouse button to check for - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT.
      Returns:
      Whether mouseButton was released in this frame.
    • isMouseScrollingHere

      public final boolean isMouseScrollingHere()
      Retrieves whether the mouse is currently scrolling on this Actor.
      Returns:
      Whether the mouse is currently scrolling.
    • draw

      public void draw()
      Renders the Actor to the screen. By default, this method renders the Actor with its image, position, rotation, and opacity taken into account, but it can be overridden for further use. When drawing, everything is relative to the Actor's position.
    • addedToWorld

      public void addedToWorld(World world)
      Called when the Actor is added to any World. By default, this method does nothing.
      Parameters:
      world - The World the Actor was added to.
    • removedFromWorld

      public void removedFromWorld(World world)
      Called when the Actor is removed from any World. By default, this method does nothing.
      Parameters:
      world - The World the Actor was removed from.
    • act

      public abstract void act(float deltaTime)
      Called once per frame. This is likely where most work with the Actor will be done.
      Parameters:
      deltaTime - The time, in seconds, since the last time this method was called.