1
0
Fork 0

Refactoring of some View code

This commit is contained in:
Michaël Lemaire 2014-12-31 01:00:00 +01:00 committed by Michaël Lemaire
parent 4a5f975d25
commit 6a30e1a045
7 changed files with 53 additions and 19 deletions

View file

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

View file

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