-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
132 lines (107 loc) · 3.04 KB
/
group.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package peano
type Group interface {
Lenght() int
GetEntities() []*Entity
OnEntityAdded(func(Group, *Entity, int, Component))
OnEntityRemoved(func(Group, *Entity, int, Component))
OnEntityUpdate(func(Group, *Entity, int, Component))
has(entity *Entity) bool
updateEntity(entity *Entity, index int, component Component)
handleEntity(entity *Entity, index int, component Component)
handleEntitySilently(entity *Entity)
addEntitySilently(entity *Entity)
addEntity(entity *Entity, index int, component Component)
removeEntitySilently(entity *Entity)
removeEntity(entity *Entity, index int, component Component)
}
type group struct {
matcher Matcher
entities map[int]*Entity
entitiesCache []*Entity
entitieSingleCache *Entity
onEntityAdded onGroup
onEntityRemoved onGroup
onEntityUpdate onGroup
}
func newGroup(matcher Matcher) Group {
return &group{
matcher: matcher,
entities: make(map[int]*Entity),
}
}
func (g *group) Lenght() int {
return len(g.entities)
}
func (g *group) GetEntities() []*Entity {
if g.entitiesCache == nil {
g.entitiesCache = make([]*Entity, 0, len(g.entities))
for _, v := range g.entities {
g.entitiesCache = append(g.entitiesCache, v)
}
}
return g.entitiesCache
}
func (g *group) GetSingleEntity() *Entity {
if g.entitieSingleCache == nil {
count := len(g.entities)
if count == 1 {
for _, v := range g.entities {
g.entitieSingleCache = v
}
} else {
return nil
}
}
return g.entitieSingleCache
}
func (g *group) OnEntityAdded(action func(Group, *Entity, int, Component)) {
g.onEntityAdded.On(action)
}
func (g *group) OnEntityRemoved(action func(Group, *Entity, int, Component)) {
g.onEntityRemoved.On(action)
}
func (g *group) OnEntityUpdate(action func(Group, *Entity, int, Component)) {
g.onEntityUpdate.On(action)
}
func (g *group) has(entity *Entity) bool {
return g.entities[entity.ID()] != nil
}
func (g *group) updateEntity(entity *Entity, index int, component Component) {
if g.onEntityUpdate != nil {
g.onEntityUpdate.Execute(g, entity, index, component)
}
}
func (g *group) handleEntity(entity *Entity, index int, component Component) {
if g.matcher.matches(entity) {
g.addEntity(entity, index, component)
} else {
g.removeEntity(entity, index, component)
}
}
func (g *group) handleEntitySilently(entity *Entity) {
if g.matcher.matches(entity) {
g.addEntitySilently(entity)
}
}
func (g *group) addEntitySilently(entity *Entity) {
g.entities[entity.ID()] = entity
if g.entitiesCache != nil {
g.entitiesCache = append(g.entitiesCache, entity)
}
}
func (g *group) addEntity(entity *Entity, index int, component Component) {
g.addEntitySilently(entity)
if g.onEntityAdded != nil {
g.onEntityAdded.Execute(g, entity, index, component)
}
}
func (g *group) removeEntitySilently(entity *Entity) {
delete(g.entities, entity.ID())
g.entitiesCache = nil
}
func (g *group) removeEntity(entity *Entity, index int, component Component) {
g.removeEntitySilently(entity)
if g.onEntityRemoved != nil {
g.onEntityRemoved.Execute(g, entity, index, component)
}
}