|
1 | | -import { Application, Container, type Ticker } from "pixi.js"; |
| 1 | +import { Application, Container, Sprite, type Ticker } from "pixi.js"; |
2 | 2 | import { derived, get, writable } from "svelte/store"; |
3 | 3 | import { Facing } from "./constants.ts"; |
4 | 4 | import { Grid, createCellsFromStageDefinition } from "./grid.ts"; |
@@ -32,19 +32,6 @@ export async function setup( |
32 | 32 | } |
33 | 33 | }; |
34 | 34 |
|
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 | | - |
48 | 35 | // Create a new application |
49 | 36 | const app = new Application(); |
50 | 37 | const stage = new Container(); |
@@ -104,11 +91,14 @@ export async function setup( |
104 | 91 | const uiContext = derived([state, history], ([$state, $history]) => { |
105 | 92 | return useUI($state, $history); |
106 | 93 | }); |
| 94 | + const highlight = new Sprite(); |
| 95 | + stage.addChild(highlight); |
107 | 96 | const cx: Context = { |
108 | 97 | _stage_container: stage, |
109 | 98 | grid, |
110 | 99 | dynamic: { |
111 | 100 | focus: null, |
| 101 | + highlight, |
112 | 102 | player: { |
113 | 103 | // HACK: these values are immediately overwritten inside Player.init(). |
114 | 104 | sprite: null, |
@@ -141,13 +131,7 @@ export async function setup( |
141 | 131 | cx.dynamic.player = Player.init(cx, bunnyTexture); |
142 | 132 | app.ticker.add(unlessPaused((ticker) => Player.tick(cx, ticker))); |
143 | 133 |
|
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))); |
151 | 135 |
|
152 | 136 | // Append the application canvas to the document body |
153 | 137 | el.appendChild(app.canvas); |
|
0 commit comments