-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.go
145 lines (122 loc) · 3.46 KB
/
storage.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
133
134
135
136
137
138
139
140
141
142
143
144
145
package gecs
// Storage 单个类型组件绑定实体的稀疏数组
type Storage struct {
com ComponentID
// comID ComponentID
*SparseSet
/*
instances 中存储的组件的数据的排序 跟 实体的排序是完全一致的
也就是说是跟 SparseSet.direct 中的排序是完全一致的
*/
instances []interface{}
}
// NewStorage 构造组件-实体的稀疏数组
func NewStorage(com ComponentID) *Storage {
return &Storage{
com: com,
SparseSet: newSparseSet(),
instances: make([]interface{}, 0),
}
}
// // Com return data type the storage binded
// func (s *Storage) Com() ComponentID {
// return s.com
// }
// Reserve Increases the capacity of a storage
func (s *Storage) Reserve(cap int) {
s.SparseSet.Reserve(cap)
s.instances = extendInterfaceSlice(s.instances, cap)
}
// Raw Direct access to the array of objects
func (s *Storage) Raw() []interface{} {
return s.instances
}
// Get returns the object associated with en entity
func (s *Storage) Get(entity EntityID) interface{} {
return s.instances[s.SparseSet.Index(entity)]
}
// TryGet returns the object associated with an entity. maybe nil.
func (s *Storage) TryGet(entity EntityID) interface{} {
if s.Has(entity) {
return s.instances[s.SparseSet.Index(entity)]
}
return nil
}
// Emplace assigns an entity to a storage and constructs its object.
// This version accept both types that can be constructed in place directly
// and types like aggregates that do not work well with a placement new as
// performed usually under the hood during an _emplace back_.
func (s *Storage) Emplace(entity EntityID, data interface{}) interface{} {
s.instances = append(s.instances, data)
s.SparseSet.Emplace(entity)
return data
}
// Destroy remove该实体绑定的组件数据,并且从 实体稀疏数组中remove掉该实体
func (s *Storage) Destroy(entity EntityID) {
back := s.instances[len(s.instances)-1]
s.instances[s.SparseSet.Index(entity)] = back
s.instances = s.instances[:len(s.instances)-1]
s.SparseSet.Destroy(entity)
}
// Replace 替换数据
func (s *Storage) Replace(entity EntityID, data interface{}) interface{} {
s.instances[s.SparseSet.Index(entity)] = data
return data
}
// Reset 组件稀疏数组,以及实体稀疏数组
func (s *Storage) Reset() {
s.SparseSet.Reset()
s.instances = s.instances[0:0]
}
// Begin 组件的迭代器
func (s *Storage) Begin() *ComponentIterator {
return &ComponentIterator{
datas: s.instances,
pos: len(s.instances),
}
}
// End 组件的迭代器
func (s *Storage) End() *ComponentIterator {
return &ComponentIterator{
datas: s.instances,
pos: 0,
}
}
// Iterator 组件的迭代器
func (s *Storage) Iterator() *ComponentIterator {
return s.Begin()
}
// ComponentIterator for interface{} type
type ComponentIterator struct {
datas []interface{}
pos int
}
// Equal other
func (i ComponentIterator) Equal(other IIterator) bool {
tOther := other.(*ComponentIterator)
return i.pos == tOther.pos && &i.datas[0] == &tOther.datas[0]
}
// Next iterator
func (i *ComponentIterator) Next() IIterator {
i.pos--
return IIterator(i)
}
// Prev iterator
func (i *ComponentIterator) Prev() IIterator {
i.pos++
return IIterator(i)
}
// Begin iterator
func (i ComponentIterator) Begin() IIterator {
i.pos = len(i.datas)
return IIterator(&i)
}
// End iterator
func (i ComponentIterator) End() IIterator {
i.pos = 0
return IIterator(&i)
}
// Data with the iterator value
func (i ComponentIterator) Data() interface{} {
return i.datas[i.pos-1]
}