1
0
Fork 0
spacetac/src/scripts/view/battle/BattleView.ts

200 lines
5.8 KiB
TypeScript
Raw Normal View History

2014-12-29 00:00:00 +00:00
module SpaceTac.View {
2015-01-07 00:00:00 +00:00
"use strict";
2014-12-29 00:00:00 +00:00
// Interactive view of a Battle
export class BattleView extends Phaser.State {
// Displayed battle
battle: Game.Battle;
// Interacting player
player: Game.Player;
// UI container
ui: UIGroup;
// Battleground container
arena: Arena;
2015-02-03 00:00:00 +00:00
// Background image
background: Phaser.Image;
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: ShipCard;
2014-12-31 00:00:00 +00:00
// Card to display hovered ship
card_hovered: ShipCard;
2014-12-31 00:00:00 +00:00
// Ship list
ship_list: ShipList;
// Action bar
action_bar: ActionBar;
2014-12-31 00:00:00 +00:00
// Currently hovered ship
ship_hovered: Game.Ship;
// Subscription to the battle log
2014-12-31 00:00:00 +00:00
log_processor: LogProcessor;
2014-12-31 00:00:00 +00:00
// True if player interaction is allowed
interacting: boolean;
// Indicator of interaction disabled
icon_waiting: Phaser.Image;
2014-12-29 00:00:00 +00:00
// Init the view, binding it to a specific battle
init(player: Game.Player, battle: Game.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;
2014-12-31 00:00:00 +00:00
this.log_processor = null;
2015-02-03 00:00:00 +00:00
this.background = null;
2014-12-29 00:00:00 +00:00
}
// Create view graphics
create() {
2014-12-31 00:00:00 +00:00
var battleview = this;
var game = this.game;
2015-02-03 00:00:00 +00:00
// Background
game.stage.backgroundColor = 0x000000;
2015-02-03 00:00:00 +00:00
this.background = new Phaser.Image(game, 0, 0, "battle-background", 0);
game.add.existing(this.background);
// Add arena (local map)
this.arena = new Arena(battleview);
game.add.existing(this.arena);
// Add UI layer
this.ui = new UIGroup(game);
game.add.existing(this.ui);
// Add UI elements
this.action_bar = new ActionBar(this);
this.ship_list = new ShipList(this);
this.card_playing = new ShipCard(this, 1060, 130);
this.card_hovered = new ShipCard(this, 1060, 430);
2015-01-08 00:00:00 +00:00
this.icon_waiting = new Phaser.Image(this.game, 640, 360, "battle-waiting", 0);
this.icon_waiting.anchor.set(0.5, 0.5);
game.add.existing(this.icon_waiting);
game.tweens.create(this.icon_waiting).to({"angle": 360}, 3000).repeat(-1).start();
2014-12-31 00:00:00 +00:00
// Start processing the battle log
this.log_processor = new LogProcessor(this);
2014-12-29 00:00:00 +00:00
}
// Leaving the view, we unbind the battle
shutdown() {
this.exitTargettingMode();
2014-12-31 00:00:00 +00:00
if (this.log_processor) {
this.log_processor.destroy();
this.log_processor = null;
2014-12-31 00:00:00 +00:00
}
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
}
// 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);
}
}
// Method called when cursor moves in space
cursorInSpace(x: number, y: number): void {
if (!this.ship_hovered) {
if (this.targetting) {
this.targetting.setTargetSpace(x, y);
}
}
}
// Method called when cursor has been clicked (in space or on a ship)
cursorClicked(): void {
if (this.targetting) {
this.targetting.validate();
}
}
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);
this.arena.setShipHovered(ship);
2015-02-04 00:00:00 +00:00
this.ship_list.setHovered(ship);
2015-01-08 00:00:00 +00:00
if (this.targetting) {
if (ship) {
this.targetting.setTargetShip(ship);
} else {
this.targetting.unsetTarget();
}
}
2014-12-29 00:00:00 +00:00
}
2014-12-31 00:00:00 +00:00
// Enable or disable the global player interaction
// Disable interaction when it is the AI turn, or when the current ship can't play
setInteractionEnabled(enabled: boolean): void {
this.exitTargettingMode();
this.interacting = enabled;
this.icon_waiting.visible = !this.interacting;
}
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 {
if (!this.interacting) {
return null;
}
if (this.targetting) {
this.exitTargettingMode();
}
2014-12-31 00:00:00 +00:00
this.targetting = new Targetting(this);
return this.targetting;
}
// Exit targetting mode
exitTargettingMode(): void {
if (this.targetting) {
this.targetting.destroy();
}
2014-12-31 00:00:00 +00:00
this.targetting = null;
}
2014-12-29 00:00:00 +00:00
}
}