Add the admin world editor for painting tiles and opening chunks.
Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -0,0 +1,53 @@
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>World editor</title>
6
<link rel="stylesheet" href="/ui/editor.css">
7
</head>
8
<body>
9
<header>
10
<h1>World editor</h1>
11
<p id="who">Connecting…</p>
12
<p id="error" class="error"></p>
13
</header>
14
<aside>
15
<p>Chunk <strong id="coords">0, 0</strong></p>
16
<p id="revision"></p>
17
<p id="authored"></p>
18
<label class="open-toggle"><input id="open" type="checkbox"> Open (enterable)</label>
19
<div class="nav">
20
<button id="north" type="button">Up</button>
21
<div>
22
<button id="west" type="button">Left</button>
23
<button id="east" type="button">Right</button>
24
</div>
25
<button id="south" type="button">Down</button>
26
</div>
27
<p>Tool: <strong id="tool"></strong></p>
28
<div class="palette">
29
<button type="button" data-tool='{"kind":"ground","typeId":1}'>Grass</button>
30
<button type="button" data-tool='{"kind":"ground","typeId":2}'>Wood</button>
31
<button type="button" data-tool='{"kind":"block","typeId":1}'>Stone wall</button>
32
<button type="button" data-tool='{"kind":"block","typeId":2}'>Torch</button>
33
<button type="button" data-tool='{"kind":"erase"}'>Erase block</button>
34
</div>
35
<button id="save" type="button">Save chunk</button>
36
<button id="apply" type="button">Apply now</button>
37
<label>Night preview <input id="night" type="range" min="0" max="24" step="0.1" value="12"><span id="night-label">12.0</span></label>
38
<fieldset>
39
<legend>World settings</legend>
40
<label>Walk tiles/s <input id="walk-speed" type="number" min="0.5" step="0.5" value="2"></label>
41
<label>Sim tick Hz <input id="tick-hz" type="number" min="5" step="1" value="20"></label>
42
<button id="save-settings" type="button">Save settings</button>
43
</fieldset>
44
<p class="hint">Saves write a pending revision. Apply now reloads the live world. Marketplace placement waits on assets.</p>
45
</aside>
46
<section id="changelog-wrap">
47
<h2>Changelog</h2>
48
<div id="changelog"></div>
49
</section>
50
<script src="/framework/framework.js"></script>
51
<script type="module" src="/src/editor/main.js"></script>
52
</body>
53
</html>
@@ -0,0 +1,53 @@1
<!DOCTYPE html>2
<html lang="en">3
<head>4
<meta charset="utf-8">5
<title>World editor</title>6
<link rel="stylesheet" href="/ui/editor.css">7
</head>8
<body>9
<header>10
<h1>World editor</h1>11
<p id="who">Connecting…</p>12
<p id="error" class="error"></p>13
</header>14
<aside>15
<p>Chunk <strong id="coords">0, 0</strong></p>16
<p id="revision"></p>17
<p id="authored"></p>18
<label class="open-toggle"><input id="open" type="checkbox"> Open (enterable)</label>19
<div class="nav">20
<button id="north" type="button">Up</button>21
<div>22
<button id="west" type="button">Left</button>23
<button id="east" type="button">Right</button>24
</div>25
<button id="south" type="button">Down</button>26
</div>27
<p>Tool: <strong id="tool"></strong></p>28
<div class="palette">29
<button type="button" data-tool='{"kind":"ground","typeId":1}'>Grass</button>30
<button type="button" data-tool='{"kind":"ground","typeId":2}'>Wood</button>31
<button type="button" data-tool='{"kind":"block","typeId":1}'>Stone wall</button>32
<button type="button" data-tool='{"kind":"block","typeId":2}'>Torch</button>33
<button type="button" data-tool='{"kind":"erase"}'>Erase block</button>34
</div>35
<button id="save" type="button">Save chunk</button>36
<button id="apply" type="button">Apply now</button>37
<label>Night preview <input id="night" type="range" min="0" max="24" step="0.1" value="12"><span id="night-label">12.0</span></label>38
<fieldset>39
<legend>World settings</legend>40
<label>Walk tiles/s <input id="walk-speed" type="number" min="0.5" step="0.5" value="2"></label>41
<label>Sim tick Hz <input id="tick-hz" type="number" min="5" step="1" value="20"></label>42
<button id="save-settings" type="button">Save settings</button>43
</fieldset>44
<p class="hint">Saves write a pending revision. Apply now reloads the live world. Marketplace placement waits on assets.</p>45
</aside>46
<section id="changelog-wrap">47
<h2>Changelog</h2>48
<div id="changelog"></div>49
</section>50
<script src="/framework/framework.js"></script>51
<script type="module" src="/src/editor/main.js"></script>52
</body>53
</html>@@ -0,0 +1,210 @@
1
import { MessageType, CHUNK_SIZE } from "/shared/protocol/constants.js";
2
import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, BlockType } from "/shared/definitions/tiles.js";
3
import { GameSocket, defaultSocketUrl } from "../net/socket.js";
4
import { PLAY_IMAGES, assertSpriteSizes, useNearestNeighbour } from "../render/sprites.js";
5
import { WorldView } from "../render/worldView.js";
6
7
const game = new Game();
8
let socket;
9
let scene;
10
let view;
11
let draft = null;
12
let chunkX = 0;
13
let chunkY = 0;
14
let tool = { kind: "ground", typeId: GroundType.grass };
15
16
function $(id)
17
{
18
return document.getElementById(id);
19
}
20
21
function selectedToolLabel ()
22
{
23
if (tool.kind === "erase")
24
{ return "Erase block"; }
25
if (tool.kind === "ground")
26
{ return GROUND_DEFINITIONS[tool.typeId].displayName; }
27
return BLOCK_DEFINITIONS[tool.typeId].displayName;
28
}
29
30
function paintAt (localX, localY)
31
{
32
if (!draft || localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
33
{ return; }
34
const index = localY * CHUNK_SIZE + localX;
35
if (tool.kind === "ground")
36
{ draft.grounds[index] = tool.typeId; }
37
else if (tool.kind === "erase")
38
{ draft.blocks[index] = null; }
39
else
40
{ draft.blocks[index] = { typeId: tool.typeId, config: null }; }
41
view.applySnapshot({
42
chunkX,
43
chunkY,
44
grounds: draft.grounds,
45
blocks: draft.blocks,
46
light: draft.light,
47
entities: []
48
});
49
}
50
51
function showChunk (payload)
52
{
53
draft = {
54
open: payload.data.open,
55
grounds: payload.data.grounds.slice(),
56
blocks: payload.data.blocks.map((block) => (block ? { ...block } : null)),
57
light: payload.data.light.slice()
58
};
59
chunkX = payload.chunkX;
60
chunkY = payload.chunkY;
61
$("coords").textContent = `${chunkX}, ${chunkY}`;
62
$("open").checked = draft.open;
63
$("revision").textContent = `active ${payload.activeRevisionId}` +
64
(payload.pendingRevisionId ? ` · pending ${payload.pendingRevisionId}` : " · no pending revision");
65
$("authored").textContent = payload.authored ? "authored" : "default grass (closed until saved open)";
66
const log = $("changelog");
67
log.innerHTML = "";
68
for (const entry of payload.changelog ?? [])
69
{
70
const item = document.createElement("article");
71
item.innerHTML = `<header>${entry.editor_admin_id} · ${new Date(entry.edited_at).toLocaleString()}</header>`;
72
const list = document.createElement("ul");
73
for (const bullet of entry.diffEntries)
74
{
75
const li = document.createElement("li");
76
li.textContent = bullet;
77
list.append(li);
78
}
79
item.append(list);
80
log.append(item);
81
}
82
view.applySnapshot({
83
chunkX,
84
chunkY,
85
grounds: draft.grounds,
86
blocks: draft.blocks,
87
light: draft.light,
88
entities: []
89
});
90
frameChunk();
91
}
92
93
function frameChunk ()
94
{
95
const size = view.tileSize();
96
view.followPlayer = false;
97
view.scene.cameraPosition.x = 24 - (chunkX * CHUNK_SIZE * size);
98
view.scene.cameraPosition.y = 24 - (chunkY * CHUNK_SIZE * size);
99
}
100
101
function loadChunk (x, y)
102
{
103
socket.send({ type: MessageType.editorLoadChunk, chunkX: x, chunkY: y });
104
}
105
106
async function main ()
107
{
108
await game.preloadImages(PLAY_IMAGES);
109
assertSpriteSizes(game.images);
110
const canvasSize = CHUNK_SIZE * 16 * 2 + 48;
111
scene = new Scene(260, 56, canvasSize, canvasSize);
112
game.loadScene(scene);
113
useNearestNeighbour(scene, game.images);
114
view = new WorldView(game, scene);
115
view.followPlayer = false;
116
view.setZoom(2);
117
const ticker = new GameObject(0, 0, null, 10);
118
ticker.update = (dt) =>
119
{
120
if (GameInput.mouseHeld(GameInput.MouseButtons.Left) && draft)
121
{
122
const canvas = scene.canvas;
123
const rect = canvas.getBoundingClientRect();
124
const mouseX = GameInput.mousePosition.x - rect.left;
125
const mouseY = GameInput.mousePosition.y - rect.top;
126
if (mouseX >= 0 && mouseY >= 0 && mouseX < rect.width && mouseY < rect.height)
127
{
128
const tile = view.screenToTile(mouseX, mouseY);
129
paintAt(tile.tileX - chunkX * CHUNK_SIZE, tile.tileY - chunkY * CHUNK_SIZE);
130
}
131
}
132
view.update(dt);
133
frameChunk();
134
};
135
scene.addObject(ticker);
136
game.startGame();
137
138
socket = new GameSocket(defaultSocketUrl(), (message) =>
139
{
140
if (message.type === MessageType.session)
141
{
142
$("who").textContent = `${message.authSubject}${message.isAdmin ? " · admin" : ""}`;
143
loadChunk(0, 0);
144
}
145
else if (message.type === MessageType.editorChunk)
146
{
147
if (message.settingsSaved)
148
{
149
$("revision").textContent = `settings saved on pending ${message.settingsSaved.revisionId}`;
150
return;
151
}
152
showChunk(message);
153
}
154
else if (message.type === MessageType.error)
155
{ $("error").textContent = message.message; }
156
});
157
await socket.connect();
158
}
159
160
$("tool").textContent = selectedToolLabel();
161
document.querySelectorAll("[data-tool]").forEach((button) =>
162
{
163
button.addEventListener("click", () =>
164
{
165
tool = JSON.parse(button.dataset.tool);
166
$("tool").textContent = selectedToolLabel();
167
});
168
});
169
170
$("north").addEventListener("click", () => loadChunk(chunkX, chunkY - 1));
171
$("south").addEventListener("click", () => loadChunk(chunkX, chunkY + 1));
172
$("west").addEventListener("click", () => loadChunk(chunkX - 1, chunkY));
173
$("east").addEventListener("click", () => loadChunk(chunkX + 1, chunkY));
174
$("open").addEventListener("change", () =>
175
{
176
if (draft)
177
{ draft.open = $("open").checked; }
178
});
179
$("save").addEventListener("click", () =>
180
{
181
$("error").textContent = "";
182
socket.send({
183
type: MessageType.editorSaveChunk,
184
chunkX,
185
chunkY,
186
draft: { open: $("open").checked, grounds: draft.grounds, blocks: draft.blocks }
187
});
188
});
189
$("apply").addEventListener("click", () => socket.send({ type: MessageType.editorApplyRevision }));
190
$("night").addEventListener("input", () =>
191
{
192
view.nightPreview = Number($("night").value);
193
$("night-label").textContent = view.nightPreview.toFixed(1);
194
});
195
$("save-settings").addEventListener("click", () =>
196
{
197
socket.send({
198
type: MessageType.editorSaveSettings,
199
settings: {
200
walkTilesPerSecond: Number($("walk-speed").value),
201
simulationTickHz: Number($("tick-hz").value)
202
}
203
});
204
});
205
206
main().catch((error) =>
207
{
208
$("error").textContent = error.message;
209
console.error(error);
210
});
@@ -0,0 +1,210 @@1
import { MessageType, CHUNK_SIZE } from "/shared/protocol/constants.js";2
import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, BlockType } from "/shared/definitions/tiles.js";3
import { GameSocket, defaultSocketUrl } from "../net/socket.js";4
import { PLAY_IMAGES, assertSpriteSizes, useNearestNeighbour } from "../render/sprites.js";5
import { WorldView } from "../render/worldView.js";6
7
const game = new Game();8
let socket;9
let scene;10
let view;11
let draft = null;12
let chunkX = 0;13
let chunkY = 0;14
let tool = { kind: "ground", typeId: GroundType.grass };15
16
function $(id)17
{18
return document.getElementById(id);19
}20
21
function selectedToolLabel ()22
{23
if (tool.kind === "erase")24
{ return "Erase block"; }25
if (tool.kind === "ground")26
{ return GROUND_DEFINITIONS[tool.typeId].displayName; }27
return BLOCK_DEFINITIONS[tool.typeId].displayName;28
}29
30
function paintAt (localX, localY)31
{32
if (!draft || localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)33
{ return; }34
const index = localY * CHUNK_SIZE + localX;35
if (tool.kind === "ground")36
{ draft.grounds[index] = tool.typeId; }37
else if (tool.kind === "erase")38
{ draft.blocks[index] = null; }39
else40
{ draft.blocks[index] = { typeId: tool.typeId, config: null }; }41
view.applySnapshot({42
chunkX,43
chunkY,44
grounds: draft.grounds,45
blocks: draft.blocks,46
light: draft.light,47
entities: []48
});49
}50
51
function showChunk (payload)52
{53
draft = {54
open: payload.data.open,55
grounds: payload.data.grounds.slice(),56
blocks: payload.data.blocks.map((block) => (block ? { ...block } : null)),57
light: payload.data.light.slice()58
};59
chunkX = payload.chunkX;60
chunkY = payload.chunkY;61
$("coords").textContent = `${chunkX}, ${chunkY}`;62
$("open").checked = draft.open;63
$("revision").textContent = `active ${payload.activeRevisionId}` +64
(payload.pendingRevisionId ? ` · pending ${payload.pendingRevisionId}` : " · no pending revision");65
$("authored").textContent = payload.authored ? "authored" : "default grass (closed until saved open)";66
const log = $("changelog");67
log.innerHTML = "";68
for (const entry of payload.changelog ?? [])69
{70
const item = document.createElement("article");71
item.innerHTML = `<header>${entry.editor_admin_id} · ${new Date(entry.edited_at).toLocaleString()}</header>`;72
const list = document.createElement("ul");73
for (const bullet of entry.diffEntries)74
{75
const li = document.createElement("li");76
li.textContent = bullet;77
list.append(li);78
}79
item.append(list);80
log.append(item);81
}82
view.applySnapshot({83
chunkX,84
chunkY,85
grounds: draft.grounds,86
blocks: draft.blocks,87
light: draft.light,88
entities: []89
});90
frameChunk();91
}92
93
function frameChunk ()94
{95
const size = view.tileSize();96
view.followPlayer = false;97
view.scene.cameraPosition.x = 24 - (chunkX * CHUNK_SIZE * size);98
view.scene.cameraPosition.y = 24 - (chunkY * CHUNK_SIZE * size);99
}100
101
function loadChunk (x, y)102
{103
socket.send({ type: MessageType.editorLoadChunk, chunkX: x, chunkY: y });104
}105
106
async function main ()107
{108
await game.preloadImages(PLAY_IMAGES);109
assertSpriteSizes(game.images);110
const canvasSize = CHUNK_SIZE * 16 * 2 + 48;111
scene = new Scene(260, 56, canvasSize, canvasSize);112
game.loadScene(scene);113
useNearestNeighbour(scene, game.images);114
view = new WorldView(game, scene);115
view.followPlayer = false;116
view.setZoom(2);117
const ticker = new GameObject(0, 0, null, 10);118
ticker.update = (dt) =>119
{120
if (GameInput.mouseHeld(GameInput.MouseButtons.Left) && draft)121
{122
const canvas = scene.canvas;123
const rect = canvas.getBoundingClientRect();124
const mouseX = GameInput.mousePosition.x - rect.left;125
const mouseY = GameInput.mousePosition.y - rect.top;126
if (mouseX >= 0 && mouseY >= 0 && mouseX < rect.width && mouseY < rect.height)127
{128
const tile = view.screenToTile(mouseX, mouseY);129
paintAt(tile.tileX - chunkX * CHUNK_SIZE, tile.tileY - chunkY * CHUNK_SIZE);130
}131
}132
view.update(dt);133
frameChunk();134
};135
scene.addObject(ticker);136
game.startGame();137
138
socket = new GameSocket(defaultSocketUrl(), (message) =>139
{140
if (message.type === MessageType.session)141
{142
$("who").textContent = `${message.authSubject}${message.isAdmin ? " · admin" : ""}`;143
loadChunk(0, 0);144
}145
else if (message.type === MessageType.editorChunk)146
{147
if (message.settingsSaved)148
{149
$("revision").textContent = `settings saved on pending ${message.settingsSaved.revisionId}`;150
return;151
}152
showChunk(message);153
}154
else if (message.type === MessageType.error)155
{ $("error").textContent = message.message; }156
});157
await socket.connect();158
}159
160
$("tool").textContent = selectedToolLabel();161
document.querySelectorAll("[data-tool]").forEach((button) =>162
{163
button.addEventListener("click", () =>164
{165
tool = JSON.parse(button.dataset.tool);166
$("tool").textContent = selectedToolLabel();167
});168
});169
170
$("north").addEventListener("click", () => loadChunk(chunkX, chunkY - 1));171
$("south").addEventListener("click", () => loadChunk(chunkX, chunkY + 1));172
$("west").addEventListener("click", () => loadChunk(chunkX - 1, chunkY));173
$("east").addEventListener("click", () => loadChunk(chunkX + 1, chunkY));174
$("open").addEventListener("change", () =>175
{176
if (draft)177
{ draft.open = $("open").checked; }178
});179
$("save").addEventListener("click", () =>180
{181
$("error").textContent = "";182
socket.send({183
type: MessageType.editorSaveChunk,184
chunkX,185
chunkY,186
draft: { open: $("open").checked, grounds: draft.grounds, blocks: draft.blocks }187
});188
});189
$("apply").addEventListener("click", () => socket.send({ type: MessageType.editorApplyRevision }));190
$("night").addEventListener("input", () =>191
{192
view.nightPreview = Number($("night").value);193
$("night-label").textContent = view.nightPreview.toFixed(1);194
});195
$("save-settings").addEventListener("click", () =>196
{197
socket.send({198
type: MessageType.editorSaveSettings,199
settings: {200
walkTilesPerSecond: Number($("walk-speed").value),201
simulationTickHz: Number($("tick-hz").value)202
}203
});204
});205
206
main().catch((error) =>207
{208
$("error").textContent = error.message;209
console.error(error);210
});@@ -0,0 +1,65 @@
1
:root {
2
color-scheme: dark;
3
font-family: "Segoe UI", sans-serif;
4
background: #12151b;
5
color: #e8e4d9;
6
}
7
8
html, body {
9
margin: 0;
10
min-height: 100%;
11
}
12
13
header {
14
padding: 0.6rem 1rem;
15
border-bottom: 1px solid #2c3340;
16
}
17
18
aside {
19
position: absolute;
20
left: 0;
21
top: 56px;
22
width: 250px;
23
bottom: 0;
24
overflow: auto;
25
padding: 0.75rem;
26
box-sizing: border-box;
27
background: #1a1e26;
28
border-right: 1px solid #2c3340;
29
}
30
31
#changelog-wrap {
32
position: absolute;
33
right: 0;
34
top: 56px;
35
width: 280px;
36
bottom: 0;
37
overflow: auto;
38
padding: 0.75rem;
39
box-sizing: border-box;
40
background: #1a1e26;
41
border-left: 1px solid #2c3340;
42
}
43
44
.nav, .palette {
45
display: flex;
46
flex-direction: column;
47
gap: 0.35rem;
48
margin: 0.75rem 0;
49
}
50
51
button, input {
52
font: inherit;
53
background: #101216;
54
color: inherit;
55
border: 1px solid #3a4250;
56
padding: 0.3rem 0.5rem;
57
}
58
59
button { cursor: pointer; }
60
.error { color: #f0a0a0; }
61
.hint { color: #9aa3b2; font-size: 0.85rem; }
62
.open-toggle { display: flex; gap: 0.4rem; align-items: center; }
63
article { margin-bottom: 0.75rem; }
64
fieldset { border-color: #3a4250; margin-top: 1rem; }
65
label { display: block; margin: 0.4rem 0; }
@@ -0,0 +1,65 @@1
:root {2
color-scheme: dark;3
font-family: "Segoe UI", sans-serif;4
background: #12151b;5
color: #e8e4d9;6
}7
8
html, body {9
margin: 0;10
min-height: 100%;11
}12
13
header {14
padding: 0.6rem 1rem;15
border-bottom: 1px solid #2c3340;16
}17
18
aside {19
position: absolute;20
left: 0;21
top: 56px;22
width: 250px;23
bottom: 0;24
overflow: auto;25
padding: 0.75rem;26
box-sizing: border-box;27
background: #1a1e26;28
border-right: 1px solid #2c3340;29
}30
31
#changelog-wrap {32
position: absolute;33
right: 0;34
top: 56px;35
width: 280px;36
bottom: 0;37
overflow: auto;38
padding: 0.75rem;39
box-sizing: border-box;40
background: #1a1e26;41
border-left: 1px solid #2c3340;42
}43
44
.nav, .palette {45
display: flex;46
flex-direction: column;47
gap: 0.35rem;48
margin: 0.75rem 0;49
}50
51
button, input {52
font: inherit;53
background: #101216;54
color: inherit;55
border: 1px solid #3a4250;56
padding: 0.3rem 0.5rem;57
}58
59
button { cursor: pointer; }60
.error { color: #f0a0a0; }61
.hint { color: #9aa3b2; font-size: 0.85rem; }62
.open-toggle { display: flex; gap: 0.4rem; align-items: center; }63
article { margin-bottom: 0.75rem; }64
fieldset { border-color: #3a4250; margin-top: 1rem; }65
label { display: block; margin: 0.4rem 0; }@@ -69,9 +69,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
69
69
70
70
## World editor (client)
71
71
72
**Status:** not implemented
73
**Spec:** Separate admin UI. One chunk at a time, chunk-step navigation, paint palette, open/closed, changelog, night preview slider, settings modal.
74
**Code:** —
72
**Status:** full
73
**Spec:** Separate `/editor` page. One chunk at a time, chunk-step navigation, paint palette (grass, wood, wall, torch, erase), open/closed, changelog, night preview slider, settings fields, save and Apply now. Marketplace placement waits on assets.
74
**Code:** `client/editor.html`, `client/src/editor/main.js`, `client/ui/editor.css`
75
75
76
76
## Per-chunk changelog
77
77
@@ -69,9 +69,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi69
69
70
## World editor (client)70
## World editor (client)71
71
72
**Status:** not implemented 72
**Status:** full 73
**Spec:** Separate admin UI. One chunk at a time, chunk-step navigation, paint palette, open/closed, changelog, night preview slider, settings modal. 73
**Spec:** Separate `/editor` page. One chunk at a time, chunk-step navigation, paint palette (grass, wood, wall, torch, erase), open/closed, changelog, night preview slider, settings fields, save and Apply now. Marketplace placement waits on assets. 74
**Code:** —74
**Code:** `client/editor.html`, `client/src/editor/main.js`, `client/ui/editor.css`75
75
76
## Per-chunk changelog76
## Per-chunk changelog77
77