1
0
Fork 0

Action plans now store the target as relative

This commit is contained in:
Michaël Lemaire 2019-05-21 22:39:11 +02:00
parent 77e5e659cc
commit bac2c6fe0a
6 changed files with 65 additions and 32 deletions

View file

@ -50,12 +50,12 @@ module TK.SpaceTac.Specs {
check.equals(planning.getShipPlan(ship).actions, []); check.equals(planning.getShipPlan(ship).actions, []);
check.equals(planning.collectAllActions(), []); check.equals(planning.collectAllActions(), []);
planning.addAction(ship, action1, Target.newFromShip(ship)); planning.addAction(ship, action1, 100, 1);
check.equals(planning.getShipPlan(ship).actions, [ 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(), [ 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, []); check.equals(planning.getShipPlan(ship).actions, []);
planning.addAction(ship, action1, Target.newFromShip(ship)); planning.addAction(ship, action1);
check.equals(planning.getShipPlan(ship).actions, [ 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, [ 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 },
{ action: action2.id, category: action2.getCategory(), target: Target.newFromShip(ship) } { 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, [ check.equals(planning.getShipPlan(ship).actions, [
{ action: action2.id, category: action2.getCategory(), target: Target.newFromShip(ship) }, { action: action2.id, category: action2.getCategory(), distance: undefined, angle: undefined },
{ action: action1.id, category: action1.getCategory(), target: Target.newFromLocation(5, 1) } { action: action1.id, category: action1.getCategory(), distance: 5, angle: undefined }
]); ]);
}); });
@ -97,22 +97,22 @@ module TK.SpaceTac.Specs {
planning.addAction(ship, active1); planning.addAction(ship, active1);
check.equals(planning.getShipPlan(ship).actions, [ 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); planning.addAction(ship, active2);
check.equals(planning.getShipPlan(ship).actions, [ 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); planning.addAction(ship, move1);
check.equals(planning.getShipPlan(ship).actions, [ check.equals(planning.getShipPlan(ship).actions, [
{ action: active2.id, category: active2.getCategory(), target: undefined }, { action: active2.id, category: active2.getCategory(), distance: undefined, angle: undefined },
{ action: move1.id, category: move1.getCategory(), target: undefined } { action: move1.id, category: move1.getCategory(), distance: undefined, angle: undefined }
]); ]);
planning.addAction(ship, move2); planning.addAction(ship, move2);
check.equals(planning.getShipPlan(ship).actions, [ check.equals(planning.getShipPlan(ship).actions, [
{ action: active2.id, category: active2.getCategory(), target: undefined }, { action: active2.id, category: active2.getCategory(), distance: undefined, angle: undefined },
{ action: move2.id, category: move2.getCategory(), target: undefined } { action: move2.id, category: move2.getCategory(), distance: undefined, angle: undefined }
]); ]);
}); });
}); });

View file

@ -35,14 +35,14 @@ namespace TK.SpaceTac {
/** /**
* Add an action to a ship plan * 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); const plan = this.getShipPlan(ship);
if (action.getCategory() == ActionCategory.PASSIVE) { if (action.getCategory() == ActionCategory.PASSIVE) {
plan.actions = plan.actions.filter(iaction => !action.is(iaction.action)); plan.actions = plan.actions.filter(iaction => !action.is(iaction.action));
} else { } else {
plan.actions = plan.actions.filter(iaction => iaction.category != action.getCategory()); 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[] { collectAllActions(): ActionPlan[] {
return flatten(flatten(this.plan.fleets.map(fleet => fleet.ships.map(ship => ship.actions)))); 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 = { export type TurnPlan = {
@ -70,6 +91,7 @@ namespace TK.SpaceTac {
export type ActionPlan = { export type ActionPlan = {
action: RObjectId action: RObjectId
category: ActionCategory category: ActionCategory
target?: Target distance?: number
angle?: number
} }
} }

View file

@ -52,7 +52,8 @@ namespace TK.SpaceTac {
if (ship.is(shipplan.ship)) { if (ship.is(shipplan.ship)) {
shipplan.actions.forEach(actionplan => { shipplan.actions.forEach(actionplan => {
if (actionplan.category === category) { if (actionplan.category === category) {
this.battle.applyOneAction(actionplan.action, actionplan.target); const target = TurnPlanning.getTargetForAction(ship, actionplan);
this.battle.applyOneAction(actionplan.action, target);
} }
}); });
} }

View file

@ -74,10 +74,17 @@ module TK.SpaceTac.UI.Specs {
let action = nn(first(ship.actions.listAll(), action => action instanceof MoveAction)); let action = nn(first(ship.actions.listAll(), action => action instanceof MoveAction));
battleview.setShipSelected(ship); 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.length, 2);
check.equals(battleview.turn_plannings[0].collectAllActions(), [ 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(), []); check.equals(battleview.turn_plannings[1].collectAllActions(), []);
}); });

View file

@ -234,7 +234,9 @@ module TK.SpaceTac.UI {
this.turn_plannings.forEach(planning => { this.turn_plannings.forEach(planning => {
const ship = this.ship_selected; const ship = this.ship_selected;
if (ship && this.player.is(planning.player) && this.player.is(ship.getPlayer())) { 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; done = true;
} }
}); });

View file

@ -55,7 +55,7 @@ module TK.SpaceTac.UI {
if (ship) { if (ship) {
const move = first(plan.actions, action => action.category === ActionCategory.MOVE); 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); const active = first(plan.actions, action => action.category === ActionCategory.ACTIVE);
this.updateMoveAction(ship, move, active, parent); this.updateMoveAction(ship, move, active, parent);
@ -69,11 +69,12 @@ module TK.SpaceTac.UI {
let builder = parent.getBuilder(); let builder = parent.getBuilder();
let child = parent.getByName("moveline"); let child = parent.getByName("moveline");
const graphics = child ? as(UIGraphics, child) : builder.graphics("moveline"); const graphics = child ? as(UIGraphics, child) : builder.graphics("moveline");
const move_location = plan ? TurnPlanning.getTargetForAction(ship, plan) : undefined;
graphics.clear(); graphics.clear();
if (plan && plan.target) { if (move_location) {
graphics.addLine({ graphics.addLine({
start: ship.location, start: ship.location,
end: plan.target, end: move_location,
width: 5, width: 5,
color: PLAN_COLOR color: PLAN_COLOR
}); });
@ -82,15 +83,15 @@ module TK.SpaceTac.UI {
child = parent.getByName("moveghost"); child = parent.getByName("moveghost");
const ghost = child ? as(UIImage, child) : builder.image(`ship-${ship.model.code}-sprite`, 0, 0, true); const ghost = child ? as(UIImage, child) : builder.image(`ship-${ship.model.code}-sprite`, 0, 0, true);
ghost.setName("moveghost"); ghost.setName("moveghost");
if (plan && plan.target) { if (move_location) {
ghost.setVisible(true); ghost.setVisible(true);
ghost.setPosition(plan.target.x, plan.target.y); ghost.setPosition(move_location.x, move_location.y);
if (builder.view.isTintSupported()) { if (builder.view.isTintSupported()) {
ghost.setTintFill(PLAN_COLOR); ghost.setTintFill(PLAN_COLOR);
} else { } else {
ghost.setAlpha(0.6); 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 { } else {
ghost.setVisible(false); ghost.setVisible(false);
} }
@ -120,7 +121,7 @@ module TK.SpaceTac.UI {
if (radius) { if (radius) {
if (angle) { 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({ graphics.addCircleArc({
center: from, center: from,
radius, radius,
@ -129,7 +130,7 @@ module TK.SpaceTac.UI {
border: { color: PLAN_COLOR_HL, alpha: 0.2, width: 4 }, border: { color: PLAN_COLOR_HL, alpha: 0.2, width: 4 },
}); });
} else { } else {
const center = plan ? nn(plan.target) : from; const center = TurnPlanning.getTargetForAction(ship, plan);
if (arenaDistance(from, center) > 0.0) { if (arenaDistance(from, center) > 0.0) {
graphics.addLine({ graphics.addLine({
start: from, start: from,