~sjm/lonely

fc258c837077386dd6345c93561edcd18f6ce998 — Sam Marshall 1 year, 3 months ago 89f1527 master
add resource to pool when resource card played
2 files changed, 31 insertions(+), 0 deletions(-)

M src/index.ts
A src/systems/add-resource.ts
M src/index.ts => src/index.ts +2 -0
@@ 12,12 12,14 @@ import { EnvironmentAI } from "./systems/environment-ai";
import { DiscardPlayed } from "./systems/discard-played";
import { RenderResources } from "./systems/render/resources";
import { ResourcePool } from "./components/resource-pool";
import { AddResource } from "./systems/add-resource";

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

  w.registerSystem(DrawCards)
    .registerSystem(AddResource)
    .registerSystem(DiscardHand)
    .registerSystem(EnvironmentAI)
    .registerSystem(DiscardPlayed)

A src/systems/add-resource.ts => src/systems/add-resource.ts +29 -0
@@ 0,0 1,29 @@
import { System, Not } from "ecsy";
import { Play } from "../components/play";
import { Resource } from "../components/resource";
import { ResourcePool } from "../components/resource-pool";

/** add resource when resource card is played */
export class AddResource extends System {
  execute() {
    if (this.queries.resources.changed) {
      this.queries.resources.results.forEach((r) => {
        const pool = this.queries.pool.results[0]?.getMutableComponent(
          ResourcePool
        );
        const toAdd = r.getComponent(Resource);
        pool.value[toAdd.type] += toAdd.amount;
      });
    }
  }

  static queries = {
    pool: { components: [ResourcePool] },
    resources: {
      components: [Play, Resource],
      listen: {
        changed: [Play, Not(Resource)] as any,
      },
    },
  };
}