2014-12-29 00:00:00 +00:00
|
|
|
module SpaceTac.View {
|
|
|
|
// Interactive view of a Battle
|
|
|
|
export class BattleView extends Phaser.State {
|
|
|
|
|
|
|
|
// Displayed battle
|
|
|
|
battle: Game.Battle;
|
|
|
|
|
2014-12-30 00:00:00 +00:00
|
|
|
// Interacting player
|
|
|
|
player: Game.Player;
|
|
|
|
|
|
|
|
// UI container
|
|
|
|
ui: UIGroup;
|
|
|
|
|
|
|
|
// Battleground container
|
|
|
|
arena: Phaser.Group;
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Targetting mode (null if we're not in this mode)
|
|
|
|
targetting: Targetting;
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Card to display current playing ship
|
|
|
|
card_playing: Widgets.ShipCard;
|
|
|
|
|
|
|
|
// Card to display hovered ship
|
|
|
|
card_hovered: Widgets.ShipCard;
|
|
|
|
|
|
|
|
// Currently hovered ship
|
|
|
|
ship_hovered: Game.Ship;
|
|
|
|
|
|
|
|
// Subscription to the battle log
|
|
|
|
log_subscription: any;
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// Init the view, binding it to a specific battle
|
2014-12-30 00:00:00 +00:00
|
|
|
init(player, battle) {
|
|
|
|
this.player = player;
|
2014-12-29 00:00:00 +00:00
|
|
|
this.battle = battle;
|
2014-12-31 00:00:00 +00:00
|
|
|
this.targetting = null;
|
2014-12-31 00:00:00 +00:00
|
|
|
this.ship_hovered = null;
|
|
|
|
this.log_subscription = null;
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create view graphics
|
|
|
|
create() {
|
2014-12-31 00:00:00 +00:00
|
|
|
var battleview = this;
|
2014-12-30 00:00:00 +00:00
|
|
|
var game = this.game;
|
|
|
|
var player = this.player;
|
|
|
|
|
|
|
|
this.ui = new UIGroup(game);
|
|
|
|
game.add.existing(this.ui);
|
|
|
|
var ui = this.ui;
|
|
|
|
|
|
|
|
this.arena = new Phaser.Group(game);
|
|
|
|
game.add.existing(this.arena);
|
|
|
|
var arena = this.arena;
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
this.card_playing = new Widgets.ShipCard(this, 500, 0);
|
|
|
|
this.card_hovered = new Widgets.ShipCard(this, 500, 300);
|
|
|
|
|
2014-12-30 00:00:00 +00:00
|
|
|
game.stage.backgroundColor = 0x000000;
|
|
|
|
|
|
|
|
// Add ship buttons to UI
|
2014-12-31 00:00:00 +00:00
|
|
|
this.battle.play_order.forEach(function (ship: Game.Ship, rank: number) {
|
|
|
|
new Widgets.ShipListItem(battleview, 0, rank * 50, ship, ship.getPlayer() === player);
|
2014-12-30 00:00:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add ship sprites to arena
|
2014-12-31 00:00:00 +00:00
|
|
|
this.battle.play_order.forEach(function (ship: Game.Ship) {
|
|
|
|
new Arena.ShipArenaSprite(battleview, ship);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Subscribe to log events
|
|
|
|
this.battle.log.subscribe((event) => {
|
|
|
|
battleview.processBattleEvent(event);
|
2014-12-30 00:00:00 +00:00
|
|
|
});
|
2014-12-31 00:00:00 +00:00
|
|
|
this.battle.injectInitialEvents();
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Leaving the view, we unbind the battle
|
|
|
|
shutdown() {
|
2014-12-31 00:00:00 +00:00
|
|
|
if (this.log_subscription) {
|
|
|
|
this.battle.log.unsubscribe(this.log_subscription);
|
|
|
|
this.log_subscription = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.ui) {
|
|
|
|
this.ui.destroy();
|
|
|
|
this.ui = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.arena) {
|
|
|
|
this.arena.destroy();
|
|
|
|
this.arena = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.card_playing) {
|
|
|
|
this.card_playing.destroy();
|
|
|
|
this.card_playing = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.card_hovered) {
|
|
|
|
this.card_hovered.destroy();
|
|
|
|
this.card_hovered = null;
|
|
|
|
}
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
this.battle = null;
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process a BaseLogEvent
|
|
|
|
processBattleEvent(event: Game.Events.BaseLogEvent) {
|
|
|
|
console.log("Battle event", event);
|
|
|
|
if (event.code == "ship_change") {
|
|
|
|
// Playing ship changed
|
|
|
|
this.card_playing.setShip(event.target.ship);
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 00:00:00 +00:00
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Method called when cursor starts hovering over a ship (or its icon)
|
|
|
|
cursorOnShip(ship: Game.Ship): void {
|
|
|
|
this.setShipHovered(ship);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method called when cursor stops hovering over a ship (or its icon)
|
|
|
|
cursorOffShip(ship: Game.Ship): void {
|
|
|
|
if (this.ship_hovered === ship) {
|
|
|
|
this.setShipHovered(null);
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 00:00:00 +00:00
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Set the currently hovered ship
|
|
|
|
setShipHovered(ship: Game.Ship): void {
|
|
|
|
this.ship_hovered = ship;
|
|
|
|
this.card_hovered.setShip(ship);
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
|
|
|
|
// Enter targetting mode
|
|
|
|
// While in this mode, the Targetting object will receive hover and click events, and handle them
|
|
|
|
enterTargettingMode(): Targetting {
|
|
|
|
this.targetting = new Targetting(this);
|
|
|
|
return this.targetting;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit targetting mode
|
|
|
|
exitTargettingMode(): void {
|
|
|
|
this.targetting = null;
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|