Class Actor
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 -
Method Summary
Modifier and TypeMethodDescriptionAdds an ability at the end of the processing order.voidSets this tick's state.voidMarks this tick's jump press as used, so later abilities do not act on it.voidReplaces this tick's state even if one was claimed, for states that override everything such as being hurt.<T extends Ability>
TgetAbility(Class<T> type) Finds an ability by class, to tune it or switch it on.intTicks since the actor last stood on something.getBody()The collision box.getBrain()The brain that drives a non-player actor, or null.intDamage dealt to actors of another team on contact.intThe switch channel the actor's defeat turns on.intFacing: 1 right, -1 left.intGravity, fixed point per tick squared.Hit points, or null for an actor that dies at the first hit.getId()The actor's id.intgetInput()This tick's input bits.getKind()The art kind the view draws this actor with ("hero","slime").intTerminal falling speed.intThe height the actor stands at, which crouching shrinks from.getState()The state settled in the last tick.intgetTeam()The team: 0 for the player's side.longhash(long h) The actor's state as a hash, for replay verification.booleanheld(int button) Whether a button is held this tick.booleanisAlive()Whether the actor is alive.booleanWhether horizontal control is locked.booleanWhether an earlier ability used this tick's jump press.booleanWhether an ability already claimed this tick's state.booleanWhether landing on this actor's head hurts it and bounces the player.voidkill()Kills the actor; it stops processing untilrespawn.voidlockControl(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.booleanpressed(int button) Whether a button went down this tick.booleanreleased(int button) Whether a button was released this tick.voidrespawn(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.voidsetFacing(int facing) Sets the facing.voidsetGravity(int gravity) Sets gravity.Gives the actor hit points.Sets the art kind.voidsetMaxFall(int maxFall) Sets the terminal falling speed.setStompable(boolean stompable) Sets whether the actor can be stomped.setTeam(int team) Sets the team.voidTurns gravity off for this tick (a ladder, a dash, a jetpack).voidtick(PlatformWorld world, int bits) Advances one tick.
-
Constructor Details
-
Actor
Creates an actor.
Parameters
id: a name, unique in its worldx: left, fixed pointy: top, fixed pointw: width, fixed pointh: standing height, fixed point
-
-
Method Details
-
addAbility
-
getAbility
-
tick
Advances one tick. Called by
PlatformWorld#step.Parameters
world: the worldbits: this tick'sInput
-
claimState
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
Replaces this tick's state even if one was claimed, for states that override everything such as being hurt.
Parameters
s: the state
-
getKind
The art kind the view draws this actor with (
"hero","slime").Returns
the kind
-
setKind
-
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
Sets the team.
Parameters
team: the team
Returns
this actor
-
getHealth
Hit points, or null for an actor that dies at the first hit.
Returns
the health
-
setHealth
-
getContactDamage
public int getContactDamage()Damage dealt to actors of another team on contact.
Returns
the damage, 0 for harmless
-
setContactDamage
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
Sets whether the actor can be stomped.
Parameters
stompable: true if stompable
Returns
this actor
-
setDefeatChannel
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
The brain that drives a non-player actor, or null.
Returns
the brain
-
setBrain
-
getId
The actor's id.
Returns
the id
-
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: anInputconstant
Returns
true while held
-
pressed
public boolean pressed(int button) Whether a button went down this tick.
Parameters
button: anInputconstant
Returns
true on the press edge
-
released
public boolean released(int button) Whether a button was released this tick.
Parameters
button: anInputconstant
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
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 byMoveAbility
-
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 untilrespawn. -
respawn
public void respawn(int x, int y) Brings the actor back at a point, standing and still.
Parameters
x: left, fixed pointy: 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
-