How fujik/rpg-cljs-develop works
Project type and overall purpose
• This is a browser-based, single-page 2D RPG “engine” written in ClojureScript.
• There’s no backend server: all logic runs in the browser, with data loaded from static EDN files and optional persistence via localStorage.
• Shadow-CLJS drives builds, live‐reloading, and test runners (Node and browser).
Main architectural components
A. Runtime & UI Layer
• index.html + Canvas 2D: hosts the game viewport.
• js/main.js: glue code that loads the compiled ClojureScript bundle into the page.
B. Application Code (ClojureScript modules)
1) src/rpg/main.cljs
• Bootstraps the app: initializes state, triggers scene loading, starts loop.
2) Data Layer (src/rpg/data/) & Static EDN (data/…)
• loader.cljs fetches data/maps/*.edn, items.edn, enemies.edn → validates them with Malli (spec.cljs) → injects into runtime state.
3) Engine Layer (src/rpg/engine/…)
• state.cljs: single atom holding the entire game state.
• input.cljs: key/mouse event listeners → dispatch events.
• map.cljs: movement rules, collision detection.
• events.cljs: RPG DSL handlers (:say, :chest, :warp, :give-item).
• dialog.cljs: simple UI overlays.
• render.cljs: draws map, player, UI on Canvas.
• loop.cljs: game loop (tick → update → render).
• storage.cljs: localStorage persistence API (save/load partial state).
4) Scenes (src/rpg/scenes/…)
• e.g. overworld.cljs: high‐level orchestrator—calls the data loader, sets up initial map, hooks loop and input.
C. Persistence & Testing
• localStorage: browser API for saves.
• Tests (test/rpg/…)
• Node‐based unit tests for pure logic (map rules, event DSL).
• Browser integration tests via shadow’s :browser-test build.
D. Build & Dev Tooling
• shadow-cljs.edn: defines three builds (:app, :node-test, :browser-test).
• package.json/npm scripts: for watch, build, testing.
• dev/: helper scripts for REPL‐driven development.
Relationships & data flows
• At startup: index.html → loads compiled bundle → main.cljs calls load-scene.
• Data loader reads EDN → Malli validates → populates the state atom.
• The loop tick reads state → input events write to state → events DSL mutates state → render reads state and draws Canvas.
• On player action (e.g. open chest) → storage module writes partial state to localStorage.
• Tests invoke pure modules directly in Node or via a headless browser.
Architectural patterns & design principles
• Data-Driven: game content (maps, items, enemies) is purely EDN + schema validation.
• Modular Engine: each subsystem (input, map, events, render) is isolated, interacts via the central state atom.
• Single Source of Truth: the state atom holds all runtime data.
• Separation of Concerns: Scenes orchestrate, engine modules implement, data layer loads.