Skip to content

Commit

Permalink
PixiAppId component
Browse files Browse the repository at this point in the history
  • Loading branch information
patreeceeo committed Nov 10, 2023
1 parent a56fd98 commit 23bd323
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
PhysicsSystem,
initializePhysicsSystem,
} from "./systems/PhysicsSystem";
import { setPixiAppId } from "./components/PixiAppId";

if (module.hot) {
module.hot.accept((getParents) => {
Expand All @@ -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);
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/components/PixiAppId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { invariant } from "../Error";
import { setRenderStateDirty } from "../systems/RenderSystem";
import { hasPixiApp } from "./PixiApp";

const DATA: Array<number> = [];

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];
}
6 changes: 3 additions & 3 deletions src/systems/EditorSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}

Expand Down
7 changes: 4 additions & 3 deletions src/systems/RenderSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 23bd323

Please sign in to comment.