~sjm/lonely

6e204027f9ff1fb7332b586cfa9fd38dca33848a — Sam Marshall 1 year, 3 months ago 33dd7e1
allow cards to be played, begin tracking resources
M src/archetypes/nanite-resource-card.ts => src/archetypes/nanite-resource-card.ts +3 -2
@@ 1,11 1,12 @@
import { Name } from "../components/name";
import { Resource, LonelyResource } from "../components/resource";
import { Resource } from "../components/resource";
import { EntityCreator } from "../entity-creator";
import { LonelyResource } from "../resource";

export const makeNaniteResourceCard = (creator: EntityCreator) => ({
  new: () =>
    creator
      .createEntity()
      .addComponent(Name, { value: "Resource: Nanites" })
      .addComponent(Name, { value: `Resource: ${LonelyResource.Nanite}` })
      .addComponent(Resource, { type: LonelyResource.Nanite, amount: 1 }),
});

A src/components/play.ts => src/components/play.ts +3 -0
@@ 0,0 1,3 @@
import { TagComponent } from "ecsy";

export class Play extends TagComponent {}

A src/components/resource-pool.ts => src/components/resource-pool.ts +23 -0
@@ 0,0 1,23 @@
import { Component } from "ecsy";
import { LonelyResource } from "../resource";

export type ResourcePoolValue = {
  [LonelyResource.Nanite]: number;
};

const defaultRPV = () => ({ [LonelyResource.Nanite]: 0 });

/** the number of each resource the player has */
export class ResourcePool extends Component {
  constructor(public value: ResourcePoolValue = defaultRPV()) {
    super();
  }

  copy(src: ResourcePool) {
    this.value[LonelyResource.Nanite] = src.value[LonelyResource.Nanite];
  }

  reset() {
    this.value = defaultRPV();
  }
}

M src/components/resource.ts => src/components/resource.ts +1 -4
@@ 1,8 1,5 @@
import { Component } from "ecsy";

export enum LonelyResource {
  Nanite,
}
import { LonelyResource } from "../resource";

/** a resource card */
export class Resource extends Component {

M src/index.ts => src/index.ts +4 -0
@@ 9,6 9,8 @@ import { DrawCards } from "./systems/draw-cards";
import { RenderHand } from "./systems/render/hand";
import { DiscardHand } from "./systems/discard-hand";
import { EnvironmentAI } from "./systems/environment-ai";
import { DiscardPlayed } from "./systems/discard-played";
import { RenderResources } from "./systems/render/resources";

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


@@ 17,9 19,11 @@ app.loader.load(() => {
  w.registerSystem(DrawCards)
    .registerSystem(DiscardHand)
    .registerSystem(EnvironmentAI)
    .registerSystem(DiscardPlayed)
    .registerSystem(AdvanceTurn);

  w.registerSystem(RenderHand)
    .registerSystem(RenderResources)
    .registerSystem(RenderTurn)
    .registerSystem(RenderDeck);


A src/resource.ts => src/resource.ts +4 -0
@@ 0,0 1,4 @@
/** Possible resources in lonely */
export enum LonelyResource {
  Nanite = "Nanite",
}

A src/systems/discard-played.ts => src/systems/discard-played.ts +28 -0
@@ 0,0 1,28 @@
import { System, Not } from "ecsy";
import { Turn } from "../components/turn";
import { Play } from "../components/play";
import { InDeck, CardStatus } from "../components/in-deck";
import { TurnState } from "../turn";

/** discard any cards which have been played by the player */
export class DiscardPlayed extends System {
  execute() {
    if (
      this.queries.turn.results[0]?.getComponent(Turn).state.machine.current ===
      TurnState.PlayerTurn
    ) {
      this.queries.played.added?.forEach((card) => {
        card.getMutableComponent(InDeck).status = CardStatus.DiscardPile;
        card.removeComponent(Play);
      });
    }
  }

  static queries = {
    turn: { components: [Turn] },
    played: {
      components: [Play, InDeck],
      listen: { added: [Play, Not(InDeck)] as any },
    },
  };
}

M src/systems/render/hand.ts => src/systems/render/hand.ts +8 -0
@@ 4,6 4,7 @@ import * as P from "pixi.js";
import { rectangle } from "../../pixi/draw";
import { Name } from "../../components/name";
import app from "../../pixi/app";
import { Play } from "../../components/play";

/** create a Pixi graphics object which will draw a card */
function makeBlankPixiCard(): P.Graphics {


@@ 49,11 50,13 @@ export class RenderHand extends System {
      this.queries.deck.removed !== undefined
    );
  }

  execute() {
    this.queries.deck.added?.forEach(() => {
      if (this.queries.deck.results.length > this.cards.length) {
        const blank = makeBlankPixiCard();
        blank.visible = false;
        blank.interactive = true;
        this.app.stage.addChild(blank);
        this.cards.push(blank);
      }


@@ 67,6 70,11 @@ export class RenderHand extends System {
        const cardName = card.getComponent(Name).value;

        (pixiCard.getChildByName("name") as P.Text).text = cardName;
        pixiCard.removeAllListeners();
        pixiCard.addListener("pointerdown", () => {
          console.log("oi oi");
          card.addComponent(Play);
        });
      });

      const startRendering = 80;

A src/systems/render/resources.ts => src/systems/render/resources.ts +5 -0
@@ 0,0 1,5 @@
import { System } from "ecsy";

export class RenderResources extends System {
  execute() {}
}