Class Actor

java.lang.Object
com.codename1.gaming.platformer.Actor

public class Actor extends Object

A player or an enemy: a Body driven by an ordered list of Abilitys.

Each PlatformWorld#step the actor takes its tick of Input bits (from a player, a replay or an AIBrain), runs its abilities, applies gravity unless an ability turned it off, moves the body, and settles a MovementState that the renderer animates. All tuning is in fixed point (Fx), in pixels and ticks at 60 ticks per second.

import com.codename1.gaming.platformer.*;

LevelData level = LevelData.parse("{\"tileSize\":16,\"spawn\":[1,1],\"tiles\":[\"........\",\"........\",\"########\"]}");
PlatformWorld world = level.createWorld(1);
Actor hero = new Actor("hero", Fx.of(32), Fx.of(80), Fx.of(12), Fx.of(14));
hero.addAbility(new MoveAbility());
hero.addAbility(new LadderAbility());
hero.addAbility(new WallJumpAbility());
hero.addAbility(new JumpAbility());
hero.addAbility(new CrouchAbility());
hero.addAbility(new DashAbility());
world.addActor(hero);
  • Constructor Summary

    Constructors
    Constructor
    Description
    Actor(String id, int x, int y, int w, int h)
    Creates an actor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds an ability at the end of the processing order.
    void
    Sets this tick's state.
    void
    Marks this tick's jump press as used, so later abilities do not act on it.
    void
    Replaces this tick's state even if one was claimed, for states that override everything such as being hurt.
    <T extends Ability>
    T
    getAbility(Class<T> type)
    Finds an ability by class, to tune it or switch it on.
    int
    Ticks since the actor last stood on something.
    The collision box.
    The brain that drives a non-player actor, or null.
    int
    Damage dealt to actors of another team on contact.
    int
    The switch channel the actor's defeat turns on.
    int
    Facing: 1 right, -1 left.
    int
    Gravity, fixed point per tick squared.
    Hit points, or null for an actor that dies at the first hit.
    The actor's id.
    int
    This tick's input bits.
    The art kind the view draws this actor with ("hero", "slime").
    int
    Terminal falling speed.
    int
    The height the actor stands at, which crouching shrinks from.
    The state settled in the last tick.
    int
    The team: 0 for the player's side.
    long
    hash(long h)
    The actor's state as a hash, for replay verification.
    boolean
    held(int button)
    Whether a button is held this tick.
    boolean
    Whether the actor is alive.
    boolean
    Whether horizontal control is locked.
    boolean
    Whether an earlier ability used this tick's jump press.
    boolean
    Whether an ability already claimed this tick's state.
    boolean
    Whether landing on this actor's head hurts it and bounces the player.
    void
    Kills the actor; it stops processing until respawn.
    void
    lockControl(int ticks)
    Locks horizontal control for a number of ticks, so a wall jump or a knock-back carries the actor away before input steers again.
    boolean
    pressed(int button)
    Whether a button went down this tick.
    boolean
    released(int button)
    Whether a button was released this tick.
    void
    respawn(int x, int y)
    Brings the actor back at a point, standing and still.
    Gives the actor a brain; the world then ignores its input slot.
    setContactDamage(int damage)
    Sets contact damage.
    setDefeatChannel(int channel)
    Sets a switch channel the actor's defeat turns on, so beating a boss can open the way out.
    void
    setFacing(int facing)
    Sets the facing.
    void
    setGravity(int gravity)
    Sets gravity.
    Gives the actor hit points.
    Sets the art kind.
    void
    setMaxFall(int maxFall)
    Sets the terminal falling speed.
    setStompable(boolean stompable)
    Sets whether the actor can be stomped.
    setTeam(int team)
    Sets the team.
    void
    Turns gravity off for this tick (a ladder, a dash, a jetpack).
    void
    tick(PlatformWorld world, int bits)
    Advances one tick.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Actor

      public Actor(String id, int x, int y, int w, int h)

      Creates an actor.

      Parameters
      • id: a name, unique in its world
      • x: left, fixed point
      • y: top, fixed point
      • w: width, fixed point
      • h: standing height, fixed point
  • Method Details

    • addAbility

      public Actor addAbility(Ability a)

      Adds an ability at the end of the processing order.

      Parameters
      • a: the ability
      Returns

      this actor, for chaining

    • getAbility

      public <T extends Ability> T getAbility(Class<T> type)

      Finds an ability by class, to tune it or switch it on.

      Parameters
      • type: the ability's class
      Returns

      the first ability of that class or a subclass of it, or null

    • tick

      public void tick(PlatformWorld world, int bits)

      Advances one tick. Called by PlatformWorld#step.

      Parameters
      • world: the world
      • bits: this tick's Input
    • claimState

      public void claimState(MovementState s)

      Sets this tick's state. The first ability to claim one wins, so the order of abilities is also their priority.

      Parameters
      • s: the state
    • forceState

      public void forceState(MovementState s)

      Replaces this tick's state even if one was claimed, for states that override everything such as being hurt.

      Parameters
      • s: the state
    • getKind

      public String getKind()

      The art kind the view draws this actor with ("hero", "slime").

      Returns

      the kind

    • setKind

      public Actor setKind(String kind)

      Sets the art kind.

      Parameters
      • kind: the kind
      Returns

      this actor

    • getTeam

      public int getTeam()

      The team: 0 for the player's side. Projectiles and contact damage never hurt their own team.

      Returns

      the team

    • setTeam

      public Actor setTeam(int team)

      Sets the team.

      Parameters
      • team: the team
      Returns

      this actor

    • getHealth

      public HitPoints getHealth()

      Hit points, or null for an actor that dies at the first hit.

      Returns

      the health

    • setHealth

      public Actor setHealth(HitPoints health)

      Gives the actor hit points.

      Parameters
      • health: the health, or null
      Returns

      this actor

    • getContactDamage

      public int getContactDamage()

      Damage dealt to actors of another team on contact.

      Returns

      the damage, 0 for harmless

    • setContactDamage

      public Actor setContactDamage(int damage)

      Sets contact damage.

      Parameters
      • damage: the damage
      Returns

      this actor

    • isStompable

      public boolean isStompable()

      Whether landing on this actor's head hurts it and bounces the player.

      Returns

      true if stompable

    • setStompable

      public Actor setStompable(boolean stompable)

      Sets whether the actor can be stomped.

      Parameters
      • stompable: true if stompable
      Returns

      this actor

    • setDefeatChannel

      public Actor setDefeatChannel(int channel)

      Sets a switch channel the actor's defeat turns on, so beating a boss can open the way out. 0, the default, turns nothing on.

      Parameters
      • channel: the channel
      Returns

      this actor

    • getDefeatChannel

      public int getDefeatChannel()

      The switch channel the actor's defeat turns on.

      Returns

      the channel, 0 for none

    • getBrain

      public AIBrain getBrain()

      The brain that drives a non-player actor, or null.

      Returns

      the brain

    • setBrain

      public Actor setBrain(AIBrain brain)

      Gives the actor a brain; the world then ignores its input slot.

      Parameters
      • brain: the brain, or null
      Returns

      this actor

    • getId

      public String getId()

      The actor's id.

      Returns

      the id

    • getBody

      public Body getBody()

      The collision box.

      Returns

      the body

    • getStandHeight

      public int getStandHeight()

      The height the actor stands at, which crouching shrinks from.

      Returns

      the standing height, fixed point

    • getInput

      public int getInput()

      This tick's input bits.

      Returns

      the input

    • held

      public boolean held(int button)

      Whether a button is held this tick.

      Parameters
      • button: an Input constant
      Returns

      true while held

    • pressed

      public boolean pressed(int button)

      Whether a button went down this tick.

      Parameters
      • button: an Input constant
      Returns

      true on the press edge

    • released

      public boolean released(int button)

      Whether a button was released this tick.

      Parameters
      • button: an Input constant
      Returns

      true on the release edge

    • getFacing

      public int getFacing()

      Facing: 1 right, -1 left.

      Returns

      the facing

    • setFacing

      public void setFacing(int facing)

      Sets the facing.

      Parameters
      • facing: 1 right, -1 left
    • getState

      public MovementState getState()

      The state settled in the last tick.

      Returns

      the state

    • isStateClaimed

      public boolean isStateClaimed()

      Whether an ability already claimed this tick's state.

      Returns

      true once claimed

    • suspendGravity

      public void suspendGravity()
      Turns gravity off for this tick (a ladder, a dash, a jetpack).
    • getGravity

      public int getGravity()

      Gravity, fixed point per tick squared.

      Returns

      the gravity

    • setGravity

      public void setGravity(int gravity)

      Sets gravity.

      Parameters
      • gravity: fixed point per tick squared
    • getMaxFall

      public int getMaxFall()

      Terminal falling speed. Keep it under a tile per tick.

      Returns

      the speed, fixed point per tick

    • setMaxFall

      public void setMaxFall(int maxFall)

      Sets the terminal falling speed.

      Parameters
      • maxFall: fixed point per tick
    • lockControl

      public void lockControl(int ticks)

      Locks horizontal control for a number of ticks, so a wall jump or a knock-back carries the actor away before input steers again.

      Parameters
      • ticks: how long input is ignored by MoveAbility
    • isControlLocked

      public boolean isControlLocked()

      Whether horizontal control is locked.

      Returns

      true while locked

    • getAirTicks

      public int getAirTicks()

      Ticks since the actor last stood on something.

      Returns

      0 while grounded

    • consumeJump

      public void consumeJump()
      Marks this tick's jump press as used, so later abilities do not act on it.
    • isJumpConsumed

      public boolean isJumpConsumed()

      Whether an earlier ability used this tick's jump press.

      Returns

      true once consumed

    • isAlive

      public boolean isAlive()

      Whether the actor is alive.

      Returns

      false after kill

    • kill

      public void kill()
      Kills the actor; it stops processing until respawn.
    • respawn

      public void respawn(int x, int y)

      Brings the actor back at a point, standing and still.

      Parameters
      • x: left, fixed point
      • y: top, fixed point
    • hash

      public long hash(long h)

      The actor's state as a hash, for replay verification.

      Parameters
      • h: the running hash
      Returns

      the updated hash