This repository was archived by the owner on Jul 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage.go
More file actions
73 lines (62 loc) · 1.78 KB
/
Copy pathstorage.go
File metadata and controls
73 lines (62 loc) · 1.78 KB
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
package ecs
func newStorage[Component any](capacity uint32) (s *Storage[Component]) {
return &Storage[Component]{
components: make([]Component, capacity),
b: newBitset(capacity),
}
}
func (s *Storage[Component]) EntityHasComponent(e Entity) bool {
return s.b.Get(e)
}
func (s *Storage[Component]) bits() *bitSet { return s.b }
type storage interface {
bits() *bitSet
clear(Entity) // zero out the component for this entity
}
// zero out the components for this entity
// does nothing if the entity is alive
func (s *Storage[Component]) clear(e Entity) {
s.bits().Clear(e)
var zero Component
s.components[e] = zero
}
// update the component of an entity. this does not check if the entity is alive
func (s *Storage[Component]) Update(e Entity, c Component) {
s.components[e] = c
}
// get a copy of a component
// this does not check if the entity is alive
func (s *Storage[Component]) Get(e Entity) Component {
return s.components[e]
}
// All entities that have this component
func (s *Storage[Component]) All() []Entity {
return s.b.ActiveIDs()
}
// All entities that have this component and the other components
func (s *Storage[Component]) And(others ...storage) []Entity {
bits := s.b.Clone()
defer bits.Release()
for _, s2 := range others {
bits.And(s2.bits())
}
return bits.ActiveIDs()
}
// All entities that have this component but not the other components
func (s *Storage[Component]) ButNot(others ...storage) []Entity {
bits := s.b.Clone()
defer bits.Release()
for _, s2 := range others {
bits.AndNot(s2.bits())
}
return bits.ActiveIDs()
}
// All entities that have either components
func (s *Storage[Component]) Or(others ...storage) []Entity {
bits := s.b.Clone()
defer bits.Release()
for _, s2 := range others {
bits.Or(s2.bits())
}
return bits.ActiveIDs()
}