From 6f1dbf6f4f0faa351eb3e2148abcf9805e663cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 20 May 2019 01:05:23 +0200 Subject: [PATCH] Disable interaction while resolution is playing --- src/ui/battle/BattleView.spec.ts | 32 ++++++++++++++++++++++++++++++ src/ui/battle/BattleView.ts | 34 +++++++++++++++++++++++++++----- src/ui/battle/LogProcessor.ts | 22 ++++++++++----------- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/src/ui/battle/BattleView.spec.ts b/src/ui/battle/BattleView.spec.ts index 2f28ff6..d4d4796 100644 --- a/src/ui/battle/BattleView.spec.ts +++ b/src/ui/battle/BattleView.spec.ts @@ -104,5 +104,37 @@ module TK.SpaceTac.UI.Specs { ]); check.equals(battleview.turn_plannings[1].collectAllActions(), []); }); + + test.case("disables player interaction while resolution is playing", check => { + const battleview = testgame.view; + const ship = battleview.actual_battle.fleets[0].ships[0]; + battleview.setShipSelected(ship); + check.in("initial", check => { + check.equals(battleview.interacting, true); + check.equals(battleview.ship_selected, ship); + }); + battleview.actual_battle.applyDiffs([new TurnStartDiff([])]); + battleview.log_processor.processPending(); + check.in("start 1", check => { + check.equals(battleview.interacting, false); + check.equals(battleview.ship_selected, null); + }); + battleview.actual_battle.applyDiffs([new TurnEndDiff()]); + battleview.log_processor.processPending(); + check.in("end 1", check => { + check.equals(battleview.interacting, true); + }); + battleview.actual_battle.endBattle(null); + battleview.battle.applyDiffs([new TurnStartDiff([])]); + battleview.log_processor.processPending(); + check.in("start 2", check => { + check.equals(battleview.interacting, false); + }); + battleview.actual_battle.applyDiffs([new TurnEndDiff()]); + battleview.log_processor.processPending(); + check.in("end 2", check => { + check.equals(battleview.interacting, false); + }); + }); }); } diff --git a/src/ui/battle/BattleView.ts b/src/ui/battle/BattleView.ts index a00801b..54c8a71 100644 --- a/src/ui/battle/BattleView.ts +++ b/src/ui/battle/BattleView.ts @@ -152,9 +152,28 @@ module TK.SpaceTac.UI { }); this.inputs.bindCheat("a", "Use AI to play", () => this.playAI()); + // Bind to log events + this.log_processor.register(diff => { + if (diff instanceof TurnStartDiff) { + return { + foreground: async () => { + this.setInteractionEnabled(false); + } + }; + } else if (diff instanceof TurnEndDiff) { + return { + foreground: async () => this.setInteractionEnabled(true) + }; + } else { + return {}; + } + }); + // "Battle" animation, then start processing the log if (this.battle.ended) { this.endBattle(); + } else if (this.gameui.isTesting) { + this.setInteractionEnabled(true); } else if (this.splash) { this.showSplash().then(() => { this.log_processor.start(); @@ -272,13 +291,13 @@ module TK.SpaceTac.UI { } /** - * Handle the pression of a validation key (enter or space) + * Handle the pressing of a validation key (enter or space) */ validationPressed(): void { if (this.targetting.active) { this.targetting.validate((action, target) => this.applyPlayerAction(action, target)); } else { - this.action_bar.keyActionPressed(-1); + this.startResolution(); } } @@ -357,10 +376,14 @@ module TK.SpaceTac.UI { this.action_bar.setShip(ship); } - // Enable or disable the global player interaction - // Disable interaction when it is the AI turn, or when the current ship can't play + /** + * Enable or disable the global player interaction + * + * Interaction should be disabled when showing the battle resolution, or when the AI is still thinking, or + * when the battle is ended + */ setInteractionEnabled(enabled: boolean): void { - if (this.session.spectator) { + if (this.session.spectator || this.battle.ended) { enabled = false; } @@ -371,6 +394,7 @@ module TK.SpaceTac.UI { if (!enabled) { this.setShipHovered(null); + this.setShipSelected(null); } } } diff --git a/src/ui/battle/LogProcessor.ts b/src/ui/battle/LogProcessor.ts index a823af7..ab1965d 100644 --- a/src/ui/battle/LogProcessor.ts +++ b/src/ui/battle/LogProcessor.ts @@ -54,21 +54,19 @@ module TK.SpaceTac.UI { * Start log processing */ start() { - if (!this.view.gameui.isTesting) { - this.log.play(async diff => { - while (this.view.isPaused()) { - await this.view.timer.sleep(500); - } + this.log.play(async diff => { + while (this.view.isPaused()) { + await this.view.timer.sleep(500); + } - await this.processBattleDiff(diff); + await this.processBattleDiff(diff); - if (this.log.atEnd()) { - this.temp_speed = undefined; - } - }); + if (this.log.atEnd()) { + this.temp_speed = undefined; + } + }); - this.transferControl(); - } + this.transferControl(); } /**