Skip to content

Commit 309018d

Browse files
Merge pull request #24 from remarkablegames/feat/restart
feat: add "Restart" button
2 parents e70460f + 5e07d91 commit 309018d

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<h1 class="basis-1/2 self-end">Code Arcade</h1>
5353
<div class="flex justify-between w-1/2">
5454
<button class="btn-primary">► Run</button>
55+
<button class="btn-tertiary">⟳ Restart</button>
5556
<button class="btn-secondary">ⓘ Hint</button>
5657
</div>
5758
</div>
@@ -62,6 +63,7 @@ <h1 class="xl:hidden">Code Arcade</h1>
6263
<div id="editor"></div>
6364
<div class="xl:hidden">
6465
<button class="btn-primary">► Run</button>
66+
<button class="btn-tertiary">⟳ Restart</button>
6567
<button class="btn-secondary">ⓘ Hint</button>
6668
</div>
6769
</div>

src/helpers/events.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ const audio = new Audio('sounds/score.mp3')
33
type Callback = () => void
44

55
/**
6-
* Attaches click listeners to "Run" and "Hint" buttons.
6+
* Attaches click listeners to "Run", "Restart", and "Hint" buttons.
77
*
88
* @param runCallback - Run callback.
9+
* @param restartCallback - Restart callback.
910
* @param hintCallback - Hint callback.
1011
*/
1112
export function addEventListeners(
1213
runCallback: Callback,
14+
restartCallback: Callback,
1315
hintCallback: Callback,
1416
) {
1517
const runButtons = document.querySelectorAll(
@@ -23,6 +25,17 @@ export function addEventListeners(
2325
}
2426
})
2527

28+
const restartButtons = document.querySelectorAll(
29+
'main .btn-tertiary',
30+
) as NodeListOf<HTMLButtonElement>
31+
32+
restartButtons.forEach((restartButton) => {
33+
restartButton.onclick = () => {
34+
audio.play()
35+
restartCallback()
36+
}
37+
})
38+
2639
const hintButtons = document.querySelectorAll(
2740
'main .btn-secondary',
2841
) as NodeListOf<HTMLButtonElement>

src/scenes/game.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ export async function go(levelNumber: number) {
3636
},
3737
})
3838

39+
run()
40+
3941
function run() {
4042
const script = editorView.state.doc.toString()
4143
iframe.srcdoc = wrapGame({ ...level, script })
4244
}
4345

44-
run()
46+
function restart() {
47+
editorView.dispatch({
48+
changes: {
49+
from: 0,
50+
to: editorView.state.doc.length,
51+
insert: level.script?.trim(),
52+
},
53+
})
54+
run()
55+
}
4556

4657
function hint() {
4758
iframe.contentWindow?.postMessage({
@@ -50,7 +61,7 @@ export async function go(levelNumber: number) {
5061
})
5162
}
5263

53-
addEventListeners(run, hint)
64+
addEventListeners(run, restart, hint)
5465
}
5566

5667
/**

src/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
.btn-secondary {
2626
@apply bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded;
2727
}
28+
29+
.btn-tertiary {
30+
@apply bg-yellow-500 hover:bg-yellow-400 text-white font-bold py-2 px-4 border-b-4 border-yellow-700 hover:border-yellow-500 rounded;
31+
}
2832
}
2933

3034
@layer utilities {

0 commit comments

Comments
 (0)