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

  1. Object
  2. 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, call PlatformWorld.complete with the winner;
  • an endless runner lost on the first hit: return true from onDeath after PlatformWorld.endGame;
  • a puzzle won when every block is on a target: count in onStep and 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)
Called once, before the first tick.

Parameters

world PlatformWorld
the world

onStep

public void onStep(PlatformWorld world)
Called at the end of every tick, after everything moved and touched.

Parameters

world PlatformWorld
the world

reachesGoal

public boolean reachesGoal(PlatformWorld world, Actor actor)
Whether reaching the goal rectangle completes the level for this actor. The default lets only the first actor (the player) finish.

Parameters

world PlatformWorld
the world
actor Actor
the actor overlapping the goal

Returns

true to complete the level

onDeath

public boolean onDeath(PlatformWorld world, Actor actor)
Called when an actor runs out of health or falls out of the level.

Parameters

world PlatformWorld
the world
actor Actor
the actor

Returns

true if the rules handled it; false for the default (a life lost and a respawn for the player, death for anyone else)