1
0
Fork 0
spacetac/src/core/BattlePlanning.ts

76 lines
2.3 KiB
TypeScript

namespace TK.SpaceTac {
/**
* A tool to manipulate battle plans (action plans for all involved ships, for one turn)
*/
export class BattlePlanning {
private plan: BattlePlan
constructor(private battle: Battle, readonly player?: Player) {
this.plan = {
fleets: battle.fleets.map(fleet => ({
fleet: fleet.id,
ships: fleet.ships.map(ship => ({
ship: ship.id,
actions: []
}))
}))
};
}
getBattlePlan(): BattlePlan {
return this.plan;
}
getFleetPlan(fleet: Fleet): FleetPlan {
const fplan = first(this.plan.fleets, ifleet => fleet.is(ifleet.fleet));
return fplan || { fleet: -1, ships: [] };
}
getShipPlan(ship: Ship): ShipPlan {
const fplan = this.getFleetPlan(ship.fleet);
const splan = first(fplan.ships, iship => ship.is(iship.ship));
return splan || { ship: -1, actions: [] };
}
/**
* Add an action to a ship plan
*/
addAction(ship: Ship, action: BaseAction, target?: Target) {
const plan = this.getShipPlan(ship);
if (action.getCategory() == ActionCategory.PASSIVE) {
plan.actions = plan.actions.filter(iaction => !action.is(iaction.action));
} else {
plan.actions = plan.actions.filter(iaction => iaction.category != action.getCategory());
}
plan.actions.push({ action: action.id, category: action.getCategory(), target });
}
/**
* Collect all planned actions for all ships
*/
collectAllActions(): ActionPlan[] {
return flatten(flatten(this.plan.fleets.map(fleet => fleet.ships.map(ship => ship.actions))));
}
}
export type BattlePlan = {
fleets: FleetPlan[]
}
export type FleetPlan = {
fleet: RObjectId,
ships: ShipPlan[]
}
export type ShipPlan = {
ship: RObjectId
actions: ActionPlan[]
}
export type ActionPlan = {
action: RObjectId
category: ActionCategory
target?: Target
}
}