diff --git a/src/App.ts b/src/App.ts index 3d077872..42060112 100644 --- a/src/App.ts +++ b/src/App.ts @@ -31,6 +31,7 @@ import { PhysicsSystem, initializePhysicsSystem, } from "./systems/PhysicsSystem"; +import { setPixiAppId } from "./components/PixiAppId"; if (module.hot) { module.hot.accept((getParents) => { @@ -51,7 +52,7 @@ export function startLoading(element: HTMLElement) { const entityId = addEntity(); setPosition(entityId, x * SPRITE_SIZE, y * SPRITE_SIZE); setLookLike(entityId, getNamedEntity(EntityName.FLOOR_IMAGE)); - setPixiApp(entityId, app); + setPixiAppId(entityId, defaultPixiAppId); setLayer(entityId, Layer.BACKGROUND); } } diff --git a/src/components/PixiAppId.ts b/src/components/PixiAppId.ts new file mode 100644 index 00000000..649853b1 --- /dev/null +++ b/src/components/PixiAppId.ts @@ -0,0 +1,25 @@ +import { invariant } from "../Error"; +import { setRenderStateDirty } from "../systems/RenderSystem"; +import { hasPixiApp } from "./PixiApp"; + +const DATA: Array = []; + +export function setPixiAppId(entityId: number, appId: number) { + invariant(hasPixiApp(appId), `PixiApp ${appId} does not exist`); + if (appId !== DATA[entityId]) { + setRenderStateDirty(); + DATA[entityId] = appId; + } +} + +export function hasPixiAppId(entityId: number): boolean { + return DATA[entityId] !== undefined; +} + +export function getPixiAppId(entityId: number): number { + invariant( + hasPixiAppId(entityId), + `Entity ${entityId} does not have a PixiAppId`, + ); + return DATA[entityId]; +} diff --git a/src/systems/EditorSystem.ts b/src/systems/EditorSystem.ts index 3ceaf8ae..add960e9 100644 --- a/src/systems/EditorSystem.ts +++ b/src/systems/EditorSystem.ts @@ -12,6 +12,7 @@ import { setIsVisible } from "../components/IsVisible"; import { Layer, getLayer, hasLayer, setLayer } from "../components/Layer"; import { setLookLike } from "../components/LookLike"; import { getPixiApp, setPixiApp } from "../components/PixiApp"; +import { setPixiAppId } from "../components/PixiAppId"; import { hasPosition, isPosition, setPosition } from "../components/Position"; import { getPositionX } from "../components/PositionX"; import { getPositionY } from "../components/PositionY"; @@ -69,7 +70,7 @@ function finishCreatingObject(cursorId: number, objectId: number) { const y = getPositionY(cursorId); setPosition(objectId, x, y); setLayer(objectId, Layer.OBJECT); - setPixiApp(objectId, getPixiApp(getNamedEntity(EntityName.DEFAULT_PIXI_APP))); + setPixiAppId(objectId, getNamedEntity(EntityName.DEFAULT_PIXI_APP)); } const OBJECT_PREFAB_FACTORY_MAP: Record< @@ -167,7 +168,6 @@ function getEntityAt(x: number, y: number, layer: Layer): number | undefined { export function EditorSystem() { const cursorIds = getEditorCursors(); - const pixiApp = getPixiApp(getNamedEntity(EntityName.DEFAULT_PIXI_APP)); if (cursorIds.length === 0) { const cursorId = addEntity(); setLookLike( @@ -176,7 +176,7 @@ export function EditorSystem() { ); setActLike(cursorId, ActLike.EDITOR_CURSOR); setPosition(cursorId, 0, 0); - setPixiApp(cursorId, pixiApp); + setPixiAppId(cursorId, getNamedEntity(EntityName.DEFAULT_PIXI_APP)); setLayer(cursorId, Layer.USER_INTERFACE); } diff --git a/src/systems/RenderSystem.ts b/src/systems/RenderSystem.ts index 29cfdab3..18cc21d4 100644 --- a/src/systems/RenderSystem.ts +++ b/src/systems/RenderSystem.ts @@ -10,10 +10,11 @@ import { hasSprite, setSprite, } from "../components/Sprite"; -import { getPixiApp } from "../components/PixiApp"; +import { getPixiAppId } from "../components/PixiAppId"; import { hasLoadingCompleted } from "../components/LoadingState"; import { Layer, getLayer, hasLayer } from "../components/Layer"; import { getIsVisible, hasIsVisible } from "../components/IsVisible"; +import { getPixiApp } from "../components/PixiApp"; const WIDTH = 768; const HEIGHT = 768; @@ -87,7 +88,7 @@ export function RenderSystem() { for (const spriteId of getEntitiesNeedingSprites()) { const image = getImage(getLookLike(spriteId)); const sprite = new Sprite(image.texture!); - const app = getPixiApp(spriteId); + const app = getPixiApp(getPixiAppId(spriteId)); sprite.x = getPositionX(spriteId); sprite.y = getPositionY(spriteId); sprite.width = SPRITE_SIZE; @@ -109,7 +110,7 @@ export function RenderSystem() { for (const spriteId of getSpriteEntities()) { const sprite = getSprite(spriteId); - const app = getPixiApp(spriteId); + const app = getPixiApp(getPixiAppId(spriteId)); const container = getParticleContainers(app, spriteId); sprite.x = getPositionX(spriteId); sprite.y = getPositionY(spriteId);