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 PlatformWorld

  1. Object
  2. PlatformWorld

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.

Fields

public static final int TICKS_PER_SECOND = 60The rate the world is designed for.
public static final int COUNTERS = 16How many game counters a world keeps.

Constructors

public PlatformWorld(TileGrid grid, long seed)Creates a world over a grid.

Methods

public void addPlatform(MovingPlatform p)Adds a moving platform.
public void addActor(Actor c)Adds an actor.
public void addEntity(LevelEntity e)Adds an entity.
public void setRules(GameRules rules)Sets the game’s rules.
public GameRules getRules()The game’s rules.
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.
public int getPlayerCount()How many of the first actors are players.
public boolean isPlayer(Actor c)Whether an actor is one of the players.
public Actor nearestPlayer(Actor from)The live player closest to an actor, for enemies that chase or aim.
public void complete(Actor by)Completes the level, won by an actor.
public void endGame()Ends the game as lost.
public Actor getWinner()The actor that completed the level.
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).
public void setCounter(int index, int value)Sets a game counter.
public void addCounter(int index, int delta)Adds to a game counter.
public void setSpawn(int x, int y)Sets where the player starts and comes back after dying.
public void setGoal(int x, int y, int w, int h)Sets the goal: the level is complete when the player overlaps it.
public void step(int[] inputs)Advances one tick with one input per actor, in the order they were added.
public void step(int input)Advances one tick for a single player.
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.
protected void onDeath(Actor c)Called when an actor runs out of health or falls out of the level.
public void emit(int type, int x, int y, String subject, String detail)Records an event for the view.
public List<WorldEvent> getEvents()The events of the last tick.
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.
public boolean isChannelOn(int channel)Whether a switch channel is on.
public void setChannel(int channel, boolean on)Turns a switch channel on or off.
public void addScore(int points)Adds to the score.
public void addCoins(int n)Adds coins.
public void addSecret()Counts a secret found.
public void addLife()Adds a life, when lives are limited.
public void addKey(String name)Adds a key to the inventory.
public boolean useKey(String name)Uses up a key.
public List<String> getKeys()The keys held.
public int getLives()Lives left; negative means unlimited.
public void setLives(int lives)Sets the lives; negative means unlimited.
public void setStompBounce(int speed)The upward speed of a stomp bounce.
public int getScore()The score.
public int getCoins()The coins collected.
public int getSecrets()The secrets found.
public TileGrid getGrid()The collision map.
public List<MovingPlatform> getPlatforms()The moving platforms.
public List<Actor> getActors()The actors, the player first.
public List<LevelEntity> getEntities()The active entities.
public Actor getPlayer()The player, the first actor added.
public Rng getRng()The world’s random generator.
public int getTick()Ticks stepped so far.
public boolean isComplete()Whether the player reached the goal.
public boolean isGameOver()Whether the player ran out of lives.
public int getDeaths()How many times the player died.
public int getSpawnX()Spawn x, fixed point.
public int getSpawnY()Spawn y, fixed point.
public int[] getGoal()The goal rectangle as {x, y, w, h}, fixed point.
public long hash()A 64-bit digest of the whole simulation state.

Inherited methods

Field details

TICKS_PER_SECOND

public static final int TICKS_PER_SECOND = 60
The rate the world is designed for.

COUNTERS

public static final int COUNTERS = 16
How many game counters a world keeps.

Constructor details

PlatformWorld

public PlatformWorld(TileGrid grid, long seed)
Creates a world over a grid.

Parameters

grid TileGrid
the level’s collision map
seed long
the random seed

Method details

addPlatform

public void addPlatform(MovingPlatform p)
Adds a moving platform.

Parameters

p MovingPlatform
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 Actor
the actor

addEntity

public void addEntity(LevelEntity e)
Adds an entity. Added during a step, it takes part from the next tick.

Parameters

e LevelEntity
the entity

setRules

public void setRules(GameRules rules)
Sets the game’s rules. Call before the first step.

Parameters

rules GameRules
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 int
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 Actor
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 Actor
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 Actor
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 int
0 to COUNTERS - 1

Returns

the value

setCounter

public void setCounter(int index, int value)
Sets a game counter.

Parameters

index int
0 to COUNTERS - 1
value int
the value

addCounter

public void addCounter(int index, int delta)
Adds to a game counter.

Parameters

index int
0 to COUNTERS - 1
delta int
the amount

setSpawn

public void setSpawn(int x, int y)
Sets where the player starts and comes back after dying.

Parameters

x int
left, fixed point
y int
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 int
left, fixed point
y int
top, fixed point
w int
width, fixed point
h int
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 int[]
Input bits per actor

step

public void step(int input)
Advances one tick for a single player.

Parameters

input int
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 Actor
the actor
amount int
hit points
fromX int
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 Actor
the actor

emit

public void emit(int type, int x, int y, String subject, String detail)
Records an event for the view.

Parameters

type int
a WorldEvent constant
x int
where, fixed point
y int
where, fixed point
subject String
the id involved, may be null
detail String
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 Actor
the actor
name String
a stock ability name (see Abilities)

isChannelOn

public boolean isChannelOn(int channel)
Whether a switch channel is on.

Parameters

channel int
0 to 31

Returns

true while on

setChannel

public void setChannel(int channel, boolean on)
Turns a switch channel on or off.

Parameters

channel int
0 to 31
on boolean
the new state

addScore

public void addScore(int points)
Adds to the score.

Parameters

points int
the points

addCoins

public void addCoins(int n)
Adds coins.

Parameters

n int
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 String
the key name

useKey

public boolean useKey(String name)
Uses up a key.

Parameters

name String
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 int
the lives

setStompBounce

public void setStompBounce(int speed)
The upward speed of a stomp bounce.

Parameters

speed int
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