LittleJS 1.28.0

Editor

LevelEditor

The level editor, open it to pause the game and edit its level, close it to play on with the changes

  • One of it, levelEditor, 0 on the debug overlay opens and closes it too
  • A game makes it its own: it sets the hooks, adds keys, panel buttons and tools, or extends this class and gives its own with setLevelEditor; the same calls work in the 2D editor and the 3D one, see EDITOR.md
  • In release builds LevelEditor is a stub that never opens: what a game adds is taken and never called

Constructor

new LevelEditor()engineEditor.js:129
levelEditor.onRestart = ()=> loadLevel(); // adds a Restart button that rebuilds the level
levelEditor.onTile = (layer, pos, tile)=> layer.setCollisionData(pos, tile === ladderTile ? -1 : tile ? 1 : 0);
levelEditor.addKey('k', ()=> clearEnemies(), 'K: clear the enemies');

// or as a class of the game's own
class MyEditor extends LevelEditor
{
    onRestart() { loadLevel(); }
    onDraw() { drawSpawnZones(); }
}
setLevelEditor(new MyEditor);

Members

Methods

Members

buttons : Array<{label: string, onClick: function(), title: string}>engineEditor.js:149

The 2D editor's edit functions, for a key, a button or a tool of the game's own: map, the Tiled map; layer, the selected TileLayer; objects, a copy of the selected object layer's; hover, the cell under the mouse; selection, the selected objects' ids; paint(cell, tile, direction, mirror), a tile index, -1 erases; changeObjects((list)=> ...); strokeEnd() and strokeCancel(); bulk(()=> ...); undo(redo); toJSON()

The 3D editor's edit functions, undefined without the 3D plugins: level; objects; selection, a Set of ids; selected(); made(id); change((list)=> ...); changePart(name, (part)=> ...); strokeEnd() and strokeCancel(); place(type, pos3D); setTransform(object, pos3D, rotationDegrees, scale3D); setProperty(object, name, value); pos(object), rotation(object) and scale(object); mousePoint(); undo(redo); toJSON()

is3D : booleanengineEditor.js:200

True when the editor in use is the 3D one, as use3D says or since a 3D level was loaded

isOpen : booleanengineEditor.js:196

True while the editor is open, the game is paused under it

keys : Object<string, {action: function(boolean): any, help: (string|undefined), warned: boolean}>engineEditor.js:147
paletteTiles : Array<number>|undefinedengineEditor.js:136

The tiles the palette shows, in its order, for a sheet that also holds sprites and art that are not level tiles; undefined shows every tile of the sheet

tool : string|undefinedengineEditor.js:144

The tool of the game's own that is on, one added with addTool, undefined while a tool of the editor's is

tools : Object<string, EditorTool<any>>engineEditor.js:151
use3D : boolean|undefinedengineEditor.js:140

Which level editor opens: true the 3D one, false the 2D one, undefined the 3D one when a level was loaded with level3DLoad and there is a Render3DPlugin

Methods

addButton(label, onClick, title)engineEditor.js:242

Add a button of the game's own to the editor's panel

label string
onClick function
title string optional

What it says when the mouse is over it

addKey(key, action, helpLine)engineEditor.js:224

Add a key of the game's own to the editor, the 2D and the 3D one

  • Escape and 0 are the editor's own, to play and to exit, and Ctrl is the one modifier: Shift is given to the action
key string

A letter or digit, 'k', or a key's name in any case, 'Delete', 'F2', 'Enter', 'Space'; 'ctrl+k' with Ctrl or Cmd

action function

Called with whether Shift is held; returning false says it did nothing, and the key is left to the browser

helpLine string optional

A line for the editor's help

addTool(name, tool)engineEditor.js:255

Add a tool of the game's own: a button in the panel by its name, and while it is on the left button in the level is its, not the editor's own tools'

  • Each callback is given {pos, cell, ray, shift, ctrl}: pos is where the mouse is in the level, a Vector2 in 2D and a Vector3 on the level or the ground in 3D; cell is the tile under it in 2D; ray is the mouse's in 3D
  • What the callbacks change through edit2D or edit3D is one undo: the editor ends the stroke at the release, and takes it back when the right button or Escape ends the press
name string
tool EditorTool<P>

{key, hint, onPress, onDrag, onRelease, onDraw}, each optional; onPress returning false says the press was not the tool's

Close the editor and end its session, the game carries on with the changes and Escape opens the debug overlay again

Called when the editor closes and the game plays on, to set or override

Called while the editor draws the level, to draw overlays of the game's own in it, to set or override

Called when the editor opens, to set or override

onPanel(element)engineEditor.js:185

Called once when the editor's panel is made, with a box in it for the game's own controls, to set or override

element HTMLElement
onPlayFrom(pos)engineEditor.js:169

Put the player at a position, a Vector2 in the 2D editor and a Vector3 in the 3D one; set it or override it and the editor has Play from mouse, which starts play there

pos any

A Vector2 from the 2D editor, a Vector3 from the 3D one

Rebuild the level from what the editor changed; set it or override it and the editor has a Restart button, which calls it after switching to play

onSave(text, fileName) → boolean|void|Promise<(boolean|void)>engineEditor.js:192

Called by Save with the text of the file and its name; return true when the game kept it itself, and the editor writes no file, to set or override; it may be async

text string
fileName string
onTile(layer, pos, tile)engineEditor.js:160

What the game does when the 2D editor paints a tile, like setting its collision or its look the way the game does when it loads the level; set it or override it, without one the collision layer gets collision 1 where there is a tile

layer TileLayer

The layer painted

pos Vector2

The cell's position in the layer

tile number|undefined

The tile painted, undefined when erased

Called each frame while the editor is open, to set or override

Open the editor, pausing the game; until close(), Escape (the debug key) switches between playing and editing