LittleJS 1.31.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:207
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:227

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)=> ...); setProperty(object, name, value); 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:287

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

isOpen : booleanengineEditor.js:283

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

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

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:222

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:229
use3D : boolean|undefinedengineEditor.js:218

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:335

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:313

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

  • Escape and 0 ('Digit0' too) 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', a key's name in any case, 'Delete', 'F2', 'Enter', 'Space', or a key code with its capital, 'ShiftLeft', 'Numpad1', 'KeyE', which both editors read; '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 in the 2D editor; the 3D editor reads keys in its update, after the browser has them, so it keeps every key added here from the browser

helpLine string optional

A line for the editor's help

addTool(name, tool)engineEditor.js:348

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, lastCell, 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, and lastCell the one at the callback before in a drag, to fill the cells a quick drag skipped; 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, Escape or a two finger pinch ends the press, with no onRelease
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:272

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:256

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 Vector2|Vector3

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

onRestart() → void|Promise<void>engineEditor.js:251

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

  • It may be async: till its promise settles, every action that starts a restart waits, with a note at the mouse: Restart, and in the 2D editor a resize, its undo and redo, and Apply or Reset of another size, an Apply of the same size going in as any tile edit, and the 3D editor takes no edit, undo or Reset; the game is held still while the editor is open, so one that waits for its frames, a fade, finishes once the editor closes, which Play and Escape do then with no player put at the mouse, and with no debug key the 3D editor stays closed till it settles, then opens again; the 2D editor takes no object edit meanwhile, which the restart would make a second time, nor an undo of one, an Apply carrying object edits, a Reset putting objects back, or a Clear, Cut, drag or stamp with All Layers holding objects, each refused whole; tile edits go in; a promise that never settles makes restarts wait till the page reloads
onSave(text, fileName) → boolean|void|Promise<(boolean|void)>engineEditor.js:279

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:238

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