1
0
Fork 0
spacetac/src/ui/battle/PlanDisplay.ts

75 lines
2.5 KiB
TypeScript

module TK.SpaceTac.UI {
/**
* Displays and maintain a battle plan
*/
export class PlanDisplay {
readonly container: UIContainer
private battle?: Battle
constructor(builder: UIBuilder) {
this.container = builder.container("battleplan");
}
update(plan: BattlePlan, battle?: Battle): void {
this.battle = battle;
this.updateBattle(plan, this.container);
}
private updateBattle(plan: BattlePlan, parent: UIContainer) {
const builder = parent.getBuilder();
if (parent.list.length > plan.fleets.length) {
builder.clear();
}
while (parent.list.length < plan.fleets.length) {
builder.container("fleetplan");
}
iforeach(izip(plan.fleets, ifilterclass(parent.list, UIContainer)), ([fleetplan, child]) => {
this.updateFleet(fleetplan, child);
});
}
private updateFleet(plan: FleetPlan, parent: UIContainer) {
const builder = parent.getBuilder();
if (parent.list.length > plan.ships.length) {
builder.clear();
}
while (parent.list.length < plan.ships.length) {
builder.container("shipplan");
}
iforeach(izip(plan.ships, ifilterclass(parent.list, UIContainer)), ([shipplan, child]) => {
this.updateShip(shipplan, child);
});
}
private updateShip(plan: ShipPlan, parent: UIContainer) {
const move = first(plan.actions, action => action.category == ActionCategory.MOVE);
const ship = this.battle ? this.battle.getShip(plan.ship) : null;
if (ship) {
this.updateMoveAction(ship, move, parent);
} else {
console.error("Ship not found to update actions", plan);
}
}
private updateMoveAction(ship: Ship, action: ActionPlan | null, parent: UIContainer) {
const child = parent.getByName("move");
const graphics = child ? as(UIGraphics, child) : parent.getBuilder().graphics("move");
graphics.clear();
if (action && action.target) {
graphics.addLine({
start: ship.location,
end: action.target,
width: 5,
color: 0xa5b7da
});
}
}
}
}