-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
125 lines (112 loc) · 2.91 KB
/
event.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
// molecular is a 3D physics engine written in Go
// Copyright (C) 2023 Kevin Z <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package molecular
import (
"time"
)
var eventWavePool = newObjPool[eventWave]()
type eventWave struct {
sender *Object
pos Vec3
alive time.Duration
speed float64
radius, maxRadius float64
heavy bool
on func(receiver *Object)
onBeforeTick func(*eventWave) bool
onRemove func()
objsCache []*Object
delay, tick int
skipped time.Duration
}
func newEventWave(sender *Object, pos Vec3, radius float64, on func(receiver *Object), heavy bool) (e *eventWave) {
e = eventWavePool.Get()
e.sender = sender
e.pos = pos
e.alive = time.Hour
e.speed = C
e.radius = 0
e.maxRadius = radius
e.on = on
e.heavy = heavy
return
}
func (f *eventWave) Sender() *Object {
return f.sender
}
// Pos returns the absolute start position when the event was sent
func (f *eventWave) Pos() Vec3 {
return f.pos
}
// If AliveTime returns zero, the event will be removed
func (f *eventWave) AliveTime() time.Duration {
return f.alive
}
func (f *eventWave) MaxSpeed() float64 {
return f.speed
}
func (f *eventWave) MaxRadius() float64 {
return f.maxRadius
}
// should this event starts from a separate goroutine
func (f *eventWave) Heavy() bool {
return f.heavy
}
func (f *eventWave) Tick(dt time.Duration, e *Engine) {
if f.delay > 0 {
f.skipped += dt
if f.tick++; f.tick < f.delay {
return
}
dt = f.skipped
f.skipped = 0
f.tick = 0
}
if f.alive -= dt; f.alive < 0 {
dt += f.alive
f.alive = 0
}
if f.onBeforeTick != nil && f.onBeforeTick(f) {
return
}
lastr := f.radius
rd := f.speed * dt.Seconds()
f.radius += rd
if f.maxRadius >= 0 && f.radius >= f.maxRadius {
f.radius = f.maxRadius
f.alive = 0
}
f.objsCache = e.appendObjsInsideRing(f.objsCache[:0], f.pos, lastr, f.radius+rd/2)
for _, o := range f.objsCache {
if o == f.sender {
continue
}
f.on(o)
}
f.objsCache = f.objsCache[:0]
}
// free will call onRemove and put this eventWave object into the pool
func (f *eventWave) free() {
f.on = nil
if f.onRemove != nil {
f.onRemove()
f.onRemove = nil
}
if f.onBeforeTick != nil {
f.onBeforeTick = nil
}
eventWavePool.Put(f)
}