-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.go
77 lines (63 loc) · 1.24 KB
/
ecs.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
package main
import "fmt"
import "mk/Apollo/events"
/* Entity Component System */
type Entity struct {
id uint64
data map[string]Data
}
func (e *Entity) String() string {
return fmt.Sprintf("Entity<%d>", e.id)
}
func (e *Entity) GetType() DataType {
return EntityType
}
func (e *Entity) Equals(other Data) bool {
switch t := other.(type) {
case *Entity:
return t.id == e.id
}
return false
}
func (e *Entity) Set(property string, value Data) {
if old, ok := e.data[property]; ok {
e.data[property] = value
// do sth with old
fmt.Printf(old.String())
} else {
e.data[property] = value
}
}
var shutdown = false
var newEntities = make(chan *Entity)
var ids = make(chan uint64)
func ECS_init() {
go generate_entity_ids(newEntities, ids)
}
func ECS_shutdown() {
shutdown = true
close(newEntities)
}
func NewEntity() *Entity {
ent := new(Entity)
newEntities <- ent
ent.id = <-ids
return ent
}
func (e *Entity) EventChannel() events.EventChannel {
// TODO: define global event channel for entity
return nil
}
func (e *Entity) EventSourceID() uint64 {
return e.id
}
func generate_entity_ids(entities chan *Entity, ids chan uint64) {
i := uint64(1)
for e := range entities {
if e.id == 0 {
ids <- i
}
i++
}
close(ids)
}