~sjm/lonely

89f1527c5009ca621b89b1833ebccc3eae12962c — Sam Marshall 1 year, 3 months ago 6e20402
display resource pool
2 files changed, 44 insertions(+), 1 deletions(-)

M src/index.ts
M src/systems/render/resources.ts
M src/index.ts => src/index.ts +2 -0
@@ 11,6 11,7 @@ import { DiscardHand } from "./systems/discard-hand";
import { EnvironmentAI } from "./systems/environment-ai";
import { DiscardPlayed } from "./systems/discard-played";
import { RenderResources } from "./systems/render/resources";
import { ResourcePool } from "./components/resource-pool";

app.loader.load(() => {
  const w = new World();


@@ 28,6 29,7 @@ app.loader.load(() => {
    .registerSystem(RenderDeck);

  w.createEntity().addComponent(Turn);
  w.createEntity().addComponent(ResourcePool);

  startingDeck.new(11);


M src/systems/render/resources.ts => src/systems/render/resources.ts +42 -1
@@ 1,5 1,46 @@
import { System } from "ecsy";
import { ResourcePool } from "../../components/resource-pool";
import * as P from "pixi.js";
import app from "../../pixi/app";
import { LonelyResource } from "../../resource";

function resourcePoolDigest(pool: ResourcePool): string {
  return (Object.keys(pool.value) as LonelyResource[])
    .filter((key) => pool.value[key] > 0)
    .map((key) => `${key}: ${pool.value[key]}`)
    .join("\n");
}

export class RenderResources extends System {
  execute() {}
  private app = app;
  private resourceText: P.Text = new P.Text("", {
    fontFamily: "Monoid, monospace",
    fontSize: 12,
    fill: 0xffffff,
    resolution: 1.5,
  });

  init() {
    this.resourceText.tint = 0xff4081;
    this.resourceText.x = 8;
    this.resourceText.y = 8;
    this.app.stage.addChild(this.resourceText);
  }

  execute() {
    if (this.queries.resourcePool.changed) {
      this.resourceText.text = resourcePoolDigest(
        this.queries.resourcePool.results.map((e) =>
          e.getComponent(ResourcePool)
        )[0]
      );
    }
  }

  static queries = {
    resourcePool: {
      components: [ResourcePool],
      listen: { changed: true },
    },
  };
}