Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public class GameRules
- Object
- GameRules
A game’s own rules over a PlatformWorld: when it is won, when it is lost,
what happens when an actor dies and what is checked every tick. The engine
knows how things move and collide; a game built on it says what they mean.
With no rules set, a world plays a classic platformer: the first actor is the player, reaching the goal rectangle completes the level, and a death costs a life and respawns at the last checkpoint. Override only what your game changes:
- a race won by whoever reaches the exit first: override
onStep, callPlatformWorld.completewith the winner; - an endless runner lost on the first hit: return true from
onDeathafterPlatformWorld.endGame; - a puzzle won when every block is on a target: count in
onStepand keep the count in a game counter (PlatformWorld.setCounter) so the HUD and the replay hash both see it.
Rules run inside the simulation, so they must be as deterministic as the
rest of it: fixed point only, no clock, randomness only from
PlatformWorld.getRng.
Constructors
public GameRules() |
Methods
public void onStart(PlatformWorld world) | Called once, before the first tick. |
public void onStep(PlatformWorld world) | Called at the end of every tick, after everything moved and touched. |
public boolean reachesGoal(PlatformWorld world, Actor actor) | Whether reaching the goal rectangle completes the level for this actor. |
public boolean onDeath(PlatformWorld world, Actor actor) | Called when an actor runs out of health or falls out of the level. |
Inherited methods
Constructor details
GameRules
public GameRules()Method details
onStart
public void onStart(PlatformWorld world)Parameters
worldPlatformWorld- the world
onStep
public void onStep(PlatformWorld world)Parameters
worldPlatformWorld- the world
reachesGoal
public boolean reachesGoal(PlatformWorld world, Actor actor)Parameters
worldPlatformWorld- the world
actorActor- the actor overlapping the goal
Returns
onDeath
public boolean onDeath(PlatformWorld world, Actor actor)Parameters
worldPlatformWorld- the world
actorActor- the actor