forked from eternaldensity/Sandcastle-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masterpiece.js
299 lines (264 loc) · 9.51 KB
/
masterpiece.js
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
// Masterpiece Pane
// Full screen image infront of game
// Fireworks in front of image
// Fanfare
Molpy.Master = {
Create: function(np,type) {
if (this.active) return;
// console.log('MM create called');
this.active = 1;
this.np = np;
this.type = type || 'long';
$('#game').addClass('hidden');
g('masters').innerHTML = '<div id=MasterBlack><div id=MasterPix></div></div>' +
'<div id=fireworkdiv><canvas id=firework width=' + window.innerWidth +
' height=' + window.innerHeight+ '></div>';
Molpy.Master.NewPix(np);
Molpy.Master.FanFare();
setTimeout(Molpy.Master.Destroy,150000); // This is a backstop
},
Destroy: function() {
// console.log('MM destroy called');
$('#game').removeClass('hidden');
g('masters').innerHTML = '';
this.fireworks = [];
this.particles = [];
this.sounds = [];
setTimeout(Molpy.Master.ReEnable,2000);
},
ReEnable: function() {
Molpy.Master.active = 0;
// console.log('MM ReEnable called');
},
NewPix: function(np) {
g('MasterBlack').width=window.innerWidth;
g('MasterBlack').height=window.innerHeight;
g('MasterPix').style.backgroundImage = Molpy.Url(Molpy.NewPixFor(np));
},
active: 0,
FanFare: function() {
// console.log('MM Fanfare called');
if (!this.active) return;
if (!g('firework')) return;
if (!this.audio) this.audio = new Audio('audio/Fanfare'+flandom(1)+'.mp3');
this.audio.play();
this.audio.addEventListener("ended",Molpy.Master.StartFireWorks);
},
StartFireWorks: function() {
// console.log('MM StartFireworks called');
mm = Molpy.Master;
if (!mm.active) return;
mm.canvas = g('firework');
if (!mm.canvas) return;
mm.ctx = mm.canvas.getContext('2d');
mm.cw = window.innerWidth-20;
mm.ch = window.innerHeight-1;
mm.fireworks = [];
mm.particles = [];
mm.sounds = [];
mm.hue = 120;
mm.limiterTotal = 5;
mm.limiterTick = 0;
mm.timerTotal = 60;
mm.timerTick = 0;
mm.mx = 0;
mm.my = 0;
mm.salvos = (mm.type == 'short')?3:10 + Math.floor(mm.np/333);
mm.salvosize = Math.floor(mm.np/444)+3;
mm.canvas.width = mm.cw;
mm.canvas.height = mm.ch;
Fireworks_loop();
},
};
/* Code below is adapted from several sources
*/
function randomRange(min, max) {
return Math.random() * (max - min) + min;
}
function calculateDistance(p1x, p1y, p2x, p2y) {
var xDistance = p1x - p2x,
yDistance = p1y - p2y;
return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
}
// create firework
function Firework(sx, sy, tx, ty) {
// actual coordinates
this.x = sx;
this.y = sy;
// starting coordinates
this.sx = sx;
this.sy = sy;
// target coordinates
this.tx = tx;
this.ty = ty;
// distance from starting point to target
this.distanceToTarget = calculateDistance(sx, sy, tx, ty);
this.distanceTraveled = 0;
// track the past coordinates of each firework to create a trail effect, increase the coordinate count to create more prominent trails
this.coordinates = [];
this.coordinateCount = 15;
// populate initial coordinate collection with the current coordinates
while (this.coordinateCount--) {
this.coordinates.push([this.x, this.y]);
}
this.angle = Math.atan2(ty - sy, tx - sx);
this.speed = 3;
this.acceleration = 1.05
this.brightness = randomRange(50, 70);
// circle target indicator radius
// this.targetRadius = 1;
this.audio = new Audio('audio/bang'+flandom(8)+'.mp3');
this.audio.play();
}
// update firework
Firework.prototype.update = function (index) {
// remove last item in coordinates array
this.coordinates.pop();
// add current coordinates to the start of the array
this.coordinates.unshift([this.x, this.y]);
// speed up the firework
this.speed *= this.acceleration;
// get the current velocities based on angle and speed
var vx = Math.cos(this.angle) * this.speed,
vy = Math.sin(this.angle) * this.speed;
// how far will the firework have traveled with velocities applied?
this.distanceTraveled = calculateDistance(this.sx, this.sy, this.x + vx, this.y + vy);
// if the distance traveled, including velocities, is greater than the initial distance to the target, then the target has been reached
if (this.distanceTraveled >= this.distanceToTarget) {
createParticles(this.tx, this.ty);
// remove the firework, use the index passed into the update function to determine which to remove
Molpy.Master.fireworks.splice(index, 1);
mm.sounds.push(new Audio('audio/fire'+flandom(2)+'.mp3'))
mm.sounds[mm.sounds.length-1].play();
} else {
// target not reached, keep traveling
this.x += vx;
this.y += vy;
}
}
// draw firework
Firework.prototype.draw = function () {
var mm = Molpy.Master;
var ctx = mm.ctx;
ctx.beginPath();
// move to the last tracked coordinate in the set, then draw a line to the current x and y
ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
ctx.lineTo(this.x, this.y);
ctx.lineWidth=3;
ctx.strokeStyle = 'hsl(' + mm.hue + ', 100%, ' + this.brightness + '%)';
ctx.stroke();
}
// create particle
function Particle(x, y) {
this.x = x;
this.y = y;
// track the past coordinates of each particle to create a trail effect, increase the coordinate count to create more prominent trails
this.coordinates = [];
this.coordinateCount = 5;
while (this.coordinateCount--) {
this.coordinates.push([this.x, this.y]);
}
// set a random angle in all possible directions, in radians
this.angle = randomRange(0, Math.PI * 2);
this.speed = randomRange(1, 10);
// friction will slow the particle down
this.friction = 0.95;
// gravity will be applied and pull the particle down
this.gravity = 1;
// set the hue to a random number +-20 of the overall hue variable
this.hue = randomRange(Molpy.Master.hue - 20, Molpy.Master.hue + 20);
this.brightness = randomRange(50, 80);
this.alpha = 1;
// set how fast the particle fades out
this.decay = randomRange(0.01, 0.02);
}
// update particle
Particle.prototype.update = function (index) {
// remove last item in coordinates array
this.coordinates.pop();
// add current coordinates to the start of the array
this.coordinates.unshift([this.x, this.y]);
// slow down the particle
this.speed *= this.friction;
// apply velocity
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed + this.gravity;
// fade out the particle
this.alpha -= this.decay;
// remove the particle once the alpha is low enough, based on the passed in index
if (this.alpha <= this.decay) {
Molpy.Master.particles.splice(index, 1);
}
}
// draw particle
Particle.prototype.draw = function () {
var ctx = Molpy.Master.ctx;
ctx.beginPath();
// move to the last tracked coordinates in the set, then draw a line to the current x and y
ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
ctx.lineWidth=3;
ctx.lineTo(this.x, this.y);
ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, ' + this.brightness + '%, ' + this.alpha + ')';
ctx.stroke();
}
// create particle group/explosion
function createParticles(x, y) {
var mm = Molpy.Master;
// increase the particle count for a bigger explosion, beware of the canvas performance hit with the increased particles though
var particleCount = 50;
while (particleCount--) {
mm.particles.push(new Particle(x, y));
}
}
// main demo loop
function Fireworks_loop() {
var mm = Molpy.Master;
var ctx = mm.ctx;
var cw = mm.cw;
var ch = mm.ch;
// this function will run endlessly with requestAnimationFrame
if (Molpy.Master.active) setTimeout(Fireworks_loop,1000/60);
// increase the hue to get different colored fireworks over time
mm.hue += 0.5;
// normally, clearRect() would be used to clear the canvas
// we want to create a trailing effect though
// setting the composite operation to destination-out will allow us to clear the canvas at a specific opacity, rather than wiping it entirely
ctx.globalCompositeOperation = 'destination-out';
// decrease the alpha property to create more prominent trails
ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';
ctx.fillRect(0, 0, cw, ch);
// change the composite operation back to our main mode
// lighter creates bright highlight points as the fireworks and particles overlap each other
ctx.globalCompositeOperation = 'lighter';
// loop over each firework, draw it, update it
var i = mm.fireworks.length;
while (i--) {
mm.fireworks[i].draw();
mm.fireworks[i].update(i);
}
// loop over each particle, draw it, update it
var i = mm.particles.length;
while (i--) {
mm.particles[i].draw();
mm.particles[i].update(i);
}
// launch fireworks automatically to random coordinates
if (mm.timerTick >= mm.timerTotal) {
if (mm.salvos > 0) {
// start the firework at the bottom middle of the screen, then set the random target coordinates,
// the random y coordinates will be set within the range of the top half of the screen
for (var i = 0; i < mm.salvosize; i++) {
setTimeout(function () {mm.fireworks.push(new Firework(cw / 2 + randomRange(-cw / 6, cw / 6), ch, randomRange(0, cw),
randomRange(0, ch / 2)));}, 250 * i);
}
mm.timerTick = 0;
mm.salvos--;
} else {
if (mm.salvos == 0) setTimeout(Molpy.Master.Destroy,5000);
mm.salvos--;
}
} else {
mm.timerTick++;
};
if (mm.limiterTick < mm.limiterTotal) mm.limiterTick++;
}