-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
47 lines (38 loc) · 876 Bytes
/
entity.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
package ecs
import (
"encoding/json"
"sync/atomic"
)
type Entity interface {
Id() uint64
GetComponents() []any
}
type BaseEntity struct {
id uint64
components []any
}
// MarshalJSON to expose the id without exporting it, as it may not be altered!
func (this *BaseEntity) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"id": this.id,
})
}
func NewEntity(counter *atomic.Uint64) (this *BaseEntity) {
this = new(BaseEntity)
this.id = counter.Add(1)
return this
}
func (this *BaseEntity) AddComponent(component any) {
this.components = append(this.components, component)
}
func (this *BaseEntity) AddComponents(component ...any) {
for _, c := range component {
this.AddComponent(c)
}
}
func (this *BaseEntity) GetComponents() []any {
return this.components
}
func (this *BaseEntity) Id() uint64 {
return this.id
}