forked from zllangct/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.go
More file actions
107 lines (89 loc) · 2.04 KB
/
Copy pathcomponent.go
File metadata and controls
107 lines (89 loc) · 2.04 KB
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
package ecs
import (
"reflect"
"sync"
"unsafe"
)
type IComponent interface {
Owner() *Entity
Type() reflect.Type
ID() int64
Instance() IComponent
Template() IComponentTemplate
}
type IComponentTemplate interface {
SetOwner(owner *Entity) IComponentTemplate
SetID(id int64) IComponentTemplate
ComponentType() reflect.Type
AddToCollection(collection interface{}) IComponent
}
type Component[T any] struct {
lock sync.Mutex
owner *Entity
id int64
realType reflect.Type
operation map[string]func()[]interface{}
}
func (c Component[T]) SetOwner(entity *Entity) IComponentTemplate {
c.owner = entity
return c
}
func (c Component[T]) SetID(id int64) IComponentTemplate {
c.id = id
return c
}
func (c Component[T]) ComponentType() reflect.Type {
return reflect.TypeOf(c)
}
func (c Component[T]) AddToCollection(collection interface{}) IComponent {
cc, ok := collection.(*Collection[T])
if !ok {
return nil
}
_, ins := cc.Add((&c).Ins())
var com IComponent
(*iface)(unsafe.Pointer(&com)).data = unsafe.Pointer(ins)
return com
}
func (c *Component[T]) setOwner(entity *Entity) {
c.owner = entity
}
func (c *Component[T]) setID(id int64){
c.id = id
}
func (c *Component[T]) ID() int64{
return c.id
}
func (c *Component[T]) Ins() *T {
return (*T)(unsafe.Pointer(c))
}
func (c *Component[T]) Instance() IComponent {
var com IComponent
(*iface)(unsafe.Pointer(&com)).data = unsafe.Pointer(c)
return com
}
func (c *Component[T]) Template() IComponentTemplate {
return (*c)
}
func (c *Component[T]) Owner() *Entity {
return c.owner
}
func (c *Component[T]) Type() reflect.Type {
if c.realType == nil {
c.realType = reflect.TypeOf(*(new(T)))
}
return c.realType
}
//func (c *Component[T]) AddToCollection(collection interface{}) IComponent {
// cc, ok := collection.(*Collection[T])
// if !ok {
// return nil
// }
// _, ins := cc.Add(c.Ins())
// var com IComponent
// (*iface)(unsafe.Pointer(&com)).data = unsafe.Pointer(ins)
// return com
//}
func (c *Component[T]) NewCollection() interface{} {
return NewCollection[T]()
}