Skip to content

Commit

Permalink
Step 6: Implement Prop
Browse files Browse the repository at this point in the history
  • Loading branch information
belen-albeza committed May 18, 2024
1 parent 8cbb405 commit e27f2d0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/prop/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Prop from "./prop";

export default Prop;
14 changes: 14 additions & 0 deletions src/prop/prop.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it, expect } from "bun:test";
import Prop from "./prop";

describe("Prop", () => {
it("Has health", () => {
const p = new Prop({ health: 1000 });
expect(p.health).toBe(1000);
});

it("Has level 1", () => {
const p = new Prop();
expect(p.level).toBe(1);
});
});
35 changes: 35 additions & 0 deletions src/prop/prop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Vec2d from "../utils/vec2d";
import { AttackTarget } from "../actions";
import { Neutral } from "../factions";

interface PropOptions {
health?: number;
position?: Vec2d;
}

export default class Prop implements AttackTarget, Neutral {
#health: number;
readonly position: Vec2d;
readonly isNeutral = true;

constructor({ health = 1000, position = new Vec2d() }: PropOptions = {}) {
this.#health = health;
this.position = position;
}

get level() {
return 1;
}

get isAlive() {
return this.health > 0;
}

get health() {
return this.#health;
}

set health(value: number) {
this.#health = Math.max(0, value);
}
}
21 changes: 21 additions & 0 deletions test/combat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it, expect } from "bun:test";
import Character from "../src/character";
import { AttackAction, HealAction, HealAnotherAction } from "../src/actions";
import FactionManager from "../src/factions";
import Prop from "../src/prop";

describe("Character combat", () => {
it("Allows characters to attack one another", () => {
Expand Down Expand Up @@ -60,3 +61,23 @@ describe("Factions", () => {
}).toThrow(/invalid attack target/i);
});
});

describe("Props", () => {
it("Cannot join factions", () => {
const house = new Prop();
const factions = new FactionManager();

expect(() => {
factions.join("foo", house);
}).toThrow(/invalid member/i);
});

it("Can be damaged by characters", () => {
const house = new Prop({ health: 1000 });
const attacker = new Character({ damage: 100 });

new AttackAction(attacker, house).run();

expect(house.health).toBe(900);
});
});

0 comments on commit e27f2d0

Please sign in to comment.