-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
48 lines (42 loc) · 1.35 KB
/
lib.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
package components
import (
"reflect"
ecs "github.com/x-hgg-x/goecs/v2"
)
// EngineComponents contains references to all engine components
type EngineComponents struct {
SpriteRender *ecs.SliceComponent
Transform *ecs.SliceComponent
Text *ecs.SliceComponent
UITransform *ecs.SliceComponent
}
// Components contains engine and game components
type Components struct {
Engine *EngineComponents
Game interface{}
}
// InitComponents initializes components
func InitComponents(manager *ecs.Manager, gameComponents interface{}) *Components {
components := &Components{Engine: &EngineComponents{}, Game: gameComponents}
initFields(manager, components.Engine)
initFields(manager, components.Game)
return components
}
func initFields(manager *ecs.Manager, components interface{}) {
if components != nil {
v := reflect.ValueOf(components).Elem()
for iField := 0; iField < v.NumField(); iField++ {
component := v.Field(iField)
switch component.Interface().(type) {
case *ecs.NullComponent:
component.Set(reflect.ValueOf(manager.NewNullComponent()))
case *ecs.SliceComponent:
component.Set(reflect.ValueOf(manager.NewSliceComponent()))
case *ecs.DenseSliceComponent:
component.Set(reflect.ValueOf(manager.NewDenseSliceComponent()))
case *ecs.MapComponent:
component.Set(reflect.ValueOf(manager.NewMapComponent()))
}
}
}
}