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
54
55
56
57
58
59
|
package main
import (
"log/slog"
"time"
"age_of_empires/ecs"
"age_of_empires/physics"
)
type spawnRequest struct {
start time.Time
}
type Spawn struct {
UnitResourceCost int
UnitSpawnDuration time.Duration
Requests []spawnRequest
SpawnTarget ecs.Component[physics.Point]
}
func NewSpawn(unitResourceCost int, unitSpawnDuration time.Duration) Spawn {
return Spawn{
UnitResourceCost: unitResourceCost,
UnitSpawnDuration: unitSpawnDuration,
Requests: make([]spawnRequest, 0),
}
}
func (spawn *Spawn) AddRequest(g *Game) {
if g.ResourceAmount < spawn.UnitResourceCost {
return
}
g.ResourceAmount -= spawn.UnitResourceCost
spawn.Requests = append(spawn.Requests, spawnRequest{start: g.Now()})
}
func UpdateSpawn(g *Game, spawn *ecs.Component[Spawn], position ecs.Component[physics.Point]) {
if !spawn.IsEnabled || !position.IsEnabled {
return
}
now := g.Now()
if len(spawn.Value.Requests) == 0 || now.Sub(spawn.Value.Requests[0].start) < time.Duration(spawn.Value.UnitSpawnDuration) {
return
}
spawnPosition, _ := physics.Closest(position.Value, physics.AdjacentPoints(position.Value), g.getMoveMap())
unit := g.UnitBuilder.Build()
unit.Position = ecs.C(spawnPosition)
g.Entities = append(g.Entities, &unit)
slog.Info("Spawned unit")
if spawn.Value.SpawnTarget.IsEnabled {
slog.Info("Unit has spawn target")
unit.MainAction(g, spawn.Value.SpawnTarget.Value, g.entityAt(spawn.Value.SpawnTarget.Value), g.getMoveMap())
}
spawn.Value.Requests = spawn.Value.Requests[1:]
if len(spawn.Value.Requests) > 0 {
spawn.Value.Requests[0].start = now
}
}
|