Skip to content

Commit

Permalink
Port SceneLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
hubol committed Jan 13, 2024
1 parent 8728a71 commit f101ee3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/igua/core/scene-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { scene } from "../globals";
import { IguaScene } from "../igua-scene-stack";

let idSource = 0;

export class SceneLocal<T> {
constructor(private readonly _factory: (scene: IguaScene) => T, private readonly _sceneUniqueKey = `Anonymous@${idSource++}`) { }

get value(): T {
const value = scene.locals[this._sceneUniqueKey];
if (value)
return value;

return scene.locals[this._sceneUniqueKey] = this._factory(scene);
}
}
3 changes: 2 additions & 1 deletion src/igua/igua-scene-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ function createIguaScene(layers: IguaLayers, source: Function, meta: IguaSceneMe
const backgroundGfx = new Graphics().tinted(0x000000).beginFill(0xffffff).drawRect(0, 0, 256, 256).show(background);

return {
root,
set backgroundTint(tint: number) {
backgroundGfx.tint = tint;
},
locals: {} as Record<string, any>,
root,
source,
stage,
ticker,
Expand Down
20 changes: 18 additions & 2 deletions src/igua/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import { IguanaLooks } from "../iguana/looks";
import { lerp } from "../../lib/game-engine/promise/lerp";
import { createDebugPanel } from "../../lib/game-engine/debug/debug-panel";
import { approachLinear } from "../../lib/math/number";
import { SceneLocal } from "../core/scene-local";

const TailTextures = Tx.Iguana.Tail.split({ width: 28, trimFrame: true });

const BigKeyTextures = Tx.BigKey1.split({ count: 3, trimFrame: true });

const Score = new SceneLocal(() => ({ current: 0 }));

export function SceneTest() {
console.log('Scene', scene.source.name);

Expand Down Expand Up @@ -101,10 +104,14 @@ export function SceneTest() {
for (const collided of guy.collidesAll(sprites)) {
collided.tint = tint;
}
if (guy.collides(s))
if (guy.collides(s)) {
s.destroy();
if (guy.collides(smiley))
Score.value.current += 1;
}
if (guy.collides(smiley)) {
smiley.destroy();
Score.value.current += 1;
}

guy.health += 1;
})
Expand Down Expand Up @@ -308,5 +315,14 @@ export function SceneTest() {

scene.backgroundTint = 0x181050;

objScore().at(4, 242).show();

// document.body.appendChild(createDebugPanel(scene.stage));
}

function objScore() {
const score = objText.Large('Score: 0', { tint: 0x00ff00 })
.step(() => score.text = `Score: ${Score.value.current}`);

return score;
}

0 comments on commit f101ee3

Please sign in to comment.