@netforge/template-survivor 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +62 -0
- package/CLAUDE.md +11 -0
- package/LICENSE +21 -0
- package/NOTICE +30 -0
- package/README.md +73 -2
- package/client/debris.ts +257 -0
- package/client/decor.ts +21 -0
- package/client/host.ts +96 -0
- package/client/hud.ts +575 -0
- package/client/icons.ts +42 -0
- package/client/index.html +277 -0
- package/client/input.ts +172 -0
- package/client/library.ts +250 -0
- package/client/main.ts +606 -0
- package/client/names.ts +57 -0
- package/client/scene.ts +1065 -0
- package/client/stake.ts +101 -0
- package/client/styled.ts +53 -0
- package/client/threats.ts +157 -0
- package/llms-full.txt +738 -0
- package/llms.txt +369 -0
- package/package.json +83 -3
- package/src/game.ts +354 -0
- package/src/manifest.ts +38 -0
- package/src/store.ts +107 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Extending EMBERWATCH
|
|
2
|
+
|
|
3
|
+
EMBERWATCH is the survivor-like co-op template of the Net Forge game engine. This file tells a coding agent how to change the game safely. `llms.txt` in this package has the full recipe book for this genre, and `llms-full.txt` adds the whole `src/game.ts` as a worked example.
|
|
4
|
+
|
|
5
|
+
## The one file to edit
|
|
6
|
+
|
|
7
|
+
Change the game by editing `src/game.ts` only. It holds declarations, not engine code. client/names.ts holds the display labels for weapon, upgrade and earn ids. In a downloaded game folder, labels players read live in src/names.json. Everything else (the client, the manifest, the store hooks, the simulation, the netcode) is template machinery.
|
|
8
|
+
|
|
9
|
+
## Import only @netforge/engine
|
|
10
|
+
|
|
11
|
+
`src/game.ts` imports from `@netforge/engine` and nothing else. The template's game imports:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { achievement, director, earn, game, keeper, mission, prefab, store, system, title, upgrade, weapon } from "@netforge/engine";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Also available for this genre: `economy`, `metaUpgrade`. `earn` has `earn.rule(...)` and `earn.stake(...)`; `store` has `store.sku(...)`. `system(id, ctx => ...)` gets the systems API described in `llms.txt`. The renderer, input, netcode and physics live elsewhere and are not reachable from game code.
|
|
18
|
+
|
|
19
|
+
## Rules the build enforces
|
|
20
|
+
|
|
21
|
+
- Ids are lower_snake_case.
|
|
22
|
+
- Every game declares at least one `earn.rule(...)` (mandatory earn) and one `earn.stake(...)` (the buy-in run), on one top-level line. A game without either does not build.
|
|
23
|
+
- Asset ids are library ids such as `style.lowpoly.turret`, never URLs or file paths, and must exist in the asset library.
|
|
24
|
+
- A survivor game needs one enemy prefab, one weapon and `players.weapon`.
|
|
25
|
+
- Drops must be pickup prefabs. At most 15 weapons (evolutions count) and 8 upgrades.
|
|
26
|
+
- `game({ style })` is the art style the creator picked (lowpoly, toon, ps1, pixel, voxel or handdrawn). Keep it.
|
|
27
|
+
- Keep the store SKUs the client is wired to, at their prices: revive and skin_aurora (and the ember_charm gear). "stake" is reserved and is never a SKU id.
|
|
28
|
+
|
|
29
|
+
## Validate
|
|
30
|
+
|
|
31
|
+
- With the MCP server (`npx -y @netforge/mcp`, bin `netforge-mcp`): call `netforge_validate_game` on the game. It returns one line per problem.
|
|
32
|
+
- In this package: `npx vitest run` runs `test/harness.test.ts`, `test/host.test.ts`, `test/store.test.ts`. A bad declaration throws `GameDefinitionError` with a line you can act on.
|
|
33
|
+
|
|
34
|
+
## Playtest
|
|
35
|
+
|
|
36
|
+
- With the MCP server: call `netforge_run_playtest`.
|
|
37
|
+
- In code: `import { engine } from "@netforge/engine"` and run `engine.test.run({ ticks: 30 * 180, bots: 4, assert: [] })`. It reports kills, wave, maxLevel, earned by rule, deaths, runs won and lost, softlock and ms per tick. With `bot: "human"` a human-like bot plays and `report.fun` holds the fun metrics the gate scores (first threat, first level, downs before 1:00, spawns on screen and more).
|
|
38
|
+
|
|
39
|
+
## Play locally
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
mkdir -p my-game/src && cd my-game
|
|
43
|
+
npm init -y && npm install @netforge/kit-play @netforge/engine @netforge/template-survivor
|
|
44
|
+
cp node_modules/@netforge/template-survivor/src/game.ts src/
|
|
45
|
+
echo '{"template":"survivor"}' > netnet.json
|
|
46
|
+
npx kit-play # opens http://127.0.0.1:4663
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The MCP tool `netforge_play_locally` does the same. Coins are play money; nothing pays out and no chain or wallet is involved. Edit `src/game.ts` and run it again to try a change.
|
|
50
|
+
|
|
51
|
+
## What not to do
|
|
52
|
+
|
|
53
|
+
- Do not write renderers, netcode, physics or bot AI. The template already has them.
|
|
54
|
+
- Do not use asset URLs or file paths. Use library ids.
|
|
55
|
+
- Do not import anything other than `@netforge/engine` in `src/game.ts`.
|
|
56
|
+
- Do not edit `client/`, `src/manifest.ts` or `src/store.ts`.
|
|
57
|
+
- Do not rename or reprice the store SKUs the client is wired to.
|
|
58
|
+
- Do not spawn enemies on screen; the director brings them in just off screen.
|
|
59
|
+
|
|
60
|
+
## Docs
|
|
61
|
+
|
|
62
|
+
https://forge.netnet.capital/docs/templates/survivor
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
Read AGENTS.md before changing this game.
|
|
4
|
+
|
|
5
|
+
The five hard rules:
|
|
6
|
+
|
|
7
|
+
1. Edit only `src/game.ts`.
|
|
8
|
+
2. Import only `@netforge/engine`.
|
|
9
|
+
3. Ids are lower_snake_case.
|
|
10
|
+
4. Declare at least one `earn.rule(...)` and exactly one `earn.stake(...)`.
|
|
11
|
+
5. No renderers, netcode, physics or asset URLs.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NetNet Capital Management
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
@netforge/template-survivor (EMBERWATCH)
|
|
2
|
+
Copyright (c) 2026 NetNet Capital Management
|
|
3
|
+
|
|
4
|
+
An original game. No names, characters, sprites, models, sounds or code
|
|
5
|
+
from any other game or franchise. Nothing from PokéSurvivor (its code and
|
|
6
|
+
sprites are non-commercial) or SpriteCollab was used or consulted for code.
|
|
7
|
+
|
|
8
|
+
Art and sound
|
|
9
|
+
- Models come from the per-style asset library
|
|
10
|
+
(@netforge/asset-library): CC0 1.0 models by Quaternius (keepers,
|
|
11
|
+
enemies) and Kenney (Nature Kit trees, rocks and stumps), each pack listed
|
|
12
|
+
with its source URL and licence verification in that package's NOTICE and
|
|
13
|
+
manifest/packs.json. The files are fetched and built, never committed
|
|
14
|
+
here; the build copies only the models this game uses.
|
|
15
|
+
- Fallback shapes (client/library.ts) are generated from three.js
|
|
16
|
+
primitives; textures are drawn on a canvas at load.
|
|
17
|
+
|
|
18
|
+
Dependencies (unmodified, via npm)
|
|
19
|
+
- three.js 0.186.1 (https://github.com/mrdoob/three.js), MIT.
|
|
20
|
+
- nipplejs 1.0.4 (https://github.com/yoannmoinet/nipplejs), MIT.
|
|
21
|
+
- @netforge/engine and @netforge/engine-sim (this repository);
|
|
22
|
+
see their NOTICE files (bitECS MPL-2.0, Rapier Apache-2.0, Colyseus MIT,
|
|
23
|
+
zod MIT).
|
|
24
|
+
|
|
25
|
+
Patterns (ideas only)
|
|
26
|
+
- The survivor-like genre's general mechanics (auto-firing weapons, XP
|
|
27
|
+
gems, pick-one-of-three level-ups, waves, a boss). Mechanics, not
|
|
28
|
+
expression; no level, item, enemy or text was copied from any game.
|
|
29
|
+
- Hit-stop, screen shake and damage numbers as described in common
|
|
30
|
+
game-feel talks and articles (e.g. "Juice it or lose it", GDC 2012).
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @netforge/template-survivor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A survivor-like co-op game template for Net Forge: 4 players against up to 2,000 enemies at once (EMBERWATCH).
|
|
4
|
+
|
|
5
|
+
Four keepers hold a field at night against a swarm that grows every wave. Weapons fire by themselves, gems level you up, you pick one of three upgrades, you stand by a downed teammate to revive them, and the team survives ten waves and breaks the Bell Warden.
|
|
6
|
+
|
|
7
|
+
The room server is authoritative at 30 Hz. Your own keeper is predicted. The spawn director brings every enemy in just off screen on the run's clock, with curves for the pacing.
|
|
8
|
+
|
|
9
|
+
Earn: waves cleared (2 and 7), the boss killed, and 500 kills, once per run each. Real-token earn and stakes run only on the hosted Net Forge server.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @netforge/template-survivor
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You normally do not import this package yourself. `kit play` runs it on your own computer, and the Net Forge MCP server and studio copy its `src/game.ts` as the starting point for a new game.
|
|
18
|
+
|
|
19
|
+
## Play it locally
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
mkdir -p my-game/src && cd my-game
|
|
23
|
+
npm init -y && npm install @netforge/kit-play @netforge/engine @netforge/template-survivor
|
|
24
|
+
cp node_modules/@netforge/template-survivor/src/game.ts src/
|
|
25
|
+
echo '{"template":"survivor"}' > netnet.json
|
|
26
|
+
npx kit-play # opens http://127.0.0.1:4663
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Coins are play money and nothing pays out. Real-token earning, stakes and claims run only on the hosted Net Forge server.
|
|
30
|
+
|
|
31
|
+
## Change the game
|
|
32
|
+
|
|
33
|
+
The ember nova is the starting weapon. Make it hit harder and reach further:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
weapon("ember_nova", {
|
|
37
|
+
kind: "nova", damage: 16, cooldown: 1.1, range: 4.4, knockback: 2.5,
|
|
38
|
+
// levels and evolve stay as they are
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Save `src/game.ts` and run `npx kit-play` again.
|
|
43
|
+
|
|
44
|
+
## Files
|
|
45
|
+
|
|
46
|
+
| Path | What |
|
|
47
|
+
|---|---|
|
|
48
|
+
| `src/game.ts` | The game, declarations only: the field, the swarm, weapons, upgrades, the director, systems, earn, store. |
|
|
49
|
+
| `src/manifest.ts` | Loads and compiles the game; `survivorAdapter()` and `survivorGame()` for the room host and the client. |
|
|
50
|
+
| `src/store.ts` | Store hooks on the Kit's names: a revive (50 coins), a cosmetic (300 coins) and the ember charm gear (150 coins). |
|
|
51
|
+
| `client/` | The browser client: three.js scene, HUD, input (keys, gamepad, touch), game feel. `client/names.ts` holds display labels. |
|
|
52
|
+
| `test/` | Playtest bots through `engine.test.run`, the local host and the store. |
|
|
53
|
+
| `llms.txt` | How to change the game, for agents. |
|
|
54
|
+
|
|
55
|
+
## Controls
|
|
56
|
+
|
|
57
|
+
Keys: WASD or arrows move, Space dash, 1 2 3 pick a card. Touch: drag on the left half, the DASH button, tap a card. The backquote key shows stats. Open the Invite link in another tab or on a phone on the same network to play together (`?room=<id>`).
|
|
58
|
+
|
|
59
|
+
## For AI agents
|
|
60
|
+
|
|
61
|
+
Read `AGENTS.md` and `llms.txt` in this package before changing the game. `llms-full.txt` adds the whole `src/game.ts` as a worked example.
|
|
62
|
+
|
|
63
|
+
Agents can also drive this template through the Net Forge MCP server: run `npx -y @netforge/mcp` (package `@netforge/mcp`, bin `netforge-mcp`). Its tools are `netforge_list_templates`, `netforge_create_game`, `netforge_explain_api`, `netforge_search_docs`, `netforge_validate_game`, `netforge_run_playtest` and `netforge_play_locally`.
|
|
64
|
+
|
|
65
|
+
To set up a coding agent in a game project, run `npx netforge setup agent`. It writes AGENTS.md, CLAUDE.md, Cursor rules and `.mcp.json` into the project. In Claude Code you can also add the server directly with `claude mcp add netforge -- npx -y @netforge/mcp`.
|
|
66
|
+
|
|
67
|
+
## Docs
|
|
68
|
+
|
|
69
|
+
- https://forge.netnet.capital/docs/templates/survivor
|
|
70
|
+
- `llms.txt` and `llms-full.txt` in this package (also exported as `@netforge/template-survivor/llms.txt` and `@netforge/template-survivor/llms-full.txt`)
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT. See `LICENSE` and `NOTICE`.
|
package/client/debris.ts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side physics dressing on engine-sim's PhysicsWorld (Rapier, the
|
|
3
|
+
* same build the server runs): chunks that fly off killed enemies and
|
|
4
|
+
* bounce off the floor and pillars, and a scatter of rocks and stumps
|
|
5
|
+
* the keepers shove around and novas blast away.
|
|
6
|
+
*
|
|
7
|
+
* Presentation only. None of it feeds back into the authoritative sim, so
|
|
8
|
+
* it never needs to match the server or other players exactly; every
|
|
9
|
+
* client drives the props from the same interpolated keeper positions, so
|
|
10
|
+
* what they see agrees closely anyway.
|
|
11
|
+
*/
|
|
12
|
+
import { Color, InstancedMesh, Matrix4, MeshStandardMaterial, Quaternion, Vector3 } from "three/webgpu";
|
|
13
|
+
import type { BufferGeometry, Material, Scene } from "three/webgpu";
|
|
14
|
+
import { CylinderGeometry, DodecahedronGeometry } from "three/webgpu";
|
|
15
|
+
import { Layer, createPhysics } from "@netforge/engine-sim";
|
|
16
|
+
import type { BodyId, PhysicsWorld, ResolvedSimConfig } from "@netforge/engine-sim";
|
|
17
|
+
|
|
18
|
+
const HZ = 60;
|
|
19
|
+
const MAX_CHUNKS = 220;
|
|
20
|
+
const MAX_STEPS = 4;
|
|
21
|
+
const CHUNK_LIFE = 2.2;
|
|
22
|
+
|
|
23
|
+
interface Chunk {
|
|
24
|
+
id: BodyId;
|
|
25
|
+
life: number;
|
|
26
|
+
max: number;
|
|
27
|
+
size: number;
|
|
28
|
+
color: Color;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Prop {
|
|
32
|
+
id: BodyId;
|
|
33
|
+
barrel: boolean;
|
|
34
|
+
/** Last speed, for impact thuds. */
|
|
35
|
+
speed: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Seeded PRNG so every client scatters the props in the same places. */
|
|
39
|
+
function rng(seed: number): () => number {
|
|
40
|
+
let s = seed >>> 0;
|
|
41
|
+
return () => {
|
|
42
|
+
s = (s + 0x6d2b79f5) >>> 0;
|
|
43
|
+
let t = s;
|
|
44
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
45
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
46
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** An art style's own drawing of a debris shape (the voxel style's cube chunks, rocks and stumps); null keeps the primitive. */
|
|
51
|
+
export type DebrisShape = (name: "chunk" | "rock" | "stump") => { readonly geometry: BufferGeometry; readonly material: Material } | null;
|
|
52
|
+
|
|
53
|
+
function instanced(geometry: BufferGeometry, color: number, capacity: number, emissive = 0x000000, styled?: ReturnType<DebrisShape>): InstancedMesh {
|
|
54
|
+
const material = styled?.material ?? new MeshStandardMaterial({ color, roughness: 0.75, flatShading: true, emissive, emissiveIntensity: 0.6 });
|
|
55
|
+
const mesh = new InstancedMesh(styled?.geometry ?? geometry, material, capacity);
|
|
56
|
+
mesh.count = 0;
|
|
57
|
+
mesh.frustumCulled = false;
|
|
58
|
+
mesh.castShadow = true;
|
|
59
|
+
mesh.receiveShadow = true;
|
|
60
|
+
const white = new Color(1, 1, 1);
|
|
61
|
+
for (let i = 0; i < capacity; i++) mesh.setColorAt(i, white);
|
|
62
|
+
return mesh;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class PhysicsDressing {
|
|
66
|
+
readonly physics: PhysicsWorld;
|
|
67
|
+
private readonly chunks: Chunk[] = [];
|
|
68
|
+
private readonly props: Prop[] = [];
|
|
69
|
+
private readonly keepers = new Map<number, BodyId>();
|
|
70
|
+
private readonly chunkMesh: InstancedMesh;
|
|
71
|
+
private readonly crateMesh: InstancedMesh;
|
|
72
|
+
private readonly barrelMesh: InstancedMesh;
|
|
73
|
+
private acc = 0;
|
|
74
|
+
private readonly m = new Matrix4();
|
|
75
|
+
private readonly q = new Quaternion();
|
|
76
|
+
private readonly p = new Vector3();
|
|
77
|
+
private readonly s = new Vector3();
|
|
78
|
+
private readonly random = rng(0xe3b1);
|
|
79
|
+
/** Impacts this frame (world position and speed), for thud sounds. */
|
|
80
|
+
readonly impacts: { x: number; y: number; z: number; speed: number }[] = [];
|
|
81
|
+
/** Effects fired so far (playtest checks). */
|
|
82
|
+
readonly counts = { chunks: 0, blasts: 0 };
|
|
83
|
+
|
|
84
|
+
constructor(scene: Scene, config: ResolvedSimConfig, opts: { props?: boolean; shape?: DebrisShape } = {}) {
|
|
85
|
+
this.physics = createPhysics({ gravity: [0, -24, 0], tickRate: HZ });
|
|
86
|
+
const P = this.physics;
|
|
87
|
+
const h = config.arena.halfExtent;
|
|
88
|
+
const wall = config.arena.wallHeight;
|
|
89
|
+
P.addStaticBox([h + 1, 0.5, h + 1], [0, -0.5, 0], { friction: 0.8, layer: Layer.Level });
|
|
90
|
+
P.addStaticBox([h + 1, wall, 0.5], [0, wall, h + 0.5], { layer: Layer.Level });
|
|
91
|
+
P.addStaticBox([h + 1, wall, 0.5], [0, wall, -h - 0.5], { layer: Layer.Level });
|
|
92
|
+
P.addStaticBox([0.5, wall, h + 1], [h + 0.5, wall, 0], { layer: Layer.Level });
|
|
93
|
+
P.addStaticBox([0.5, wall, h + 1], [-h - 0.5, wall, 0], { layer: Layer.Level });
|
|
94
|
+
for (const pl of config.arena.pillars) {
|
|
95
|
+
P.addBody({ type: "static", shape: { cylinder: [pl.height / 2, pl.radius] }, position: [pl.x, pl.height / 2, pl.z], layer: Layer.Level, friction: 0.6 });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Props: crates and barrels around the pillars and in the open, the same scatter on every client.
|
|
99
|
+
const props = rng(0x51f7);
|
|
100
|
+
const spots: [number, number][] = [];
|
|
101
|
+
for (const pl of config.arena.pillars) {
|
|
102
|
+
for (let k = 0; k < 3; k++) {
|
|
103
|
+
const a = props() * Math.PI * 2;
|
|
104
|
+
const d = pl.radius + 1.2 + props() * 1.5;
|
|
105
|
+
spots.push([pl.x + Math.cos(a) * d, pl.z + Math.sin(a) * d]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (let k = 0; k < 14; k++) {
|
|
109
|
+
const a = props() * Math.PI * 2;
|
|
110
|
+
const d = 6 + props() * (h - 12);
|
|
111
|
+
spots.push([Math.cos(a) * d, Math.sin(a) * d]);
|
|
112
|
+
}
|
|
113
|
+
if (opts.props === false) spots.length = 0;
|
|
114
|
+
for (const [x, z] of spots) {
|
|
115
|
+
const barrel = props() < 0.4;
|
|
116
|
+
const yaw = props() * Math.PI;
|
|
117
|
+
const id = P.addBody({
|
|
118
|
+
type: "dynamic",
|
|
119
|
+
shape: barrel ? { cylinder: [0.45, 0.35] } : { box: [0.4, 0.4, 0.4] },
|
|
120
|
+
position: [x, barrel ? 0.45 : 0.4, z],
|
|
121
|
+
rotation: [0, Math.sin(yaw / 2), 0, Math.cos(yaw / 2)],
|
|
122
|
+
density: 40,
|
|
123
|
+
friction: 0.7,
|
|
124
|
+
restitution: 0.15,
|
|
125
|
+
linearDamping: 0.4,
|
|
126
|
+
angularDamping: 0.6,
|
|
127
|
+
layer: Layer.Prop,
|
|
128
|
+
mask: Layer.Level | Layer.Prop | Layer.Player | Layer.Debris,
|
|
129
|
+
});
|
|
130
|
+
this.props.push({ id, barrel, speed: 0 });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.chunkMesh = instanced(new DodecahedronGeometry(1, 0), 0xffffff, MAX_CHUNKS, 0, opts.shape?.("chunk"));
|
|
134
|
+
// One family with the field's stone and the Nature Kit stumps: loose rocks and cut stumps, not crates and barrels.
|
|
135
|
+
this.crateMesh = instanced(new DodecahedronGeometry(0.5), 0x5e5246, Math.max(1, this.props.length), 0, opts.shape?.("rock"));
|
|
136
|
+
this.barrelMesh = instanced(new CylinderGeometry(0.33, 0.38, 0.9, 7), 0x4e3a28, Math.max(1, this.props.length), 0, opts.shape?.("stump"));
|
|
137
|
+
scene.add(this.chunkMesh, this.crateMesh, this.barrelMesh);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Keepers push props: one kinematic capsule per player, moved to where it is drawn. */
|
|
141
|
+
keeper(owner: number, x: number, y: number, z: number, up: boolean): void {
|
|
142
|
+
let id = this.keepers.get(owner);
|
|
143
|
+
if (id === undefined) {
|
|
144
|
+
id = this.physics.addBody({ type: "kinematic", shape: { capsule: [0.5, 0.4] }, position: [x, y, z], layer: Layer.Player, mask: Layer.Prop | Layer.Debris });
|
|
145
|
+
this.keepers.set(owner, id);
|
|
146
|
+
}
|
|
147
|
+
this.physics.setKinematicTarget(id, [x, up ? y : -5, z]);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Forget keepers not seen this frame. */
|
|
151
|
+
keepOnly(owners: ReadonlySet<number>): void {
|
|
152
|
+
for (const [owner, id] of this.keepers) {
|
|
153
|
+
if (owners.has(owner)) continue;
|
|
154
|
+
this.physics.removeBody(id);
|
|
155
|
+
this.keepers.delete(owner);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Chunks off a killed enemy, thrown along the push direction. */
|
|
160
|
+
shatter(x: number, z: number, radius: number, color: Color, count: number, dirX = 0, dirZ = 0): void {
|
|
161
|
+
const P = this.physics;
|
|
162
|
+
this.counts.chunks += count;
|
|
163
|
+
for (let i = 0; i < count; i++) {
|
|
164
|
+
if (this.chunks.length >= MAX_CHUNKS) this.retire(0);
|
|
165
|
+
const size = radius * (0.22 + this.random() * 0.25);
|
|
166
|
+
const a = this.random() * Math.PI * 2;
|
|
167
|
+
const sp = 2 + this.random() * 3.5;
|
|
168
|
+
const id = P.addBody({
|
|
169
|
+
type: "dynamic",
|
|
170
|
+
shape: { box: [size, size, size] },
|
|
171
|
+
position: [x + Math.cos(a) * radius * 0.5, radius + 0.2 + this.random() * radius, z + Math.sin(a) * radius * 0.5],
|
|
172
|
+
density: 8,
|
|
173
|
+
friction: 0.6,
|
|
174
|
+
restitution: 0.35,
|
|
175
|
+
angularDamping: 0.3,
|
|
176
|
+
linearVelocity: [Math.cos(a) * sp + dirX * 5, 3.5 + this.random() * 4, Math.sin(a) * sp + dirZ * 5],
|
|
177
|
+
angularVelocity: [(this.random() - 0.5) * 18, (this.random() - 0.5) * 18, (this.random() - 0.5) * 18],
|
|
178
|
+
layer: Layer.Debris,
|
|
179
|
+
mask: Layer.Level | Layer.Prop,
|
|
180
|
+
});
|
|
181
|
+
const life = CHUNK_LIFE * (0.7 + this.random() * 0.6);
|
|
182
|
+
this.chunks.push({ id, life, max: life, size, color });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** A blast (nova) shoves props and chunks out of `radius`. */
|
|
187
|
+
blast(x: number, z: number, radius: number, strength: number): void {
|
|
188
|
+
const P = this.physics;
|
|
189
|
+
this.counts.blasts++;
|
|
190
|
+
const hits = P.overlap({ cylinder: [1.5, radius] }, [x, 1.5, z], { mask: Layer.Prop | Layer.Debris });
|
|
191
|
+
for (const id of hits) P.applyKnockback(id, [x, 0, z], strength, { lift: strength * 0.35 });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private retire(i: number): void {
|
|
195
|
+
const c = this.chunks[i];
|
|
196
|
+
if (!c) return;
|
|
197
|
+
this.physics.removeBody(c.id);
|
|
198
|
+
this.chunks[i] = this.chunks[this.chunks.length - 1] as Chunk;
|
|
199
|
+
this.chunks.pop();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
update(dt: number): void {
|
|
203
|
+
this.impacts.length = 0;
|
|
204
|
+
this.acc = Math.min(this.acc + dt, MAX_STEPS / HZ);
|
|
205
|
+
while (this.acc >= 1 / HZ) {
|
|
206
|
+
this.acc -= 1 / HZ;
|
|
207
|
+
this.physics.step();
|
|
208
|
+
}
|
|
209
|
+
for (let i = this.chunks.length - 1; i >= 0; i--) {
|
|
210
|
+
const c = this.chunks[i] as Chunk;
|
|
211
|
+
c.life -= dt;
|
|
212
|
+
if (c.life <= 0) this.retire(i);
|
|
213
|
+
}
|
|
214
|
+
// Draw.
|
|
215
|
+
const P = this.physics;
|
|
216
|
+
let n = 0;
|
|
217
|
+
for (const c of this.chunks) {
|
|
218
|
+
const [x, y, z] = P.position(c.id);
|
|
219
|
+
const [qx, qy, qz, qw] = P.rotation(c.id);
|
|
220
|
+
const k = Math.min(1, c.life / (c.max * 0.35));
|
|
221
|
+
this.p.set(x, y, z);
|
|
222
|
+
this.q.set(qx, qy, qz, qw);
|
|
223
|
+
this.s.setScalar(c.size * 1.25 * k);
|
|
224
|
+
this.m.compose(this.p, this.q, this.s);
|
|
225
|
+
this.chunkMesh.setMatrixAt(n, this.m);
|
|
226
|
+
this.chunkMesh.setColorAt(n, c.color);
|
|
227
|
+
n++;
|
|
228
|
+
}
|
|
229
|
+
this.chunkMesh.count = n;
|
|
230
|
+
this.chunkMesh.instanceMatrix.needsUpdate = true;
|
|
231
|
+
if (this.chunkMesh.instanceColor) this.chunkMesh.instanceColor.needsUpdate = true;
|
|
232
|
+
let crates = 0;
|
|
233
|
+
let barrels = 0;
|
|
234
|
+
this.s.setScalar(1);
|
|
235
|
+
for (const prop of this.props) {
|
|
236
|
+
const [x, y, z] = P.position(prop.id);
|
|
237
|
+
const [vx, vy, vz] = P.velocity(prop.id);
|
|
238
|
+
const speed = Math.hypot(vx, vy, vz);
|
|
239
|
+
if (prop.speed - speed > 3) this.impacts.push({ x, y, z, speed: prop.speed - speed });
|
|
240
|
+
prop.speed = speed;
|
|
241
|
+
const [qx, qy, qz, qw] = P.rotation(prop.id);
|
|
242
|
+
this.p.set(x, y, z);
|
|
243
|
+
this.q.set(qx, qy, qz, qw);
|
|
244
|
+
this.m.compose(this.p, this.q, this.s);
|
|
245
|
+
if (prop.barrel) this.barrelMesh.setMatrixAt(barrels++, this.m);
|
|
246
|
+
else this.crateMesh.setMatrixAt(crates++, this.m);
|
|
247
|
+
}
|
|
248
|
+
this.crateMesh.count = crates;
|
|
249
|
+
this.barrelMesh.count = barrels;
|
|
250
|
+
this.crateMesh.instanceMatrix.needsUpdate = true;
|
|
251
|
+
this.barrelMesh.instanceMatrix.needsUpdate = true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
get liveChunks(): number {
|
|
255
|
+
return this.chunks.length;
|
|
256
|
+
}
|
|
257
|
+
}
|
package/client/decor.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Library models the EMBERWATCH scene draws on its own (template machinery,
|
|
3
|
+
* not game content): the forest past the wall.
|
|
4
|
+
* The build copies these with the models game.ts uses.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Enemy models are drawn about this many hit radii tall. */
|
|
8
|
+
export const ENEMY_HEIGHT_PER_RADIUS = 3.2;
|
|
9
|
+
/** Keepers are drawn this tall, m. */
|
|
10
|
+
export const KEEPER_HEIGHT = 1.8;
|
|
11
|
+
|
|
12
|
+
/** [library id, height m, how many] in a ring outside the wall. */
|
|
13
|
+
export const DECOR: readonly (readonly [string, number, number])[] = [
|
|
14
|
+
["style.lowpoly.tree_pine", 5.5, 26],
|
|
15
|
+
["style.lowpoly.tree_pine_tall", 7.5, 14],
|
|
16
|
+
["style.lowpoly.tree_dark", 5, 14],
|
|
17
|
+
["style.lowpoly.rock_large", 1.8, 12],
|
|
18
|
+
["style.lowpoly.stump", 0.7, 10],
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
export const SCENE_MODELS: readonly string[] = DECOR.map(([id]) => id);
|
package/client/host.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Net Forge as host. Framed by Net Forge, the page URL carries
|
|
3
|
+
* the Kit flags and the host posts one message; played on its own, none of
|
|
4
|
+
* this applies and the game behaves exactly as before.
|
|
5
|
+
*
|
|
6
|
+
* ?host=netnet the host draws Invite and sign-in; the game hides its own
|
|
7
|
+
* ?coin=TICKER the coin the HUD names (2 to 6 capitals or digits)
|
|
8
|
+
* ?title=NAME the game's own name for the wordmark and the tab title
|
|
9
|
+
* ?player=guest no wallet is connected: play pays nothing, and the HUD
|
|
10
|
+
* says "not earning" instead of a balance
|
|
11
|
+
* ?player=play the token has not launched: coins are play money
|
|
12
|
+
*
|
|
13
|
+
* The player flag changes while the game runs (a wallet connects), so the
|
|
14
|
+
* host also posts { kind: "netnet-kit", type: "player", mode }. Only the
|
|
15
|
+
* parent window is listened to, and only when the URL says it is the host.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type PlayerMode = "earning" | "guest" | "play";
|
|
19
|
+
|
|
20
|
+
export interface HostFlags {
|
|
21
|
+
/** Framed by Net Forge. */
|
|
22
|
+
readonly framed: boolean;
|
|
23
|
+
/** The coin's ticker, when the host names one. */
|
|
24
|
+
readonly coin: string | null;
|
|
25
|
+
/** The game's own name, when the host names one. */
|
|
26
|
+
readonly title: string | null;
|
|
27
|
+
/** What play pays the person at the screen. Standalone play is "earning" (the game's own kit decides). */
|
|
28
|
+
readonly player: PlayerMode;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const TICKER_RE = /^[A-Z0-9]{2,6}$/;
|
|
32
|
+
|
|
33
|
+
export function hostFlags(params: URLSearchParams): HostFlags {
|
|
34
|
+
const framed = params.get("host") === "netnet";
|
|
35
|
+
const coin = params.get("coin");
|
|
36
|
+
// eslint-disable-next-line no-control-regex
|
|
37
|
+
const title = (params.get("title") ?? "").replace(/[\u0000-\u001f\u007f]/g, " ").replace(/\s+/g, " ").trim().slice(0, 40);
|
|
38
|
+
const player = params.get("player");
|
|
39
|
+
return {
|
|
40
|
+
framed,
|
|
41
|
+
coin: coin && TICKER_RE.test(coin) ? coin : null,
|
|
42
|
+
title: title || null,
|
|
43
|
+
player: framed && (player === "guest" || player === "play") ? player : "earning",
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The player mode the host named on the URL, or null when it named none (a
|
|
49
|
+
* connected player on a live game gets no flag, only the posted message) or
|
|
50
|
+
* the page is not framed by Net Forge.
|
|
51
|
+
*/
|
|
52
|
+
export function namedPlayer(params: URLSearchParams): PlayerMode | null {
|
|
53
|
+
if (params.get("host") !== "netnet") return null;
|
|
54
|
+
const player = params.get("player");
|
|
55
|
+
return player === "earning" || player === "guest" || player === "play" ? player : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The store's note under its prices, from one source of truth. Once the host
|
|
60
|
+
* has named the player mode (on the URL or in a message), that mode decides,
|
|
61
|
+
* exactly as it does for the coin box: a live game never calls its coins play
|
|
62
|
+
* money. Standalone play, or a host that names no mode (the studio's drafts),
|
|
63
|
+
* keeps the room's own word (`welcome.earnLive`).
|
|
64
|
+
*/
|
|
65
|
+
export function storeNote(hostMode: PlayerMode | null, earnLive: boolean, symbol: string): string {
|
|
66
|
+
if (hostMode === "play") return "Play money until launch";
|
|
67
|
+
if (hostMode === "guest") return `Connect a wallet to earn ${symbol}`;
|
|
68
|
+
if (hostMode === "earning") return "";
|
|
69
|
+
return earnLive ? "" : "Play money until launch";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A host message, checked: anything else is ignored. */
|
|
73
|
+
export function playerFrom(data: unknown): PlayerMode | null {
|
|
74
|
+
if (typeof data !== "object" || data === null) return null;
|
|
75
|
+
const m = data as Record<string, unknown>;
|
|
76
|
+
if (m["kind"] !== "netnet-kit" || m["type"] !== "player") return null;
|
|
77
|
+
const mode = m["mode"];
|
|
78
|
+
return mode === "earning" || mode === "guest" || mode === "play" ? mode : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Calls `cb` with each player mode the host posts. Framed pages only. */
|
|
82
|
+
export function onHostPlayer(flags: HostFlags, cb: (mode: PlayerMode) => void): void {
|
|
83
|
+
if (!flags.framed || window.parent === window) return;
|
|
84
|
+
window.addEventListener("message", (e: MessageEvent<unknown>) => {
|
|
85
|
+
if (e.source !== window.parent) return;
|
|
86
|
+
const mode = playerFrom(e.data);
|
|
87
|
+
if (mode) cb(mode);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Tells the host the game is up, so it can post the current player mode. */
|
|
92
|
+
export function postReady(flags: HostFlags): void {
|
|
93
|
+
if (!flags.framed || window.parent === window) return;
|
|
94
|
+
// The host's origin is not known to an opaque-origin frame; the message is one public flag.
|
|
95
|
+
window.parent.postMessage({ kind: "netnet-kit", type: "ready" }, "*");
|
|
96
|
+
}
|