aboutsummaryrefslogtreecommitdiff
path: root/engine/spawn.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-06-06 20:59:12 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-06-06 20:59:12 +0200
commitd9a54f1319cc9dcc2861cecc9edfde14cf039c5b (patch)
tree74df791d6eda0ffe6c09cb32e5ffd76c4849cbac /engine/spawn.go
parent18592fd9f520a33ab307e496b995c1a7bf284893 (diff)
Add examples catalog with subcommand dispatch
- Extract game engine from package main to engine/ library package - Create examples/ package with registry, Entry type, and BuilderDeps - Register default world as an example (moved from main()) - Add combat example: two teams of units facing each other - Rewrite main.go as thin dispatch: go run . example [name] - Update README with examples catalog one-liner
Diffstat (limited to 'engine/spawn.go')
-rw-r--r--engine/spawn.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/engine/spawn.go b/engine/spawn.go
new file mode 100644
index 0000000..fc32a84
--- /dev/null
+++ b/engine/spawn.go
@@ -0,0 +1,53 @@
+package engine
+
+import (
+ "log/slog"
+ "time"
+
+ "age_of_empires/ecs"
+ "age_of_empires/monad"
+ "age_of_empires/physics"
+)
+
+type SpawnRequest struct {
+ Start time.Time
+}
+
+type Spawn struct {
+ UnitResourceCost int
+ UnitSpawnDuration time.Duration
+ Requests []SpawnRequest
+ SpawnTarget monad.Option[physics.Point]
+}
+
+func NewSpawn(unitResourceCost int, unitSpawnDuration time.Duration) Spawn {
+ return Spawn{
+ UnitResourceCost: unitResourceCost,
+ UnitSpawnDuration: unitSpawnDuration,
+ Requests: make([]SpawnRequest, 0),
+ }
+}
+
+func UpdateSpawnSystem(g *Game, now time.Time, masks []ecs.Mask, spawns []Spawn, positions []physics.Point) {
+ required := ecs.CM_Position | ecs.CM_Spawn
+ for i, mask := range masks {
+ if mask&required == required {
+ spawn := &spawns[i]
+ position := positions[i]
+ if len(spawn.Requests) == 0 || now.Sub(spawn.Requests[0].Start) < time.Duration(spawn.UnitSpawnDuration) {
+ continue
+ }
+ spawnPosition, _ := physics.Closest(position, physics.AdjacentPoints(position), g.GetMoveMap())
+ unit := g.Append(g.UnitBuilder.WithPosition(spawnPosition).Build())
+ slog.Info("Spawned unit")
+ if spawn.SpawnTarget.Ok {
+ slog.Info("Unit has spawn target")
+ MainAction(g, unit, spawn.SpawnTarget.Value, g.GetMoveMap())
+ }
+ spawn.Requests = spawn.Requests[1:]
+ if len(spawn.Requests) > 0 {
+ spawn.Requests[0].Start = now
+ }
+ }
+ }
+}