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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package main
import (
"image/color"
"age_of_empires/ecs"
"age_of_empires/physics"
"age_of_empires/selection"
"github.com/hajimehoshi/ebiten/v2"
)
type Components struct {
Position physics.Point
RelBounds physics.Rectangle
Image *ebiten.Image
Selection selection.Selection
Move physics.Move
Order OrderKind
ResourceGatherer ResourceGatherer
ResourceSource ResourceSource
ResourceStorage ResourceStorage
Spawn Spawn
}
type EntityBuilder struct {
mask ecs.Mask
components Components
}
func NewEntityBuilder() EntityBuilder {
return EntityBuilder{
components: Components{},
}
}
func (b EntityBuilder) WithPosition(position physics.Point) EntityBuilder {
b.components.Position = position
b.mask |= ecs.CM_Position
return b
}
func (b EntityBuilder) WithSolid(image *ebiten.Image) EntityBuilder {
b.components.Image = image
b.mask |= ecs.CM_Image
b.components.RelBounds = image.Bounds()
b.mask |= ecs.CM_RelBounds
return b
}
var Red = color.RGBA{0xff, 0x00, 0x00, 0xff}
func (b EntityBuilder) WithSelection(haloKind string, priority selection.Priority) EntityBuilder {
b.components.Selection = selection.Selection{
IsSelected: false,
Halo: nil,
Priority: priority,
}
b.mask |= ecs.CM_Selection
switch haloKind {
case "round":
b.components.Selection.Halo = NewStrokeCircleImage(110, selection.HaloWidth, Red)
case "square":
b.components.Selection.Halo = NewStrokeRectangleImage(physics.Point{X: 110, Y: 110}, selection.HaloWidth, Red)
}
return b
}
func (b EntityBuilder) WithResourceStorage() EntityBuilder {
b.components.ResourceStorage = ResourceStorage{}
b.mask |= ecs.CM_ResourceStorage
return b
}
func (b EntityBuilder) WithResourceSource(amount int) EntityBuilder {
b.components.ResourceSource = ResourceSource{Remaining: amount}
b.mask |= ecs.CM_ResourceSource
return b
}
func (b EntityBuilder) WithSpawn(spawn Spawn) EntityBuilder {
b.components.Spawn = spawn
b.mask |= ecs.CM_Spawn
return b
}
func (b EntityBuilder) WithMove() EntityBuilder {
b.components.Move = physics.Move{IsActive: false}
b.mask |= ecs.CM_Move
return b
}
func (b EntityBuilder) WithOrder() EntityBuilder {
b.components.Order = OrderKindNone
b.mask |= ecs.CM_Order
return b
}
func (b EntityBuilder) WithResourceGatherer(maxCapacity int) EntityBuilder {
b.components.ResourceGatherer = ResourceGatherer{MaxCapacity: maxCapacity}
b.mask |= ecs.CM_ResourceGatherer
return b
}
func (b EntityBuilder) Build() (Components, ecs.Mask) {
return b.components, b.mask
}
|