From 6a30e1a045ceaca328e7f2d7001f410132e506f4 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] Refactoring of some View code --- .../view/{widgets => battle}/ActionIcon.ts | 0 src/scripts/view/{ => battle}/BattleView.ts | 26 +++-------- src/scripts/view/battle/LogProcessor.ts | 46 +++++++++++++++++++ .../view/{arena => battle}/ShipArenaSprite.ts | 0 .../view/{widgets => battle}/ShipCard.ts | 0 .../view/{widgets => battle}/ShipListItem.ts | 0 src/scripts/view/{ => battle}/Targetting.ts | 0 7 files changed, 53 insertions(+), 19 deletions(-) rename src/scripts/view/{widgets => battle}/ActionIcon.ts (100%) rename src/scripts/view/{ => battle}/BattleView.ts (82%) create mode 100644 src/scripts/view/battle/LogProcessor.ts rename src/scripts/view/{arena => battle}/ShipArenaSprite.ts (100%) rename src/scripts/view/{widgets => battle}/ShipCard.ts (100%) rename src/scripts/view/{widgets => battle}/ShipListItem.ts (100%) rename src/scripts/view/{ => battle}/Targetting.ts (100%) diff --git a/src/scripts/view/widgets/ActionIcon.ts b/src/scripts/view/battle/ActionIcon.ts similarity index 100% rename from src/scripts/view/widgets/ActionIcon.ts rename to src/scripts/view/battle/ActionIcon.ts diff --git a/src/scripts/view/BattleView.ts b/src/scripts/view/battle/BattleView.ts similarity index 82% rename from src/scripts/view/BattleView.ts rename to src/scripts/view/battle/BattleView.ts index fe63bbe..c550b34 100644 --- a/src/scripts/view/BattleView.ts +++ b/src/scripts/view/battle/BattleView.ts @@ -27,7 +27,7 @@ module SpaceTac.View { ship_hovered: Game.Ship; // Subscription to the battle log - log_subscription: any; + log_processor: LogProcessor; // Init the view, binding it to a specific battle init(player, battle) { @@ -35,7 +35,7 @@ module SpaceTac.View { this.battle = battle; this.targetting = null; this.ship_hovered = null; - this.log_subscription = null; + this.log_processor = null; } // Create view graphics @@ -67,18 +67,15 @@ module SpaceTac.View { new ShipArenaSprite(battleview, ship); }); - // Subscribe to log events - this.battle.log.subscribe((event) => { - battleview.processBattleEvent(event); - }); - this.battle.injectInitialEvents(); + // Start processing the battle log + this.log_processor = new LogProcessor(this); } // Leaving the view, we unbind the battle shutdown() { - if (this.log_subscription) { - this.battle.log.unsubscribe(this.log_subscription); - this.log_subscription = null; + if (this.log_processor) { + this.log_processor.destroy(); + this.log_processor = null; } if (this.ui) { @@ -104,15 +101,6 @@ module SpaceTac.View { this.battle = null; } - // Process a BaseLogEvent - processBattleEvent(event: Game.BaseLogEvent) { - console.log("Battle event", event); - if (event.code == "ship_change") { - // Playing ship changed - this.card_playing.setShip(event.target.ship); - } - } - // Method called when cursor starts hovering over a ship (or its icon) cursorOnShip(ship: Game.Ship): void { this.setShipHovered(ship); diff --git a/src/scripts/view/battle/LogProcessor.ts b/src/scripts/view/battle/LogProcessor.ts new file mode 100644 index 0000000..0dfe63b --- /dev/null +++ b/src/scripts/view/battle/LogProcessor.ts @@ -0,0 +1,46 @@ +module SpaceTac.View { + // Processor of battle log events + // This will process incoming battle events, and update the battleview accordingly + export class LogProcessor { + // Link to the battle view + private view: BattleView; + + // Link to the battle + private battle: Game.Battle; + + // Link to the battle log + private log: Game.BattleLog; + + // Subscription identifier + private subscription: any; + + // Create a log processor, linked to a battleview + constructor(view: BattleView) { + this.view = view; + this.battle = view.battle; + this.log = view.battle.log; + + this.subscription = this.log.subscribe((event) => { + this.processBattleEvent(event); + }); + this.battle.injectInitialEvents(); + } + + // Process a BaseLogEvent + processBattleEvent(event: Game.BaseLogEvent) { + console.log("Battle event", event); + if (event.code == "ship_change") { + // Playing ship changed + this.view.card_playing.setShip(event.target.ship); + } + } + + // Destroy the log processor + destroy() { + if (this.subscription) { + this.log.unsubscribe(this.subscription); + this.subscription = null; + } + } + } +} \ No newline at end of file diff --git a/src/scripts/view/arena/ShipArenaSprite.ts b/src/scripts/view/battle/ShipArenaSprite.ts similarity index 100% rename from src/scripts/view/arena/ShipArenaSprite.ts rename to src/scripts/view/battle/ShipArenaSprite.ts diff --git a/src/scripts/view/widgets/ShipCard.ts b/src/scripts/view/battle/ShipCard.ts similarity index 100% rename from src/scripts/view/widgets/ShipCard.ts rename to src/scripts/view/battle/ShipCard.ts diff --git a/src/scripts/view/widgets/ShipListItem.ts b/src/scripts/view/battle/ShipListItem.ts similarity index 100% rename from src/scripts/view/widgets/ShipListItem.ts rename to src/scripts/view/battle/ShipListItem.ts diff --git a/src/scripts/view/Targetting.ts b/src/scripts/view/battle/Targetting.ts similarity index 100% rename from src/scripts/view/Targetting.ts rename to src/scripts/view/battle/Targetting.ts