Add poison tick at start of enemy turn
This commit is contained in:
parent
02f9e1ec2c
commit
dd343be64a
3 changed files with 47 additions and 1 deletions
|
|
@ -38,11 +38,24 @@ export function startTurn(state) {
|
||||||
export function resolveEnemyTurn(state) {
|
export function resolveEnemyTurn(state) {
|
||||||
// reset block on all enemies before they act
|
// reset block on all enemies before they act
|
||||||
const enemies = state.enemies.map((e) => ({ ...e, block: 0 }));
|
const enemies = state.enemies.map((e) => ({ ...e, block: 0 }));
|
||||||
let next = { ...state, enemies, enemy: enemies[0] };
|
|
||||||
|
// poison tick — damage and decrement before enemies act
|
||||||
|
const afterPoison = enemies.map((e) => {
|
||||||
|
if (e.poison > 0) {
|
||||||
|
return {
|
||||||
|
...e,
|
||||||
|
hp: Math.max(0, e.hp - e.poison),
|
||||||
|
poison: e.poison - 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
});
|
||||||
|
let next = { ...state, enemies: afterPoison, enemy: afterPoison[0] };
|
||||||
|
|
||||||
// each enemy acts; enemies target player 0 by default
|
// each enemy acts; enemies target player 0 by default
|
||||||
for (let i = 0; i < next.enemies.length; i++) {
|
for (let i = 0; i < next.enemies.length; i++) {
|
||||||
const enemy = next.enemies[i];
|
const enemy = next.enemies[i];
|
||||||
|
if (enemy.hp <= 0) continue; // dead enemies don't act
|
||||||
const action = resolveEnemyAction(
|
const action = resolveEnemyAction(
|
||||||
enemy,
|
enemy,
|
||||||
next.combat.dieResult,
|
next.combat.dieResult,
|
||||||
|
|
|
||||||
|
|
@ -221,4 +221,36 @@ describe("poison tick", () => {
|
||||||
const next = resolveEnemyTurn(state);
|
const next = resolveEnemyTurn(state);
|
||||||
expect(next.enemies[0].poison).toBe(0);
|
expect(next.enemies[0].poison).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("enemy killed by poison does not act", () => {
|
||||||
|
let state = createCombatState("ironclad", "jaw_worm");
|
||||||
|
const poisonedEnemy = { ...state.enemies[0], poison: 10, hp: 3 };
|
||||||
|
state = {
|
||||||
|
...state,
|
||||||
|
enemies: [poisonedEnemy],
|
||||||
|
enemy: poisonedEnemy,
|
||||||
|
players: [{ ...state.players[0], hp: 11, block: 0 }],
|
||||||
|
player: { ...state.players[0], hp: 11, block: 0 },
|
||||||
|
combat: { ...state.combat, dieResult: 1 },
|
||||||
|
};
|
||||||
|
const next = resolveEnemyTurn(state);
|
||||||
|
expect(next.enemies[0].hp).toBe(0);
|
||||||
|
expect(next.players[0].hp).toBe(11); // enemy died before acting
|
||||||
|
});
|
||||||
|
|
||||||
|
test("poison ignores enemy block", () => {
|
||||||
|
let state = createCombatState("ironclad", "jaw_worm");
|
||||||
|
const blockedEnemy = { ...state.enemies[0], poison: 3, hp: 10, block: 5 };
|
||||||
|
state = {
|
||||||
|
...state,
|
||||||
|
enemies: [blockedEnemy],
|
||||||
|
enemy: blockedEnemy,
|
||||||
|
combat: { ...state.combat, dieResult: 1 },
|
||||||
|
};
|
||||||
|
const next = resolveEnemyTurn(state);
|
||||||
|
expect(next.enemies[0].hp).toBe(7); // 10 - 3, block didn't absorb poison
|
||||||
|
// block resets at turn start; enemy may regain some via action — not 5 from before
|
||||||
|
expect(next.enemies[0].block).toBeLessThan(5);
|
||||||
|
expect(next.enemies[0].poison).toBe(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ function renderEnemy(state) {
|
||||||
if (enemy.vulnerable > 0) tokens.push(`vuln ${enemy.vulnerable}`);
|
if (enemy.vulnerable > 0) tokens.push(`vuln ${enemy.vulnerable}`);
|
||||||
if (enemy.weak > 0) tokens.push(`weak ${enemy.weak}`);
|
if (enemy.weak > 0) tokens.push(`weak ${enemy.weak}`);
|
||||||
if (enemy.strength > 0) tokens.push(`str ${enemy.strength}`);
|
if (enemy.strength > 0) tokens.push(`str ${enemy.strength}`);
|
||||||
|
if (enemy.poison > 0) tokens.push(`psn ${enemy.poison}`);
|
||||||
statusEl.textContent = tokens.join(" | ");
|
statusEl.textContent = tokens.join(" | ");
|
||||||
|
|
||||||
const intentEl = document.getElementById("enemy-intent");
|
const intentEl = document.getElementById("enemy-intent");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue