-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingleview.go
70 lines (58 loc) · 1.83 KB
/
singleview.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
package gecs
import "fmt"
// SingleView Single component view specialization.
type SingleView struct {
Pool *Storage
}
// NewSingleView new single view
func newSingleView(pool *Storage) *SingleView {
return &SingleView{
Pool: pool,
}
}
// Size of the entities that has the given component
func (v *SingleView) Size() int {
return v.Pool.Size()
}
// Empty check if the entities is empty
func (v *SingleView) Empty() bool {
return v.Pool.Empty()
}
// Raw return the array of components
func (v *SingleView) Raw() []interface{} {
return v.Pool.Raw()
}
// Data direct access to the list of entities
func (v *SingleView) Data() []EntityID {
return v.Pool.Data()
}
// Begin an iterator to the first entity that has the given component
func (v *SingleView) Begin() *_EntityIDIterator {
return v.Pool.SparseSet.Begin()
}
// End an iterator to the entity following the last entity that has the given component
func (v *SingleView) End() *_EntityIDIterator {
return v.Pool.SparseSet.End()
}
// Contains check if a view contains en entity
func (v *SingleView) Contains(entity EntityID) bool {
return v.Pool.Has(entity)
}
// Get the component data assigned the entity
func (v *SingleView) Get(entity EntityID) interface{} {
if !v.Contains(entity) {
panic(fmt.Sprintf("entity(%v)should have the component", entity))
}
return v.Pool.Get(entity)
}
// Each Iterates entities and components and applies the given function object to them
// func (v *SingleView) Each(fn func(entity EntityID, data interface{})) {
func (v *SingleView) Each(fn func(entity EntityID, datas map[ComponentID]interface{})) {
comIter := v.Pool.Iterator()
entityIter := v.Pool.SparseSet.Iterator()
Each(entityIter, func(data interface{}) {
// fn(data.(EntityID), comIter.Data())
fn(data.(EntityID), map[ComponentID]interface{}{v.Pool.com: comIter.Data()})
comIter.Next()
})
}