README
¶
Bramble Gate: a tower defense template
A small, complete 2D tower defense game built with NSGo and github.com/nsgo-dev/game. Enemies walk a road from the spawn to the gate; you build towers on the grass beside it, upgrade them with the money the enemies drop, and call each wave when you are ready. Stop all five waves to win; let ten lives' worth of enemies through the gate and you lose.
It shows:
- A level, its waves and its enemies in
data/level.yaml, the towers and their upgrades indata/towers.yaml, and the projectiles indata/projectiles.yaml, all loaded throughgame.yaml's autoloads and checked against each other at startup. - The road found with
ai.AStarover the map's road tiles, and the map drawn bytilemapfrom tiles painted in code. - Tower and enemy stats on
stats.Sheets: upgrades are permanent stat effects, and a frost tower's slow is a timed modifier on the enemy's speed. - Towers firing
bulletspatterns that lead their target, with splash damage and slows read from the tower's stats. - A fixed-step
game.Loop, ascreen.Viewportthat fits the map to any window, and a native HUD of NSGo text and buttons. - Keyboard, gamepad and touch controls from
game.yaml's input map, and headlessnsgotestplaythroughs that win and lose the game with each of them.
Edit the name and identifier in game.yaml before shipping it, then write them into nsgo.yaml and check the project:
go run github.com/nsgo-dev/game/cmd/gameproject sync
go run github.com/nsgo-dev/game/cmd/gameproject check
Running and testing
go tool nsgo dev # this machine, with hot reload
go tool nsgo dev -target web # the browser
go tool nsgo dev -target ios_simulator
go tool nsgo dev -target android
go tool nsgo test ./... # headless tests
go tool nsgo check -target macos,windows,linux,android,web
go tool nsgo build -target macos_arm64,windows_x64,linux_x64,web
Controls
| Action | Keyboard | Gamepad | Touch and mouse |
|---|---|---|---|
| Move the cursor | Arrow keys or WASD | D-pad or left stick | Tap a tile |
| Choose a tower | 1, 2, 3, or Q and E | Shoulder buttons | Tower buttons |
| Build | Enter or Space | A (south) | Tap the cursor's tile again |
| Upgrade | U | Y (north) | Upgrade |
| Sell | X or Backspace | X (west) | Sell |
| Call the next wave | N | Start | Next wave |
| Speed 1x, 2x, 3x | F | Right trigger | Speed |
| Pause | P or Escape | Back | Pause |
| Restart | R | Start or A on the end screen only | Play again |
The bindings are the input section of game.yaml: add a key or a button to an action there.
Layout
game.yaml: the project file: name, id and version, window and virtual resolution, the input map, the data files to autoload and the export presets.data/level.yaml: the level: tile size, starting money and lives, the map, the enemy kinds and the waves.data/towers.yaml: the tower kinds: cost, color, stats, upgrades and the pattern each fires, and the share of the cost selling returns.data/projectiles.yaml: the projectiles, abulletspattern file.data.go: the level and tower formats, their validation, andLoadData, which reads the three files throughgame.yaml.world.go: the simulation: the road, spawning, walking, targeting, firing, hits, money, lives and the outcome, on fixed steps with no clock or randomness.game.go: the controller: the loop, input actions, the cursor, and the HUD's state.view.go: the NSGo views (status bar, battlefield canvas, toolbar, end banner) and the drawing of the battlefield.art.go: the ground tiles and projectiles, drawn in code.project.go,main.go:game.yamlembedded asProject, and the entry point.*_test.go: format, simulation and playthrough tests.game.index.yaml: the stable IDs of the project's files, kept bygame index.
Where to change it
- The map: edit
mapindata/level.yaml. Each character is a tile:.grass you can build on,=road,Sthe spawn,Gthe gate,^rock and~water. Every row has the same length, there is exactly oneSand oneG, and road must join them; enemies take the shortest road. The map's size in game points,tileSize(at most 256) times its columns bytileSizetimes its rows, must equalwidthandheightunderdisplayingame.yaml, so change both together;LoadDatarefuses a map that does not match. - Waves and enemies: each wave in
data/level.yamlis groups that spawn side by side, each acountof one enemy everyintervalafter adelay, and abonuspaid when the wave is cleared. Enemies havehealth,speedand an optionalarmorstat, arewardand how many lives aleakcosts. - Towers: in
data/towers.yaml, a tower needsdamage,rangeandcooldownstats;splashmakes its hits damage everything around the target, andslowwithchillslows what it hits. Each upgrade is a cost and stat modifiers (add,muloroverride). Its projectile is thepatternit names indata/projectiles.yaml, which fires one shot. - Art:
art.gopaints every tile and projectile. ReplacemakeAtlaswithsprite.LoadAtlasand an atlas of your own to use image files. - Rules:
world.goholds them all, so a new kind of tower behavior is a new stat read inWorld.hitorWorld.fire.
After changing a data file, run go tool nsgo test ./...: TestDataFilesAreCanonical checks the files load and are in the form the game writes, and the playthrough tests show whether winningPlan in plan_test.go still wins. Run go run github.com/nsgo-dev/game/cmd/game index to update game.index.yaml.