Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Fixes 0 health actor being selected #34

Merged
merged 5 commits into from
Mar 9, 2018
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/BattleMovr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import { IBattleMovr, IBattleMovrSettings } from "./IBattleMovr";
import { ISelectorFactories } from "./Selectors";
import { IActionsOrderer, ITeamBase, ITeamDescriptor, Team } from "./Teams";

/**
* Finds the index of the first alive actor.
*
* @param actors A list of actors to be sent out into battle.
* @returns Index of the first alive actor.
*/
const findFirstAliveIndex = (actors: IActor[]) => {
for (let i = 0; i < actors.length; i += 1) {
if (actors[i].statistics.health.current !== 0) {
return i;
}
}

throw new Error("Cannot create team since no actors are alive.");
};

/**
* Drives RPG-like battles between two teams of actors.
*/
Expand Down Expand Up @@ -160,11 +176,12 @@ export class BattleMovr implements IBattleMovr {
throw new Error(`Unknown selector type: '${team.selector}.`);
}

const firstAliveIndex = findFirstAliveIndex(team.actors);
return {
...team,
orderedActors: team.actors.slice(),
selectedActor: team.actors[0],
selectedIndex: 0,
selectedActor: team.actors[firstAliveIndex],
selectedIndex: firstAliveIndex,
selector: selectorFactory(),
};
}
Expand Down