From bac2c6fe0aeb291292f70148e1a72d758a3be9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Tue, 21 May 2019 22:39:11 +0200 Subject: [PATCH] Action plans now store the target as relative --- src/core/TurnPlanning.spec.ts | 34 ++++++++++++++++---------------- src/core/TurnPlanning.ts | 28 +++++++++++++++++++++++--- src/core/TurnResolution.ts | 3 ++- src/ui/battle/BattleView.spec.ts | 11 +++++++++-- src/ui/battle/BattleView.ts | 4 +++- src/ui/battle/TurnPlanDisplay.ts | 17 ++++++++-------- 6 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/core/TurnPlanning.spec.ts b/src/core/TurnPlanning.spec.ts index e7b234d..912f090 100644 --- a/src/core/TurnPlanning.spec.ts +++ b/src/core/TurnPlanning.spec.ts @@ -50,12 +50,12 @@ module TK.SpaceTac.Specs { check.equals(planning.getShipPlan(ship).actions, []); check.equals(planning.collectAllActions(), []); - planning.addAction(ship, action1, Target.newFromShip(ship)); + planning.addAction(ship, action1, 100, 1); check.equals(planning.getShipPlan(ship).actions, [ - { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) } + { action: action1.id, category: action1.getCategory(), distance: 100, angle: 1 } ]); check.equals(planning.collectAllActions(), [ - { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) } + { action: action1.id, category: action1.getCategory(), distance: 100, angle: 1 } ]); }); @@ -68,21 +68,21 @@ module TK.SpaceTac.Specs { check.equals(planning.getShipPlan(ship).actions, []); - planning.addAction(ship, action1, Target.newFromShip(ship)); + planning.addAction(ship, action1); check.equals(planning.getShipPlan(ship).actions, [ - { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) } + { action: action1.id, category: action1.getCategory(), distance: undefined, angle: undefined } ]); - planning.addAction(ship, action2, Target.newFromShip(ship)); + planning.addAction(ship, action2); check.equals(planning.getShipPlan(ship).actions, [ - { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) }, - { action: action2.id, category: action2.getCategory(), target: Target.newFromShip(ship) } + { action: action1.id, category: action1.getCategory(), distance: undefined, angle: undefined }, + { action: action2.id, category: action2.getCategory(), distance: undefined, angle: undefined } ]); - planning.addAction(ship, action1, Target.newFromLocation(5, 1)); + planning.addAction(ship, action1, 5); check.equals(planning.getShipPlan(ship).actions, [ - { action: action2.id, category: action2.getCategory(), target: Target.newFromShip(ship) }, - { action: action1.id, category: action1.getCategory(), target: Target.newFromLocation(5, 1) } + { action: action2.id, category: action2.getCategory(), distance: undefined, angle: undefined }, + { action: action1.id, category: action1.getCategory(), distance: 5, angle: undefined } ]); }); @@ -97,22 +97,22 @@ module TK.SpaceTac.Specs { planning.addAction(ship, active1); check.equals(planning.getShipPlan(ship).actions, [ - { action: active1.id, category: active1.getCategory(), target: undefined } + { action: active1.id, category: active1.getCategory(), distance: undefined, angle: undefined } ]); planning.addAction(ship, active2); check.equals(planning.getShipPlan(ship).actions, [ - { action: active2.id, category: active2.getCategory(), target: undefined } + { action: active2.id, category: active2.getCategory(), distance: undefined, angle: undefined } ]); planning.addAction(ship, move1); check.equals(planning.getShipPlan(ship).actions, [ - { action: active2.id, category: active2.getCategory(), target: undefined }, - { action: move1.id, category: move1.getCategory(), target: undefined } + { action: active2.id, category: active2.getCategory(), distance: undefined, angle: undefined }, + { action: move1.id, category: move1.getCategory(), distance: undefined, angle: undefined } ]); planning.addAction(ship, move2); check.equals(planning.getShipPlan(ship).actions, [ - { action: active2.id, category: active2.getCategory(), target: undefined }, - { action: move2.id, category: move2.getCategory(), target: undefined } + { action: active2.id, category: active2.getCategory(), distance: undefined, angle: undefined }, + { action: move2.id, category: move2.getCategory(), distance: undefined, angle: undefined } ]); }); }); diff --git a/src/core/TurnPlanning.ts b/src/core/TurnPlanning.ts index 6d24e72..12ec343 100644 --- a/src/core/TurnPlanning.ts +++ b/src/core/TurnPlanning.ts @@ -35,14 +35,14 @@ namespace TK.SpaceTac { /** * Add an action to a ship plan */ - addAction(ship: Ship, action: BaseAction, target?: Target) { + addAction(ship: Ship, action: BaseAction, distance?: number, angle?: number) { 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 }); + plan.actions.push({ action: action.id, category: action.getCategory(), distance, angle }); } /** @@ -51,6 +51,27 @@ namespace TK.SpaceTac { collectAllActions(): ActionPlan[] { return flatten(flatten(this.plan.fleets.map(fleet => fleet.ships.map(ship => ship.actions)))); } + + /** + * Get the target object for a given ship + */ + static getTargetForAction(ship: Ship, action: ActionPlan): Target { + if (typeof action.angle != "undefined") { + if (action.distance) { + return Target.newFromLocation( + ship.arena_x + action.distance * Math.cos(action.angle), + ship.arena_y + action.distance * Math.sin(action.angle), + ); + } else { + return Target.newFromLocation( + ship.arena_x + Math.cos(action.angle), + ship.arena_y + Math.sin(action.angle), + ); + } + } else { + return Target.newFromShip(ship); + } + } } export type TurnPlan = { @@ -70,6 +91,7 @@ namespace TK.SpaceTac { export type ActionPlan = { action: RObjectId category: ActionCategory - target?: Target + distance?: number + angle?: number } } diff --git a/src/core/TurnResolution.ts b/src/core/TurnResolution.ts index e6100b8..db0b1cb 100644 --- a/src/core/TurnResolution.ts +++ b/src/core/TurnResolution.ts @@ -52,7 +52,8 @@ namespace TK.SpaceTac { if (ship.is(shipplan.ship)) { shipplan.actions.forEach(actionplan => { if (actionplan.category === category) { - this.battle.applyOneAction(actionplan.action, actionplan.target); + const target = TurnPlanning.getTargetForAction(ship, actionplan); + this.battle.applyOneAction(actionplan.action, target); } }); } diff --git a/src/ui/battle/BattleView.spec.ts b/src/ui/battle/BattleView.spec.ts index bb5a341..a183df7 100644 --- a/src/ui/battle/BattleView.spec.ts +++ b/src/ui/battle/BattleView.spec.ts @@ -74,10 +74,17 @@ module TK.SpaceTac.UI.Specs { let action = nn(first(ship.actions.listAll(), action => action instanceof MoveAction)); battleview.setShipSelected(ship); - battleview.applyPlayerAction(action, Target.newFromLocation(0, 0)); + battleview.applyPlayerAction(action); check.equals(battleview.turn_plannings.length, 2); check.equals(battleview.turn_plannings[0].collectAllActions(), [ - { action: action.id, category: action.getCategory(), target: Target.newFromLocation(0, 0) } + { action: action.id, category: action.getCategory(), distance: undefined, angle: undefined } + ]); + check.equals(battleview.turn_plannings[1].collectAllActions(), []); + + battleview.applyPlayerAction(action, Target.newFromLocation(ship.arena_x + 50, ship.arena_y)); + check.equals(battleview.turn_plannings.length, 2); + check.equals(battleview.turn_plannings[0].collectAllActions(), [ + { action: action.id, category: action.getCategory(), distance: 50, angle: 0 } ]); check.equals(battleview.turn_plannings[1].collectAllActions(), []); }); diff --git a/src/ui/battle/BattleView.ts b/src/ui/battle/BattleView.ts index 206bd6c..bcce62a 100644 --- a/src/ui/battle/BattleView.ts +++ b/src/ui/battle/BattleView.ts @@ -234,7 +234,9 @@ module TK.SpaceTac.UI { this.turn_plannings.forEach(planning => { const ship = this.ship_selected; if (ship && this.player.is(planning.player) && this.player.is(ship.getPlayer())) { - planning.addAction(ship, action, target); + const distance = target ? arenaDistance(ship.location, target) : undefined; + const angle = target && distance ? arenaAngle(ship.location, target) : undefined; + planning.addAction(ship, action, distance, angle); done = true; } }); diff --git a/src/ui/battle/TurnPlanDisplay.ts b/src/ui/battle/TurnPlanDisplay.ts index a9c07c8..b14fcd0 100644 --- a/src/ui/battle/TurnPlanDisplay.ts +++ b/src/ui/battle/TurnPlanDisplay.ts @@ -55,7 +55,7 @@ module TK.SpaceTac.UI { if (ship) { const move = first(plan.actions, action => action.category === ActionCategory.MOVE); - const final_location = (move && move.target) ? move.target : ship.location; + const final_location = (move && move.distance) ? TurnPlanning.getTargetForAction(ship, move) : ship.location; const active = first(plan.actions, action => action.category === ActionCategory.ACTIVE); this.updateMoveAction(ship, move, active, parent); @@ -69,11 +69,12 @@ module TK.SpaceTac.UI { let builder = parent.getBuilder(); let child = parent.getByName("moveline"); const graphics = child ? as(UIGraphics, child) : builder.graphics("moveline"); + const move_location = plan ? TurnPlanning.getTargetForAction(ship, plan) : undefined; graphics.clear(); - if (plan && plan.target) { + if (move_location) { graphics.addLine({ start: ship.location, - end: plan.target, + end: move_location, width: 5, color: PLAN_COLOR }); @@ -82,15 +83,15 @@ module TK.SpaceTac.UI { child = parent.getByName("moveghost"); const ghost = child ? as(UIImage, child) : builder.image(`ship-${ship.model.code}-sprite`, 0, 0, true); ghost.setName("moveghost"); - if (plan && plan.target) { + if (move_location) { ghost.setVisible(true); - ghost.setPosition(plan.target.x, plan.target.y); + ghost.setPosition(move_location.x, move_location.y); if (builder.view.isTintSupported()) { ghost.setTintFill(PLAN_COLOR); } else { ghost.setAlpha(0.6); } - ghost.setRotation((active && active.target) ? arenaAngle(plan.target, active.target) : arenaAngle(ship.location, plan.target)); + ghost.setRotation((active && active.angle) ? active.angle : arenaAngle(ship.location, move_location)); } else { ghost.setVisible(false); } @@ -120,7 +121,7 @@ module TK.SpaceTac.UI { if (radius) { if (angle) { - const base_angle = plan.target ? Math.atan2(plan.target.y - from.y, plan.target.x - from.x) : 0; + const base_angle = plan.angle || 0; graphics.addCircleArc({ center: from, radius, @@ -129,7 +130,7 @@ module TK.SpaceTac.UI { border: { color: PLAN_COLOR_HL, alpha: 0.2, width: 4 }, }); } else { - const center = plan ? nn(plan.target) : from; + const center = TurnPlanning.getTargetForAction(ship, plan); if (arenaDistance(from, center) > 0.0) { graphics.addLine({ start: from,