Game: Tower Defense
Theme: Medieval
Map: Single Path
Date: 2026-04-06

## Prompt

Generate a complete, self-contained single-file 3D tower defense game using Three.js with medieval theme and single-path map. The game must be fully playable with strategic depth and visual polish.

### Core Systems

#### Tower Types (6 archetypes)

Each tower has base cost, range, damage, fire rate, and upgrade tree.

1. **Archer Tower** (Basic)
   - Cost: 80 gold
   - Range: 5 tiles
   - Damage: 10
   - Fire rate: 1 shot per second
   - Projectile: Arrow (thin cylinder)
   - Level 1: Base stats
   - Level 2 (120 gold): +40% damage (14), +10% range (5.5 tiles), visual: larger platform
   - Level 3 (200 gold): +100% damage (20), +25% range (6.25 tiles), ability: piercing shots (hit 2 enemies in line)

2. **Crossbow Tower** (Sniper)
   - Cost: 150 gold
   - Range: 8 tiles
   - Damage: 30
   - Fire rate: 0.5 shots per second
   - Projectile: Bolt (thicker cylinder)
   - Level 1: Base stats
   - Level 2 (225 gold): +40% damage (42), +10% range (8.8 tiles), visual: longer barrel
   - Level 3 (375 gold): +100% damage (60), +25% range (10 tiles), ability: critical hits (2x damage, 20% chance)

3. **Catapult** (Splash)
   - Cost: 200 gold
   - Range: 6 tiles
   - Damage: 25 (area, radius 2 tiles)
   - Fire rate: 0.8 shots per second
   - Projectile: Boulder (sphere)
   - Level 1: Base stats
   - Level 2 (300 gold): +40% damage (35), +10% range (6.6 tiles), visual: larger arm
   - Level 3 (500 gold): +100% damage (50), +25% range (7.5 tiles), ability: burn DOT (5 damage per second for 3 seconds)

4. **Frost Mage** (Slow)
   - Cost: 120 gold
   - Range: 4 tiles
   - Damage: 5
   - Fire rate: 1.5 shots per second
   - Projectile: Ice Shard (blue tetrahedron)
   - Effect: Slows enemy by 50% for 2 seconds
   - Level 1: Base stats
   - Level 2 (180 gold): +40% damage (7), +10% range (4.4 tiles), visual: larger crystal
   - Level 3 (300 gold): +100% damage (10), +25% range (5 tiles), ability: freeze (complete stop for 1 second)

5. **Repeating Ballista** (Rapid)
   - Cost: 100 gold
   - Range: 4 tiles
   - Damage: 6
   - Fire rate: 3 shots per second
   - Projectile: Bolt (small cylinder)
   - Level 1: Base stats
   - Level 2 (150 gold): +40% damage (8.4), +10% range (4.4 tiles), visual: second barrel added
   - Level 3 (250 gold): +100% damage (12), +25% range (5 tiles), ability: double barrel (2 projectiles per shot)

6. **Alchemist** (Special)
   - Cost: 250 gold
   - Range: 5 tiles
   - Damage: 15
   - Fire rate: 0.7 shots per second
   - Projectile: Potion (green sphere)
   - Effect: Poison DOT (10 damage over 3 seconds)
   - Level 1: Base stats
   - Level 2 (375 gold): +40% damage (21), +10% range (5.5 tiles), visual: bubbling cauldron
   - Level 3 (625 gold): +100% damage (30), +25% range (6.25 tiles), ability: chain poison (poison spreads to nearby enemies)

#### Tower Targeting Priority (cycle by clicking tower)
- First: Target enemy closest to base
- Strongest: Highest HP
- Weakest: Lowest HP
- Fastest: Highest speed
Default: First. Show current mode on tower info panel.

#### Enemy Types (6 types)

Base stats scale per wave with caps (constitution §2).

1. **Scout** (Fast, low HP)
   - Base HP: 20
   - Base Speed: 3 tiles per second
   - Armor: None
   - Gold reward: 5
   - Visual: Small cube with cone helmet (knight)

2. **Soldier** (Balanced)
   - Base HP: 50
   - Base Speed: 1.8 tiles per second
   - Armor: Light (10% damage reduction)
   - Gold reward: 10
   - Visual: Capsule with shield

3. **Tank** (High HP, slow)
   - Base HP: 150
   - Base Speed: 1 tile per second
   - Armor: Heavy (25% damage reduction)
   - Gold reward: 25
   - Visual: Large box with armor plates

4. **Sprinter** (Very fast, low HP)
   - Base HP: 15
   - Base Speed: 4 tiles per second
   - Armor: None
   - Gold reward: 8
   - Visual: Low capsule (wolf)

5. **Healer** (Medium)
   - Base HP: 40
   - Base Speed: 1.5 tiles per second
   - Armor: None
   - Gold reward: 12
   - Ability: Heals nearby enemies for 5 HP per second (radius 2 tiles)
   - Visual: Sphere with glowing cross

6. **Boss** (Every 5 waves)
   - Base HP: 500
   - Base Speed: 0.7 tiles per second
   - Armor: Heavy (30% damage reduction)
   - Gold reward: 100
   - Ability: Takes 3 lives when reaching base
   - Visual: Dragon (winged composite shape), 2x size, distinct red glow

#### Wave System

20 waves total. Wave composition predefined (not random). Boss every 5 waves. Wave 1 tutorial: only scouts.

Wave scaling (caps per constitution):
- HP = baseHP * (1 + 0.15 * wave), capped at 8x baseHP
- Speed = baseSpeed * (1 + 0.05 * wave), capped at 2.5x baseSpeed

Example wave compositions:
- Wave 1: 10 Scouts (delay between spawns: 1s)
- Wave 2: 8 Scouts, 4 Soldiers
- Wave 3: 6 Soldiers, 2 Tanks
- Wave 4: 12 Scouts, 6 Soldiers, 1 Sprinter
- Wave 5: Boss + 4 Soldiers + 6 Scouts
- Continue with increasing variety and numbers.

Swarm waves (e.g., wave 10): 25 Scouts.
Boss waves: 5, 10, 15, 20.

Inter-wave break: 15 seconds between waves. "Next Wave" button to skip timer.
Wave preview: Show upcoming wave composition (icons and counts) before wave starts.

#### Economy

- Starting gold: 300 (enough for 3-4 basic towers)
- Gold per kill: As per enemy type
- Interest: 2% interest on unspent gold between waves (capped at 50 gold max interest per wave)
- Sell towers: Refund 60% of total investment (base + upgrades)
- No softlock: Ensure player can always earn enough gold to progress.

#### Base / Lives

- Base HP: 20 lives
- Each enemy reaching base costs 1 life (boss costs 3)
- Game over at 0 lives
- Victory after wave 20

#### Map Layout

Single path map, grid 15x12 tiles, tile size 2.0 units.

Path winding: Entry at top-left, exit at bottom-right, with 5 turns. Path width 1 tile. Buildable tiles are all adjacent to path.

Terrain: Grass ground plane with stone path (grey). Castle walls at base end. Trees as decorative non-buildable obstacles (10% of map).

#### 3D Rendering

- Camera: Isometric perspective angled 45 degrees down, positioned to view entire map with slight margin.
- Lighting: Ambient light + directional light with shadows.
- Skybox: Warm sunset gradient (golden hour).
- Tower models: As described in medieval theme reference.
- Enemy models: Simple geometric shapes colored by type.
- Projectiles: Visible travel time (speed 15 units per second).
- Effects: Particle systems for explosions, ice crystals, poison clouds.
- Health bars: Billboard sprites above enemies.
- Range indicators: Semi-transparent circle when hovering tower or placement.
- Grid overlay: Faint lines on ground.

#### UI Requirements

DOM overlay over Three.js canvas.

HUD (top bar):
- Gold counter with coin icon
- Lives counter with heart icon
- Wave counter (current / total)
- Timer until next wave
- Game speed buttons (1x, 2x, 3x)

Tower panel (sidebar left):
- 6 tower buttons with icon, name, cost, greyed if cannot afford
- Selected tower info (range, damage, fire rate, description)
- Upgrade button (when existing tower selected)
- Sell button (when existing tower selected)
- Confirmation dialog for expensive sells (>50% of current gold)

Game states:
- Start screen: Title "The Last Gatehouse", tagline "Hold until dawn", Start Game button, difficulty selection (Normal/Hard).
- Building phase: Full HUD, tower panel, wave preview, countdown.
- Wave active: Full HUD, "Next Wave" hidden, wave progress bar.
- Paused: Overlay with Resume, Restart, Quit.
- Game Over: Stats (waves survived, enemies killed, towers built), Play Again.
- Victory: Celebration, stats, option to continue endless mode.

Mobile controls: Touch-friendly buttons, pinch zoom, two-finger pan.

#### Performance Budgets

- Active enemies cap: 50
- Active projectiles cap: 100
- Active particles cap: 500
- Towers placed cap: 40

Use object pooling for projectiles and particles.

#### Audio

Web Audio API procedural sounds:
- Tower fire: short burst per tower type
- Enemy death: pop
- Wave start: alert tone
- Boss incoming: low rumble
- Game over: descending tone
- Victory: ascending chord
- Mute button in HUD

#### SEO Metadata

```html
<meta name="description" content="The Last Gatehouse — Medieval tower defense. Hold the castle against waves of knights, orcs, and dragons.">
<meta property="og:title" content="The Last Gatehouse">
<meta property="og:description" content="Medieval tower defense. Hold the castle against waves of knights, orcs, and dragons.">
<meta property="og:type" content="website">
<meta property="og:url" content="">
<meta property="og:image" content="">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="The Last Gatehouse">
<meta name="twitter:description" content="Medieval tower defense. Hold the castle against waves of knights, orcs, and dragons.">
<meta name="twitter:image" content="">
```

## Notes

- Balance ensures no softlock: basic towers can survive early waves, gold income sufficient.
- Wave scaling capped to avoid impossible late waves.
- Tower upgrade trees provide meaningful choices.
- Visual distinctness: each tower type and enemy type easily recognizable.
- Path deterministic: enemies follow same path given same map state.
- Projectile travel time adds strategic depth (fast enemies can dodge).
- Boss waves announced 3 seconds before spawn.