/
source.go
243 lines (214 loc) · 5.84 KB
/
source.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
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
package particle
import (
"math"
"time"
"github.com/200sc/go-dist/intrange"
"github.com/oakmound/oak/dlog"
"github.com/oakmound/oak/event"
"github.com/oakmound/oak/physics"
"github.com/oakmound/oak/render"
"github.com/oakmound/oak/timing"
)
const (
//IgnoreEnd refers to the life value given to particles that want to skip their source's end function.
IgnoreEnd = -2000 / 2
)
// A Source is used to store and control a set of particles.
type Source struct {
render.Layered
Generator Generator
particles [blockSize]Particle
nextPID int
CID event.CID
pIDBlock int
stackLevel int
paused bool
EndFunc func()
}
// NewSource creates a new source
func NewSource(g Generator, stackLevel int) *Source {
ps := new(Source)
ps.Generator = g
ps.stackLevel = stackLevel
ps.Init()
return ps
}
// Init allows a source to be considered as an entity, and initializes it
func (ps *Source) Init() event.CID {
CID := event.NextID(ps)
CID.Bind(rotateParticles, event.Enter)
ps.CID = CID
ps.pIDBlock = Allocate(ps.CID)
if ps.Generator.GetBaseGenerator().Duration != Inf {
go func(ps_p *Source, duration intrange.Range) {
timing.DoAfter(time.Duration(duration.Poll())*time.Millisecond, func() {
ps_p.Stop()
})
}(ps, ps.Generator.GetBaseGenerator().Duration)
}
return CID
}
func (ps *Source) cycleParticles() bool {
pg := ps.Generator.GetBaseGenerator()
cycled := false
for i := 0; i < ps.nextPID; i++ {
p := ps.particles[i]
bp := p.GetBaseParticle()
for bp.Life <= 0 {
p.Undraw()
cycled = true
if pg.EndFunc != nil && bp.Life > IgnoreEnd {
pg.EndFunc(p)
}
ps.nextPID--
if i == ps.nextPID {
return cycled
}
ps.particles[i], ps.particles[ps.nextPID] = ps.particles[ps.nextPID], ps.particles[i]
p = ps.particles[i]
p.setPID(i + ps.pIDBlock*blockSize)
bp = p.GetBaseParticle()
}
// Ignore dead particles
if bp.Life > 0 {
cycled = true
bp.Life--
// Apply rotational acceleration
if pg.Rotation != nil {
bp.Vel = bp.Vel.Rotate(pg.Rotation.Poll())
}
if pg.SpeedDecay.X() != 0 {
bp.Vel = bp.Vel.SetX(bp.Vel.X() * pg.SpeedDecay.X())
if math.Abs(bp.Vel.X()) < 0.2 {
bp.Vel = bp.Vel.SetX(0)
}
}
if pg.SpeedDecay.Y() != 0 {
bp.Vel = bp.Vel.SetY(bp.Vel.Y() * pg.SpeedDecay.Y())
if math.Abs(bp.Vel.Y()) < 0.2 {
bp.Vel = bp.Vel.SetY(0)
}
}
bp.Vel.Add(pg.Gravity)
bp.Add(bp.Vel)
bp.SetLayer(ps.Layer(bp.GetPos()))
p.Cycle(ps.Generator)
}
}
return cycled
}
// Layer is shorthand for getting the base generator behind a source's layer
func (ps *Source) Layer(v physics.Vector) int {
return ps.Generator.GetBaseGenerator().LayerFunc(v)
}
func (ps *Source) addParticles() {
pg := ps.Generator.GetBaseGenerator()
// Regularly create particles (up until max particles)
newParticleCount := int(pg.NewPerFrame.Poll())
if ps.nextPID+newParticleCount >= blockSize {
newParticleCount = blockSize - ps.nextPID
}
var p Particle
var bp *baseParticle
for i := 0; i < newParticleCount; i++ {
angle := pg.Angle.Poll()
speed := pg.Speed.Poll()
startLife := pg.LifeSpan.Poll()
// If this particle has not been allocated yet
if ps.particles[ps.nextPID] == nil {
bp = &baseParticle{
LayeredPoint: render.NewLayeredPoint(
pg.X()+floatFromSpread(pg.Spread.X()),
pg.Y()+floatFromSpread(pg.Spread.Y()),
0,
),
Src: ps,
Vel: physics.NewVector(
speed*math.Cos(angle)*-1,
speed*math.Sin(angle)*-1),
Life: startLife,
totalLife: startLife,
pID: ps.nextPID + ps.pIDBlock*blockSize,
}
p = ps.Generator.GenerateParticle(bp)
// If this is a 'recycled' particle waiting to be redrawn
} else {
bp = ps.particles[ps.nextPID].GetBaseParticle()
bp.LayeredPoint = render.NewLayeredPoint(
pg.X()+floatFromSpread(pg.Spread.X()),
pg.Y()+floatFromSpread(pg.Spread.Y()),
0)
bp.Vel = physics.NewVector(
speed*math.Cos(angle)*-1,
speed*math.Sin(angle)*-1)
bp.Life = startLife
bp.totalLife = startLife
p = ps.Generator.GenerateParticle(bp)
}
ps.particles[ps.nextPID] = p
ps.nextPID++
p.SetLayer(ps.Layer(bp.GetPos()))
_, err := render.Draw(p, ps.stackLevel)
dlog.ErrorCheck(err)
}
}
// rotateParticles updates particles over time as long
// as a Source is active.
func rotateParticles(id int, nothing interface{}) int {
ps := event.GetEntity(id).(*Source)
if !ps.paused {
ps.cycleParticles()
ps.addParticles()
}
return 0
}
// clearParticles is used after a Source has been stopped
// to continue moving old particles for as long as they exist.
func clearParticles(id int, nothing interface{}) int {
if ps, ok := event.GetEntity(id).(*Source); ok {
if !ps.paused {
if ps.cycleParticles() {
} else {
if ps.EndFunc != nil {
ps.EndFunc()
}
event.DestroyEntity(id)
Deallocate(ps.pIDBlock)
return event.UnbindEvent
}
}
return 0
}
return event.UnbindEvent
}
// Stop manually stops a Source, if its duration is infinite
// or if it should be stopped before expriring naturally.
func (ps *Source) Stop() {
if ps == nil {
return
}
ps.CID.UnbindAllAndRebind([]event.Bindable{clearParticles}, []string{event.Enter})
}
// Pause on a Source just stops the repetition
// of its rotation function, which moves, destroys,
// ages and generates particles. Existing particles will
// stay in place.
func (ps *Source) Pause() {
ps.paused = true
}
// UnPause on a source a Source rebinds it's rotate function.
func (ps *Source) UnPause() {
ps.paused = false
}
// ShiftX shift's a source's underlying generator
func (ps *Source) ShiftX(x float64) {
ps.Generator.ShiftX(x)
}
// ShiftY shift's a source's underlying generator (todo: consider if this shoud be composed)
func (ps *Source) ShiftY(y float64) {
ps.Generator.ShiftY(y)
}
// SetPos sets a source's underlying generator
func (ps *Source) SetPos(x, y float64) {
ps.Generator.SetPos(x, y)
}