From 9f170969ba46d58a3f273e9ecb62c49a7b382321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 10 Jul 2017 23:40:52 +0200 Subject: [PATCH] Fixed events being broadcast after a battle end --- TODO.md | 1 - src/core/BattleLog.spec.ts | 12 ++++++++++++ src/core/BattleLog.ts | 18 +++++++++++++----- src/core/StarLocation.spec.ts | 2 ++ src/core/StarLocation.ts | 7 +++---- src/ui/battle/BattleView.ts | 7 ------- src/ui/battle/LogProcessor.ts | 6 ------ 7 files changed, 30 insertions(+), 23 deletions(-) diff --git a/TODO.md b/TODO.md index 1578281..fd57971 100644 --- a/TODO.md +++ b/TODO.md @@ -36,7 +36,6 @@ Character sheet Battle ------ -* Fix numerous effects being displayed on ships at the end, behind outcome dialog * Add a voluntary retreat option * Display effects description instead of attribute changes * Display radius for area effects (both on action hover, and while action is active) diff --git a/src/core/BattleLog.spec.ts b/src/core/BattleLog.spec.ts index e838d6f..796a65e 100644 --- a/src/core/BattleLog.spec.ts +++ b/src/core/BattleLog.spec.ts @@ -86,5 +86,17 @@ module TS.SpaceTac { } checkEvent(result[16], playing, "ship_change", playing); }); + + it("stop accepting events once the battle is ended", function () { + let log = new BattleLog(); + + log.add(new ValueChangeEvent(new Ship(), new ShipValue("test"), 1)); + log.add(new EndBattleEvent(new BattleOutcome(null))); + log.add(new ShipChangeEvent(new Ship(), new Ship())); + + expect(log.events.length).toBe(2); + expect(log.events[0] instanceof ValueChangeEvent).toBe(true); + expect(log.events[1] instanceof EndBattleEvent).toBe(true); + }); }); } diff --git a/src/core/BattleLog.ts b/src/core/BattleLog.ts index 7b4839f..ce877bd 100644 --- a/src/core/BattleLog.ts +++ b/src/core/BattleLog.ts @@ -9,13 +9,16 @@ module TS.SpaceTac { // It also allows to register a callback to receive these events export class BattleLog { // Full list of battle events - events: BaseBattleEvent[]; + events: BaseBattleEvent[] // List of subscribers - private subscribers: LogSubscriber[]; + private subscribers: LogSubscriber[] // List of event codes to ignore - private filters: string[]; + private filters: string[] + + // Indicator that the battle has ended + private ended = false // Create an initially empty log constructor() { @@ -30,6 +33,7 @@ module TS.SpaceTac { // Clear the stored events clear(): void { + this.ended = false; this.events = []; } @@ -37,12 +41,12 @@ module TS.SpaceTac { add(event: BaseBattleEvent): void { // Apply filters var filtered = false; - this.filters.forEach((code: string) => { + this.filters.forEach(code => { if (event.code === code) { filtered = true; } }); - if (filtered) { + if (filtered || this.ended) { return; } @@ -51,6 +55,10 @@ module TS.SpaceTac { this.subscribers.forEach(subscriber => { subscriber(event); }); + + if (event instanceof EndBattleEvent) { + this.ended = true; + } } // Filter out a type of event diff --git a/src/core/StarLocation.spec.ts b/src/core/StarLocation.spec.ts index b5c93ca..f5d3d7e 100644 --- a/src/core/StarLocation.spec.ts +++ b/src/core/StarLocation.spec.ts @@ -3,6 +3,7 @@ module TS.SpaceTac.Specs { it("removes generated encounters that lose", function () { var location = new StarLocation(undefined, StarLocationType.PLANET, 0, 0); var fleet = new Fleet(); + fleet.addShip(); location.encounter_random = new SkewedRandomGenerator([0]); var battle = location.enterLocation(fleet); @@ -17,6 +18,7 @@ module TS.SpaceTac.Specs { it("leaves generated encounters that win", function () { var location = new StarLocation(undefined, StarLocationType.PLANET, 0, 0); var fleet = new Fleet(); + fleet.addShip(); location.encounter_random = new SkewedRandomGenerator([0]); var battle = location.enterLocation(fleet); diff --git a/src/core/StarLocation.ts b/src/core/StarLocation.ts index a34b7ad..2456f77 100644 --- a/src/core/StarLocation.ts +++ b/src/core/StarLocation.ts @@ -86,10 +86,9 @@ module TS.SpaceTac { var encounter = this.tryGenerateEncounter(); if (encounter) { var battle = new Battle(fleet, encounter); - battle.log.subscribe((event: BaseBattleEvent) => { - if (event.code === "endbattle") { - var endbattle = event; - if (!endbattle.outcome.draw && endbattle.outcome.winner !== encounter) { + battle.log.subscribe(event => { + if (event instanceof EndBattleEvent) { + if (!event.outcome.draw && event.outcome.winner !== encounter) { // The encounter fleet lost, remove it this.encounter = null; } diff --git a/src/ui/battle/BattleView.ts b/src/ui/battle/BattleView.ts index ea299c9..225cdef 100644 --- a/src/ui/battle/BattleView.ts +++ b/src/ui/battle/BattleView.ts @@ -150,13 +150,6 @@ module TS.SpaceTac.UI { } } - // Leaving the view, we unbind the battle - shutdown() { - this.log_processor.destroy(); - - super.shutdown(); - } - // Display an animated "BATTLE" text in the center of the view displayFightMessage(): void { var text = this.game.add.text(this.getMidWidth(), this.getMidHeight(), "BATTLE !", diff --git a/src/ui/battle/LogProcessor.ts b/src/ui/battle/LogProcessor.ts index 32aad85..1a7303f 100644 --- a/src/ui/battle/LogProcessor.ts +++ b/src/ui/battle/LogProcessor.ts @@ -229,11 +229,6 @@ module TS.SpaceTac.UI { } } - // Destroy the log processor - destroy() { - // TODO interrupt current processing or delay - } - // Playing ship changed private processShipChangeEvent(event: ShipChangeEvent): number { this.view.arena.setShipPlaying(event.new_ship); @@ -275,7 +270,6 @@ module TS.SpaceTac.UI { // Battle ended (victory or defeat) private processEndBattleEvent(event: EndBattleEvent): number { this.view.endBattle(); - this.destroy(); return 0; }