Skip to content

Commit 6101831

Browse files
committed
Revert "highlight周りのtick関数を名前変更&移動"
This reverts commit 9f8c188.
1 parent 9f8c188 commit 6101831

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/main.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Container, Sprite, type Ticker } from "pixi.js";
1+
import { Application, Container, type Ticker } from "pixi.js";
22
import { derived, get, writable } from "svelte/store";
33
import { Facing } from "./constants.ts";
44
import { Grid, createCellsFromStageDefinition } from "./grid.ts";
@@ -32,6 +32,19 @@ export async function setup(
3232
}
3333
};
3434

35+
function tick() {
36+
// highlight is re-rendered every tick
37+
const highlight = Player.createHighlight(cx);
38+
if (highlight) {
39+
stage.addChild(highlight);
40+
}
41+
return () => {
42+
if (highlight) {
43+
stage.removeChild(highlight);
44+
}
45+
};
46+
}
47+
3548
// Create a new application
3649
const app = new Application();
3750
const stage = new Container();
@@ -91,14 +104,11 @@ export async function setup(
91104
const uiContext = derived([state, history], ([$state, $history]) => {
92105
return useUI($state, $history);
93106
});
94-
const highlight = new Sprite();
95-
stage.addChild(highlight);
96107
const cx: Context = {
97108
_stage_container: stage,
98109
grid,
99110
dynamic: {
100111
focus: null,
101-
highlight,
102112
player: {
103113
// HACK: these values are immediately overwritten inside Player.init().
104114
sprite: null,
@@ -131,7 +141,13 @@ export async function setup(
131141
cx.dynamic.player = Player.init(cx, bunnyTexture);
132142
app.ticker.add(unlessPaused((ticker) => Player.tick(cx, ticker)));
133143

134-
app.ticker.add(unlessPaused(() => Player.highlightTick(cx)));
144+
let cleanup: undefined | (() => void) = undefined;
145+
app.ticker.add(
146+
unlessPaused(() => {
147+
if (cleanup) cleanup();
148+
cleanup = tick();
149+
}),
150+
);
135151

136152
// Append the application canvas to the document body
137153
el.appendChild(app.canvas);

src/player.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,20 @@ export function getCoords(cx: Context) {
7070
const y = Math.round((coords.y - marginY) / blockSize) - 1; // it was not working well so take my patch
7171
return { x, y };
7272
}
73-
export function highlightTick(cx: Context) {
73+
export function createHighlight(cx: Context) {
7474
const state = get(cx.state);
7575
const player = cx.dynamic.player;
76-
const highlight = cx.dynamic.highlight;
7776
const { blockSize, marginY } = get(cx.config);
78-
if (!player.holdingKeys[Inputs.Ctrl] || !player.onGround) {
79-
// no highlight
80-
highlight.scale = 0;
81-
return;
82-
}
83-
highlight.texture =
77+
if (!player.holdingKeys[Inputs.Ctrl] || !player.onGround) return;
78+
const texture =
8479
state.inventory === null ? highlightTexture : highlightHoldTexture;
85-
highlight.scale = 1;
80+
const highlight: Sprite = new Sprite(texture);
8681
highlight.width = blockSize;
8782
highlight.height = blockSize;
8883
const highlightCoords = Ability.focusCoord(getCoords(cx), player.facing);
8984
highlight.x = highlightCoords.x * blockSize;
9085
highlight.y = highlightCoords.y * blockSize + marginY;
86+
return highlight;
9187
}
9288

9389
export function handleInput(

src/public-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export type Context = {
6363
// no need to serialize.
6464
dynamic: {
6565
focus: Coords | null; // current focus coordinates
66-
highlight: Sprite;
6766
player: {
6867
holdingKeys: { [key in Inputs]?: boolean };
6968
coords: () => { x: number; y: number };

0 commit comments

Comments
 (0)