2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac.UI {
|
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 {
|
2017-02-12 22:18:36 +00:00
|
|
|
// Link to battleview
|
|
|
|
battleview: BattleView;
|
|
|
|
|
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
|
|
|
// List of ship sprites
|
2017-02-08 18:54:02 +00:00
|
|
|
private ship_sprites: ArenaShip[] = [];
|
|
|
|
|
|
|
|
// List of drone sprites
|
|
|
|
private drone_sprites: ArenaDrone[] = [];
|
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;
|
|
|
|
|
2017-02-13 19:31:45 +00:00
|
|
|
// Layer for particles
|
|
|
|
layer_weapon_effects: Phaser.Group;
|
|
|
|
|
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) {
|
2016-10-26 21:15:04 +00:00
|
|
|
super(battleview.game);
|
2017-01-08 22:04:07 +00:00
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
this.battleview = battleview;
|
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
|
|
|
|
2017-01-08 22:04:07 +00:00
|
|
|
var offset_x = 133;
|
|
|
|
var offset_y = 132;
|
2015-04-26 19:34:55 +00:00
|
|
|
|
2015-01-23 00:00:00 +00:00
|
|
|
var background = new Phaser.Button(battleview.game, 0, 0, "battle-arena-background");
|
2015-04-26 19:34:55 +00:00
|
|
|
var expected_width = battleview.getWidth() - offset_x;
|
|
|
|
var expected_height = battleview.getHeight() - offset_y;
|
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-04-26 19:34:55 +00:00
|
|
|
this.position.set(offset_x, offset_y);
|
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);
|
|
|
|
|
2017-01-19 00:27:42 +00:00
|
|
|
this.scale.set(1.78, 1.78);
|
|
|
|
|
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-03-06 00:00:00 +00:00
|
|
|
super.destroy();
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
2015-01-06 00:00:00 +00:00
|
|
|
|
|
|
|
// Initialize state (create sprites)
|
|
|
|
init(): void {
|
|
|
|
// Add ship sprites
|
2017-02-12 22:18:36 +00:00
|
|
|
this.battleview.battle.play_order.forEach(ship => {
|
|
|
|
var sprite = new ArenaShip(this, ship);
|
2015-03-06 00:00:00 +00:00
|
|
|
this.addChild(sprite);
|
|
|
|
this.ship_sprites.push(sprite);
|
2015-01-06 00:00:00 +00:00
|
|
|
});
|
2017-02-13 19:31:45 +00:00
|
|
|
|
|
|
|
this.layer_weapon_effects = new Phaser.Group(this.game);
|
|
|
|
this.addChild(this.layer_weapon_effects);
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 00:00:35 +00:00
|
|
|
// Get the current MainUI instance
|
|
|
|
getGame(): MainUI {
|
2015-04-22 21:53:13 +00:00
|
|
|
return this.battleview.gameui;
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:00:00 +00:00
|
|
|
// Remove a ship sprite
|
2017-02-09 00:00:35 +00:00
|
|
|
markAsDead(ship: Ship): void {
|
2015-02-18 00:00:00 +00:00
|
|
|
var sprite = this.findShipSprite(ship);
|
|
|
|
if (sprite) {
|
2017-01-12 00:37:05 +00:00
|
|
|
sprite.alpha = 0.5;
|
2017-02-14 00:30:50 +00:00
|
|
|
sprite.displayEffect("stasis", false);
|
2015-02-18 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Find the sprite for a ship
|
2017-02-09 00:00:35 +00:00
|
|
|
findShipSprite(ship: Ship): ArenaShip {
|
2015-01-21 00:00:00 +00:00
|
|
|
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
|
2017-02-09 00:00:35 +00:00
|
|
|
setShipHovered(ship: Ship): void {
|
2015-01-23 00:00:00 +00:00
|
|
|
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
|
2017-02-09 00:00:35 +00:00
|
|
|
setShipPlaying(ship: Ship): void {
|
2015-01-23 00:00:00 +00:00
|
|
|
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
|
|
|
|
2015-04-22 21:42:40 +00:00
|
|
|
this.battleview.gameui.audio.playOnce("battle-ship-change");
|
2015-01-23 00:00:00 +00:00
|
|
|
}
|
2017-02-08 18:54:02 +00:00
|
|
|
|
2017-02-14 00:30:50 +00:00
|
|
|
/**
|
|
|
|
* Spawn a new drone
|
|
|
|
*
|
|
|
|
* Return the duration of deploy animation
|
|
|
|
*/
|
|
|
|
addDrone(drone: Drone): number {
|
2017-02-08 18:54:02 +00:00
|
|
|
if (!any(this.drone_sprites, sprite => sprite.drone == drone)) {
|
|
|
|
let sprite = new ArenaDrone(this.battleview, drone);
|
|
|
|
this.addChild(sprite);
|
|
|
|
this.drone_sprites.push(sprite);
|
|
|
|
|
|
|
|
sprite.position.set(drone.owner.arena_x, drone.owner.arena_y);
|
|
|
|
this.game.tweens.create(sprite.position).to({ x: drone.x, y: drone.y }, 1800, Phaser.Easing.Sinusoidal.InOut, true, 200);
|
2017-02-14 00:30:50 +00:00
|
|
|
this.game.tweens.create(sprite.radius.scale).from({ x: 0.01, y: 0.01 }, 1800, Phaser.Easing.Linear.None, true, 200);
|
|
|
|
|
|
|
|
return 2000;
|
2017-02-08 18:54:02 +00:00
|
|
|
} else {
|
|
|
|
console.error("Drone added twice to arena", drone);
|
2017-02-14 00:30:50 +00:00
|
|
|
return 0;
|
2017-02-08 18:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a destroyed drone
|
2017-02-09 00:00:35 +00:00
|
|
|
removeDrone(drone: Drone): void {
|
2017-02-08 18:54:02 +00:00
|
|
|
let sprite = first(this.drone_sprites, sprite => sprite.drone == drone);
|
|
|
|
if (sprite) {
|
|
|
|
remove(this.drone_sprites, sprite);
|
|
|
|
sprite.destroy();
|
|
|
|
} else {
|
|
|
|
console.error("Drone not found in arena for removal", drone);
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|