Skip to content

Commit 9f8c188

Browse files
committed
highlight周りのtick関数を名前変更&移動
1 parent 9093ca5 commit 9f8c188

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

src/main.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Container, type Ticker } from "pixi.js";
1+
import { Application, Container, Sprite, 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,19 +32,6 @@ 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-
4835
// Create a new application
4936
const app = new Application();
5037
const stage = new Container();
@@ -104,11 +91,14 @@ export async function setup(
10491
const uiContext = derived([state, history], ([$state, $history]) => {
10592
return useUI($state, $history);
10693
});
94+
const highlight = new Sprite();
95+
stage.addChild(highlight);
10796
const cx: Context = {
10897
_stage_container: stage,
10998
grid,
11099
dynamic: {
111100
focus: null,
101+
highlight,
112102
player: {
113103
// HACK: these values are immediately overwritten inside Player.init().
114104
sprite: null,
@@ -141,13 +131,7 @@ export async function setup(
141131
cx.dynamic.player = Player.init(cx, bunnyTexture);
142132
app.ticker.add(unlessPaused((ticker) => Player.tick(cx, ticker)));
143133

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

152136
// Append the application canvas to the document body
153137
el.appendChild(app.canvas);

src/player.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,24 @@ 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 createHighlight(cx: Context) {
73+
export function highlightTick(cx: Context) {
7474
const state = get(cx.state);
7575
const player = cx.dynamic.player;
76+
const highlight = cx.dynamic.highlight;
7677
const { blockSize, marginY } = get(cx.config);
77-
if (!player.holdingKeys[Inputs.Ctrl] || !player.onGround) return;
78-
const texture =
78+
if (!player.holdingKeys[Inputs.Ctrl] || !player.onGround) {
79+
// no highlight
80+
highlight.scale = 0;
81+
return;
82+
}
83+
highlight.texture =
7984
state.inventory === null ? highlightTexture : highlightHoldTexture;
80-
const highlight: Sprite = new Sprite(texture);
85+
highlight.scale = 1;
8186
highlight.width = blockSize;
8287
highlight.height = blockSize;
8388
const highlightCoords = Ability.focusCoord(getCoords(cx), player.facing);
8489
highlight.x = highlightCoords.x * blockSize;
8590
highlight.y = highlightCoords.y * blockSize + marginY;
86-
return highlight;
8791
}
8892

8993
export function handleInput(

src/public-types.ts

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

0 commit comments

Comments
 (0)