1
0
Fork 0

Added event hooks for drones (to apply their effects)

This commit is contained in:
Michaël Lemaire 2017-02-08 01:47:18 +01:00
parent 07f8ef4d20
commit fbf3181920
5 changed files with 60 additions and 2 deletions

1
TODO
View file

@ -1,5 +1,4 @@
* Restore serialization
* Drones: add hooks on game events
* Drones: add sprite, radius and tooltip
* Allow to cancel last moves
* Effect should be random in a range (eg. "damage target 50-75")

View file

@ -217,6 +217,7 @@ module TS.SpaceTac.Game {
let battle = new Battle();
let ship = new Ship();
let drone = new Drone(ship);
let onDeploy = spyOn(drone, "onDeploy");
expect(battle.drones).toEqual([]);
expect(battle.log.events).toEqual([]);
@ -225,11 +226,13 @@ module TS.SpaceTac.Game {
expect(battle.drones).toEqual([drone]);
expect(battle.log.events).toEqual([new DroneDeployedEvent(drone)]);
expect(onDeploy).toHaveBeenCalledTimes(1);
battle.addDrone(drone);
expect(battle.drones).toEqual([drone]);
expect(battle.log.events).toEqual([new DroneDeployedEvent(drone)]);
expect(onDeploy).toHaveBeenCalledTimes(1);
battle.removeDrone(drone);

View file

@ -271,6 +271,7 @@ module TS.SpaceTac.Game {
*/
addDrone(drone: Drone, log = true) {
if (add(this.drones, drone)) {
drone.onDeploy(this.play_order);
if (log) {
this.log.add(new DroneDeployedEvent(drone));
}

View file

@ -269,5 +269,44 @@ module TS.SpaceTac.Game.Specs {
expect(ship.isInCircle(5, 7, 0.9)).toBe(false);
expect(ship.isInCircle(12, -4, 5)).toBe(false);
});
it("broadcasts to drones", function () {
let battle = new Battle();
let fleet = new Fleet();
fleet.setBattle(battle);
let ship = new Ship(fleet);
let drone = new Drone(ship);
let onTurnStart = spyOn(drone, "onTurnStart");
let onTurnEnd = spyOn(drone, "onTurnEnd");
let onShipMove = spyOn(drone, "onShipMove");
battle.addDrone(drone);
expect(onTurnStart).toHaveBeenCalledTimes(0);
expect(onTurnEnd).toHaveBeenCalledTimes(0);
expect(onShipMove).toHaveBeenCalledTimes(0);
ship.startTurn();
expect(onTurnStart).toHaveBeenCalledTimes(1);
expect(onTurnStart).toHaveBeenCalledWith(ship);
expect(onTurnEnd).toHaveBeenCalledTimes(0);
expect(onShipMove).toHaveBeenCalledTimes(0);
ship.moveTo(10, 10);
expect(onTurnStart).toHaveBeenCalledTimes(1);
expect(onTurnEnd).toHaveBeenCalledTimes(0);
expect(onShipMove).toHaveBeenCalledTimes(1);
expect(onShipMove).toHaveBeenCalledWith(ship);
ship.endTurn();
expect(onTurnStart).toHaveBeenCalledTimes(1);
expect(onTurnEnd).toHaveBeenCalledTimes(1);
expect(onTurnEnd).toHaveBeenCalledWith(ship);
expect(onShipMove).toHaveBeenCalledTimes(1);
});
});
}

View file

@ -272,6 +272,14 @@ module TS.SpaceTac.Game {
this.setValue("power", -value, true);
}
// Call a method for each drone of the battlefield
forEachDrone(callback: (drone: Drone) => any) {
let battle = this.getBattle();
if (battle) {
battle.drones.forEach(callback);
}
}
// Method called at the start of battle
startBattle() {
this.updateAttributes();
@ -279,7 +287,6 @@ module TS.SpaceTac.Game {
this.initializeActionPoints();
}
// Method called at the start of this ship turn
startTurn(): void {
if (this.playing) {
@ -294,6 +301,9 @@ module TS.SpaceTac.Game {
// Apply sticky effects
this.sticky_effects.forEach(effect => effect.startTurn(this));
this.cleanStickyEffects();
// Broadcast to drones
this.forEachDrone(drone => drone.onTurnStart(this));
}
// Method called at the end of this ship turn
@ -304,6 +314,9 @@ module TS.SpaceTac.Game {
}
this.playing = false;
// Broadcast to drones
this.forEachDrone(drone => drone.onTurnEnd(this));
// Recover action points for next turn
this.updateAttributes();
this.recoverActionPoints();
@ -355,6 +368,9 @@ module TS.SpaceTac.Game {
if (log) {
this.addBattleEvent(new MoveEvent(this, x, y));
}
// Broadcast to drones
this.forEachDrone(drone => drone.onShipMove(this));
}
// Set the death status on this ship