-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame-globals.js
More file actions
264 lines (261 loc) · 8.38 KB
/
game-globals.js
File metadata and controls
264 lines (261 loc) · 8.38 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
global.tau = Math.PI * 2
global.ticksPerSecond = 100
global.screenWidth = 2
global.arrayRemove = (array, item) => {
let index
while ((index = array.indexOf(item)) !== -1) {
array.splice(index, 1)
}
return array
}
global.rangeRand = (min, max) => {
const diff = max - min
return min + (diff * Math.random())
}
global.mapRange = (a, b, x, y, valueAB) => {
const diff0 = a - b
const diff1 = x - y
const valueDiff = (valueAB - b) / diff0
return y + (valueDiff * diff1)
}
global.bound = (min, max, value) => {
return Math.min(max, Math.max(min, value))
}
global.detectCollision = (a, b) => {
return global.getDistance(a, b) < a.radius + b.radius
}
global.getDistance = (a, b) => {
const diffX = a.x - b.x
const diffY = a.y - b.y
return global.getLength(diffX, diffY)
}
global.getLength = (x, y) => { return Math.sqrt((x * x) + (y * y)) }
global.wrapAxis = (n) => {
return (Math.abs(n) < 1) ? n : n + (Math.sign(n) * -2)
}
global.wrap = (target) => {
target.x = global.wrapAxis(target.x)
target.y = global.wrapAxis(target.y)
}
global.randomEdgePosition = () => {
const angle = Math.random() * global.tau
const radius = 2
return {
x: global.bound(-1, 1, Math.cos(angle) * radius),
y: global.bound(-1, 1, Math.sin(angle) * radius)
}
}
global.lerp = (a, b, progress) => {
return a + ((b - a) * progress)
}
global.shipDrag = 0.955
global.shipMaxSpeed = 1 / 80
global.playerMaxForceAddedPerFrame = 1 / 2000
global.tickPlayers = (now, players, state, options) => {
state.ships.forEach(ship => {
let player = players[ship.id]
ship.xVel *= global.shipDrag
ship.yVel *= global.shipDrag
if (player.onTime !== null && !ship.hit) {
const timeDiff = now - player.onTime
const accelerationRampUp = Math.min(1, timeDiff / 1000)
const playerAddedForce = player.force * accelerationRampUp * global.playerMaxForceAddedPerFrame
const xVel = ship.xVel + Math.cos(player.angle) * playerAddedForce
const yVel = ship.yVel + Math.sin(player.angle) * playerAddedForce
ship.angle = Math.atan2(yVel, xVel)
ship.force = Math.min(global.getLength(xVel, yVel), global.shipMaxSpeed)
ship.xVel = Math.cos(ship.angle) * ship.force
ship.yVel = Math.sin(ship.angle) * ship.force
} else {
ship.force = global.getLength(ship.xVel, ship.yVel)
if (ship.hit && (ship.force < 0.001)) {
if (player.onTime !== null) {
player.onTime = now
}
delete ship.hit
}
}
ship.x += ship.xVel
ship.y += ship.yVel
if (!options || !options.noWrap) {
global.wrap(ship)
}
})
}
global.activityCircleDefaults = {
x: 0,
y: 0,
frac: 0,
radius: 1 / 8,
hue: 0,
sec: 0,
tick: 0,
ticksToActivate: 3 * global.ticksPerSecond
}
global.createActivityCircle = (config) => {
return Object.assign({}, global.activityCircleDefaults, config)
}
global.playersInCircle = (circle, players, state) => {
const result = {
in: [],
out: []
}
state.ships.forEach((ship) => {
const inCircle = global.detectCollision(ship, circle)
const player = players[ship.id]
const destination = inCircle ? 'in' : 'out'
result[destination].push(player)
})
return result
}
global.durationInactivityBoot = 10 * 1000 // time in ms
global.circleSelectCountdown = (now, circle, players, state, bootIfInactive) => {
const playerCircleStates = global.playersInCircle(circle, players, state)
const readyPlayerCount = playerCircleStates.in.length
const allPlayersReady = readyPlayerCount && readyPlayerCount === state.ships.length
let activate = false
if (!allPlayersReady && bootIfInactive) {
playerCircleStates.out.forEach((player) => {
if (now - player.lastActiveTime > global.durationInactivityBoot) {
player.needsBooting = true
}
})
}
if (allPlayersReady) {
global.tickCircle(circle, circle.tick + 1)
activate = circle.tick >= circle.ticksToActivate
if (activate) {
setTimeout(() => { global.tickCircle(circle, 0) }, 200)
}
} else {
global.tickCircle(circle, Math.max(0, circle.tick - 1))
}
return activate
}
global.tickCircle = (circle, tick = circle.tick) => {
circle.tick = tick
circle.frac = circle.tick / circle.ticksToActivate
circle.sec = circle.frac ? parseFloat(((circle.ticksToActivate / 100) * (1 - circle.frac)).toPrecision(2)).toFixed(1) : undefined
}
global.totalPlayerScores = (players, state) => {
const snapshots = {
shipStateInitial: [],
shipStateA: [],
shipStateC: []
}
state.mode = 'score'
state.timer = global.durationScore
let highScore = 0
let shipCount = state.ships.length
const shipStartYPosition = 0.8
state.ships.forEach((ship) => {
ship.score = parseFloat(ship.score) || 0 // NaN protection
highScore = Math.max(highScore, ship.score)
ship.hit = false
})
highScore = highScore || 1 // protect against division by 0 if all players score 0
snapshots.shipStateInitial = JSON.parse(JSON.stringify(state.ships))
snapshots.shipStateA = JSON.parse(JSON.stringify(snapshots.shipStateInitial))
snapshots.shipStateA.forEach((ship, index) => {
ship.score = 0
ship.x = (((index + 0.5) / shipCount) - 0.5) * (0.8 * 2)
ship.y = shipStartYPosition
ship.angle = global.tau * 0.75
})
snapshots.shipStateC = JSON.parse(JSON.stringify(snapshots.shipStateA))
const mapY = (score) => {
return shipStartYPosition - (score / highScore)
}
const yMax = mapY(highScore)
snapshots.shipStateC.forEach((ship, index) => {
ship.score = state.ships[index].score
state.ships[index].score = 0
ship.scoreMax = highScore
ship.y = mapY(ship.score)
ship.yMax = yMax
})
state.scoreSnapshots = snapshots
}
global.durationScore = 10 * global.ticksPerSecond // Score display time = 15s,
global.durationScoreA = 0.95 * global.durationScore
global.durationScoreB = 0.85 * global.durationScore
global.durationScoreC = 0.45 * global.durationScore
global.durationScoreD = 0.4 * global.durationScore
global.animatePlayerScores = (players, state) => {
const snapshots = state.scoreSnapshots
let proceedToNextState = false
state.timer -= 1
if (state.timer <= 0) {
proceedToNextState = true
} else if (state.timer >= global.durationScoreA) {
let diff = state.timer - global.durationScoreA
let total = global.durationScore - global.durationScoreA
let progress = 1 - (diff / total)
global.lerpShips(state, snapshots.shipStateInitial, snapshots.shipStateA, progress)
} else if (state.timer >= global.durationScoreB) {
// just a pause
} else if (state.timer >= global.durationScoreC) {
let diff = state.timer - global.durationScoreC
let total = global.durationScoreB - global.durationScoreC
let progress = 1 - (diff / total)
global.lerpShips(state, snapshots.shipStateA, snapshots.shipStateC, progress)
} else if (state.timer <= global.durationScoreD) {
const blink = Math.floor((state.timer % 100) / 50) < 1
state.ships.forEach((ship, index) => {
const sourceData = snapshots.shipStateC[index]
if (sourceData) {
const status = sourceData.score === sourceData.scoreMax ? 'Winner!' : 'Loser!'
ship.score = blink ? status : ''
}
})
}
if (proceedToNextState) {
state.events.emit('end')
}
}
global.lerpShips = (state, startState, targetState, progress) => {
state.ships.forEach((ship, index) => {
const a = startState[index]
const b = targetState[index]
if (a !== undefined && b !== undefined) {
global.lerpShip(ship, a, b, progress)
global.wrap(ship)
}
})
}
global.lerpButStopEarly = (a, b, progress, earlyStop) => {
const rateOfChangeFromStart = global.lerp(
a,
b,
progress
) - a
const destinationDistanceFromStart = earlyStop - a
return a + (
Math.min(
Math.abs(rateOfChangeFromStart),
Math.abs(destinationDistanceFromStart)
) * Math.sign(destinationDistanceFromStart)
)
}
global.lerpShip = (target, a, b, progress) => {
target.x = global.lerp(a.x, b.x, progress)
target.angle = global.lerp(a.angle, b.angle, progress)
if (b.yMax === undefined) {
target.y = global.lerp(a.y, b.y, progress)
target.score = Math.floor(global.lerp(a.score, b.score, progress))
} else {
target.y = global.lerpButStopEarly(
a.y,
b.yMax,
progress,
b.y
)
const scoreLerp = global.lerpButStopEarly(
a.score,
b.scoreMax,
progress,
b.score
)
target.score = b.score !== scoreLerp ? Math.floor(scoreLerp) : b.score
}
}