-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathexplosion.js
145 lines (135 loc) · 3.93 KB
/
explosion.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
import { randomBetween, jitterCoordinate } from "../helpers/helpers.js";
import { LANDER_WIDTH, LANDER_HEIGHT } from "../helpers/constants.js";
import { makeParticle } from "../particle.js";
export const makeExplosion = (
state,
position,
velocity,
fill,
size,
amount,
useTerrain = true
) => {
const smallExplosionChunks = new Array(amount)
.fill()
.map(() =>
makeParticle(
state,
jitterCoordinate(position),
jitterCoordinate(velocity),
randomBetween(size / 4, size),
randomBetween(size / 4, size),
fill,
false,
useTerrain
)
);
return {
draw: (deltaTime) => smallExplosionChunks.forEach((e) => e.draw(deltaTime)),
};
};
export const makeLanderExplosion = (
state,
position,
velocity,
angle,
useTerrain = true
) => {
const gradient = state.get("theme").landerGradient;
const noseCone = makeParticle(
state,
position,
jitterCoordinate(velocity),
LANDER_WIDTH,
LANDER_HEIGHT / 2,
gradient,
(CTX, position, _, rotationAngle, fill) => {
// In order to render three separate pieces in the same location as the
// single-piece lander, we have to render them all at the x and y
// and rotation of the lander, then offset each piece on the y axis. We
// also want these pieces to move and rotate independently. To do this,
// they need their own axis of rotation, but we have to move the lander
// x and y rather than the offset piece's x and y.
//
// Step 1: move to lander position and rotation. This is the position
// which will be updated by the ballistic update. This rotation is the
// angle of the lander on crash and will not be updated.
CTX.translate(position.x, position.y);
CTX.rotate(angle);
// Step 2: adjust this chunk to its own offset position and own axis of
// rotation. This is the rotation that's updated by the ballistic update
CTX.fillStyle = fill;
CTX.translate(0, -LANDER_HEIGHT / 2 + 4);
CTX.rotate(rotationAngle);
CTX.beginPath();
CTX.moveTo(-LANDER_WIDTH / 2, LANDER_HEIGHT / 4 - 4);
CTX.lineTo(0, -LANDER_HEIGHT / 4 - 4);
CTX.lineTo(LANDER_WIDTH / 2, LANDER_HEIGHT / 4 - 4);
CTX.closePath();
CTX.fill();
},
useTerrain
);
const chunk1 = makeParticle(
state,
position,
jitterCoordinate(velocity),
LANDER_WIDTH,
LANDER_HEIGHT / 2,
gradient,
(CTX, position, _, rotationAngle, fill) => {
CTX.translate(position.x, position.y);
CTX.rotate(angle);
CTX.fillStyle = fill;
CTX.translate(0, LANDER_HEIGHT / 2);
CTX.rotate(rotationAngle);
CTX.beginPath();
CTX.moveTo(-LANDER_WIDTH / 2, -LANDER_HEIGHT / 4);
CTX.lineTo(LANDER_WIDTH / 2, -LANDER_HEIGHT / 4);
CTX.lineTo(LANDER_WIDTH / 2, LANDER_HEIGHT / 4);
CTX.lineTo(-LANDER_WIDTH / 2, LANDER_HEIGHT / 4);
CTX.closePath();
CTX.fill();
},
useTerrain
);
const chunk2 = makeParticle(
state,
position,
jitterCoordinate(velocity),
LANDER_WIDTH,
LANDER_HEIGHT / 2,
gradient,
(CTX, position, _, rotationAngle, fill) => {
CTX.translate(position.x, position.y);
CTX.rotate(angle);
CTX.fillStyle = fill;
CTX.translate(0, 0);
CTX.rotate(rotationAngle);
CTX.beginPath();
CTX.lineTo(-LANDER_WIDTH / 2, -LANDER_HEIGHT / 4);
CTX.lineTo(LANDER_WIDTH / 2, -LANDER_HEIGHT / 4);
CTX.lineTo(LANDER_WIDTH / 2, LANDER_HEIGHT / 4);
CTX.lineTo(-LANDER_WIDTH / 2, LANDER_HEIGHT / 4);
CTX.closePath();
CTX.fill();
},
useTerrain
);
const randomPieces = makeExplosion(
state,
position,
velocity,
gradient,
randomBetween(2, 20),
32,
useTerrain
);
const draw = (deltaTime) => {
noseCone.draw(deltaTime);
chunk1.draw(deltaTime);
chunk2.draw(deltaTime);
randomPieces.draw(deltaTime);
};
return { draw };
};