From 006d95ae6b68faebf5e9b535f34aef0ca5c0dbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 31 Dec 2014 01:00:00 +0100 Subject: [PATCH] Added code to advance to next ship in play order --- src/index.html | 7 ++++- src/scripts/game/Battle.ts | 27 +++++++++++++++++++ src/scripts/specs/Battle.spec.ts | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/index.html b/src/index.html index 7c39d0d..2b14687 100644 --- a/src/index.html +++ b/src/index.html @@ -31,7 +31,12 @@ currentWindowOnload(); } - new SpaceTac.GameRouter(); + var queryString = new jasmine.QueryString({ + getWindowLocation: function() { return window.location; } + }); + if (!queryString.getParam('onlytests')) { + new SpaceTac.GameRouter(); + } }; diff --git a/src/scripts/game/Battle.ts b/src/scripts/game/Battle.ts index fcba39f..4e4d23d 100644 --- a/src/scripts/game/Battle.ts +++ b/src/scripts/game/Battle.ts @@ -7,10 +7,16 @@ module SpaceTac.Game { // List of ships, sorted by their initiative throw play_order: Ship[]; + // Current ship whose turn it is to play + playing_ship_index: number; + playing_ship: Ship; + // Create a battle between two fleets constructor(fleet1: Fleet, fleet2: Fleet) { this.fleets = [fleet1, fleet2]; this.play_order = []; + this.playing_ship_index = null; + this.playing_ship = null; } // Create play order, performing an initiative throw @@ -55,11 +61,32 @@ module SpaceTac.Game { this.placeFleetShips(this.fleets[1], 300, 100, Math.PI); } + // End the current ship turn, passing control to the next one in play order + // If at the end of the play order, next turn will start automatically + // Member 'play_order' must be defined before calling this function + advanceToNextShip(): void { + if (this.play_order.length == 0) { + this.playing_ship_index = null; + this.playing_ship = null; + } else { + if (this.playing_ship_index == null) { + this.playing_ship_index = 0; + } else { + this.playing_ship_index += 1; + } + if (this.playing_ship_index >= this.play_order.length) { + this.playing_ship_index = 0; + } + this.playing_ship = this.play_order[this.playing_ship_index]; + } + } + // Start the battle // This will call all necessary initialization steps (initiative, placement...) start(): void { this.placeShips(); this.throwInitiative(); + this.advanceToNextShip(); } // Create a quick random battle, for testing purposes diff --git a/src/scripts/specs/Battle.spec.ts b/src/scripts/specs/Battle.spec.ts index bda0016..7b50d64 100644 --- a/src/scripts/specs/Battle.spec.ts +++ b/src/scripts/specs/Battle.spec.ts @@ -60,5 +60,50 @@ module SpaceTac.Specs { expect(ship5.arena_y).toBeCloseTo(75, 0.0001); expect(ship5.arena_angle).toBeCloseTo(Math.PI, 0.0001); }); + + it("advances to next ship in play order", function(){ + var fleet1 = new Game.Fleet(null); + var fleet2 = new Game.Fleet(null); + + var ship1 = new Game.Ship(fleet1, "F1S1"); + var ship2 = new Game.Ship(fleet1, "F1S2"); + var ship3 = new Game.Ship(fleet2, "F2S1"); + + var battle = new Game.Battle(fleet1, fleet2); + + // Check empty play_order case + expect(battle.playing_ship).toBeNull(); + expect(battle.playing_ship_index).toBeNull(); + battle.advanceToNextShip(); + expect(battle.playing_ship).toBeNull(); + expect(battle.playing_ship_index).toBeNull(); + + // Force play order + var gen = new Game.RandomGenerator(0.1, 0.2, 0.0); + battle.throwInitiative(gen); + + expect(battle.playing_ship).toBeNull(); + expect(battle.playing_ship_index).toBeNull(); + + battle.advanceToNextShip(); + + expect(battle.playing_ship).toBe(ship2); + expect(battle.playing_ship_index).toBe(0); + + battle.advanceToNextShip(); + + expect(battle.playing_ship).toBe(ship1); + expect(battle.playing_ship_index).toBe(1); + + battle.advanceToNextShip(); + + expect(battle.playing_ship).toBe(ship3); + expect(battle.playing_ship_index).toBe(2); + + battle.advanceToNextShip(); + + expect(battle.playing_ship).toBe(ship2); + expect(battle.playing_ship_index).toBe(0); + }); }); }