Skip to content

Commit 786db71

Browse files
committed
stage番号もクエリパラメータに含めて、ポーズメニューから現在のステージを選択した状態のステージセレクトに戻れるようにする
1 parent c4a1d30 commit 786db71

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

routes/stage-select/+page.svelte

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
import Key from "@/ui-components/Key.svelte";
33
import { onMount } from "svelte";
44
import "@/ui-components/menu/menu.css";
5+
import { replaceState } from "$app/navigation";
56
6-
let w = "1";
7+
let w: string | null = null;
8+
let selected: number | null = null;
79
810
onMount(() => {
911
const params = new URLSearchParams(window.location.search);
1012
w = params.get("w") ?? "1";
13+
selected = Number(params.get("s") ?? "1") - 1;
1114
});
1215
1316
$: blocks = [
@@ -17,7 +20,6 @@ $: blocks = [
1720
{ label: "4", link: `/game?stage=${w}-4` },
1821
];
1922
20-
let selected = 0;
2123
let lastKeyTime = 0;
2224
let lastKey: string | null = null;
2325
const KEY_REPEAT_DELAY = 180; // ms
@@ -27,15 +29,22 @@ function prevWorld() {
2729
w = String(Math.max(1, Number(w) - 1));
2830
const url = new URL(window.location.href);
2931
url.searchParams.set("w", w);
30-
window.history.replaceState(null, "", url.toString());
32+
replaceState(url.toString(), {});
3133
}
3234
function nextWorld() {
3335
// wを数値として1増やす(4より大きくしない)
3436
w = String(Math.min(4, Number(w) + 1));
3537
const url = new URL(window.location.href);
3638
url.searchParams.set("w", w);
37-
window.history.replaceState(null, "", url.toString());
39+
replaceState(url.toString(), {});
3840
}
41+
function select(index: number): void {
42+
selected = index;
43+
const url = new URL(window.location.href);
44+
url.searchParams.set("s", String(index + 1));
45+
replaceState(url.toString(), {});
46+
}
47+
3948
function handleKey(e: KeyboardEvent): void {
4049
const now = Date.now();
4150
if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
@@ -46,23 +55,27 @@ function handleKey(e: KeyboardEvent): void {
4655
lastKey = e.key;
4756
}
4857
58+
if (selected === null) {
59+
return;
60+
}
61+
4962
if (e.key === "ArrowRight") {
5063
if (selected === blocks.length - 1) {
5164
if (w !== "4") {
5265
nextWorld();
53-
selected = 0;
66+
select(0);
5467
}
5568
} else {
56-
selected += 1;
69+
select(selected + 1);
5770
}
5871
} else if (e.key === "ArrowLeft") {
5972
if (selected === 0) {
6073
if (w !== "1") {
6174
prevWorld();
62-
selected = blocks.length - 1;
75+
select(blocks.length - 1);
6376
}
6477
} else {
65-
selected -= 1;
78+
select(selected - 1);
6679
}
6780
} else if (e.key === "Enter" || e.key === " ") {
6881
window.location.href = blocks[selected].link;
@@ -73,10 +86,6 @@ function handleKeyUp() {
7386
lastKey = null;
7487
}
7588
76-
function handleClick(index: number): void {
77-
selected = index;
78-
}
79-
8089
let container: HTMLDivElement | null = null;
8190
8291
onMount(() => {
@@ -131,7 +140,7 @@ onMount(() => {
131140
class={`appearance-none focus:outline-none bg-white border-6 pt-8 pb-6 pl-8 pr-6 transition-colors duration-200 text-7xl cursor-pointer ${
132141
selected === i ? 'border-red-500 ring ring-red-500' : 'border-base'
133142
}`}
134-
on:click={() => handleClick(i)}
143+
on:click={() => select(i)}
135144
>
136145
{block.label}
137146
</button>
@@ -145,7 +154,13 @@ onMount(() => {
145154
<div class="flex justify-center items-center basis-2/5 min-h-0 shrink grow-2">
146155
<!-- 画像を中央に配置 -->
147156
<div class="h-full ">
148-
<img src="/assets/thumbnail{w}-{selected+1}.png" alt="" class="h-full " />
157+
{#if selected !== null}
158+
<img
159+
src="/assets/thumbnail{w}-{selected+1}.png"
160+
alt=""
161+
class="h-full "
162+
/>
163+
{/if}
149164
</div>
150165
<!-- テキストを画像の右側に配置 -->
151166
<div class="flex-none w-max max-h-full flex flex-col items-start bg-white/90 p-4 m-4 rounded-lg border-2">

src/ui-components/Game.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ onDestroy(() => bindings.destroy());
6363
// if this isn't working well, we can use window.location.reload(); instead
6464
bindings.reset();
6565
}}
66+
stageNum={stageNum}
6667
/>
6768
<GoalMenu
6869
goaled={uiContext.goaled}
6970
{nextStage}
7071
reset={() => bindings.reset()}
72+
stageNum={stageNum}
7173
/>
72-
<GameOverMenu gameover={uiContext.gameover} reset={() => bindings.reset()} />
74+
<GameOverMenu gameover={uiContext.gameover} reset={() => bindings.reset()} stageNum={stageNum} />
7375
<div
7476
class="uiBackground"
7577
style="position: fixed; left: 0; top: 0; right: 0; display: flex; align-items: end; "

src/ui-components/menu/GameOverMenu.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import "./menu.css";
55
type Props = {
66
gameover: boolean;
77
reset: () => void;
8+
stageNum: string;
89
};
9-
const { gameover, reset }: Props = $props();
10+
const { gameover, reset, stageNum }: Props = $props();
1011
let el: HTMLDialogElement;
1112
$effect(() => {
1213
if (gameover) {
@@ -36,6 +37,6 @@ document.addEventListener("keydown", (ev) => {
3637
>
3738
Restart
3839
</button>
39-
<a class="btn modal-btn" href="/stage-select">Back to Stage Select</a>
40+
<a class="btn modal-btn" href="/stage-select?w={stageNum.split("-")[0]}&s={stageNum.split("-")[1]}"> Back to Stage Select </a>
4041
</div>
4142
</dialog>

src/ui-components/menu/GoalMenu.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ type Props = {
77
goaled: boolean;
88
nextStage: string;
99
reset: () => void;
10+
stageNum: string;
1011
};
11-
const { goaled, nextStage, reset }: Props = $props();
12+
const { goaled, nextStage, reset, stageNum }: Props = $props();
1213
let el: HTMLDialogElement;
1314
$effect(() => {
1415
if (goaled) {
@@ -41,6 +42,6 @@ onDestroy(() => {
4142
>
4243
Restart
4344
</button>
44-
<a class="btn modal-btn" href="/stage-select">Back to Stage Select</a>
45+
<a class="btn modal-btn" href="/stage-select?w={stageNum.split("-")[0]}&s={stageNum.split("-")[1]}"> Back to Stage Select </a>
4546
</div>
4647
</dialog>

src/ui-components/menu/PauseMenu.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ type Props = {
66
onresume: () => void;
77
onpause: () => void;
88
onreset: () => void;
9+
stageNum: string;
910
};
10-
let { paused, alreadyStopped, onresume, onpause, onreset }: Props = $props();
11+
let { paused, alreadyStopped, onresume, onpause, onreset, stageNum }: Props = $props();
1112
let el: HTMLDialogElement;
1213
$effect(() => {
1314
if (paused && !alreadyStopped) {
@@ -33,6 +34,6 @@ document.addEventListener("keydown", (ev) => {
3334
<button class="btn modal-btn" type="submit">Resume</button>
3435
</form>
3536
<button class="btn modal-btn" onclick={() => onreset()}>Restart</button>
36-
<a class="btn modal-btn" href="/stage-select"> Back to Stage Select </a>
37+
<a class="btn modal-btn" href="/stage-select?w={stageNum.split("-")[0]}&s={stageNum.split("-")[1]}"> Back to Stage Select </a>
3738
</div>
3839
</dialog>

0 commit comments

Comments
 (0)