1
0
Fork 0

Added code to advance to next ship in play order

This commit is contained in:
Michaël Lemaire 2014-12-31 01:00:00 +01:00 committed by Michaël Lemaire
parent 791a545abe
commit 006d95ae6b
3 changed files with 78 additions and 1 deletions

View file

@ -31,7 +31,12 @@
currentWindowOnload();
}
<!-- /build -->
new SpaceTac.GameRouter();
var queryString = new jasmine.QueryString({
getWindowLocation: function() { return window.location; }
});
if (!queryString.getParam('onlytests')) {
new SpaceTac.GameRouter();
}
};
</script>

View file

@ -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

View file

@ -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);
});
});
}