Maps & the world
Maps are 2D tile worlds drawn with PixiJS. You make them in Tiled and add behaviour in TypeScript.
Tiled maps
Put the .tmx next to the map's index.ts. Every time you save it in Tiled, a typed town.gen.ts is written next to it.
// src/content/maps/countryside/town/index.ts
import { defineMap } from '@/core/define'
import town from './town.gen'
export default defineMap({
title: 'Town',
music: 'music/town',
outdoor: true,
tiled: town,
// Behaviour for objects placed in Tiled, by their Tiled name (typed: a wrong name is an error).
objects: {
jack: { character: 'jack', facing: 'left', onInteract: ({ game }) => game.events.start('jack/hello') },
sign: { onInteract: ({ game }) => game.events.start('town/sign') },
},
// Extra objects made in code, added to the ones from Tiled.
extraObjects: [],
})
.tmx. Use code for behaviour (onInteract, schedules, scripts).What Tiled things mean
| In Tiled | Meaning |
|---|---|
Tile property solid = true | The whole tile blocks movement |
| Shapes drawn in the Tile Collision Editor | Only that part of the tile blocks (a fence: its bottom half). Wins over solid. See Collision smaller than a tile |
Layer property collision = true | Not drawn; any tile on it is solid |
Layers after the first object layer, or layer property above = true | Always drawn over the player (treetops, the map border) |
Layer property sorted = true | Buildings, walls, fences: in front of or behind the player depending on who is lower on screen. See Houses and roofs |
| Tile animations (Tiled's animation editor) | Play on the map |
| Groups, layer opacity and visibility | Respected (hidden layers aren't drawn) |
Object class spawn (+ property facing) | A named start position. The first spawn is the default. |
Object class door | An exit, see Doors |
Object class zone | Its own music/ambient sound, see Audio |
Object class encounter | Random battles (properties enemies and / or troops), see Encounters |
Object class enemy (+ property enemy, optional troop) | An enemy to fight, see Enemies on the map |
Object class = a prop name (chest) | That prop, see Props. Custom properties are passed to it. |
Object properties solid, label, color, facing | Simple objects; a name lets code give it behaviour |
| Tile objects (inserted tiles) | Drawn from the tileset, anchored bottom-left like in Tiled. Collision shapes on their tile make them solid there (unless solid is set) |
| Objects placed between tiles | Stay exactly where you put them, see Off-grid objects |
- Only orthogonal, finite maps. Tilesets: embedded, or external
.tsj(JSON). Not.tsx, which clashes with TypeScript's file extension. - Saving a tileset (
.tsj) regenerates every map that uses it. content/maps.test.tschecks every map: spawns on walkable tiles, doors to real maps and spawns, existing props, sprite animations, schedule spots.
Collision smaller than a tile
In the tileset, select a tile and open Tile Collision Editor (View > Tile Collision Editor). Draw rectangles over the parts that should block. A fence drawn with one rectangle over its bottom half lets the player walk right up to it from above.
- Use rectangles. Ellipses and polygons work, but only their bounding box counts (you get a warning).
- Shapes flip and rotate with the tile, and work on tiles in structures too.
- The dev panel's collision button shows them (orange boxes).
- NPC paths (
walkTo, schedules) treat a tile with any shape on it as blocked.
Houses and roofs (sorted layers)
A roof on an above layer is always drawn over the player. Standing right in front of the house, a tall sprite's head goes under the roof. Instead, paint the whole building (roof and wall) on a tile layer with the property sorted = true:
- Touching tiles on that layer become one piece. Each piece is sorted by its bottom edge, like characters are by their feet.
- Feet below the bottom edge (in front of the house): the player is drawn over the whole house. Feet above it (behind): the house covers the player.
- Two things that touch (a house and a tree next to it) become one piece. Leave a gap, or put them on two sorted layers.
- Fences go on a sorted layer too, so walking behind one hides your legs.
- Collision still comes from
solid, collision layers and collision shapes. Keepabovelayers for things that are always on top (treetops, the map border).
Example: maps/countryside/town/town.tmx, layer Buildings.
Picture layers: far hills, fog, clouds
A Tiled image layer becomes a picture behind the map or over it. Below the first object layer it is drawn behind everything; after it (or with the property above) it covers the map and the player.
| In Tiled | |
|---|---|
| Parallax X / Y | 1 = moves with the map, 0.5 = half as fast (far away), 0 = fixed on the screen. |
| Repeat X / Y | Tiles the picture in that direction. |
| Offset, Opacity | Where it starts (map pixels) and how see-through it is. |
Properties driftx, drifty | It moves by itself, map pixels per second (clouds, fog). |
Property blend | add, multiply or screen (light shafts, cloud shadows). |
Off-grid objects
Objects don't have to sit on the grid. Turn off snapping in Tiled (or hold Ctrl while dragging) and place them anywhere. Their at becomes the nearest tile and the rest goes in offset (in tiles, e.g. { x: 0.375, y: 0.25 }).
- Drawing, labels, lights, NPC collision slices and sorting use the exact spot.
- Solid simple objects block their exact area. Triggers (
onEnter) fire when the player's centre is inside it. Interaction checks the exact area too. - NPCs start at the exact spot. Scripted walks move them by whole tiles from there.
- In code, set
offsetyourself:{ at: { x: 4, y: 2 }, offset: { x: 0.5, y: 0 }, texture: lamp }.
Map options and hooks
| Option | |
|---|---|
title | Name (save slots, dev tools) |
music | Track id. null = silence. Leave it out to keep what's playing. |
ambient | Background loop. Left out = the weather's sound. null = none. |
zoom | How close this map is: 2 = twice as close, 0.5 = see twice as much. Default 1: map.tilesHigh tiles fit the screen height (see Zoom). |
background | Colour around the map |
outdoor | Tinted by the time of day |
lighting | A fixed colour over the map: { tint: '#000', strength: 0.6 } (any CSS colour) (caves, indoors at night) |
weather | { kind: 'rain', intensity: 0.5 }: rain, snow, petals, embers, or your own from content/weather.ts |
setup(ctx) | Right after the map is built, before it fades in. May return a cleanup function (runs when leaving). |
onEnter(ctx) / onExit(ctx) | Once the map is visible / before leaving it |
Hooks get { game, world, params }. world is the running map:
world.player.x / y / facing // position in tiles (x 2.5 = middle of column 2)
world.player.teleport({ x: 3, y: 4 }, 'down')
world.npc('jack') // script an NPC or prop (see Characters)
world.camera.follow('jack'); world.camera.panTo({ x: 10, y: 5 })
await world.camera.zoom(2, { duration: 800 }) // closer (on top of the map's zoom); 1 = normal
world.camera.reset() // follow the player again at zoom 1
world.lighting('#000000', { strength: 0.6, duration: 500 })
world.weather('snow', { intensity: 0.4 })
world.onTick((dt) => { … }) // every frame while the map is shown; returns an unsubscribe
world.root / world.tileSize // the PixiJS container, for custom drawing
Zoom
The map is sized by the screen height, not its resolution: the same screen ratio shows the same part of the map at the same size on 720p, 1080p or 4K. Set it in src/content/config/screen.ts:
map: {
tilesHigh: 12, // tiles that fit the screen height
textHeight: 720, // labels, bubbles and the prompt have their normal size at this height, and scale with it
pixelPerfect: false, // true = whole screen pixels per art pixel (even pixels, view size varies a little)
},
Three levels multiply: tilesHigh (whole game) × the map's zoom × world.camera.zoom() (scripts, also game.maps.camera.zoom(); back to 1 on a map change or camera.reset()).
Changing maps
await game.maps.go('countryside/forest', {
spawn: 'from_town', // typed spawn names; default: the first
transition: 'fade', // or 'instant'
params: { boss: true }, // if the map takes params
position: { x: 4.5, y: 2.5, facing: 'up' }, // exact spot instead of a spawn
})
game.maps.preload('countryside/forest') // load in the background
game.maps.current() // 'countryside/town'
What happens, in order: fade out (300 ms) → load (a loading screen appears if it takes over 250 ms) → old map's onExit and cleanup → build → setup → intro event (if any, see below) → fade in → onEnter.
I'm stuck
The pause menu's "I'm stuck" (and game.maps.unstuck()) fades out and puts the player on the map's when_stuck spawn. Without one, it uses spawn. In Tiled, make a spawn object named when_stuck.
Intro events
An intro event starts while the black cover is still up, so the map is never seen before the scene. Use it for a map's opening scene instead of onEnter:
export default defineMap({
intro: 'heaven/before_god/spawning', // or a function: ({ game }) => 'x' | false
introOnce: true, // skip it after it ran once in this playthrough
…
})
Its first background() appears at once (no crossfade from the map). Old saves and mid-scene loads do not start the intro.
Starting an event with no map was considered and not done: saves and replays need a map.
Calls made during a change wait their turn. The world is frozen while it changes and while an object's handler runs.
Map params: annotate the context type and the params become required in maps.go:
setup: ({ params }: MapContext<{ boss: boolean }>) => { if (params.boss) … }
Map objects
Everything placed on a map is an object: NPCs, signs, exits, invisible triggers. You give objects placed in Tiled behaviour by name in objects; objects that only exist in code go in extraObjects, written whole.
| Field | |
|---|---|
at, size | Tile position and area ({ w, h }, default 1×1) |
offset | Placed between tiles: { x, y } in tiles from at, see Off-grid objects |
id | Name for scripts (Tiled: the object's name) |
label, color, texture, solid | Looks and collision of simple objects |
onInteract(ctx) | Confirm while facing it |
onEnter(ctx) | Stepping onto it |
character, sprite, facing, animation, speed, turnToPlayer, schedule | Animated NPCs, see Characters |
prop, properties | A prop and its settings |
door | An exit |
light | Glows at night |
music, ambient | A sound zone |
encounters | Random battles in its area |
enemy, fight | An enemy standing here, see Enemies on the map |
Handlers get { game, world, object } and may be async; the world waits for them (the player can't move or trigger anything meanwhile).
Doors
In Tiled: an object with class door. No code needed.
| Property | |
|---|---|
map | Target map id, e.g. countryside/forest (required) |
spawn | Spawn in the target map (default: its first) |
transition | fade (default) or instant |
interact (bool) | Needs the confirm button instead of walking onto it (house doors) |
// The same in code:
{ id: 'to_town', at: { x: 0, y: 5 }, size: { w: 1, h: 2 }, door: { map: 'countryside/town', spawn: 'from_forest' } }
Props
Reusable objects with art and behaviour: chests, barrels, trees, campfires, pressure plates. Folder: props/<name>/index.ts with the art in props/<name>/sprites/<name>.aseprite.
import { defineProp } from '@/core/define'
import type { ItemId } from '@/core/registry'
import sprite from './sprites/chest.gen'
export default defineProp({
sprite,
label: 'Chest',
animation: 'closed', // start animation (default 'idle')
solid: true, // default true
light: undefined, // e.g. { radius: 4, flicker: 0.3 } for a campfire
setup: ({ prop, memory }) => { // when it appears on a map
if (memory.get('open')) prop.idle('open')
},
onInteract: async ({ game, prop, memory, properties }) => {
if (memory.get('open')) return game.toast(game.t('The chest is empty.'))
memory.set('open', true) // saved, per map and per prop
await prop.play('opening', { hold: true })
prop.idle('open')
game.items.add((properties.item ?? 'apple') as ItemId, 1, { notify: true })
},
// onEnter: walking over its "trigger" slices (or onto its tile)
})
- In Tiled: object class = the prop's name. The object's custom properties arrive as
properties(the chest above readsitem). Unnamed props get the idchest_x_y. - Collision and interaction come from the sprite's slices (see Art).
memoryis a small saved key/value store per prop (open, broken, used…).
await game.props.spawn('barrel', { at: { x: 20, y: 9 }, id: 'house_barrel', map: 'countryside/town' })
await game.props.spawn('barrel', { at: { x: 5, y: 5 }, save: false }) // gone after leaving the map
game.props.remove('house_barrel'); game.props.has('house_barrel'); game.props.list()
await game.props.get('house_barrel').play('idle') // script any prop, also ones from Tiled
Structures
A small Tiled map stamped into a map while playing: a house that gets built, a bridge that appears. Tiles, collision and objects come along. Placements are saved.
// src/content/structures/house/index.ts (+ house.tmx next to it)
export default defineStructure({
tiled: house,
objects: { door: { onInteract: ({ game }) => game.events.start('jack/house_door') } },
})
await game.structures.place('house', { at: { x: 21, y: 3 }, id: 'jacks_house', map: 'countryside/town' })
game.structures.has('jacks_house', { map: 'countryside/town' })
await game.structures.canPlace('house', { x: 21, y: 3 }) // null, or what's wrong
await game.structures.remove('jacks_house')
- Placing on another map just records it; it appears when you go there.
- Placing throws if it's out of the map or would trap the player, unless
force: true. A player standing on a new wall is moved to the nearest free tile. - The same id again moves/replaces it.
Fast travel and the world map
// src/content/travel.ts
export default defineTravel({
inMenu: true, // "Travel" in the pause menu once two places are known
discoverOnVisit: true, // walking onto a place's map adds it
blockedMaps: ['heaven/before_god'], // no travelling away from these
worldMap: { show: true, ratio: 16 / 9 /*, image: worldPng */ }, // false = a plain list
places: {
town: { name: tx('Town'), map: 'countryside/town', spawn: 'start', known: false, at: { x: 32, y: 58 } },
forest: { name: tx('Forest'), map: 'countryside/forest', spawn: 'from_town', known: false, at: { x: 68, y: 40 }, cost: 10 },
},
})
game.travel.open() // the window (world map)
game.travel.discover('forest', { notify: true }); game.travel.knows('forest'); game.travel.forget('forest')
await game.travel.go('forest') // null = arrived; else 'unknown' | 'here' | 'blocked' | 'busy' | 'money'
game.travel.list() // known places: { id, name, description, here, problem }
const places = usePlaces() // React
| Field | |
|---|---|
map, spawn | Where the place leads. Without spawn: the map's first spawn. |
known: false | Hidden until found. Leave it out for places known from the start. |
at | Its spot on the world map, in % of the board (0,0 = top left). Places without one are listed under the map. |
cost | Money per trip. |
The world map is a plain board until you give it a picture (worldMap.image, with ratio = the picture's width / height). Travelling works only from a map, not during events or minigames. The window is content/windows/travel; found places are saved.
Minimap
// src/content/config/screen.ts
minimap: {
show: true, // false = no minimap at all; true = shown, players can hide it (Settings)
size: 120, // longest side, px
radius: 12, // tiles around the player; 0 = always the whole map
colors: { ground: 'rgba(20, 28, 36, 0.78)', wall: 'rgba(170, 190, 205, 0.85)', player: '#ffd54a', door: '#5ad1ff', actor: '#ff8a65' },
doors: true, actors: true, opacity: 0.9,
},
// src/content/ui/Hud.tsx: where it sits
<Minimap sx={{ position: 'absolute', right: 16, top: 72 }} /> // the bottom corners belong to the touch controls
// one map without it (a maze, a dream)
export default defineMap({ minimap: false, /* … */ })
It shows what blocks the player (the same grid the collision uses), doors, characters and the player. It needs no art and follows built structures and spawned props by itself.
With show: true players get Show minimap in Settings → General to hide it for themselves. With show: false there is no minimap and no switch (also never in visual novel mode).
Time of day
// src/content/config/clock.ts
export default defineClock({
minutesPerSecond: 1, // a day lasts 24 real minutes; 0 = time stands still
startHour: 8,
tints: [
{ hour: 0, tint: '#0b1640', strength: 0.55 },
{ hour: 9, tint: '#ffffff', strength: 0 },
{ hour: 19, tint: '#ff8a4c', strength: 0.22 },
], // blended between hours, wraps around midnight; any CSS colour (#hex, rgb(), names)
})
game.clock.hour(); game.clock.minute(); game.clock.day(); game.clock.label() // "08:30"
game.clock.set(18, 30); game.clock.set(7, 0, { nextDay: true }); game.clock.advance(60)
game.clock.pause(); game.clock.resume()
Time runs only while the player is free on a map, and is saved. It stops on the title screen, during events, minigames, open windows (menu, shop, inventory), map changes and while an object's onInteract runs. Scripts can still move it with set/advance. Only maps with outdoor: true follow the tint. NPC schedules and shop restocks use the clock too.
Calendar: weekdays, months, dated events
// src/content/config/clock.ts
export default defineClock({
// …
calendar: {
weekdays: [tx('Monday'), tx('Tuesday'), /* … */ tx('Sunday')],
months: [{ name: tx('Spring'), days: 30 }, { name: tx('Summer'), days: 30 }, /* … */], // a year = all of them
start: { year: 1, month: 1, day: 1, weekday: 'Monday' }, // a new game's first day
format: tx('{weekday}, {day} {month}'), // also {monthNumber} {year}
dates: {
market_day: { name: tx('Market day'), weekday: 'Saturday' }, // every week
new_year: { name: tx("New Year's Day"), month: 1, day: 1 }, // once a year
harvest: { name: tx('Harvest festival'), month: 3, day: 15, event: 'town/festival', hour: 18 },
},
},
})
game.clock.date() // { index, year, month, monthName, day, weekday, weekdayIndex }
game.clock.date(1).weekday // tomorrow
game.clock.dateLabel() // "Monday, 1 Spring" (translated)
game.clock.is('market_day') // today? is('market_day', 1) = tomorrow?
game.clock.today() // [{ id: 'market_day', name: 'Market day' }]
game.clock.daysUntil('harvest') // 0 = today, -1 = never
const date = useDate() // React: re-drawn when the day changes
- A named date matches when every field it gives matches:
month+dayis once a year,weekdayevery week,dayalone every month,yearnarrows it to one year. - With
event, that event starts by itself on such a day: once per day, as soon as the player is free on a map, not beforehour. Sleeping past the day (clock.set(…, { nextDay: true })) skips it. - Use dates anywhere: a shop that opens on
market_day, a line of dialogue, an NPC'sonInteract. - Without
calendarthe game only counts days (game.clock.day()).
Lighting and weather
game.maps.lighting('#000000', { strength: 0.6, duration: 500 }) // until the next map; null = off
game.maps.weather('rain', { intensity: 0.5 }) // 'snow' | null
A map's own lighting wins over the clock tint. Rain plays its sound automatically (see ambient).
Terrain: what the ground does
// src/content/terrain.ts
export default defineTerrain({
footsteps: { volume: 0.4, everyTiles: 1, pitchSpread: 0.08 /*, default: 'sfx/step' */ },
kinds: {
grass: { footstep: 'sfx/step_grass' },
tall_grass: { encounters: 2, speed: 0.85 }, // twice the random battles, a little slower
road: { encounters: 0 },
mud: { speed: 0.55 },
lava: { damage: 3, damageSound: 'sfx/hit' }, // HP per tile, stops at 1 HP unless lethal: true
ladder: { ladder: true, speed: 0.6 }, // faces up, moves only up / down
},
})
game.maps.terrain() // the name under the player's feet, or undefined
| In Tiled | |
|---|---|
Tile property terrain | On a tile in the tileset (string, a name from the list): every place that tile is painted. |
Layer property terrain | On a tile layer: every cell painted on it, whatever the tile. The layer may be hidden (a marker layer). An empty value clears the terrain there. |
Later layers win. lethal: true ground can bring the whole party down, which is a game over. Difficulty can scale the damage and the battle chance.
Pushable objects
// a prop: src/content/props/barrel/index.ts
export default defineProp({
sprite,
pushable: true,
// pushReset: true, // back at its start every time the map is entered
onPushed: ({ game, to, on }) => { // also `from`; `on` = ids of objects under it now
if (on.includes('plate')) game.flags.set('door_open', true)
},
})
// or one object on one map
objects: { crate: { pushable: true, onPushed: ({ on }) => { … } } }
// src/content/config/player.ts
push: { delayMs: 220, speed: 4 /*, sound: 'sfx/push' */ },
Walk against it for delayMs and it slides one tile, unless a wall, the map edge or another object is behind it. Where it stops is saved. Only sprite objects standing on whole tiles can be pushed. In Tiled: the boolean property pushable (and pushReset) on the object. Example: the barrels in the town.
Jumps and ledges
// src/content/terrain.ts
gap: { jump: true }, // hop over it from any side
ledge_down: { jump: 'down' }, // only when moving down: a one-way ledge
// src/content/config/player.ts
jump: { delayMs: 120, ms: 280, height: 0.6 /*, sound: 'sfx/jump' */ },
Give a blocking tile (a hole, a cliff edge) one of these terrains. Walking against it makes the player hop to the tile behind it, when that tile is free. Jumps are one tile wide.
Lights
// any object or prop
light: { radius: 4, color: '#ffa04a', flicker: 0.35, intensity: 1, offset: { x: 0, y: -4 } }
// an item (content/items/lantern): around the player while it is carried and switched on
defineItem({ light: { radius: 3.5, color: '#ffe0a0' }, /* … */ })
// the player (content/config/player.ts): always
light: { radius: 2 }
Lights lift the darkness in a soft circle and add a faint glow. They only show when the map is dark (night tint or lighting); by day nothing changes. Radius is in tiles, offset in pixels. Hidden NPCs give no light. Item lights: Items that do something while carried.