fantasy — Starfall Keep

fantasy — Starfall Keep

English | 日本語

Starfall Keep

Overview

A small turn-based battle on a 9×9 map with three elevation levels. Command a knight, mage, and ranger against three enemies. This sample adds moving Nodes, picking, DOM controls, GPU particles, victory, defeat, and restart to a PBR scene.

Primitive geometry builds the characters, and parent-child Nodes connect their limbs and equipment.

The keep is submerged. Core caustics illuminate terrain, scenery, and moving units; blue-green light, distance fog, and drifting particles give the battlefield an underwater atmosphere. Toggle 水中のコースティクス (Underwater Caustics) to compare the moving illumination. Underwater colors and fog remain when caustics are off.

Run and controls

Open fantasy.html in a WebGPU browser. Serve the repository over HTTP using localhost or HTTPS. Startup prepares PBR and GPU particles.

  1. Select an ally in the 3D scene or party panel.
  2. Click a cyan tile to move.
  3. Click an enemy within range to attack.
  4. Command the remaining allies and press 味方ターンを終了 (End Ally Turn).

Each ally can move once and attack once per turn, but movement must come first. An attack made without moving ends that unit's actions. After moving, attack or choose 待機 (Wait) to finish that unit's turn. Enemies move into attack range, then target the available ally with the lowest HP.

Movement uses four neighbors and permits one elevation step. Climbing costs 2 movement points; level and downhill movement cost 1. Living units and obstacles block movement. Attacking from higher ground adds 3 damage. Range uses Manhattan distance; ranged attacks pass over obstacles and always hit.

Defeat all enemies to win; losing every ally ends the battle. 最初から (Start Over) and もう一度戦う (Fight Again) restart it. Expand マスをボタンで操作 (Use Grid Buttons) to control the map with Tab and Enter; these buttons trigger the same actions as 3D picking.

Drag to rotate and use the wheel to zoom. The initial view uses perspective projection. Canvas size is 960×720 on large screens; narrow screens reduce its width and place the controls below it.

webg features

The app uses physics:false. Movement and attacks use grid rules; Node transforms animate motion and character parts.

Verification points

Read the code in this order

  1. `main.js`: `start()` — initialize through one entry point, add moving Nodes and input, then start frames. WebgApp runs the render loop and particle steps.
  2. `scene.js`: `createManifest()` — read initial placement, materials, PBR, and emitters. Grid coordinates and display height come from rules.mjs.
  3. `visuals.js`: `part()` and `character()` — examine RGBA/PBR material parameters, Node hierarchy, and ownership of manually created Shapes.
  4. `main.js`: `choose()` → `move()` → `updateMotion()` — select a tile, await animation, and commit coordinates. rules.mjs: paths() includes uphill movement costs.
  5. `main.js`: `makePointerRay()`, `pickTile()`, `updateLabels()` — use the same camera for picking and DOM projection.
  6. `FantasyApp.js` and cleanup — synchronize CSS, Canvas, and PBR sizes, then release the observer, manual Shapes, and standard app separately.

The reset() function in main.js restarts this game. It is distinct from WebgSceneApp.reset(); HP and action flags belong to the game.

Underwater lighting

connectWater() in main.js creates an 18m-wide, 18m-deep WaterBody, with its mean surface at 6m. Terrain and unit root Nodes are receivers; child Shapes inherit registration, including animated limbs and equipment. Movement markers and the selection ring are excluded. update() advances only the shared wave time through setTime().

await sceneApp.renderer.setWater(water, {
  causticsEnabled: true,
  surfaceEnabled: false,
  quality: "high"
});

When the caustics control changes, its handler pauses rendering while it awaits setWater(), then resumes the app. Turning caustics off releases the dedicated GPU resources. Turning them on again preserves the battle state and registered Nodes; restarting the battle reuses the same water body.

Caustics, teal lighting, and distance fog create the underwater atmosphere:

Change one thing at a time

After each change, use the verification points to compare its effect with the starting behavior.

Camera reference and rule checks

camera_reference.json records the initial camera reference. The book's From a PBR Scene to a Small Game explains how the individual feature examples connect in this app.

Run from the repository root to check paths, occupancy, range, enemy planning, and outcomes without a GPU. Rendering and interaction require separate browser verification.

node --test samples/fantasy/rules.test.mjs