Skip to content

Commit 96ffc9c

Browse files
committed
ポーズ時にはtickを実行しない
1 parent c620055 commit 96ffc9c

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/main.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Container } from "pixi.js";
1+
import { Application, Container, type Ticker } from "pixi.js";
22
import type { Writable } from "svelte/store";
33
import type { Context, UIContext } from "./context.ts";
44
import { Grid } from "./grid.ts";
@@ -11,6 +11,16 @@ export async function setup(
1111
stageDefinition: StageDefinition,
1212
uiContext: Writable<UIContext>,
1313
) {
14+
let paused = false;
15+
uiContext.subscribe((v) => {
16+
paused = v.paused;
17+
});
18+
const unlessPaused = (f: (ticker: Ticker) => void) => (ticker: Ticker) => {
19+
if (!paused) {
20+
f(ticker);
21+
}
22+
};
23+
1424
function tick() {
1525
// highlight is re-rendered every tick
1626
const highlight = player.createHighlight(cx);
@@ -50,19 +60,23 @@ export async function setup(
5060
elapsed: 0,
5161
uiContext,
5262
};
53-
app.ticker.add((ticker) => {
54-
cx.elapsed += ticker.deltaTime;
55-
});
63+
app.ticker.add(
64+
unlessPaused((ticker) => {
65+
cx.elapsed += ticker.deltaTime;
66+
}),
67+
);
5668

5769
const player = new Player(cx, bunnyTexture);
58-
app.ticker.add((ticker) => player.tick(cx, ticker));
70+
app.ticker.add(unlessPaused((ticker) => player.tick(cx, ticker)));
5971
app.stage.addChild(player.sprite);
6072

6173
let cleanup: undefined | (() => void) = undefined;
62-
app.ticker.add(() => {
63-
if (cleanup) cleanup();
64-
cleanup = tick();
65-
});
74+
app.ticker.add(
75+
unlessPaused(() => {
76+
if (cleanup) cleanup();
77+
cleanup = tick();
78+
}),
79+
);
6680

6781
// Append the application canvas to the document body
6882
el.appendChild(app.canvas);

0 commit comments

Comments
 (0)