@@ -20,45 +20,40 @@ type Entry struct {
20
20
}
21
21
22
22
// Get returns the component from the entry
23
- func Get [T any ](e * Entry , ctype component.IComponentType ) * T {
24
- return (* T )(e .Component (ctype ))
23
+ func Get [T any ](e * Entry , c component.IComponentType ) * T {
24
+ return (* T )(e .Component (c ))
25
25
}
26
26
27
27
// GetComponents uses reflection to convert the unsafe.Pointers of an entry into its component data instances.
28
28
// Note that this is likely to be slow and should not be used in hot paths or if not necessary.
29
29
func GetComponents (e * Entry ) []any {
30
- archetypeIdx := e .loc .Archetype
31
- s := e .World .StorageAccessor ().Archetypes [archetypeIdx ]
32
- cs := s .ComponentTypes ()
33
30
var instances []any
34
- for _ , ctyp := range cs {
35
- instancePtr := e .Component (ctyp )
36
- componentType := ctyp .Typ ()
37
- val := reflect .NewAt (componentType , instancePtr )
38
- valInstance := reflect .Indirect (val ).Interface ()
39
- instances = append (instances , valInstance )
31
+ for _ , c := range e .World .StorageAccessor ().Archetypes [e .loc .Archetype ].ComponentTypes () {
32
+ instances = append (instances , reflect .Indirect (
33
+ reflect .NewAt (c .Typ (), e .Component (c ))).Interface (),
34
+ )
40
35
}
41
36
return instances
42
37
}
43
38
44
39
// Add adds the component to the entry.
45
- func Add [T any ](e * Entry , ctype component.IComponentType , component * T ) {
46
- e .AddComponent (ctype , unsafe .Pointer (component ))
40
+ func Add [T any ](e * Entry , c component.IComponentType , component * T ) {
41
+ e .AddComponent (c , unsafe .Pointer (component ))
47
42
}
48
43
49
44
// Set sets the comopnent of the entry.
50
- func Set [T any ](e * Entry , ctype component.IComponentType , component * T ) {
51
- e .SetComponent (ctype , unsafe .Pointer (component ))
45
+ func Set [T any ](e * Entry , c component.IComponentType , component * T ) {
46
+ e .SetComponent (c , unsafe .Pointer (component ))
52
47
}
53
48
54
49
// SetValue sets the value of the component.
55
- func SetValue [T any ](e * Entry , ctype component.IComponentType , value T ) {
56
- * Get [T ](e , ctype ) = value
50
+ func SetValue [T any ](e * Entry , c component.IComponentType , value T ) {
51
+ * Get [T ](e , c ) = value
57
52
}
58
53
59
54
// GetValue gets the value of the component.
60
- func GetValue [T any ](e * Entry , ctype component.IComponentType ) T {
61
- return * Get [T ](e , ctype )
55
+ func GetValue [T any ](e * Entry , c component.IComponentType ) T {
56
+ return * Get [T ](e , c )
62
57
}
63
58
64
59
// Remove removes the component from the entry.
@@ -85,60 +80,50 @@ func (e *Entry) Entity() Entity {
85
80
}
86
81
87
82
// Component returns the component.
88
- func (e * Entry ) Component (ctype component.IComponentType ) unsafe.Pointer {
89
- c := e .loc .Component
90
- a := e .loc .Archetype
91
- return e .World .components .Storage (ctype ).Component (a , c )
83
+ func (e * Entry ) Component (c component.IComponentType ) unsafe.Pointer {
84
+ return e .World .components .Storage (c ).Component (e .loc .Archetype , e .loc .Component )
92
85
}
93
86
94
87
// SetComponent sets the component.
95
- func (e * Entry ) SetComponent (ctype component.IComponentType , component unsafe.Pointer ) {
96
- c := e .loc .Component
97
- a := e .loc .Archetype
98
- e .World .components .Storage (ctype ).SetComponent (a , c , component )
88
+ func (e * Entry ) SetComponent (c component.IComponentType , component unsafe.Pointer ) {
89
+ e .World .components .Storage (c ).SetComponent (e .loc .Archetype , e .loc .Component , component )
99
90
}
100
91
101
92
// AddComponent adds the component to the entity.
102
- func (e * Entry ) AddComponent (ctype component.IComponentType , components ... unsafe.Pointer ) {
93
+ func (e * Entry ) AddComponent (c component.IComponentType , components ... unsafe.Pointer ) {
103
94
if len (components ) > 1 {
104
95
panic ("AddComponent: component argument must be a single value" )
105
96
}
106
- if ! e .HasComponent (ctype ) {
107
- c := e .loc .Component
108
- a := e .loc .Archetype
109
-
110
- base_layout := e .World .archetypes [a ].Layout ().Components ()
111
- target_arc := e .World .getArchetypeForComponents (append (base_layout , ctype ))
112
- e .World .TransferArchetype (a , target_arc , c )
113
-
97
+ if ! e .HasComponent (c ) {
98
+ archetypeIndex := e .loc .Archetype
99
+ targetArchetype := e .World .getArchetypeForComponents (
100
+ append (e .World .archetypes [archetypeIndex ].Layout ().Components (), c ),
101
+ )
102
+ e .World .TransferArchetype (archetypeIndex , targetArchetype , e .loc .Component )
114
103
e .loc = e .World .Entry (e .entity ).loc
115
104
}
116
105
if len (components ) == 1 {
117
- e .SetComponent (ctype , components [0 ])
106
+ e .SetComponent (c , components [0 ])
118
107
}
119
108
}
120
109
121
110
// RemoveComponent removes the component from the entity.
122
- func (e * Entry ) RemoveComponent (ctype component.IComponentType ) {
123
- if ! e .Archetype ().Layout ().HasComponent (ctype ) {
111
+ func (e * Entry ) RemoveComponent (c component.IComponentType ) {
112
+ if ! e .Archetype ().Layout ().HasComponent (c ) {
124
113
return
125
114
}
126
115
127
- c := e .loc .Component
128
- a := e .loc .Archetype
129
-
130
- base_layout := e .World .archetypes [a ].Layout ().Components ()
131
- target_layout := make ([]component.IComponentType , 0 , len (base_layout )- 1 )
132
- for _ , c2 := range base_layout {
133
- if c2 == ctype {
116
+ baseLayout := e .World .archetypes [e .loc .Archetype ].Layout ().Components ()
117
+ targetLayout := make ([]component.IComponentType , 0 , len (baseLayout )- 1 )
118
+ for _ , c2 := range baseLayout {
119
+ if c2 == c {
134
120
continue
135
121
}
136
- target_layout = append (target_layout , c2 )
122
+ targetLayout = append (targetLayout , c2 )
137
123
}
138
124
139
- target_arc := e .World .getArchetypeForComponents (target_layout )
140
- e .World .TransferArchetype (e .loc .Archetype , target_arc , c )
141
-
125
+ targetArchetype := e .World .getArchetypeForComponents (targetLayout )
126
+ e .World .TransferArchetype (e .loc .Archetype , targetArchetype , e .loc .Component )
142
127
e .loc = e .World .Entry (e .entity ).loc
143
128
}
144
129
@@ -159,8 +144,8 @@ func (e *Entry) Archetype() *storage.Archetype {
159
144
}
160
145
161
146
// HasComponent returns true if the entity has the given component type.
162
- func (e * Entry ) HasComponent (componentType component.IComponentType ) bool {
163
- return e .Archetype ().Layout ().HasComponent (componentType )
147
+ func (e * Entry ) HasComponent (c component.IComponentType ) bool {
148
+ return e .Archetype ().Layout ().HasComponent (c )
164
149
}
165
150
166
151
func (e * Entry ) String () string {
0 commit comments