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