-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
96 lines (76 loc) · 1.7 KB
/
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
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
package ospokemon
import (
"time"
"github.com/ospokemon/ospokemon/event"
"github.com/ospokemon/ospokemon/space"
"taylz.io/types"
)
const PARTentity = "entity"
type Entity struct {
Id uint
UniverseId uint
space.Shape
Parts
}
type Entities map[uint]*Entity
func MakeEntity() *Entity {
entity := &Entity{
Shape: &space.Rect{},
Parts: make(Parts),
}
return entity
}
func (e *Entity) SetClass(c *Class) {
r := e.Shape.(*space.Rect)
r.Dimension.DX = c.Dimension.DX
r.Dimension.DY = c.Dimension.DY
e.AddPart(BuildImaging(c.Animations))
}
func (e *Entity) Part() string {
return PARTentity
}
func (parts Parts) GetEntity() *Entity {
entity, _ := parts[PARTentity].(*Entity)
return entity
}
func (e *Entity) Update(u *Universe, d time.Duration) {
for _, part := range e.Parts {
if updater, ok := part.(Updater); ok {
updater.Update(u, e, d)
}
}
}
func (e *Entity) Move(vector space.Vector, universe *Universe) {
e.Shape = e.Shape.Move(vector)
event.Fire(event.Movement, e, vector)
if vector.Length() < 1 {
return
}
for entityId, entity := range universe.Entities {
if entity == nil {
continue
}
if e.Id == entityId {
continue
}
if space.DistanceShapeShape(e.Shape, entity.Shape) < 1 {
event.Fire(event.Collision, e, entity, universe, vector)
}
}
}
func (entity *Entity) Json() types.Dict {
json := types.Dict{
"id": entity.Id,
"shape": entity.Shape.Snapshot(),
}
if imaging := entity.GetImaging(); imaging != nil {
json["image"] = imaging.Image
}
if player := entity.GetPlayer(); player != nil {
json["player"] = player.Json()
}
if chatmessage := entity.GetChatMessage(); chatmessage != nil {
json["chat"] = chatmessage.Message
}
return json
}