67 lines
1.8 KiB
TypeScript
67 lines
1.8 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 (any(plan.actions, iaction => action.is(iaction.action))) {
|
|
// TODO replace (or remove if toggle action ?)
|
|
} else {
|
|
plan.actions.push({ action: action.id, target });
|
|
}
|
|
}
|
|
}
|
|
|
|
export type BattlePlan = {
|
|
fleets: FleetPlan[]
|
|
}
|
|
|
|
export type FleetPlan = {
|
|
fleet: RObjectId,
|
|
ships: ShipPlan[]
|
|
}
|
|
|
|
export type ShipPlan = {
|
|
ship: RObjectId
|
|
actions: ActionPlan[]
|
|
}
|
|
|
|
export type ActionPlan = {
|
|
action: RObjectId
|
|
target?: Target
|
|
}
|
|
}
|