/
entity_manager.go
166 lines (140 loc) · 3.98 KB
/
entity_manager.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
. "github.com/stojg/pants/physics"
"github.com/stojg/pants/structs"
. "github.com/stojg/pants/vector"
"github.com/stojg/tree"
"math"
)
func NewEntityManager() *EntityManager {
return &EntityManager{
entities: make(map[uint64]*Entity),
controllers: make(map[uint64]Controller),
physics: make(map[uint64]*Physics),
boundingBoxes: make(map[uint64]*tree.Rectangle),
updated: make(map[uint64]bool),
forceRegistry: &PhysicsForceRegistry{},
collisionManager: NewCollisionManager(),
grid: structs.NewGrid(1000.0, 1000.0, 20.0),
}
}
type EntityManager struct {
lastEntityID uint64
entities map[uint64]*Entity
controllers map[uint64]Controller
physics map[uint64]*Physics
boundingBoxes map[uint64]*tree.Rectangle
updated map[uint64]bool
forceRegistry *PhysicsForceRegistry
collisionManager *CollisionManager
grid *structs.Grid
}
var gravity *StaticForce
func init() {
gravity = &StaticForce{}
gravity.SetForce(&Vec2{0, 10})
}
func (em *EntityManager) NewEntity(x, y float64, entType EntityType) uint64 {
em.lastEntityID += 1
// copy the properties from the data file
props := &EntityProperty{}
*props = *entityProperties[entType]
entity := &Entity{
Id: em.lastEntityID,
Type: entType,
inputs: make([]*InputRequest, 0),
Properties: props,
}
em.entities[entity.Id] = entity
data := structs.NewData()
data.Position.X = x
data.Position.Y = y
data.Orientation = 3.14 * 2
data.InvMass = props.invMass
data.Width = props.Width
data.Height = props.Height
em.physics[entity.Id] = NewPhysics(data)
em.physics[entity.Id].Data.Damping = 0.99
// em.forceRegistry.Add(em.physics[entity.Id], gravity)
if props.ai {
em.controllers[entity.Id] = NewBasicAI(entity.Id)
}
em.updated[entity.Id] = true
return entity.Id
}
func (em *EntityManager) Remove(id uint64) {
em.entities[id].Dead = true
delete(em.physics, id)
delete(em.controllers, id)
em.updated[id] = true
}
func (em *EntityManager) Update(w *World, duration float64) {
em.forceRegistry.Update(duration)
for id, ctrl := range em.controllers {
ctrl.Update(id)
}
for id, p := range em.physics {
data := p.Data
p.Integrate(data, duration)
// update collision volume
if data.Width > data.Height {
p.CollisionGeometry.(*Circle).Radius = data.Width / 2
} else {
p.CollisionGeometry.(*Circle).Radius = data.Height / 2
}
moved := false
if !data.Position.Equals(p.PrevData.Position) {
moved = true
}
if math.Abs(data.Orientation-p.PrevData.Orientation) < EPSILON {
moved = true
}
p.PrevData.Copy(data)
if moved {
em.updated[id] = true
}
}
em.grid.Clear()
collisions := em.collisionManager.DetectCollisions(em.physics, em.grid)
for _, collision := range collisions {
evt := &CollisionEvent{}
evt.a, evt.b = collision.Pair()
events.ScheduleEvent(evt)
}
em.collisionManager.ResolveCollisions(collisions, duration)
}
func (em *EntityManager) Changed() []*EntityUpdate {
// @todo(stig): it is possible that this might be called before the em.sprite has been fully setup, ie a race condition
ids := make([]uint64, 0)
for k := range em.entities {
ids = append(ids, k)
}
return em.entityUpdates(ids)
}
func (em *EntityManager) entityUpdates(entityIds []uint64) []*EntityUpdate {
entities := make([]*EntityUpdate, 0, len(entityIds))
for _, id := range entityIds {
update := &EntityUpdate{
Id: id,
Type: em.entities[id].Type,
Properties: em.entities[id].Properties,
Dead: em.entities[id].Dead,
}
if physics, ok := em.physics[id]; ok {
update.Height = physics.Data.Height
update.Width = physics.Data.Width
update.X = physics.Data.Position.X
update.Y = physics.Data.Position.Y
update.Orientation = physics.Data.Orientation
}
entities = append(entities, update)
}
return entities
}
type CollisionEvent struct {
a uint64
b uint64
}
func (c *CollisionEvent) Code() EventCode {
return EVT_COLLISION
}