From aa3af26790324d23a05d363c767bae061d32652f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 19 Feb 2017 22:52:11 +0100 Subject: [PATCH] Consume power before action starts --- src/core/Ship.ts | 15 ++++++++++++--- src/core/actions/BaseAction.ts | 15 ++++++--------- src/core/actions/DeployDroneAction.ts | 3 +-- src/core/actions/EndTurnAction.ts | 3 +-- src/core/actions/FireWeaponAction.ts | 4 +--- src/core/actions/MoveAction.spec.ts | 16 ++++++++-------- src/core/actions/MoveAction.ts | 3 +-- src/core/ai/BullyAI.spec.ts | 14 +++++++------- src/core/equipments/PowerDepleter.spec.ts | 2 +- 9 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/core/Ship.ts b/src/core/Ship.ts index 3412b08..87723a8 100644 --- a/src/core/Ship.ts +++ b/src/core/Ship.ts @@ -269,9 +269,18 @@ module TS.SpaceTac { } } - // Consumes action points - useActionPoints(value: number): void { - this.setValue("power", -value, true); + /** + * Consumes action points + * + * Return true if it was possible, false if there wasn't enough points. + */ + useActionPoints(value: number): boolean { + if (this.getValue("power") >= value) { + this.setValue("power", -value, true); + return true; + } else { + return false; + } } // Call a method for each drone of the battlefield diff --git a/src/core/actions/BaseAction.ts b/src/core/actions/BaseAction.ts index 8539a96..a364e05 100644 --- a/src/core/actions/BaseAction.ts +++ b/src/core/actions/BaseAction.ts @@ -100,23 +100,20 @@ module TS.SpaceTac { return false; } - var cost = this.getActionPointsUsage(battle, ship, target); - if (this.customApply(battle, ship, target)) { - if (cost > 0) { - ship.useActionPoints(cost); - } - return true; - } else { + let cost = this.getActionPointsUsage(battle, ship, target); + if (!ship.useActionPoints(cost)) { return false; } + + this.customApply(battle, ship, target); + return true; } else { return false; } } // Method to reimplement to apply a action - protected customApply(battle: Battle, ship: Ship, target: Target): boolean { - return false; + protected customApply(battle: Battle, ship: Ship, target: Target) { } } } diff --git a/src/core/actions/DeployDroneAction.ts b/src/core/actions/DeployDroneAction.ts index 81bf3c2..a285f14 100644 --- a/src/core/actions/DeployDroneAction.ts +++ b/src/core/actions/DeployDroneAction.ts @@ -15,7 +15,7 @@ module TS.SpaceTac { return target; } - protected customApply(battle: Battle, ship: Ship, target: Target): boolean { + protected customApply(battle: Battle, ship: Ship, target: Target) { let drone = new Drone(ship, this.equipment.code); drone.x = target.x; drone.y = target.y; @@ -23,7 +23,6 @@ module TS.SpaceTac { drone.effects = this.equipment.target_effects; drone.duration = this.equipment.duration; battle.addDrone(drone); - return true; } } } diff --git a/src/core/actions/EndTurnAction.ts b/src/core/actions/EndTurnAction.ts index 970bf88..6a87162 100644 --- a/src/core/actions/EndTurnAction.ts +++ b/src/core/actions/EndTurnAction.ts @@ -5,10 +5,9 @@ module TS.SpaceTac { super("endturn", "End ship's turn", false); } - protected customApply(battle: Battle, ship: Ship, target: Target): boolean { + protected customApply(battle: Battle, ship: Ship, target: Target) { ship.endTurn(); battle.advanceToNextShip(); - return true; } } } diff --git a/src/core/actions/FireWeaponAction.ts b/src/core/actions/FireWeaponAction.ts index 8fb53e3..60b36ce 100644 --- a/src/core/actions/FireWeaponAction.ts +++ b/src/core/actions/FireWeaponAction.ts @@ -35,7 +35,7 @@ module TS.SpaceTac { } } - protected customApply(battle: Battle, ship: Ship, target: Target): boolean { + protected customApply(battle: Battle, ship: Ship, target: Target) { var affected: Ship[] = []; var blast = this.getBlastRadius(ship); @@ -58,8 +58,6 @@ module TS.SpaceTac { effect.applyOnShip(affship); }); }); - - return true; } } } diff --git a/src/core/actions/MoveAction.spec.ts b/src/core/actions/MoveAction.spec.ts index 3b58a56..c5a09ee 100644 --- a/src/core/actions/MoveAction.spec.ts +++ b/src/core/actions/MoveAction.spec.ts @@ -65,16 +65,16 @@ module TS.SpaceTac { expect(battle.log.events.length).toBe(2); - expect(battle.log.events[0].code).toEqual("move"); + expect(battle.log.events[0].code).toEqual("value"); expect(battle.log.events[0].ship).toBe(ship); - expect(battle.log.events[0].target.ship).toBeNull(); - expect(battle.log.events[0].target.x).toBeCloseTo(3.535533, 0.00001); - expect(battle.log.events[0].target.y).toBeCloseTo(3.535533, 0.00001); - - expect(battle.log.events[1].code).toEqual("value"); - expect(battle.log.events[1].ship).toBe(ship); - expect((battle.log.events[1]).value).toEqual( + expect((battle.log.events[0]).value).toEqual( new ShipValue("power", 0, 20)); + + expect(battle.log.events[1].code).toEqual("move"); + expect(battle.log.events[1].ship).toBe(ship); + expect(battle.log.events[1].target.ship).toBeNull(); + expect(battle.log.events[1].target.x).toBeCloseTo(3.535533, 0.00001); + expect(battle.log.events[1].target.y).toBeCloseTo(3.535533, 0.00001); }); it("can't move too much near another ship", function () { diff --git a/src/core/actions/MoveAction.ts b/src/core/actions/MoveAction.ts index c5f79f5..b8a811d 100644 --- a/src/core/actions/MoveAction.ts +++ b/src/core/actions/MoveAction.ts @@ -59,9 +59,8 @@ module TS.SpaceTac { return target; } - protected customApply(battle: Battle, ship: Ship, target: Target): boolean { + protected customApply(battle: Battle, ship: Ship, target: Target) { ship.moveTo(target.x, target.y); - return true; } } } diff --git a/src/core/ai/BullyAI.spec.ts b/src/core/ai/BullyAI.spec.ts index 1c90922..d1bf0cf 100644 --- a/src/core/ai/BullyAI.spec.ts +++ b/src/core/ai/BullyAI.spec.ts @@ -239,14 +239,14 @@ module TS.SpaceTac.Specs { expect(battle.log.events.length).toBe(7); - expect(battle.log.events[0]).toEqual(new MoveEvent(ship1, 2, 0)); - expect(battle.log.events[1]).toEqual(new ValueChangeEvent(ship1, new ShipValue("power", 2, 10), -4)); + expect(battle.log.events[0]).toEqual(new ValueChangeEvent(ship1, new ShipValue("power", 2, 10), -4)); + expect(battle.log.events[1]).toEqual(new MoveEvent(ship1, 2, 0)); - expect(battle.log.events[2]).toEqual(new FireEvent(ship1, weapon, Target.newFromShip(ship2))); - expect(battle.log.events[3]).toEqual(new ValueChangeEvent(ship2, new ShipValue("shield", 0), -10)); - expect(battle.log.events[4]).toEqual(new ValueChangeEvent(ship2, new ShipValue("hull", 5), -10)); - expect(battle.log.events[5]).toEqual(new DamageEvent(ship2, 10, 10)); - expect(battle.log.events[6]).toEqual(new ValueChangeEvent(ship1, new ShipValue("power", 1, 10), -1)); + expect(battle.log.events[2]).toEqual(new ValueChangeEvent(ship1, new ShipValue("power", 1, 10), -1)); + expect(battle.log.events[3]).toEqual(new FireEvent(ship1, weapon, Target.newFromShip(ship2))); + expect(battle.log.events[4]).toEqual(new ValueChangeEvent(ship2, new ShipValue("shield", 0), -10)); + expect(battle.log.events[5]).toEqual(new ValueChangeEvent(ship2, new ShipValue("hull", 5), -10)); + expect(battle.log.events[6]).toEqual(new DamageEvent(ship2, 10, 10)); }); }); } diff --git a/src/core/equipments/PowerDepleter.spec.ts b/src/core/equipments/PowerDepleter.spec.ts index dbd1d24..f8dc971 100644 --- a/src/core/equipments/PowerDepleter.spec.ts +++ b/src/core/equipments/PowerDepleter.spec.ts @@ -6,8 +6,8 @@ module TS.SpaceTac.Specs { var ship = new Ship(); var target = new Ship(); + TestTools.setShipAP(ship, 50); TestTools.setShipAP(target, 7, 2); - spyOn(equipment.action, "canBeUsed").and.returnValue(true); expect(target.values.power.get()).toBe(7); expect(target.sticky_effects).toEqual([]);