Class LevelData

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

public final class LevelData extends Object

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.

  • Method Details

    • parse

      public static LevelData parse(String text)

      Reads a level.

      Parameters
      • text: 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: 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: the random seed
      • factory: 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: the actor
      • t: the tuning object, may be null