-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
248 lines (203 loc) · 5.64 KB
/
event.go
File metadata and controls
248 lines (203 loc) · 5.64 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package feather
import (
"unsafe"
"github.com/akmonengine/feather/actor"
"github.com/akmonengine/feather/constraint"
)
const (
TRIGGER_ENTER EventType = iota
COLLISION_ENTER
TRIGGER_STAY
COLLISION_STAY
TRIGGER_EXIT
COLLISION_EXIT
ON_SLEEP
ON_WAKE
)
type pairKey struct {
bodyA *actor.RigidBody
bodyB *actor.RigidBody
}
// makePairKey creates a normalized pair key with consistent ordering
func makePairKey(bodyA, bodyB *actor.RigidBody) pairKey {
ptrA := uintptr(unsafe.Pointer(bodyA))
ptrB := uintptr(unsafe.Pointer(bodyB))
if ptrB < ptrA {
bodyA, bodyB = bodyB, bodyA
}
return pairKey{bodyA: bodyA, bodyB: bodyB}
}
type EventType uint8
// Event interface - all events implement this
type Event interface {
Type() EventType
}
// Trigger events
type TriggerEnterEvent struct {
BodyA *actor.RigidBody
BodyB *actor.RigidBody
}
func (e TriggerEnterEvent) Type() EventType { return TRIGGER_ENTER }
type TriggerStayEvent struct {
BodyA *actor.RigidBody
BodyB *actor.RigidBody
}
func (e TriggerStayEvent) Type() EventType { return TRIGGER_STAY }
type TriggerExitEvent struct {
BodyA *actor.RigidBody
BodyB *actor.RigidBody
}
func (e TriggerExitEvent) Type() EventType { return TRIGGER_EXIT }
// Collision events
type CollisionEnterEvent struct {
BodyA *actor.RigidBody
BodyB *actor.RigidBody
}
func (e CollisionEnterEvent) Type() EventType { return COLLISION_ENTER }
type CollisionStayEvent struct {
BodyA *actor.RigidBody
BodyB *actor.RigidBody
}
func (e CollisionStayEvent) Type() EventType { return COLLISION_STAY }
type CollisionExitEvent struct {
BodyA *actor.RigidBody
BodyB *actor.RigidBody
}
func (e CollisionExitEvent) Type() EventType { return COLLISION_EXIT }
// Sleep/Wake events
type SleepEvent struct {
Body *actor.RigidBody
}
func (e SleepEvent) Type() EventType { return ON_SLEEP }
type WakeEvent struct {
Body *actor.RigidBody
}
func (e WakeEvent) Type() EventType { return ON_WAKE }
// EventListener - callback for events
type EventListener func(event Event)
// Events manager
type Events struct {
// Listeners by event type
listeners map[EventType][]EventListener
// Event buffer to send at flush
buffer []Event
// Collision tracking for Enter/Stay/Exit detection
previousActivePairs map[pairKey]bool
currentActivePairs map[pairKey]bool
sleepStates map[*actor.RigidBody]bool
}
func NewEvents() Events {
return Events{
listeners: make(map[EventType][]EventListener),
buffer: make([]Event, 0, 256),
previousActivePairs: make(map[pairKey]bool),
currentActivePairs: make(map[pairKey]bool),
sleepStates: make(map[*actor.RigidBody]bool),
}
}
// Subscribe adds a listener for an event type
func (e *Events) Subscribe(eventType EventType, listener EventListener) {
e.listeners[eventType] = append(e.listeners[eventType], listener)
}
// recordCollision is called during substeps to record a collision/trigger
func (e *Events) recordCollisions(constraints []*constraint.ContactConstraint) []*constraint.ContactConstraint {
n := 0
for _, c := range constraints {
pair := makePairKey(c.BodyA, c.BodyB)
e.currentActivePairs[pair] = true
if !c.BodyA.IsTrigger && !c.BodyB.IsTrigger {
constraints[n] = c
n++
}
}
constraints = constraints[:n]
return constraints
}
// processCollisionEvents compares current and previous pairs to detect Enter/Stay/Exit
// Should be called after all substeps
func (e *Events) processCollisionEvents() {
// Detect Enter and Stay events
for pair := range e.currentActivePairs {
// Skip if both bodies are sleeping, to avoid spamming events
if pair.bodyA.IsSleeping && pair.bodyB.IsSleeping {
continue
}
isTrigger := pair.bodyA.IsTrigger || pair.bodyB.IsTrigger
if e.previousActivePairs[pair] {
// Pair was active before and still is, Stay
if isTrigger {
e.buffer = append(e.buffer, TriggerStayEvent{
BodyA: pair.bodyA,
BodyB: pair.bodyB,
})
} else {
e.buffer = append(e.buffer, CollisionStayEvent{
BodyA: pair.bodyA,
BodyB: pair.bodyB,
})
}
} else {
// New pair, Enter
if isTrigger {
e.buffer = append(e.buffer, TriggerEnterEvent{
BodyA: pair.bodyA,
BodyB: pair.bodyB,
})
} else {
e.buffer = append(e.buffer, CollisionEnterEvent{
BodyA: pair.bodyA,
BodyB: pair.bodyB,
})
}
}
}
// Detect Exit events
for pair := range e.previousActivePairs {
if !e.currentActivePairs[pair] {
// Pair was active but is no longer, Exit
isTrigger := pair.bodyA.IsTrigger || pair.bodyB.IsTrigger
if isTrigger {
e.buffer = append(e.buffer, TriggerExitEvent{
BodyA: pair.bodyA,
BodyB: pair.bodyB,
})
} else {
e.buffer = append(e.buffer, CollisionExitEvent{
BodyA: pair.bodyA,
BodyB: pair.bodyB,
})
}
}
}
// Swap for next frame and clear current
e.previousActivePairs, e.currentActivePairs = e.currentActivePairs, e.previousActivePairs
clear(e.currentActivePairs)
}
func (e *Events) processSleepEvents(bodies []*actor.RigidBody) {
for _, body := range bodies {
trackedState, exists := e.sleepStates[body]
if !exists {
e.sleepStates[body] = body.IsSleeping
continue
}
if !trackedState && body.IsSleeping {
e.buffer = append(e.buffer, SleepEvent{Body: body})
e.sleepStates[body] = true
} else if trackedState && !body.IsSleeping {
e.buffer = append(e.buffer, WakeEvent{Body: body})
e.sleepStates[body] = false
}
}
}
// flush sends all buffered events and clears the buffer
func (e *Events) flush() {
e.processCollisionEvents()
for _, event := range e.buffer {
if listeners, ok := e.listeners[event.Type()]; ok {
for _, listener := range listeners {
listener(event)
}
}
}
e.buffer = e.buffer[:0]
}