-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayground.svelte
404 lines (373 loc) · 10.4 KB
/
Playground.svelte
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<script>
import { onMount, onDestroy } from "svelte";
import { CLICK_POINTS, MAX_TURNS, TURN_SUCCESS } from "../constants.js";
import { clamp } from "../helpers.js";
import gameState, { send } from "../state/game.js";
import { gamePhaseDurations, instaDeath, playgroundSize, targetSize } from "../state/setup.js";
import Button from "./Button.svelte";
import Countdown from "./Countdown.svelte";
let turnSuccess;
let turnsCount = 0;
let score = 0;
let successCount = 0;
let misclickCount = 0;
let progressPercent = "";
let timeout;
let misclickPosition = { x: -100, y: -100 };
let misclickStateClass = "";
let targetPosition = { x: -100, y: -100 };
let targetStateClass = "";
$: {
progressPercent = clamp(0, Math.round((turnsCount / MAX_TURNS) * 100), 100);
targetStateClass = getTargetStateClass(turnSuccess, $instaDeath);
}
// Listen to state transitions.
// This should give us a first state with `{ playing: "start" }` as its value.
onMount(() => {
gameState.onTransition(startPhase);
});
// Clean up listeners and timers
onDestroy(() => {
gameState.off(startPhase);
clearTimeout(timeout);
});
/**
* At the start of a game phase (countdown, turn or cooldown),
* update the UI and schedule the next phase.
*/
function startPhase(state) {
const phaseKey = state.value.playing;
if (!state.changed || !phaseKey || !(phaseKey in $gamePhaseDurations)) {
return;
}
// Different events can lead to competing timeouts
clearTimeout(timeout);
// Event to send at the end of this phase
let onPhaseEnd = () => send("NEXT");
// Start new turns with a blank success state and new position
if (phaseKey === "turn") {
turnsCount += 1;
turnSuccess = TURN_SUCCESS.PLAYING;
targetPosition = getTargetPosition(targetPosition);
}
// In the cooldown phase, analyse the last turn
if (phaseKey === "cooldown") {
// Register the previous turn's failure or success
if (turnSuccess === TURN_SUCCESS.SUCCESS) {
successCount += 1;
}
// If not successful when cooldown starts, set the turnSuccess to
// update the UI and make the target disappear
if (turnSuccess === TURN_SUCCESS.PLAYING) {
turnSuccess = TURN_SUCCESS.MISSED;
}
// Should we end the game at the end of the cooldown?
// Normal mode: after MAX_TURNS
// InstaDeath: if the previous turn was a failure
if (
(!$instaDeath && turnsCount >= MAX_TURNS) ||
($instaDeath && turnSuccess !== TURN_SUCCESS.SUCCESS)
) {
onPhaseEnd = showResults;
}
}
// Schedule next phase
timeout = setTimeout(onPhaseEnd, $gamePhaseDurations[phaseKey]);
}
function showResults() {
send({
type: "SHOW_RESULTS",
resultData: {
score,
turnsCount,
successCount,
misclickCount,
},
});
}
/**
* In normal mode, players can click however many times they want,
* but misclicks will dock their score.
*/
function recordClick(onTarget) {
// Conditions for success: on target, only counted once, and during the turn
// (not during cooldown while the target is fading out).
if (
onTarget === true &&
turnSuccess === TURN_SUCCESS.PLAYING &&
$gameState.matches("playing.turn")
) {
score += CLICK_POINTS.SUCCESS;
turnSuccess = TURN_SUCCESS.SUCCESS;
}
// Count all misclicks, at any phase in the game.
// Do not set the TURN_SUCCESS.MISSED state, since it’s final and will make
// the target disappear. Instead, we want players to still be able to try.
if (onTarget === false) {
score = Math.max(0, score + CLICK_POINTS.MISSED);
misclickCount += 1;
}
}
/**
* In instadeath mode, any click ends the turn.
*/
function recordInstadeathClick(onTarget) {
if (onTarget === true && turnSuccess === TURN_SUCCESS.PLAYING) {
turnSuccess = TURN_SUCCESS.SUCCESS;
} else {
turnSuccess = TURN_SUCCESS.MISSED;
}
send("END_TURN");
}
function onSuccess(event) {
event.preventDefault();
event.stopPropagation();
if ($instaDeath) {
recordInstadeathClick(true);
} else {
recordClick(true);
}
}
function onMisclick(event) {
event.preventDefault();
event.stopPropagation();
displayMisclick(event.offsetX, event.offsetY);
if ($instaDeath) {
recordInstadeathClick(false);
} else {
recordClick(false);
}
}
function getTargetStateClass(state, isInstaDeath) {
switch (state) {
case TURN_SUCCESS.SUCCESS:
return "__success__";
case TURN_SUCCESS.MISSED:
return isInstaDeath ? "__fatal__" : "__missed__";
case TURN_SUCCESS.PLAYING:
return "__playing__";
default:
return "__hidden__";
}
}
function displayMisclick(x, y) {
const container = document.querySelector(".precision-container");
container.classList.remove("__misclick__");
misclickStateClass = "";
misclickPosition = { x, y };
requestAnimationFrame(() => {
misclickStateClass = "__reveal__";
container.classList.add("__misclick__");
});
}
function getTargetPosition(oldPosition) {
const maxLength = $playgroundSize - $targetSize;
const minDiff = maxLength / 2;
let i = 0;
let newPosition;
while (i < 20) {
i++;
newPosition = newTargetPosition(maxLength, 10);
if (!oldPosition) {
break;
}
const diffX = Math.max(oldPosition.x, newPosition.x) - Math.min(oldPosition.x, newPosition.x);
const diffY = Math.max(oldPosition.y, newPosition.y) - Math.min(oldPosition.y, newPosition.y);
if (diffX > minDiff || diffY > minDiff) {
break;
}
}
return newPosition;
}
function newTargetPosition(maxLength, padding = 0) {
const max = maxLength + 1 - padding * 2;
const x = Math.floor(Math.random() * max) + padding;
const y = Math.floor(Math.random() * max) + padding;
return { x, y };
}
function restartGame() {
send("SHOW_SETUP");
}
</script>
<style>
/* Toolbar */
.precision-toolbar {
position: relative;
display: flex;
flex-wrap: wrap;
align-items: center;
font-size: 14px;
min-height: 44px;
padding: 4px;
font-size: 16px;
color: var(--color-text-light);
background-color: var(--color-background-dark);
}
.precision-toolbar-score {
flex-grow: 1;
padding: 4px 12px;
}
.precision-toolbar-buttons {
padding: 4px 6px;
text-align: right;
}
.precision-progress {
position: absolute;
bottom: 0;
left: 0;
max-width: 100%;
height: 6px;
background-color: rgba(0, 0, 0, 0.6);
transition: width calc(var(--game-speed) * 0.5) cubic-bezier(0.22, 0.61, 0.36, 1);
}
/* Play area */
.precision-playground {
position: relative;
min-height: var(--playground-size);
box-sizing: content-box;
-webkit-user-select: none;
user-select: none;
}
/* Clickable target */
.precision-target-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.precision-target {
position: absolute;
box-sizing: border-box;
top: var(--y);
left: var(--x);
width: var(--target-size);
height: var(--target-size);
margin: 0;
padding: 0;
border-radius: 2px;
border: solid 2px var(--target-border);
background-color: var(--target-background);
}
.precision-target[aria-hidden="true"] {
display: none;
}
.precision-target.__hidden__0 {
visibility: hidden;
}
.precision-target.__success__ {
background-color: lime;
box-shadow: 0 0 10px lime;
animation: 350ms ease-out forwards success-flash-out;
}
.precision-target.__missed__ {
pointer-events: none;
background-color: hsl(0, 0%, 75%);
border-color: transparent;
border-radius: 15%;
animation: 250ms ease-out forwards missed-flash-out;
}
.precision-target.__fatal__ {
pointer-events: none;
background-color: red;
border-color: transparent;
animation: calc(var(--game-speed) * 0.5) ease-out forwards missed-explode;
}
/* Flashing indicator for misclicks */
.precision-misclick {
will-change: transform;
position: absolute;
box-sizing: border-box;
top: calc(var(--y) - 16px);
left: calc(var(--x) - 16px);
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
background-color: red;
border-radius: 50%;
pointer-events: none;
opacity: 0;
}
.precision-misclick.__reveal__ {
animation: 250ms ease-out forwards misclick-flash-out;
}
.precision-misclick::before {
content: "";
display: block;
width: 6px;
height: 6px;
border-radius: 50%;
background-color: white;
}
@keyframes success-flash-out {
0% {
opacity: 1;
pointer-events: all;
}
79% {
pointer-events: all;
}
80% {
pointer-events: none;
}
100% {
transform: scale(1.5);
opacity: 0;
pointer-events: none;
}
}
@keyframes missed-flash-out {
0% {
opacity: 1;
}
100% {
opacity: 0;
transform: scale(0.25) rotate(0.4turn);
}
}
@keyframes missed-explode {
0% {
opacity: 1;
}
100% {
opacity: 0;
transform: scale(12) rotate(2turn);
}
}
@keyframes misclick-flash-out {
from {
opacity: 1;
transform: scale(1);
}
to {
transform: scale(2);
opacity: 0;
}
}
</style>
<header class="precision-toolbar">
<span class="precision-toolbar-score"> <strong>Score : {score}</strong> </span>
<span class="precision-toolbar-buttons">
<Button dark on:click={restartGame} text="🔄 Recommencer" />
</span>
{#if !$instaDeath}<span class="precision-progress" style="width: {progressPercent}%" />{/if}
</header>
<div class="precision-playground">
<div class="precision-target-wrapper" on:mousedown={onMisclick}>
<button
aria-hidden={String(!($gameState.matches('playing.turn') || $gameState.matches('playing.cooldown')))}
aria-label="Cible"
class="precision-target {targetStateClass}"
style="--x: {targetPosition.x}px; --y: {targetPosition.y}px;"
on:mousedown={onSuccess} />
</div>
{#if $gameState.matches('playing.countdown')}
<Countdown />
{/if}
<span
class="precision-misclick {misclickStateClass}"
style="--x: {misclickPosition.x}px; --y: {misclickPosition.y}px;" />
</div>