gitworld / dist /src /main.ts
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/gitworld
6281708 verified
Raw
History Blame Contribute Delete
3.31 kB
import { EVENT_TYPES, TOOLBELT, commandTranscript } from './shared.js';
import {
advanceWorldState,
createWorldState,
getNearbyDistrict,
renderMinimap,
renderWorld,
worldReducer
} from './world-architect.js';
import { bindUi, renderHud } from './ui-reality.js';
const app = document;
const worldViewport = app.getElementById('world-viewport');
const minimap = app.getElementById('minimap-viewport');
let state = createWorldState();
function dispatch(event) {
state = worldReducer(state, event);
window.gitWorldState = state;
render();
}
function render() {
const nearby = getNearbyDistrict(state);
if (worldViewport) renderWorld(worldViewport, state, (districtId) => dispatch({ type: EVENT_TYPES.SELECT_DISTRICT, districtId }));
if (minimap) renderMinimap(minimap, state);
renderHud(app, state, nearby);
}
function move(dx, dy) {
dispatch({ type: EVENT_TYPES.PLAYER_MOVE, dx, dy });
}
function useToolByKey(key) {
const tool = TOOLBELT.find((candidate) => candidate.key === key);
if (!tool) return;
dispatch({ type: EVENT_TYPES.USE_TOOL, toolId: tool.id, message: `${tool.name}: ${tool.verb}.` });
}
function inspectNearest() {
const nearby = getNearbyDistrict(state);
if (nearby.inRange) {
dispatch({ type: EVENT_TYPES.ENTER_DISTRICT, districtId: nearby.district.id });
} else {
dispatch({ type: EVENT_TYPES.SELECT_DISTRICT, districtId: nearby.district.id });
}
}
function onKeyDown(event) {
const target = event.target;
const typing = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement;
if (typing && event.key !== 'Escape') return;
const key = event.key.toLowerCase();
const movement = {
w: [0, -1], arrowup: [0, -1],
s: [0, 1], arrowdown: [0, 1],
a: [-1, 0], arrowleft: [-1, 0],
d: [1, 0], arrowright: [1, 0]
}[key];
if (movement) {
event.preventDefault();
move(movement[0], movement[1]);
return;
}
if (key === 'e' || event.key === 'Enter') {
event.preventDefault();
inspectNearest();
return;
}
if (key === 't') {
event.preventDefault();
dispatch({ type: EVENT_TYPES.OPEN_TERMINAL });
return;
}
if (key === 'i') {
event.preventDefault();
dispatch({ type: EVENT_TYPES.OPEN_INSPECTOR });
return;
}
if (/^[1-8]$/.test(event.key)) {
event.preventDefault();
useToolByKey(event.key);
return;
}
if (event.key === 'Escape') {
dispatch(state.terminal.open ? { type: EVENT_TYPES.CLOSE_TERMINAL } : { type: EVENT_TYPES.CLOSE_INSPECTOR });
}
}
bindUi(app, dispatch);
app.addEventListener('keydown', onKeyDown);
window.addEventListener('resize', render);
window.gitWorldState = state;
render();
// Fixed-step browser-local simulation: the world advances on a deterministic clock,
// while the render layer simply reflects the latest serializable state.
window.setInterval(() => {
const advanced = advanceWorldState(state);
state = advanced.state;
window.gitWorldState = state;
render();
}, 1600);
// Keep the transcript helper reachable for the UI's delegated terminal buttons in
// environments where the app is embedded without a module bundler.
window.gitWorldCommandTranscript = commandTranscript;