Skip to content

Commit

Permalink
feat(game-state): add GameStateHurtPartyAction
Browse files Browse the repository at this point in the history
 - the opposite of healing, it hurts the party members for a given value
  • Loading branch information
justindujardin committed Nov 3, 2022
1 parent ec89b52 commit 9cc2ed5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/app/models/entity/entity.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { addEntityToCollection, EntityCollectionRecord } from '../entity-collect
import {
GameStateEquipItemAction,
GameStateHealPartyAction,
GameStateHurtPartyAction,
GameStateUnequipItemAction,
} from '../game-state/game-state.actions';
import { Item } from '../item';
Expand Down Expand Up @@ -273,6 +274,39 @@ describe('Entity', () => {
expect(secondHealed.mp).toBe(secondHealed.maxmp);
});
});
describe('GameStateHurtPartyAction', () => {
it('should restore all entities in partyIds hp and mp to their maximum', () => {
const secondId: string = 'MotTon';
const first = entityFactory({
eid: testId,
hp: 25,
maxhp: 25,
mp: 0,
maxmp: 25,
});
const second = entityFactory({
eid: secondId,
hp: 20,
maxhp: 20,
mp: 0,
maxmp: 25,
});
const initial: EntityStateRecord = defaultState('beings', [first, second]);
const damage = 10;
const actual = entityReducer(
initial,
new GameStateHurtPartyAction({
damage,
partyIds: [testId, secondId],
})
);
const firstHurt: IPartyMember = actual.beings.byId.get(testId);
expect(firstHurt.hp).toBe(first.hp - damage);

const secondHurt: IPartyMember = actual.beings.byId.get(secondId);
expect(secondHurt.hp).toBe(second.hp - damage);
});
});

describe('CombatVictoryAction', () => {
function summaryData(
Expand Down
19 changes: 19 additions & 0 deletions src/app/models/entity/entity.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
GameStateEquipItemAction,
GameStateHealPartyAction,
GameStateHurtPartyAction,
GameStateUnequipItemAction,
} from '../game-state/game-state.actions';
import { Armor, Item, Magic, Weapon } from '../item';
Expand Down Expand Up @@ -101,6 +102,7 @@ type EntityReducerTypes =
| GameStateEquipItemAction
| GameStateUnequipItemAction
| GameStateHealPartyAction
| GameStateHurtPartyAction
| CombatVictoryAction;

export function entityReducer(
Expand Down Expand Up @@ -152,6 +154,23 @@ export function entityReducer(
return updateBeingsResult;
});
}
case GameStateHurtPartyAction.typeId: {
const result: EntityStateRecord = state;
const partyAction: GameStateHurtPartyAction = action;
return result.updateIn(['beings'], (beings: EntityCollectionRecord) => {
let updateBeingsResult = beings;
const damage = action.payload.damage;
partyAction.payload.partyIds.forEach((partyMemberId: string) => {
const newHp = state.beings.byId.get(partyMemberId).hp - damage;
updateBeingsResult = mergeEntityInCollection(
updateBeingsResult,
{ hp: newHp },
partyMemberId
);
});
return updateBeingsResult;
});
}
case GameStateEquipItemAction.typeId: {
let result: EntityStateRecord = state;
const current: Entity = state.beings.byId.get(action.payload.entityId);
Expand Down
13 changes: 13 additions & 0 deletions src/app/models/game-state/game-state.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ export class GameStateHealPartyAction implements Action {
) {}
}

export class GameStateHurtPartyAction implements Action {
static typeId: 'GAME_HURT_PARTY' = type('GAME_HURT_PARTY');
type = GameStateHurtPartyAction.typeId;

constructor(
public payload: {
damage: number;
partyIds: string[];
}
) {}
}

export class GameStateSetBattleCounterAction implements Action {
static typeId: 'GAME_SET_BATTLE_COUNTER' = type('GAME_SET_BATTLE_COUNTER');
type = GameStateSetBattleCounterAction.typeId;
Expand Down Expand Up @@ -265,6 +277,7 @@ export type GameStateActions =
| GameStateBoardShipAction
| GameStateAddGoldAction
| GameStateHealPartyAction
| GameStateHurtPartyAction
| GameStateEquipItemAction
| GameStateUnequipItemAction
| GameStateAddInventoryAction
Expand Down
2 changes: 2 additions & 0 deletions src/app/models/game-state/game-state.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GameStateDeleteSuccessAction,
GameStateEquipItemAction,
GameStateHealPartyAction,
GameStateHurtPartyAction,
GameStateLoadAction,
GameStateLoadFailAction,
GameStateLoadSuccessAction,
Expand Down Expand Up @@ -127,6 +128,7 @@ export function gameStateReducer(
case GameStateSaveAction.typeId:
case GameStateSaveSuccessAction.typeId:
case GameStateSaveFailAction.typeId:
case GameStateHurtPartyAction.typeId:
return state;
case GameStateMoveAction.typeId: {
if (state.boardedShip) {
Expand Down

0 comments on commit 9cc2ed5

Please sign in to comment.