diff --git a/src/game/actions.js b/src/game/actions.js index b8db4ec0..40e869d0 100644 --- a/src/game/actions.js +++ b/src/game/actions.js @@ -250,21 +250,24 @@ function playCard(state, {card, target}) { */ export function useCardActions(state, {target, card}) { if (!card.actions) return state - let newState = state + + let nextState = state + card.actions.forEach((action) => { // Don't run action if it has an invalid condition. if (action.conditions && !conditionsAreValid(state, action.conditions)) { - return newState + return } - if (!action.parameter) action.parameter = {} // Make sure the action is called with a target, preferably the target you dropped the card on. + if (!action.parameter) action.parameter = {} action.parameter.target = target // Run the action (and add the `card` to the parameters - newState = allActions[action.type](newState, {...action.parameter, card}) + nextState = allActions[action.type](nextState, {...action.parameter, card}) }) - return newState + + return nextState } /** @@ -321,7 +324,7 @@ function addEnergyToPlayer(state, props) { * Removes health from a target, respecting vulnerable and block. * @type {ActionFn<{target: string, amount: number}>} */ -const removeHealth = (state, {target, amount}) => { +const removeHealth = (state, {target, amount = 0}) => { return produce(state, (draft) => { getRoomTargets(draft, target).forEach((t) => { if (t.powers.vulnerable) amount = powers.vulnerable.use(amount) diff --git a/src/game/utils-state.js b/src/game/utils-state.js index d4da45bf..05629843 100644 --- a/src/game/utils-state.js +++ b/src/game/utils-state.js @@ -47,16 +47,17 @@ export function getCurrRoom(state) { */ export function getRoomTargets(state, targetQuery) { if (!targetQuery || typeof targetQuery !== 'string') throw new Error('Bad query string') - const room = getCurrRoom(state) // Player if (targetQuery.includes(CardTargets.player)) return [state.player] + // All enemies - if (targetQuery === CardTargets.allEnemies) return room.monsters + if (targetQuery === CardTargets.allEnemies) return getCurrRoom(state).monsters + // Single enemy if (targetQuery.startsWith(CardTargets.enemy)) { const index = Number(targetQuery.split('enemy')[1]) - const monster = room.monsters[index] + const monster = getCurrRoom(state).monsters[index] if (monster) return [monster] } diff --git a/tests/actions.js b/tests/actions.js index a99a81cf..b85fd079 100644 --- a/tests/actions.js +++ b/tests/actions.js @@ -373,12 +373,6 @@ test('Flourish card adds a healing "regen" buff', (t) => { t.is(state.player.currentHealth, 72) let state2 = a.playCard(state, {target: 'player', card: flourish}) - // Pacify the monster... - // produce(state2, (draft) => { - // getCurrRoom(draft).monsters[0].intents = [] - // getTargets(state, 'enemy0').intents = [] - // }) - t.is(state2.player.powers.regen, flourish.powers.regen, 'regen is applied to player') state2 = a.endTurn(state2) t.is(state2.player.currentHealth, 72, 'it doesnt go above max hp') @@ -409,10 +403,7 @@ test('target "allEnemies" works for damage as well as power', (t) => { t.is(room.monsters.length, 2, 'we have two enemies') t.is(room.monsters[0].currentHealth, 24) t.is(room.monsters[1].currentHealth, 13) - t.falsy( - room.monsters[0].powers.vulnerable && room.monsters[1].powers.vulnerable, - 'none are vulnerable', - ) + t.falsy(room.monsters[0].powers.vulnerable && room.monsters[1].powers.vulnerable, 'none are vulnerable') const card = createCard('Thunderclap') const nextState = a.playCard(state, {card}) t.is(getRoomTargets(nextState, 'enemy0')[0].currentHealth, 24 - card.damage) @@ -508,3 +499,13 @@ test('upgraded cards are really upgraded', (t) => { test.todo('playing defend on an enemy ?') test.todo('can apply a power to a specific monster') + +test('"Deal damage equal to block" mechanic works', (t) => { + const {state} = t.context + t.is(state.player.currentHealth, 72) + t.is(getRoomTargets(state, 'enemy0')[0].currentHealth, 42) + const state2 = a.playCard(state, {card: createCard('Defend'), target: 'player'}) + t.is(state2.player.block, 5) + const state3 = a.playCard(state2, {card: createCard('Body Slam'), target: 'enemy0'}) + t.is(getRoomTargets(state3, 'enemy0')[0].currentHealth, 42 - 5) +})