1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
}
}
}
}
|