# g-amador/hold-the-trench architecture

> Architecture diagram of the GitHub repository g-amador/hold-the-trench, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/g-amador/hold-the-trench
- Repository: https://github.com/g-amador/hold-the-trench

## Overview

Hold the Trench is an offline, single-player tactical defense game. The runtime starts in the menu, creates a battlefield, and enters an assault state where the player moves, builds defenses with supplies, fights enemy infantry, and protects a bunker; destroying the opposing bunker leads to victory, while losing yours leads to defeat. Victory offers continuation choices. The graph preserves the README’s combat, defense, supply, battlefield, and post-battle capabilities. Some gameplay wiring is supported by partial excerpts and README statements; unsampled details are not inferred.

Main entry points: [`src/states/assault_phase.py`](https://github.com/g-amador/hold-the-trench/blob/main/src/states/assault_phase.py) (Assault phase), [`src/states/main_menu.py`](https://github.com/g-amador/hold-the-trench/blob/main/src/states/main_menu.py) (Main menu), [`src/world/tilemap.py`](https://github.com/g-amador/hold-the-trench/blob/main/src/world/tilemap.py) (Tile map), [`src/engine/game.py`](https://github.com/g-amador/hold-the-trench/blob/main/src/engine/game.py) (Game loop)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Game runtime"]
  node_game["Game loop<br/>[game.py]"]
  node_menu["Main menu<br/>[main_menu.py]"]
  node_config["Game settings<br/>[config.py]"]
end

subgraph group_battlefield["Battlefield"]
  node_map["Tile map<br/>[tilemap.py]"]
  node_generator["Battlefield layout"]
  node_trench["Trench entity<br/>[trench.py]"]
end

subgraph group_gameplay["Combat and economy"]
  node_assault["Assault phase<br/>[assault_phase.py]"]
  node_player_unit["Player unit<br/>[player.py]"]
  node_defenses["Buildable defenses"]
  node_infantry["Enemy infantry<br/>[enemy.py]"]
  node_friendly["Friendly infantry"]
  node_barracks["Barracks spawning<br/>[barracks.py]"]
  node_combat["Combat system<br/>[combat_system.py]"]
  node_bullet["Projectile<br/>[bullet.py]"]
  node_economy["Supplies economy<br/>[economy_system.py]"]
  node_pickup["Supply pickup<br/>[gold_pickup.py]"]
  node_building_query["Building query<br/>[building_query.py]"]
  node_hud["Battle HUD<br/>[hud.py]"]
end

subgraph group_outcomes["Battle outcomes"]
  node_game_over["Defeat state<br/>[game_over.py]"]
  node_victory["Victory state<br/>[win_state.py]"]
end

node_player(("Player"))

node_player -->|"starts"| node_game
node_game -->|"initializes"| node_menu
node_game -->|"creates"| node_map
node_menu -->|"recreates"| node_map
node_menu -->|"starts battle"| node_assault
node_map -->|"loads layout"| node_generator
node_assault -->|"reads battlefield"| node_map
node_assault -->|"updates"| node_player_unit
node_assault -->|"queries buildings"| node_building_query
node_building_query -->|"reads tiles"| node_map
node_assault -->|"spends supplies"| node_economy
node_assault -->|"places and updates"| node_defenses
node_assault -->|"updates spawning"| node_barracks
node_barracks -->|"spawns"| node_infantry
node_barracks -->|"spawns"| node_friendly
node_assault -->|"updates melee"| node_friendly
node_assault -->|"updates combat"| node_infantry
node_assault -.->|"coordinates combat"| node_combat
node_player_unit -->|"fires"| node_bullet
node_assault -->|"updates hits"| node_bullet
node_assault -->|"collects drops"| node_pickup
node_infantry -->|"creates on defeat"| node_pickup
node_assault -->|"renders status"| node_hud
node_assault -->|"transitions on defeat"| node_game_over
node_assault -->|"transitions on victory"| node_victory
node_game_over -->|"restarts at"| node_menu
node_victory -->|"offers menu return"| node_menu

click node_game "https://github.com/g-amador/hold-the-trench/blob/main/src/engine/game.py"
click node_menu "https://github.com/g-amador/hold-the-trench/blob/main/src/states/main_menu.py"
click node_config "https://github.com/g-amador/hold-the-trench/blob/main/src/config.py"
click node_map "https://github.com/g-amador/hold-the-trench/blob/main/src/world/tilemap.py"
click node_generator "https://github.com/g-amador/hold-the-trench/blob/main/src/world/trench_generator.py"
click node_assault "https://github.com/g-amador/hold-the-trench/blob/main/src/states/assault_phase.py"
click node_player_unit "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/player.py"
click node_defenses "https://github.com/g-amador/hold-the-trench/tree/main/src/entities"
click node_infantry "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/enemy.py"
click node_friendly "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/friendly_infantry.py"
click node_barracks "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/barracks.py"
click node_combat "https://github.com/g-amador/hold-the-trench/blob/main/src/systems/combat_system.py"
click node_bullet "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/bullet.py"
click node_economy "https://github.com/g-amador/hold-the-trench/blob/main/src/systems/economy_system.py"
click node_pickup "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/gold_pickup.py"
click node_building_query "https://github.com/g-amador/hold-the-trench/blob/main/src/systems/building_query.py"
click node_hud "https://github.com/g-amador/hold-the-trench/blob/main/src/ui/hud.py"
click node_trench "https://github.com/g-amador/hold-the-trench/blob/main/src/entities/trench.py"
click node_game_over "https://github.com/g-amador/hold-the-trench/blob/main/src/states/game_over.py"
click node_victory "https://github.com/g-amador/hold-the-trench/blob/main/src/states/win_state.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_game,node_menu,node_config toneBlue
class node_map,node_generator,node_trench toneAmber
class node_assault,node_player_unit,node_defenses,node_infantry,node_friendly,node_barracks,node_combat,node_bullet,node_economy,node_pickup,node_building_query,node_hud toneMint
class node_game_over,node_victory toneRose
class node_player toneIndigo
```

## Components

### Game runtime

- [Game loop](https://github.com/g-amador/hold-the-trench/blob/main/src/engine/game.py): `src/engine/game.py`
- [Main menu](https://github.com/g-amador/hold-the-trench/blob/main/src/states/main_menu.py): `src/states/main_menu.py`
- [Game settings](https://github.com/g-amador/hold-the-trench/blob/main/src/config.py): `src/config.py`

### Battlefield

- [Tile map](https://github.com/g-amador/hold-the-trench/blob/main/src/world/tilemap.py): `src/world/tilemap.py`
- [Battlefield layout](https://github.com/g-amador/hold-the-trench/blob/main/src/world/trench_generator.py): `src/world/trench_generator.py`
- [Trench entity](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/trench.py): `src/entities/trench.py`

### Combat and economy

- [Assault phase](https://github.com/g-amador/hold-the-trench/blob/main/src/states/assault_phase.py): `src/states/assault_phase.py`
- [Player unit](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/player.py): `src/entities/player.py`
- [Buildable defenses](https://github.com/g-amador/hold-the-trench/tree/main/src/entities): `src/entities`
- [Enemy infantry](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/enemy.py): `src/entities/enemy.py`
- [Friendly infantry](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/friendly_infantry.py): `src/entities/friendly_infantry.py`
- [Barracks spawning](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/barracks.py): `src/entities/barracks.py`
- [Combat system](https://github.com/g-amador/hold-the-trench/blob/main/src/systems/combat_system.py): `src/systems/combat_system.py`
- [Projectile](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/bullet.py): `src/entities/bullet.py`
- [Supplies economy](https://github.com/g-amador/hold-the-trench/blob/main/src/systems/economy_system.py): `src/systems/economy_system.py`
- [Supply pickup](https://github.com/g-amador/hold-the-trench/blob/main/src/entities/gold_pickup.py): `src/entities/gold_pickup.py`
- [Building query](https://github.com/g-amador/hold-the-trench/blob/main/src/systems/building_query.py): `src/systems/building_query.py`
- [Battle HUD](https://github.com/g-amador/hold-the-trench/blob/main/src/ui/hud.py): `src/ui/hud.py`

### Battle outcomes

- [Defeat state](https://github.com/g-amador/hold-the-trench/blob/main/src/states/game_over.py): `src/states/game_over.py`
- [Victory state](https://github.com/g-amador/hold-the-trench/blob/main/src/states/win_state.py): `src/states/win_state.py`

### Other components

- **Player**

## Connections

- Player → Game loop: starts (evidence: `README.md`)
- Game loop → Main menu: initializes (evidence: `src/engine/game.py`)
- Game loop → Tile map: creates (evidence: `src/engine/game.py`)
- Main menu → Tile map: recreates (evidence: `src/states/main_menu.py`)
- Main menu → Assault phase: starts battle (evidence: `src/states/main_menu.py`)
- Tile map → Battlefield layout: loads layout (evidence: `src/world/tilemap.py`)
- Assault phase → Tile map: reads battlefield (evidence: `src/states/assault_phase.py`)
- Assault phase → Player unit: updates (evidence: `src/states/assault_phase.py`)
- Assault phase → Building query: queries buildings (evidence: `src/states/assault_phase.py`)
- Building query → Tile map: reads tiles (evidence: `src/systems/building_query.py`)
- Assault phase → Supplies economy: spends supplies (evidence: `src/states/assault_phase.py`)
- Assault phase → Buildable defenses: places and updates (evidence: `src/states/assault_phase.py`)
- Assault phase → Barracks spawning: updates spawning (evidence: `src/states/assault_phase.py`)
- Barracks spawning → Enemy infantry: spawns (evidence: `src/entities/barracks.py`)
- Barracks spawning → Friendly infantry: spawns (evidence: `src/entities/barracks.py`)
- Assault phase → Friendly infantry: updates melee (evidence: `src/states/assault_phase.py`)
- Assault phase → Enemy infantry: updates combat (evidence: `src/states/assault_phase.py`)
- Assault phase → Combat system: coordinates combat
- Player unit → Projectile: fires (evidence: `src/entities/player.py`)
- Assault phase → Projectile: updates hits (evidence: `src/states/assault_phase.py`)
- Assault phase → Supply pickup: collects drops (evidence: `src/states/assault_phase.py`)
- Enemy infantry → Supply pickup: creates on defeat (evidence: `src/entities/enemy.py`)
- Assault phase → Battle HUD: renders status (evidence: `src/states/assault_phase.py`)
- Assault phase → Defeat state: transitions on defeat (evidence: `src/states/assault_phase.py`)
- Assault phase → Victory state: transitions on victory (evidence: `src/states/assault_phase.py`)
- Defeat state → Main menu: restarts at (evidence: `src/states/game_over.py`)
- Victory state → Main menu: offers menu return (evidence: `src/states/win_state.py`)
