Maps
Designers make maps in the map editor while programmers write rules, in the same repository. The editor runs in the dev server, saves maps as files of the project, and takes tools and panels of your game. The full editor guide is The map editor.
Quick start
import { openEditor } from '@v12-engine/editor'
import '@v12-engine/editor/styles.css'
import { game } from './game'
import { DEFAULT_MAP, mapNamed } from './maps'
import { onStart } from './rules'
// it starts on the game's first map, as a file of the project: Save writes it back
const opened = openEditor({ target: '#game', game, onStart, startMap: () => mapNamed(), startFile: DEFAULT_MAP })
Object.assign(window, { opened })import { editorFiles } from '@v12-engine/editor/vite'
import { defineConfig } from 'vite'
export default defineConfig({ plugins: [editorFiles({ gameFolder: () => 'src' })] })The editor opens on your first map and Save writes it back to src/maps. A project from npm create @v12-engine has this already: /?editor opens it.
Common tasks
Load maps in your game
import { type GameMap, mapsFrom } from '@v12-engine/engine'
import bay from './maps/bay.vgmap.json'
export const maps: Readonly<Record<string, GameMap>> = mapsFrom({ './maps/bay.vgmap.json': bay })Save writes <name>.vgmap.json and <name>.names.ts (the map's names as types). The rules find things by name: world.map.path('road'), world.map.point('entry'), world.named('gate'); a misspelled name is a compile error. parseMap brings older files up to date.
Merge map changes in git
Map files are written for diffs: one object a line, one row of ground a line, new ids starting at random. What git can't merge, mergeMaps(base, ours, theirs) merges thing by thing and cell by cell. In .gitattributes:
*.vgmap.json merge=vgmapand once per clone:
git config merge.vgmap.driver "npm run --silent map merge %O %A %B"When a map changes on disk while open (a git pull), the editor asks: load theirs, save as another file, or keep yours.
Check maps in CI
import { checkMap, createRegistry } from '@v12-engine/engine'
import { maps } from '../src/maps'
import { rules } from '../src/rules'
const registry = createRegistry(rules)
for (const [name, map] of Object.entries(maps)) {
const errors = checkMap(map, registry).filter((problem) => problem.error)
if (errors.length) throw new Error(`${name}: ${errors.map((problem) => problem.message).join('; ')}`)
}Add your own checks with MapEditorDef.check (the game module's editor field); Play refuses a map with an error.
Work on maps from the command line
npm run map check # every map; exits 1 on an error
npm run map -- picture src/maps/road.vgmap.json road.png --scale 4
npm run map -- generate my-game big --seed 7 --size 128 # a map by the game's generator, into src/maps
npm run map -- diff before.vgmap.json after.vgmap.json changes.png
npm run map -- diff before.vgmap.json after.vgmap.json --text # in words, for a review
npm run map -- run scripts/widen.ts src/maps/test.vgmap.json # a script of yours, given the MapEditor
npm run map merge base.vgmap.json ours.vgmap.json theirs.vgmap.jsonThe command is @v12-engine/editor/map-command (mapCommand(args, { games, mapsOf, load })).
Let designers change numbers
export const campaign = layData(
{
// ...
editable: {
units: (def) => (def.kind === 'building' ? ['name', { path: 'stats.health', min: 1 }, { path: 'cost.gold', kind: 'integer', min: 0 }] : null),
items: [{ path: 'cost.gold', kind: 'integer', min: 0 }, 'description']
}
},
dataFile // import dataFile from './data.json'
)The Data panel shows these fields; a change applies at once, and Keep writes it to data.json. In code: editor.data. See The game's data.
Give the editor your game's tools
An editor scene or openEditor takes tools (GameToolDef), panels (EditorPanelDef), kinds (MapKindDef: a kind of thing the engine doesn't have, read by the rules with world.map.things(kind)), inspector, menus, search, icons, actions, and fields on placed things (defineMapField in editor.fields). See Tools and panels of a game's own and Panels of the editor.
Reference
openEditor options
| Option | What |
|---|---|
target | the element |
game | always given: the editor has no game of its own |
onStart | what the game runs on a map when you press Play here |
startMap | the map it opens on when nothing was kept |
startFile | the file that map came from, so Save writes it back |
project | false: no project, keep maps in the browser (editor.project, a Project) |
opened.editor is the EditorClient; opened.dispose() removes it; editor.mapFiles.openFile(name) opens another map. In an app of scenes, the same options go to editorScene.
editorFiles(options) (EditorFilesOptions)
games, gameFolder, public, code and more. With no options it expects maps in src/games/<game>/maps. Without the plugin, the editor keeps maps in the browser, and Export and Import move them as files.
Moving around in the editor
| Input | Does |
|---|---|
| right button held + W A S D (Q, E, Shift) | fly |
| Alt + drag | orbit |
| Alt + right button | dolly |
| middle button | pan |
| F | frame the selection (the whole map with nothing) |
| W, E, R / Q | move, rotate, scale handles / none |
| right click | a menu for what is under it |
| drag from the tools | put a unit, prefab or doodad down there |
In code: editor.camera.look, dolly, frame, topDown; editor.framing.frameSelection(), framing.topView(); editor.contextMenus.for('object', ({ target }) => [{ label: 'Make it a boss', run }]); editor.dropAt(tool, x, y); the dock with editor.dock.open('hierarchy'), close, focus, layout(), setLayout(layout), reset(), editor.dock.add({ id, title, mount }). Every key is an action you can rebind (Help › Every key; editor.keys).
In the example
apps/example/src/scenes/index.ts: melee's editor scene.apps/example/src/games/melee/maps: melee's maps and names files.apps/example/scripts/mapCommand.ts: thenpm run mapcommand.- The
campaignstarter'ssrc/campaign-editor.tsandsrc/waves-panel.ts: a panel, icons, search and a cinematic lane of a game's own.
What a map is to the engine: Maps.