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 final class LevelData

  1. Object
  2. LevelData

A platformer level as data, read from JSON and turned into a PlatformWorld.

The format is meant to be written by hand, by the level editor and by a language model alike. Positions and sizes are in tiles, speeds in pixels per tick, and every number may be a decimal (read exactly, see LevelJson):

{
  "name": "First steps",
  "tileSize": 16,
  "tiles": [
    "#..............#",
    "#.........====.#",
    "#.....H........#",
    "#.....H..../####",
    "################"
  ],
  "spawn": [1, 3],
  "goal": [14, 2, 1, 1],
  "platforms": [{"path": [[3, 2], [8, 2]], "w": 2, "speed": 0.75, "pause": 30}],
  "abilities": ["move", "ladder", "walljump", "jump", "crouch", "dash"],
  "tuning": {"maxJumps": 2, "jumpSpeed": 4.6}
}

tiles uses the actors listed on Tile. abilities lists the hero’s abilities in processing order (names on Abilities); omitted, the hero gets move, ladder, walljump, jump, crouch and dash. tuning keys are listed on applyTuning. health gives the hero hit points (otherwise one hit kills) and lives limits retries (otherwise unlimited). players (default 1) makes that many heroes, hero, hero2, …, side by side at the spawn, for a shared-screen game.

entities places everything else, each with "type" and "at": [col, row]: coin, gem, heart, life, key and powerup (name: the key or the ability), spring (power), checkpoint, door (key, h), gate (channel, w, h, inverted), lever and plate (channel), crumble (w, delay, respawn), crate (a PushBlock) and enemy (kind, brain, hp, damage, speed, range, stompable, and opens: a channel its defeat turns on). Other types go to an EntityFactory.

Methods

public static LevelData parse(String text)Reads a level.
public String getName()The level’s name.
public int getTileSize()The tile size in pixels.
public Map<String, Object> getJson()The parsed JSON, for game-specific fields (enemies, pickups, art).
public String[] getTiles()The tile rows.
public PlatformWorld createWorld(long seed)Builds a fresh world with the hero at the spawn point.
public PlatformWorld createWorld(long seed, EntityFactory factory)Builds a fresh world, creating unknown entity types through a factory.
public static void applyTuning(Actor c, Map<String, Object> t)Applies tuning to an actor’s abilities.

Inherited methods

Method details

parse

public static LevelData parse(String text)
Reads a level.

Parameters

text String
the level JSON

Returns

the level

Throws

IllegalArgumentException
when the JSON or the level is malformed

getName

public String getName()
The level’s name.

Returns

the name

getTileSize

public int getTileSize()
The tile size in pixels.

Returns

the tile size

getJson

public Map<String, Object> getJson()
The parsed JSON, for game-specific fields (enemies, pickups, art).

Returns

the root object

getTiles

public String[] getTiles()
The tile rows.

Returns

a copy of the rows, top first

createWorld

public PlatformWorld createWorld(long seed)
Builds a fresh world with the hero at the spawn point.

Parameters

seed long
the random seed

Returns

a world ready to step

createWorld

public PlatformWorld createWorld(long seed, EntityFactory factory)
Builds a fresh world, creating unknown entity types through a factory.

Parameters

seed long
the random seed
factory EntityFactory
the game’s own entity types, may be null

Returns

a world ready to step

applyTuning

public static void applyTuning(Actor c, Map<String, Object> t)
Applies tuning to an actor’s abilities. Keys, all optional, in pixels and ticks: gravity, maxFall, walkSpeed, runSpeed, jumpSpeed, maxJumps, coyoteTicks, bufferTicks, climbSpeed, dashSpeed, dashTicks, wallSlide, automaticFire, shotGravity, fireCooldown. Unknown keys are ignored so a game can keep its own keys in the same object.

Parameters

c Actor
the actor
t Map<String, Object>
the tuning object, may be null