NET FORGE DOCS

Tower defense template (COGSPIRE)

Last updated Checked against @netforge/engine 0.2.0View as Markdown

Net Forge, the open-source multiplayer browser game engine built for AI coding agents, is published on npm under @netforge with source at github.com/netnetdev/net-forge.

COGSPIRE is a complete co-op tower defense: creeps walk a fixed road toward the spire in waves, and up to four players build and upgrade towers beside the road with their own gold. Every kill pays its tower's builder a bounty and the rest of the team a share, the team shares the spire's lives, and clearing all twenty waves wins the run.

npm create netforge@latest my-game -- --template tower-defense --yes
cd my-game
npm run play

--template tower and --template td work too. Click a tile to build the tower picked in the bar (keys 1 to 4), click a tower to upgrade or sell it, and press Space to call the next wave. On touch, tap a tile to aim and tap again to build.

What you get

PartWhat it doesWhere
The boardSize in tiles (a tile is 2 m), the road as tile waypoints, rocks nobody can build on, lives and starting goldtowerBoard({...})
Towers1 to 8 towers of kind bolt, splash, frost or chain, each with upgrade levelstower(...)
CreepsHealth, speed, gold bounty, lives a leak costs, bosses with a health barprefab(...)
WavesGroups that join at a wave, grow each wave, and a boss every tenth wavewaves({...})
Co-opShared lives, per-player gold, kill bounties shared with the team, a ready votethe template
Netcode30 Hz authoritative room; your cursor is predicted; builds, upgrades and sells are validated actionsthe template package
Playtest botsFour builders for four minutes: kills, wave reached, leaks, earnnpm test

Measured with the template's playtest on an Apple M2 Pro, Node 22: four builders over four minutes (7,200 ticks at 30 Hz) killed 132 creeps and reached wave 8 with no leaks, at 0.008 ms per server tick on average. A board game's rules are integer state, so they cost far less than an action game's physics.

1. Change the road

The road is a list of tile waypoints. Each step changes x or y, never both, and the road starts and ends on the board's edge. Longer roads with more turns give towers more time.

src/game.ts
import { towerBoard } from "@netforge/engine";

towerBoard({
  size: [20, 12],
  path: [[0, 5], [6, 5], [6, 1], [13, 1], [13, 10], [19, 10]],
  lives: 20,
  gold: 100,
});

A game has exactly one towerBoard(...) and one waves(...).

2. Add a tower and a creep

Damage is per hit, rate is shots a second, and range and radius are in tiles. splash hits a radius around the target, frost slows by slow for slowSeconds, and chain jumps chains times, 30% weaker each jump.

src/game.ts
import { prefab, tower } from "@netforge/engine";

tower("sky_harpoon", {
  model: "style.lowpoly.rocket",
  kind: "bolt",
  cost: 120,
  damage: 60,
  rate: 0.4,
  range: 6,
  levels: [{ cost: 100, damage: 90 }],
});

prefab("ironclad", { model: "style.lowpoly.robot_walker", health: 160, speed: 0.9, value: 14, damage: 3 });

Bring the creep in by adding a group to waves, for example { prefab: "ironclad", from: 8, count: 2, grow: 0.5, gap: 2 }, and give the tower a label in src/names.json. The bar shows towers in the order you declare them.

3. Make it harder or easier

Raise waves health growth, group counts and grow, or lower starting gold and lives. A shorter prep and breather on the board makes it faster. More builders make tougher creeps (30% more health for each player past the first).

4. Check it with bots

npm test

The playtest requires that four builders kill creeps, clear wave 3 and fire an earn rule within four minutes, and that the spire survives to wave 4.

How it works

Tower defense is a board genre: its rules are a deterministic module in @netforge/engine/rules (integer state, a seeded random number, no clocks) behind the same simulation interface as the action templates. That means the same room host, netcode, run verification and playtest serve it. Towers aim at the creep furthest along the road in range. Your cursor is predicted so pointing never waits for the server, and every build, upgrade, sell and ready vote is an action the room checks (gold, tile, ownership). See Simulation and ECS.

The template also declares earn rules and store items priced in coins. Locally they show and pay nothing; paying them out in a game token is an optional upgrade on the hosted Net Forge service.

The full guide for this genre is Make a browser tower defense game.

Alternatives

  • Phaser is a 2D HTML5 framework with tilemap support and a large example library. Pick it for a single-player 2D tower defense with hand-drawn sprites.
  • Godot has a tilemap editor, navigation and a web export. Pick it when you want to lay out maps in an editor and ship to desktop as well.
  • Net Forge fits when you want four players on one board with server-checked builds, shared lives and bots that playtest every change.

FAQ

Can four people build on the same tower defense board?

Yes. Up to four builders share one board, each with their own gold, and share the spire's lives.

How do creeps find the path?

They follow the road you declare as tile waypoints in towerBoard({ path }). Towers cannot be built on the road or on blocked tiles.

Which tower types are built in?

bolt (single target), splash (an area around the target), frost (slows) and chain (jumps between creeps). Each tower can declare upgrade levels with their own cost and stats.

Can a player join a run that has already started?

Yes. They build from that point on. A player who joins mid-run is never credited for waves cleared before they joined.

Is there a boss wave?

Yes. A creep declared with boss: true gets a health bar, and a wave group with every: 10 brings one every tenth wave.