Class GameRules

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

public class GameRules extends Object

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.

  • Constructor Details

    • GameRules

      public GameRules()
  • Method Details

    • onStart

      public void onStart(PlatformWorld world)

      Called once, before the first tick.

      Parameters
      • world: the world
    • onStep

      public void onStep(PlatformWorld world)

      Called at the end of every tick, after everything moved and touched.

      Parameters
      • world: 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: the world
      • 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: the world
      • 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)