Class PlatformWorld

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

public class PlatformWorld extends Object

The whole platformer simulation: a TileGrid, its MovingPlatforms, the Actors and LevelEntitys in it, advanced one fixed tick (1/60 s) at a time.

The world is deterministic. Given the same level and the same Input bits per tick it reaches the same hash() on every device and on the server, which is what Replay relies on to verify a run. Rendering never feeds back into it: a view reads positions after each step, interpolates between them, and plays the tick's getEvents().

It also keeps the level's bookkeeping, the part of Corgi Engine's level and game managers that affects play: score, coins, secrets, keys, lives, the respawn point and the switch channels.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    How many game counters a world keeps.
    static final int
    The rate the world is designed for.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PlatformWorld(TileGrid grid, long seed)
    Creates a world over a grid.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds an actor.
    void
    addCoins(int n)
    Adds coins.
    void
    addCounter(int index, int delta)
    Adds to a game counter.
    void
    Adds an entity.
    void
    addKey(String name)
    Adds a key to the inventory.
    void
    Adds a life, when lives are limited.
    void
    Adds a moving platform.
    void
    addScore(int points)
    Adds to the score.
    void
    Counts a secret found.
    void
    Completes the level, won by an actor.
    boolean
    damage(Actor c, int amount, int fromX)
    Hurts an actor: removes hit points (respecting invulnerability), knocks it away from fromX, and kills it at zero.
    void
    emit(int type, int x, int y, String subject, String detail)
    Records an event for the view.
    void
    Switches an ability on for an actor, creating it if the actor lacks it, as a power-up does.
    void
    Ends the game as lost.
    The actors, the player first.
    int
    The coins collected.
    int
    getCounter(int index)
    A game counter: a number the rules keep that the engine does not interpret (distance run, blocks on targets, rounds won).
    int
    How many times the player died.
    The active entities.
    The events of the last tick.
    int[]
    The goal rectangle as {x, y, w, h}, fixed point.
    The collision map.
    The keys held.
    int
    Lives left; negative means unlimited.
    The moving platforms.
    The player, the first actor added.
    int
    How many of the first actors are players.
    The world's random generator.
    The game's rules.
    int
    The score.
    int
    The secrets found.
    int
    Spawn x, fixed point.
    int
    Spawn y, fixed point.
    int
    Ticks stepped so far.
    The actor that completed the level.
    long
    A 64-bit digest of the whole simulation state.
    boolean
    isChannelOn(int channel)
    Whether a switch channel is on.
    boolean
    Whether the player reached the goal.
    boolean
    Whether the player ran out of lives.
    boolean
    Whether an actor is one of the players.
    The live player closest to an actor, for enemies that chase or aim.
    protected void
    Called when an actor runs out of health or falls out of the level.
    void
    setChannel(int channel, boolean on)
    Turns a switch channel on or off.
    void
    setCounter(int index, int value)
    Sets a game counter.
    void
    setGoal(int x, int y, int w, int h)
    Sets the goal: the level is complete when the player overlaps it.
    void
    setLives(int lives)
    Sets the lives; negative means unlimited.
    void
    setPlayerCount(int count)
    Sets how many of the first actors are players: they take input, and each respawns on death instead of dying.
    void
    Sets the game's rules.
    void
    setSpawn(int x, int y)
    Sets where the player starts and comes back after dying.
    void
    setStompBounce(int speed)
    The upward speed of a stomp bounce.
    void
    step(int input)
    Advances one tick for a single player.
    void
    step(int[] inputs)
    Advances one tick with one input per actor, in the order they were added.
    boolean
    useKey(String name)
    Uses up a key.

    Methods inherited from class Object

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

    • TICKS_PER_SECOND

      public static final int TICKS_PER_SECOND
      The rate the world is designed for.
      See Also:
    • COUNTERS

      public static final int COUNTERS
      How many game counters a world keeps.
      See Also:
  • Constructor Details

    • PlatformWorld

      public PlatformWorld(TileGrid grid, long seed)

      Creates a world over a grid.

      Parameters
      • grid: the level's collision map
      • seed: the random seed
  • Method Details

    • addPlatform

      public void addPlatform(MovingPlatform p)

      Adds a moving platform.

      Parameters
      • p: the platform
    • addActor

      public void addActor(Actor c)

      Adds an actor. The first one added is the player, which step drives with its first input; actors with a brain ignore their input slot. Unlike an entity, an actor added during a step (from the rules, a touch or a brain) joins at once and is stepped, touched and hit in that same tick; it is deterministic either way, so replays agree.

      Parameters
      • c: the actor
    • addEntity

      public void addEntity(LevelEntity e)

      Adds an entity. Added during a step, it takes part from the next tick.

      Parameters
      • e: the entity
    • setRules

      public void setRules(GameRules rules)

      Sets the game's rules. Call before the first step.

      Parameters
      • rules: the rules, or null for a classic platformer
    • getRules

      public GameRules getRules()

      The game's rules.

      Returns

      the rules, or null

    • setPlayerCount

      public void setPlayerCount(int count)

      Sets how many of the first actors are players: they take input, and each respawns on death instead of dying. Two for a shared-screen game.

      Parameters
      • count: one or more
    • getPlayerCount

      public int getPlayerCount()

      How many of the first actors are players.

      Returns

      the count

    • isPlayer

      public boolean isPlayer(Actor c)

      Whether an actor is one of the players.

      Parameters
      • c: the actor
      Returns

      true for one of the first getPlayerCount() actors

    • nearestPlayer

      public Actor nearestPlayer(Actor from)

      The live player closest to an actor, for enemies that chase or aim. With one player it is that player while alive.

      Parameters
      • from: the actor looking
      Returns

      the nearest live player, or the first player when none is alive, or null with no actors

    • complete

      public void complete(Actor by)

      Completes the level, won by an actor. Rules call this for a win that is not the goal rectangle.

      Parameters
      • by: the winner, may be null
    • endGame

      public void endGame()
      Ends the game as lost. Rules call this for a loss that is not running out of lives.
    • getWinner

      public Actor getWinner()

      The actor that completed the level.

      Returns

      the winner, or null

    • getCounter

      public int getCounter(int index)

      A game counter: a number the rules keep that the engine does not interpret (distance run, blocks on targets, rounds won). Counters are part of hash(), so a replay checks them too.

      Parameters
      • index: 0 to COUNTERS - 1
      Returns

      the value

    • setCounter

      public void setCounter(int index, int value)

      Sets a game counter.

      Parameters
      • index: 0 to COUNTERS - 1
      • value: the value
    • addCounter

      public void addCounter(int index, int delta)

      Adds to a game counter.

      Parameters
      • index: 0 to COUNTERS - 1
      • delta: the amount
    • setSpawn

      public void setSpawn(int x, int y)

      Sets where the player starts and comes back after dying.

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

      public void setGoal(int x, int y, int w, int h)

      Sets the goal: the level is complete when the player overlaps it.

      Parameters
      • x: left, fixed point
      • y: top, fixed point
      • w: width, fixed point
      • h: height, fixed point
    • step

      public void step(int[] inputs)

      Advances one tick with one input per actor, in the order they were added. Actors with no input entry get no buttons.

      Parameters
      • inputs: Input bits per actor
    • step

      public void step(int input)

      Advances one tick for a single player.

      Parameters
      • input: the player's Input bits
    • damage

      public boolean damage(Actor c, int amount, int fromX)

      Hurts an actor: removes hit points (respecting invulnerability), knocks it away from fromX, and kills it at zero. An actor without HitPoints dies at once.

      Parameters
      • c: the actor
      • amount: hit points
      • fromX: where the hit came from, fixed point x
      Returns

      true if the hit landed

    • onDeath

      protected void onDeath(Actor c)

      Called when an actor runs out of health or falls out of the level. The player loses a life and respawns at the last checkpoint, or the game is over; any other actor dies. Override to change the rules.

      Parameters
      • c: the actor
    • emit

      public void emit(int type, int x, int y, String subject, String detail)

      Records an event for the view.

      Parameters
      • type: a WorldEvent constant
      • x: where, fixed point
      • y: where, fixed point
      • subject: the id involved, may be null
      • detail: extra detail, may be null
    • getEvents

      public List<WorldEvent> getEvents()

      The events of the last tick.

      Returns

      the live list, cleared at the start of each step

    • enableAbility

      public void enableAbility(Actor c, String name)

      Switches an ability on for an actor, creating it if the actor lacks it, as a power-up does.

      Parameters
      • c: the actor
      • name: a stock ability name (see Abilities)
    • isChannelOn

      public boolean isChannelOn(int channel)

      Whether a switch channel is on.

      Parameters
      • channel: 0 to 31
      Returns

      true while on

    • setChannel

      public void setChannel(int channel, boolean on)

      Turns a switch channel on or off.

      Parameters
      • channel: 0 to 31
      • on: the new state
    • addScore

      public void addScore(int points)

      Adds to the score.

      Parameters
      • points: the points
    • addCoins

      public void addCoins(int n)

      Adds coins.

      Parameters
      • n: the coins
    • addSecret

      public void addSecret()
      Counts a secret found.
    • addLife

      public void addLife()
      Adds a life, when lives are limited.
    • addKey

      public void addKey(String name)

      Adds a key to the inventory.

      Parameters
      • name: the key name
    • useKey

      public boolean useKey(String name)

      Uses up a key.

      Parameters
      • name: the key name
      Returns

      true if the key was held

    • getKeys

      public List<String> getKeys()

      The keys held.

      Returns

      the live list

    • getLives

      public int getLives()

      Lives left; negative means unlimited.

      Returns

      the lives

    • setLives

      public void setLives(int lives)

      Sets the lives; negative means unlimited.

      Parameters
      • lives: the lives
    • setStompBounce

      public void setStompBounce(int speed)

      The upward speed of a stomp bounce.

      Parameters
      • speed: fixed point per tick
    • getScore

      public int getScore()

      The score.

      Returns

      the score

    • getCoins

      public int getCoins()

      The coins collected.

      Returns

      the coins

    • getSecrets

      public int getSecrets()

      The secrets found.

      Returns

      the secrets

    • getGrid

      public TileGrid getGrid()

      The collision map.

      Returns

      the grid

    • getPlatforms

      public List<MovingPlatform> getPlatforms()

      The moving platforms.

      Returns

      the live list

    • getActors

      public List<Actor> getActors()

      The actors, the player first.

      Returns

      the live list

    • getEntities

      public List<LevelEntity> getEntities()

      The active entities.

      Returns

      the live list

    • getPlayer

      public Actor getPlayer()

      The player, the first actor added.

      Returns

      the player, or null in an empty world

    • getRng

      public Rng getRng()

      The world's random generator. Only the simulation may draw from it.

      Returns

      the generator

    • getTick

      public int getTick()

      Ticks stepped so far.

      Returns

      the tick count

    • isComplete

      public boolean isComplete()

      Whether the player reached the goal.

      Returns

      true once complete

    • isGameOver

      public boolean isGameOver()

      Whether the player ran out of lives.

      Returns

      true once the game is over

    • getDeaths

      public int getDeaths()

      How many times the player died.

      Returns

      the death count

    • getSpawnX

      public int getSpawnX()

      Spawn x, fixed point.

      Returns

      the spawn's left edge

    • getSpawnY

      public int getSpawnY()

      Spawn y, fixed point.

      Returns

      the spawn's top edge

    • getGoal

      public int[] getGoal()

      The goal rectangle as {x, y, w, h}, fixed point.

      Returns

      a new array

    • hash

      public long hash()

      A 64-bit digest of the whole simulation state. Two worlds that stepped the same level with the same inputs have the same hash; a replay compares it.

      Returns

      the hash