-
Notifications
You must be signed in to change notification settings - Fork 9
/
component_meta.go
130 lines (113 loc) · 3.08 KB
/
component_meta.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
package ecs
import (
"fmt"
"reflect"
)
func GetComponentMeta[T ComponentObject](world IWorld) *ComponentMetaInfo {
return world.getComponentMetaInfoByType(TypeOf[T]())
}
type ComponentMetaInfo struct {
it uint16
componentType ComponentType
o1 uint8
typ reflect.Type
}
type componentMeta struct {
world *ecsWorld
seq uint16
types map[reflect.Type]uint16
infos *SparseArray[uint16, ComponentMetaInfo]
disposable map[reflect.Type]uint16
free map[reflect.Type]uint16
}
func NewComponentMeta(world *ecsWorld) *componentMeta {
return &componentMeta{
world: world,
seq: 0,
types: make(map[reflect.Type]uint16),
infos: NewSparseArray[uint16, ComponentMetaInfo](),
disposable: map[reflect.Type]uint16{},
free: map[reflect.Type]uint16{},
}
}
func (c *componentMeta) CreateComponentMetaInfo(typ reflect.Type, ct ComponentType) *ComponentMetaInfo {
c.world.checkMainThread()
c.seq++
info := &ComponentMetaInfo{}
info.it = c.seq
info.componentType = ct
info.typ = typ
c.types[typ] = info.it
info = c.infos.Add(info.it, info)
if ct&ComponentTypeDisposableMask > 0 {
c.disposable[typ] = info.it
}
if ct&ComponentTypeFreeMask > 0 {
c.free[typ] = info.it
}
return info
}
func (c *componentMeta) GetDisposableTypes() map[reflect.Type]uint16 {
return c.disposable
}
func (c *componentMeta) GetFreeTypes() map[reflect.Type]uint16 {
return c.free
}
func (c *componentMeta) Exist(typ reflect.Type) bool {
_, ok := c.types[typ]
return ok
}
func (c *componentMeta) GetOrCreateComponentMetaInfo(com IComponent) *ComponentMetaInfo {
it, ok := c.types[com.Type()]
if !ok {
it = c.CreateComponentMetaInfo(com.Type(), com.getComponentType()).it
}
return c.infos.Get(it)
}
func (c *componentMeta) GetComponentMetaInfoByIntType(it uint16) *ComponentMetaInfo {
info := c.infos.Get(it)
if info == nil {
panic("must register component first")
}
return info
}
func (c *componentMeta) GetComponentMetaInfoByType(typ reflect.Type) *ComponentMetaInfo {
it, ok := c.types[typ]
if !ok {
panic(fmt.Sprintf("must register component %s first", typ.String()))
}
return c.infos.Get(it)
}
func (c *componentMeta) ComponentMetaInfoPrint() {
fn := func(m map[reflect.Type]uint16) {
total := len(m)
count := 0
prefix := "│ ├─"
prefix2 := "│ └─"
str := ""
for typ, _ := range m {
str += " " + typ.Name()
count++
if count%5 == 0 {
if count == total {
Log.Infof("%s%s", prefix2, str)
} else {
Log.Infof("%s%s", prefix, str)
}
str = ""
}
}
if str != "" {
Log.Infof("%s%s", prefix2, str)
str = ""
}
}
Log.Infof("┌──────────────── # Component Info # ─────────────────")
Log.Infof("├─ Total: %d", len(c.types))
fn(c.types)
Log.Infof("├─ Disposable: %d", len(c.disposable))
fn(c.disposable)
Log.Infof("├─ Free: %d", len(c.free))
fn(c.free)
Log.Infof("└────────────── # Component Info End # ───────────────")
}