Class 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHow many game counters a world keeps.static final intThe rate the world is designed for. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an actor.voidaddCoins(int n) Adds coins.voidaddCounter(int index, int delta) Adds to a game counter.voidAdds an entity.voidAdds a key to the inventory.voidaddLife()Adds a life, when lives are limited.voidAdds a moving platform.voidaddScore(int points) Adds to the score.voidCounts a secret found.voidCompletes the level, won by an actor.booleanHurts an actor: removes hit points (respecting invulnerability), knocks it away fromfromX, and kills it at zero.voidRecords an event for the view.voidenableAbility(Actor c, String name) Switches an ability on for an actor, creating it if the actor lacks it, as a power-up does.voidendGame()Ends the game as lost.The actors, the player first.intgetCoins()The coins collected.intgetCounter(int index) A game counter: a number the rules keep that the engine does not interpret (distance run, blocks on targets, rounds won).intHow many times the player died.The active entities.The events of the last tick.int[]getGoal()The goal rectangle as{x, y, w, h}, fixed point.getGrid()The collision map.getKeys()The keys held.intgetLives()Lives left; negative means unlimited.The moving platforms.The player, the first actor added.intHow many of the first actors are players.getRng()The world's random generator.getRules()The game's rules.intgetScore()The score.intThe secrets found.intSpawn x, fixed point.intSpawn y, fixed point.intgetTick()Ticks stepped so far.The actor that completed the level.longhash()A 64-bit digest of the whole simulation state.booleanisChannelOn(int channel) Whether a switch channel is on.booleanWhether the player reached the goal.booleanWhether the player ran out of lives.booleanWhether an actor is one of the players.nearestPlayer(Actor from) The live player closest to an actor, for enemies that chase or aim.protected voidCalled when an actor runs out of health or falls out of the level.voidsetChannel(int channel, boolean on) Turns a switch channel on or off.voidsetCounter(int index, int value) Sets a game counter.voidsetGoal(int x, int y, int w, int h) Sets the goal: the level is complete when the player overlaps it.voidsetLives(int lives) Sets the lives; negative means unlimited.voidsetPlayerCount(int count) Sets how many of the first actors are players: they take input, and each respawns on death instead of dying.voidSets the game's rules.voidsetSpawn(int x, int y) Sets where the player starts and comes back after dying.voidsetStompBounce(int speed) The upward speed of a stomp bounce.voidstep(int input) Advances one tick for a single player.voidstep(int[] inputs) Advances one tick with one input per actor, in the order they were added.booleanUses up a key.
-
Field Details
-
TICKS_PER_SECOND
public static final int TICKS_PER_SECONDThe rate the world is designed for.- See Also:
-
COUNTERS
public static final int COUNTERSHow many game counters a world keeps.- See Also:
-
-
Constructor Details
-
PlatformWorld
Creates a world over a grid.
Parameters
grid: the level's collision mapseed: the random seed
-
-
Method Details
-
addPlatform
Adds a moving platform.
Parameters
p: the platform
-
addActor
Adds an actor. The first one added is the player, which
stepdrives 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
Adds an entity. Added during a step, it takes part from the next tick.
Parameters
e: the entity
-
setRules
Sets the game's rules. Call before the first step.
Parameters
rules: the rules, or null for a classic platformer
-
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
Whether an actor is one of the players.
Parameters
c: the actor
Returns
true for one of the first
getPlayerCount()actors -
nearestPlayer
-
complete
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
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 toCOUNTERS - 1
Returns
the value
-
setCounter
public void setCounter(int index, int value) Sets a game counter.
Parameters
index: 0 toCOUNTERS - 1value: the value
-
addCounter
public void addCounter(int index, int delta) Adds to a game counter.
Parameters
index: 0 toCOUNTERS - 1delta: the amount
-
setSpawn
public void setSpawn(int x, int y) Sets where the player starts and comes back after dying.
Parameters
x: left, fixed pointy: 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 pointy: top, fixed pointw: width, fixed pointh: 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:Inputbits per actor
-
step
public void step(int input) Advances one tick for a single player.
Parameters
input: the player'sInputbits
-
damage
Hurts an actor: removes hit points (respecting invulnerability), knocks it away from
fromX, and kills it at zero. An actor withoutHitPointsdies at once.Parameters
c: the actoramount: hit pointsfromX: where the hit came from, fixed point x
Returns
true if the hit landed
-
onDeath
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
-
getEvents
The events of the last tick.
Returns
the live list, cleared at the start of each step
-
enableAbility
-
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 31on: 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
Adds a key to the inventory.
Parameters
name: the key name
-
useKey
Uses up a key.
Parameters
name: the key name
Returns
true if the key was held
-
getKeys
-
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
The collision map.
Returns
the grid
-
getPlatforms
The moving platforms.
Returns
the live list
-
getActors
-
getEntities
The active entities.
Returns
the live list
-
getPlayer
The player, the first actor added.
Returns
the player, or null in an empty world
-
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
-