76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
namespace TK.SpaceTac {
|
|
/**
|
|
* A tool to manipulate the plans for a turn (action plans for all involved ships, for one turn)
|
|
*/
|
|
export class TurnPlanning {
|
|
private plan: TurnPlan
|
|
|
|
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: []
|
|
}))
|
|
}))
|
|
};
|
|
}
|
|
|
|
getTurnPlan(): TurnPlan {
|
|
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 TurnPlan = {
|
|
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
|
|
}
|
|
}
|