diff --git a/src/scripts/game/Ship.ts b/src/scripts/game/Ship.ts index 6692f3b..7ed481c 100644 --- a/src/scripts/game/Ship.ts +++ b/src/scripts/game/Ship.ts @@ -79,6 +79,14 @@ module SpaceTac.Game { return this.fleet.player; } + // Get the list of actions available + // This list does not filter out actions unavailable due to insufficient AP, it justs filter out + // actions that are not allowed/available at all on the ship + getAvailableActions(): Actions.BaseAction[] { + // TODO + return [new Actions.MoveAction()]; + } + // Consumes action points useActionPoints(ap: number): void { this.ap_current -= ap; diff --git a/src/scripts/game/actions/BaseAction.ts b/src/scripts/game/actions/BaseAction.ts new file mode 100644 index 0000000..7ba2f8a --- /dev/null +++ b/src/scripts/game/actions/BaseAction.ts @@ -0,0 +1,41 @@ +module SpaceTac.Game.Actions { + // Base class for action definitions + export class BaseAction { + // Identifier code for the type of action + code: string; + + constructor(code: string) { + this.code = code; + } + + // Check basic conditions to know if the ship can use this action at all + // Method to reimplement to set conditions + canBeUsed(battle: Battle, ship: Ship): boolean { + return true; + } + + // Method to check if a target is applicable for this action + // Will call checkLocationTarget or checkShipTarget by default + checkTarget(battle: Battle, ship: Ship, target: Target): Target { + if (!this.canBeUsed(battle, ship)) { + return null; + } else if (target.ship) { + return this.checkShipTarget(battle, ship, target); + } else { + return this.checkLocationTarget(battle, ship, target); + } + } + + // Method to reimplement to check if a space target is applicable + // Must return null if the target can't be applied, an altered target, or the original target + checkLocationTarget(battle: Battle, ship: Ship, target: Target): Target { + return null; + } + + // Method to reimplement to check if a ship target is applicable + // Must return null if the target can't be applied, an altered target, or the original target + checkShipTarget(battle: Battle, ship: Ship, target: Target): Target { + return null; + } + } +} \ No newline at end of file diff --git a/src/scripts/game/actions/MoveAction.ts b/src/scripts/game/actions/MoveAction.ts new file mode 100644 index 0000000..0328586 --- /dev/null +++ b/src/scripts/game/actions/MoveAction.ts @@ -0,0 +1,18 @@ +module SpaceTac.Game.Actions { + // Action to move to a given location + export class MoveAction extends BaseAction { + constructor() { + super("move"); + } + + canBeUsed(battle: Battle, ship: Ship): boolean { + return ship.ap_current > 0; + } + + checkLocationTarget(battle: Battle, ship: Ship, target: Target): Target { + // TODO Should forbid to move too much near another ship + var coords = ship.getLongestMove(target.x, target.y); + return Target.newFromLocation(coords[0], coords[1]); + } + } +} \ No newline at end of file diff --git a/src/scripts/game/specs/MoveAction.spec.ts b/src/scripts/game/specs/MoveAction.spec.ts new file mode 100644 index 0000000..348718a --- /dev/null +++ b/src/scripts/game/specs/MoveAction.spec.ts @@ -0,0 +1,34 @@ +module SpaceTac.Game.Specs { + describe("MoveAction", function () { + it("checks movement against remaining AP", function(){ + var ship = new Ship(null, "Test"); + ship.ap_current = 6; + ship.movement_cost = 2; + ship.arena_x = 0; + ship.arena_y = 0; + var action = new Actions.MoveAction(); + + var result = action.checkTarget(null, ship, Target.newFromLocation(0, 2)); + expect(result).toEqual(Target.newFromLocation(0, 2)); + + var result = action.checkTarget(null, ship, Target.newFromLocation(0, 8)); + expect(result).toEqual(Target.newFromLocation(0, 3)); + + ship.ap_current = 0; + var result = action.checkTarget(null, ship, Target.newFromLocation(0, 8)); + expect(result).toBeNull(); + }); + + it("forbids targetting a ship", function(){ + var ship1 = new Ship(null, "Test1"); + var ship2 = new Ship(null, "Test2"); + var action = new Actions.MoveAction(); + + var result = action.checkTarget(null, ship1, Target.newFromShip(ship1)); + expect(result).toBeNull(); + + var result = action.checkTarget(null, ship1, Target.newFromShip(ship2)); + expect(result).toBeNull(); + }); + }); +} \ No newline at end of file diff --git a/src/scripts/view/widgets/CapabilityIcon.ts b/src/scripts/view/widgets/ActionIcon.ts similarity index 60% rename from src/scripts/view/widgets/CapabilityIcon.ts rename to src/scripts/view/widgets/ActionIcon.ts index 8796cc0..205a902 100644 --- a/src/scripts/view/widgets/CapabilityIcon.ts +++ b/src/scripts/view/widgets/ActionIcon.ts @@ -1,8 +1,8 @@ module SpaceTac.View.Widgets { // Icon to activate a ship capability (move, fire...) - export class CapabilityIcon extends Phaser.Button { + export class ActionIcon extends Phaser.Button { constructor(battleview: BattleView, x: number, y:number, code: string) { - super(battleview.game, x, y, 'capability-' + code); + super(battleview.game, x, y, 'action-' + code); } } } \ No newline at end of file