-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-cosmic-dash.js
309 lines (306 loc) · 8.66 KB
/
game-cosmic-dash.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
299
300
301
302
303
304
305
306
307
308
309
const inside = require('point-in-polygon')
const game = {
trackVertRadius: 0.2,
durationPlay: 30 * global.ticksPerSecond,
activate: (players, state) => {
Object.assign(
state,
{
track: {
radius: game.trackVertRadius,
verts: [],
innerPoly: [],
outerPoly: [],
tangentPoints: [],
isValid: false,
meta: {
quads: [],
playerQuadBooleanStates: {}
}
}
}
)
game.changeModeToIntro(players, state)
},
changeModeToIntro: (players, state) => {
const now = Date.now()
state.mode = 'intro'
state.ships.forEach(ship => {
ship.score = 0
players[ship.id].lastActiveTime = now
})
state.startCircle = global.createActivityCircle({
label: 'Start',
radius: game.trackVertRadius,
ticksToActivate: 3 * global.ticksPerSecond
})
game.createRandomTrackVerts(state)
},
changeModeToPlay: (players, state) => {
delete state.startCircle
state.mode = 'play'
state.track.meta.playerQuadBooleanStates = {}
state.timer = game.durationPlay
state.ships.forEach(ship => {
ship.score = 0
})
state.events.emit('start')
},
changeModeToScore: (players, state) => {
delete state.track
global.totalPlayerScores(players, state)
},
createRandomTrackVerts: (state) => {
const count = 12
const verts = []
while (verts.length < count) {
const angle = (verts.length / count) * global.tau
const radius = global.rangeRand(
0.2,
1 - (game.trackVertRadius * 1.5)
)
verts.push([
Math.cos(angle) * radius,
Math.sin(angle) * radius
])
}
state.track.verts = verts
game.centerTrackVerts(state)
},
centerTrackVerts: (state) => {
const verts = state.track.verts
const bounds = {
xMin: verts[0][0],
yMin: verts[0][0],
xMax: verts[0][1],
yMax: verts[0][1]
}
verts.forEach((a) => {
bounds.xMin = Math.min(a[0], bounds.xMin)
bounds.xMax = Math.max(a[0], bounds.xMax)
bounds.yMin = Math.min(a[1], bounds.yMin)
bounds.yMax = Math.max(a[1], bounds.yMax)
})
const centerX = 0 - global.lerp(bounds.xMin, bounds.xMax, 0.5)
const centerY = 0 - global.lerp(bounds.yMin, bounds.yMax, 0.5)
state.track.verts = verts.map((a) => {
return [
a[0] + centerX,
a[1] + centerY
]
})
},
inflateTrack: (state) => {
const verts = state.track.verts
const stepSize = game.trackVertRadius / 200
const allButLast = verts.slice(0, -1)
const velocities = verts.map(() => [0, 0])
let anyCollisions = false
allButLast.forEach((a, indexA) => {
const subset = verts.slice(indexA + 1)
subset.forEach((b, offset) => {
const indexB = indexA + 1 + offset
const comparison = game.comparePoints(a, b)
if (comparison.hit) {
anyCollisions = true
const velocity = [
Math.cos(comparison.angle) * stepSize,
Math.sin(comparison.angle) * stepSize
]
velocities[indexA][0] += velocity[0]
velocities[indexA][1] += velocity[1]
velocities[indexB][0] += -velocity[0]
velocities[indexB][1] += -velocity[1]
}
})
})
state.track.verts = verts.map((a, index) => {
return [
a[0] + velocities[index][0],
a[1] + velocities[index][1]
]
})
game.centerTrackVerts(state)
game.findTrackVertTangents(state)
if (!anyCollisions) {
state.track.isValid = true
game.createQuads(state)
game.putShipsAtStart(state)
}
},
findTrackVertTangents: (state) => {
const track = state.track
const innerPoly = []
const outerPoly = []
const length = track.verts.length
track.verts.forEach((b, index, array) => {
const set = [
(index + length - 1) % length,
index,
(index + length + 1) % length
]
const a = array[set[0]]
const c = array[set[2]]
const abAngle = Math.atan2(
b[1] - a[1],
b[0] - a[0]
)
const cbAngle = Math.atan2(
b[1] - c[1],
b[0] - c[0]
)
const tangentAngle = ((((cbAngle - abAngle) * 0.5) + abAngle) + global.tau) % global.tau
const tangentVert = [
Math.cos(tangentAngle) * game.trackVertRadius,
Math.sin(tangentAngle) * game.trackVertRadius
]
const tangentVertA = [
b[0] + tangentVert[0],
b[1] + tangentVert[1]
]
const tangentVertB = [
b[0] - tangentVert[0],
b[1] - tangentVert[1]
]
let vA = tangentVertA
let vB = tangentVertB
if (inside(tangentVertA, track.verts)) {
vA = tangentVertB
vB = tangentVertA
}
outerPoly.push(vA)
innerPoly.push(vB)
})
state.track.innerPoly = innerPoly
state.track.outerPoly = outerPoly
},
createQuads: (state) => {
state.track.meta.quads = state.track.verts.map((vert, index) => {
return game.createQuadByIndices(
index,
(index + 1) % state.track.verts.length,
state
)
})
},
createQuadByIndices: (indexA, indexB, state) => {
return [
state.track.innerPoly[indexA],
state.track.innerPoly[indexB],
state.track.outerPoly[indexB],
state.track.outerPoly[indexA]
]
},
comparePoints: (a, b) => {
const diffX = a[0] - b[0]
const diffY = a[1] - b[1]
const distance = global.getLength(diffX, diffY)
return {
hit: distance < (game.trackVertRadius * 2),
angle: Math.atan2(diffY, diffX)
}
},
tickGame: (now, players, state) => {
if (state.mode === 'intro') {
if (!state.track.isValid) {
game.inflateTrack(state)
state.startCircle.x = state.track.verts[3][0]
state.startCircle.y = state.track.verts[3][1]
} else {
let startGame = global.circleSelectCountdown(
now,
state.startCircle,
players,
state,
true
)
if (startGame) {
game.changeModeToPlay(players, state)
}
}
}
if (state.mode !== 'score') {
global.tickPlayers(now, players, state)
game.testPlayersAgainstTrackBounds(state)
if (state.track.isValid) {
game.testPlayersAgainstTrackQuads(state)
}
}
if (state.mode === 'play') {
state.timer -= 1
if (state.timer <= 0) {
game.changeModeToScore(players, state)
}
}
if (state.mode === 'score') {
global.animatePlayerScores(players, state)
}
return state
},
testPlayersAgainstTrackBounds (state) {
state.ships.forEach((ship) => {
const shipVert = [ship.x, ship.y]
const isShipInsideOuterPoly = inside(shipVert, state.track.outerPoly)
const isShipOutsideInnerPoly = !inside(shipVert, state.track.innerPoly)
const positionValid = isShipInsideOuterPoly && isShipOutsideInnerPoly
if (!positionValid) {
if (!ship.outCount) {
ship.xVel *= -1
ship.yVel *= -1
ship.x += ship.xVel
ship.y += ship.yVel
ship.hit = true
ship.outCount = (ship.outCount || 0) + 1
} else {
ship.x = state.track.verts[3][0]
ship.y = state.track.verts[3][1]
ship.xVel = 0
ship.yVel = 0
}
} else {
delete ship.outCount
}
})
},
testPlayersAgainstTrackQuads: (state) => {
const quads = state.track.meta.quads
const quadPlayerMap = state.track.meta.playerQuadBooleanStates
state.ships.forEach((ship) => {
let playerState = (
quadPlayerMap[ship.id] ||
quads.map(() => false)
)
let pointsThisLap = 0
let lastQuad = 0
playerState.forEach((playerHasBeenToThisQuadThisLap, index) => {
if (
!playerHasBeenToThisQuadThisLap &&
inside([ship.x, ship.y], quads[index])
) {
playerHasBeenToThisQuadThisLap = playerState[index] = true
ship.score += 1
lastQuad = index
}
if (playerHasBeenToThisQuadThisLap) {
pointsThisLap += 1
}
})
if (pointsThisLap === quads.length) {
playerState = quads.map(() => false)
playerState[lastQuad] = true
}
quadPlayerMap[ship.id] = playerState
})
},
putShipsAtStart: (state) => {
const a = state.track.outerPoly[3]
const b = state.track.innerPoly[3]
state.ships.forEach((ship, index) => {
const frac = (index + 0.5) / state.ships.length
const closer = global.mapRange(0, 1, 0.2, 0.8, frac)
ship.x = global.lerp(a[0], b[0], closer) - 0.05
ship.y = global.lerp(a[1], b[1], closer)
})
}
}
module.exports = game