-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheffects.ts
More file actions
326 lines (298 loc) · 14.4 KB
/
Copy patheffects.ts
File metadata and controls
326 lines (298 loc) · 14.4 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
namespace extraEffects {
let cachedSin: Fx8[]
let cachedCos: Fx8[]
let cachedFourPixelCircle: Image
let cachedSixPixelCircle: Image
let sineShiftPhase: number = 0
let sineShiftLUT: Fx8[]
let spawnSpreadLUT: Fx8[]
const NUM_SLICES = 90
const MAX_LUT_SLICES = 30
const MAX_TWEEN_SLICES = 20
const TWEEN_PCT_PER_SLICE = 1.0 / MAX_TWEEN_SLICES
const TWEEN_OUT_MULTIPLIER = 0.9
const fxTweenOutMultiplier = Fx8(TWEEN_OUT_MULTIPLIER)
const fxOneHundred = Fx8(100)
const galois = new Math.FastRandom()
function initCache() {
if (!cachedSin) {
cachedSin = particles.cacheSin(NUM_SLICES)
cachedCos = particles.cacheCos(NUM_SLICES)
cachedFourPixelCircle =
img`
. F F .
F F F F
F F F F
. F F .
`
cachedSixPixelCircle =
img`
. . F F . .
. F F F F .
F F F F F F
F F F F F F
. F F F F .
. . F F . .
`
sineShiftLUT = []
for (let i = 0; i < NUM_SLICES; i++) {
const curr = (sineShiftPhase + i) % NUM_SLICES
const next = (sineShiftPhase + i + 1) % NUM_SLICES
sineShiftLUT.push(Fx.sub(cachedSin[curr], cachedSin[next]))
}
forever(() => {
sineShiftPhase = (sineShiftPhase + 10) % NUM_SLICES
pause(100)
})
}
}
function createNumberRangeLookupTable(min: number, max: number): Fx8[] {
let table: Fx8[] = []
for (let i = 0; i < MAX_LUT_SLICES; i++) {
table.push(Fx8(min + (max - min) * i / MAX_LUT_SLICES))
}
return table
}
function createCircumferenceProbabilityLookupTable(min: number, max: number): Fx8[] {
let circumferences: number[] = []
let area: number = 0
let r: number = 0
let c: number = 0
const minArea = 2 * Math.PI * min
for(r=min; r<=max; r++) {
c = 2 * Math.PI * r
area += c - minArea
circumferences.push(area)
}
r = min
let table: Fx8[] = []
for (let i = 0; i < MAX_LUT_SLICES; i++) {
const percentageOfArea = i * area / MAX_LUT_SLICES
c = 0
while(c < circumferences.length && percentageOfArea > circumferences[c]) {
c++
}
table.push(Fx8(min + c))
}
return table
}
class SpreadParticleFactory extends particles.ParticleFactory {
private sizeSlice: number
private colorSlice: number
private tweenOutSlice: number
private spawnSpreadLookupTable: Fx8[]
private lifespanSpreadLookupTable: Fx8[]
private extraVX: Fx8
private extraVY: Fx8
private sineShiftRadius: Fx8
private extraVelocityPercentageMultiplierLookupTable: Fx8[]
/**
* Creates a spread particle factory
* @param colorLookupTable a lookup table of color index values used to color the particles over time
* @param duoColor define whether large particles should be one or two color
* @param sizeLookupTable a lookup table of particle radius used to size the particles over time
* @param minLifespan minimum randomized particle lifespan
* @param maxLifespan maximum randomized particle lifespan
* @param minSpawnSpread minimum randomized spawn distance away from center
* @param maxSpawnSpread maximum randomized spawn distance away from center
* @param minLifespanSpread minimum randomized distance traveled over the lifespan of the particle
* @param maxLifespanSpread maximum randomized distance traveled over the lifespan of the particle
* @param extraVX extra x velocity added to the particle on spawn
* @param extraVY extra y velocity added to the particle on spawn
* @param minExtraVelocityPercentageMultiplier minimum randomized percentage multiplier for the added velocity
* @param maxExtraVelocityPercentageMultiplier maximum randomized percentage multiplier for the added velocity
* @param sinShiftRadius adds a horizontal pixel shift that resembles a sine wave
* @param tweenOutAfterLifespanPastPercentage lifespan percentage cutoff before the particle velocity tweens out
*/
constructor(
private colorLookupTable: number[],
private duoColor: boolean,
private sizeLookupTable: number[],
private minLifespan: number,
private maxLifespan: number,
minSpawnSpread: number,
maxSpawnSpread: number,
minLifespanSpread: number,
maxLifespanSpread: number,
extraVX: number,
extraVY: number,
minExtraVelocityPercentageMultiplier: number,
maxExtraVelocityPercentageMultiplier: number,
sineShiftRadius: number,
private tweenOutAfterLifespanPastPercentage: number,
) {
super()
initCache()
this.colorLookupTable = this.colorLookupTable.slice()
this.colorLookupTable.reverse()
this.colorSlice = this.maxLifespan / this.colorLookupTable.length
this.sizeLookupTable = this.sizeLookupTable.slice()
this.sizeLookupTable.reverse()
this.sizeSlice = this.maxLifespan / this.sizeLookupTable.length
this.tweenOutSlice = this.maxLifespan / MAX_TWEEN_SLICES
this.spawnSpreadLookupTable = createCircumferenceProbabilityLookupTable(minSpawnSpread, maxSpawnSpread)
this.extraVX = Fx8(extraVX)
this.extraVY = Fx8(extraVY)
this.sineShiftRadius = Fx8(sineShiftRadius)
this.extraVelocityPercentageMultiplierLookupTable = createNumberRangeLookupTable(minExtraVelocityPercentageMultiplier, maxExtraVelocityPercentageMultiplier)
this.tweenOutAfterLifespanPastPercentage = 100 - tweenOutAfterLifespanPastPercentage
// simulate the spread distance lost from tweening out, and compensate by increasing initial velocity
if(this.tweenOutAfterLifespanPastPercentage > 0) {
let tweenOutSpreadAsPercentageOfNormalVelocity = Math.floor((100 - this.tweenOutAfterLifespanPastPercentage) / 100.0 * MAX_TWEEN_SLICES) * TWEEN_PCT_PER_SLICE
const tweenOutSlices = Math.ceil(this.tweenOutAfterLifespanPastPercentage / 100.00 * MAX_TWEEN_SLICES)
let percentagePerSlice = TWEEN_PCT_PER_SLICE
for(let tweenOut = 0; tweenOut < tweenOutSlices; tweenOut++) {
percentagePerSlice *= TWEEN_OUT_MULTIPLIER
tweenOutSpreadAsPercentageOfNormalVelocity += percentagePerSlice
}
minLifespanSpread = Math.floor(minLifespanSpread / tweenOutSpreadAsPercentageOfNormalVelocity)
maxLifespanSpread = Math.floor(maxLifespanSpread / tweenOutSpreadAsPercentageOfNormalVelocity)
}
this.lifespanSpreadLookupTable = createNumberRangeLookupTable(minLifespanSpread * 1000 / maxLifespan, maxLifespanSpread * 1000 / maxLifespan)
}
createParticle(anchor: particles.ParticleAnchor) {
const p: particles.Particle = super.createParticle(anchor);
p.lifespan = galois.randomRange(this.minLifespan, this.maxLifespan - 1)
p.data = (this.tweenOutAfterLifespanPastPercentage * MAX_TWEEN_SLICES / 100) * this.tweenOutSlice
p.color = Math.floor(galois.randomRange(0, NUM_SLICES - 1))
const angle = galois.randomRange(0, NUM_SLICES - 1)
const spawnSpreadMultiplier = galois.pickRandom(this.spawnSpreadLookupTable)
const velocityMultiplier = galois.pickRandom(this.lifespanSpreadLookupTable)
const extraVelocityMultiplier = Fx.div(galois.pickRandom(this.extraVelocityPercentageMultiplierLookupTable), fxOneHundred)
p._x = Fx.add(p._x, Fx.mul(cachedCos[angle], spawnSpreadMultiplier))
p._y = Fx.add(p._y, Fx.mul(cachedSin[angle], spawnSpreadMultiplier))
p.vx = Fx.add(Fx.mul(cachedCos[angle], velocityMultiplier), Fx.mul(this.extraVX, extraVelocityMultiplier))
p.vy = Fx.add(Fx.mul(cachedSin[angle], velocityMultiplier), Fx.mul(this.extraVY, extraVelocityMultiplier))
return p;
}
drawParticle(p: particles.Particle, x: Fx8, y: Fx8) {
const size = this.sizeLookupTable[Math.floor(p.lifespan / this.sizeSlice)]
const radius = Fx.div(Fx8(size), Fx.twoFx8)
const colorIndex = Math.floor(p.lifespan / this.colorSlice)
const color = this.colorLookupTable[colorIndex]
const darkerColor = colorIndex === 0 ? color : this.colorLookupTable[colorIndex - 1]
switch (size) {
case 0:
break
case 1:
screen.setPixel(Fx.toInt(x), Fx.toInt(y), color)
break
case 2:
screen.drawRect(Fx.toInt(x), Fx.toInt(y), 2, 2, color)
break
case 3:
case 4:
const fourPixelImage = cachedFourPixelCircle.clone()
fourPixelImage.replace(0xF, color)
screen.drawTransparentImage(fourPixelImage, Fx.toInt(Fx.sub(x, Fx.oneFx8)), Fx.toInt(Fx.sub(y, Fx.oneFx8)))
break
case 5:
case 6:
const sixPixelImage = cachedSixPixelCircle.clone()
sixPixelImage.replace(0xF, color)
screen.drawTransparentImage(sixPixelImage, Fx.toInt(Fx.sub(x, Fx.twoFx8)), Fx.toInt(Fx.sub(y, Fx.twoFx8)))
break
case 7:
case 8:
case 9:
case 10:
screen.fillCircle(Fx.toInt(x), Fx.toInt(y), Fx.toInt(radius), color)
break
default:
if(this.duoColor) {
screen.fillCircle(Fx.toInt(x), Fx.toInt(y), Fx.toInt(radius), darkerColor)
screen.fillCircle(Fx.toInt(x), Fx.toInt(y), Fx.toInt(Fx.sub(radius, Fx.twoFx8)), color)
} else {
screen.fillCircle(Fx.toInt(x), Fx.toInt(y), Fx.toInt(radius), color)
}
break
}
p._x = Fx.add(p._x, Fx.mul(sineShiftLUT[(sineShiftPhase + p.color) % NUM_SLICES], this.sineShiftRadius))
while (p.lifespan < p.data) {
p.data -= this.tweenOutSlice
p.vx = Fx.mul(p.vx, fxTweenOutMultiplier)
p.vy = Fx.mul(p.vy, fxTweenOutMultiplier)
}
}
}
/**
* Create a particle source on a center spread trajectory
* @param anchor anchor for the center point of all spawned particles
* @param totalLifespan lifespan of all particles including the lifespan of the last particle spawned
* @param colorLookupTable a lookup table of color index values used to color the particles over time
* @param duoColor define whether large particles should be one or two color
* @param sizeLookupTable a lookup table of particle radius used to size the particles over time
* @param particlesPerSecond number of particles to generate per second
* @param minParticleLifespan minimum randomized particle lifespan
* @param maxParticleLifespan maximum randomized particle lifespan
* @param minSpawnSpread minimum randomized spawn distance away from center
* @param maxSpawnSpread maximum randomized spawn distance away from center
* @param minLifespanSpread minimum randomized distance traveled over the lifespan of the particle
* @param maxLifespanSpread maximum randomized distance traveled over the lifespan of the particle
* @param extraVX extra x velocity added to the particle on spawn
* @param extraVY extra y velocity added to the particle on spawn
* @param minExtraVelocityPercentageMultiplier minimum randomized percentage multiplier for the added velocity
* @param maxExtraVelocityPercentageMultiplier maximum randomized percentage multiplier for the added velocity
* @param gravity y acceleration added to the velocity over time
* @param tweenOutAfterLifespanPastPercentage lifespan percentage cutoff before the particle velocity tweens out
* @returns the newly created particle source
*/
export function createSpreadParticleSource(
anchor: particles.ParticleAnchor,
colorLookupTable: number[],
duoColor: boolean,
sizeLookupTable: number[],
particlesPerSecond: number,
totalLifespan: number,
minParticleLifespan: number = 200,
maxParticleLifespan: number = 200,
minSpawnSpread: number = 0,
maxSpawnSpread: number = 0,
minLifespanSpread: number = 0,
maxLifespanSpread: number = 0,
extraVX: number = 0,
extraVY: number = 0,
minExtraVelocityPercentageMultiplier: number = 100,
maxExtraVelocityPercentageMultiplier: number = 100,
gravity: number = 0,
sineShiftRadius: number = 0,
tweenOutAfterLifespanPastPercentage: number = 50,
z: number = 0,
): particles.ParticleSource {
const factory = new SpreadParticleFactory(
colorLookupTable,
duoColor,
sizeLookupTable,
minParticleLifespan,
maxParticleLifespan,
minSpawnSpread,
maxSpawnSpread,
minLifespanSpread,
maxLifespanSpread,
extraVX,
extraVY,
minExtraVelocityPercentageMultiplier,
maxExtraVelocityPercentageMultiplier,
sineShiftRadius,
tweenOutAfterLifespanPastPercentage,
);
let sourceLifespan = totalLifespan
if (sourceLifespan >= 0) {
sourceLifespan = Math.max(200, sourceLifespan - maxParticleLifespan)
if (sourceLifespan < 1000) {
particlesPerSecond *= 500 / sourceLifespan
}
}
const src = new particles.ParticleSource(
anchor,
particlesPerSecond,
factory
)
if (sourceLifespan >= 0) {
src.lifespan = sourceLifespan
}
src.z = z
src.setAcceleration(0, gravity)
return src
}
}