2014-12-31 00:00:00 +00:00
|
|
|
module SpaceTac.View {
|
2015-01-07 00:00:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Graphical representation of a battle
|
|
|
|
// This is the area in the BattleView that will display ships with their real positions
|
|
|
|
export class Arena extends Phaser.Group {
|
2015-01-06 00:00:00 +00:00
|
|
|
// Arena background
|
2015-01-02 00:00:00 +00:00
|
|
|
background: Phaser.Button;
|
2015-01-06 00:00:00 +00:00
|
|
|
|
2015-03-03 00:00:00 +00:00
|
|
|
// Hint for weapon or move range
|
|
|
|
range_hint: RangeHint;
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Input callback to receive mouse move events
|
2014-12-31 00:00:00 +00:00
|
|
|
private input_callback: any;
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Link to battleview
|
|
|
|
private battleview: BattleView;
|
|
|
|
|
|
|
|
// List of ship sprites
|
2015-01-21 00:00:00 +00:00
|
|
|
private ship_sprites: ArenaShip[];
|
2015-01-06 00:00:00 +00:00
|
|
|
|
2015-01-23 00:00:00 +00:00
|
|
|
// Currently hovered ship
|
|
|
|
private hovered: ArenaShip;
|
2015-01-23 00:00:00 +00:00
|
|
|
// Currently playing ship
|
|
|
|
private playing: ArenaShip;
|
|
|
|
|
2015-01-21 00:00:00 +00:00
|
|
|
// Create a graphical arena for ship sprites to fight in a 2D space
|
2014-12-31 00:00:00 +00:00
|
|
|
constructor(battleview: BattleView) {
|
2015-01-06 00:00:00 +00:00
|
|
|
this.battleview = battleview;
|
|
|
|
this.ship_sprites = [];
|
2015-01-23 00:00:00 +00:00
|
|
|
this.playing = null;
|
2015-01-23 00:00:00 +00:00
|
|
|
this.hovered = null;
|
2015-03-03 00:00:00 +00:00
|
|
|
this.range_hint = null;
|
2015-01-06 00:00:00 +00:00
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
super(battleview.game);
|
|
|
|
|
2015-01-23 00:00:00 +00:00
|
|
|
var background = new Phaser.Button(battleview.game, 0, 0, "battle-arena-background");
|
2015-02-03 00:00:00 +00:00
|
|
|
var expected_width = 1280 - 416;
|
2015-01-22 00:00:00 +00:00
|
|
|
var expected_height = 720 - 100;
|
2015-01-21 00:00:00 +00:00
|
|
|
background.scale.set(expected_width / background.width, expected_height / background.height);
|
2014-12-31 00:00:00 +00:00
|
|
|
this.background = background;
|
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
// Capture clicks on background
|
|
|
|
background.onInputUp.add(() => {
|
|
|
|
battleview.cursorClicked();
|
|
|
|
});
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Watch mouse move to capture hovering over background
|
2015-01-06 00:00:00 +00:00
|
|
|
this.input_callback = this.game.input.addMoveCallback((pointer: Phaser.Pointer) => {
|
2014-12-31 00:00:00 +00:00
|
|
|
var point = new Phaser.Point();
|
|
|
|
if (battleview.game.input.hitTest(background, pointer, point)) {
|
2015-01-06 00:00:00 +00:00
|
|
|
battleview.cursorInSpace(point.x * background.scale.x, point.y * background.scale.y);
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}, null);
|
|
|
|
|
2015-02-03 00:00:00 +00:00
|
|
|
this.position.set(196, 100);
|
2015-01-21 00:00:00 +00:00
|
|
|
this.addChild(this.background);
|
2015-01-06 00:00:00 +00:00
|
|
|
|
2015-03-03 00:00:00 +00:00
|
|
|
this.range_hint = new RangeHint(this);
|
|
|
|
this.addChild(this.range_hint);
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
this.init();
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.game.input.deleteMoveCallback(this.input_callback);
|
|
|
|
}
|
2015-01-06 00:00:00 +00:00
|
|
|
|
|
|
|
// Initialize state (create sprites)
|
|
|
|
init(): void {
|
|
|
|
var arena = this;
|
|
|
|
|
|
|
|
// Add ship sprites
|
|
|
|
this.battleview.battle.play_order.forEach(function (ship: Game.Ship) {
|
2015-01-21 00:00:00 +00:00
|
|
|
var sprite = new ArenaShip(arena.battleview, ship);
|
|
|
|
arena.addChild(sprite);
|
2015-01-06 00:00:00 +00:00
|
|
|
arena.ship_sprites.push(sprite);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:00:00 +00:00
|
|
|
// Remove a ship sprite
|
|
|
|
removeShip(ship: Game.Ship): void {
|
|
|
|
var sprite = this.findShipSprite(ship);
|
|
|
|
if (sprite) {
|
|
|
|
this.ship_sprites.splice(this.ship_sprites.indexOf(sprite), 1);
|
|
|
|
sprite.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Find the sprite for a ship
|
2015-01-21 00:00:00 +00:00
|
|
|
findShipSprite(ship: Game.Ship): ArenaShip {
|
|
|
|
var result: ArenaShip = null;
|
|
|
|
this.ship_sprites.forEach((sprite: ArenaShip) => {
|
2015-01-06 00:00:00 +00:00
|
|
|
if (sprite.ship === ship) {
|
|
|
|
result = sprite;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2015-01-21 00:00:00 +00:00
|
|
|
|
|
|
|
// Set the hovered state on a ship sprite
|
2015-01-23 00:00:00 +00:00
|
|
|
setShipHovered(ship: Game.Ship): void {
|
|
|
|
if (this.hovered) {
|
|
|
|
this.hovered.setHovered(false);
|
|
|
|
}
|
2015-01-21 00:00:00 +00:00
|
|
|
var arena_ship = this.findShipSprite(ship);
|
|
|
|
if (arena_ship) {
|
2015-01-23 00:00:00 +00:00
|
|
|
arena_ship.setHovered(true);
|
2015-01-21 00:00:00 +00:00
|
|
|
}
|
2015-01-23 00:00:00 +00:00
|
|
|
this.hovered = arena_ship;
|
2015-01-21 00:00:00 +00:00
|
|
|
}
|
2015-01-23 00:00:00 +00:00
|
|
|
|
|
|
|
// Set the playing state on a ship sprite
|
|
|
|
setShipPlaying(ship: Game.Ship): void {
|
|
|
|
if (this.playing) {
|
|
|
|
this.playing.setPlaying(false);
|
|
|
|
}
|
|
|
|
var arena_ship = this.findShipSprite(ship);
|
|
|
|
if (arena_ship) {
|
|
|
|
arena_ship.setPlaying(true);
|
|
|
|
}
|
|
|
|
this.playing = arena_ship;
|
2015-02-16 00:00:00 +00:00
|
|
|
|
|
|
|
Sound.playOnce(this.game, "battle-ship-change");
|
2015-01-23 00:00:00 +00:00
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|