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
|
|
|
// Targetting system
|
2015-01-02 00:00:00 +00:00
|
|
|
// Allows to pick a target for an action
|
2014-12-31 00:00:00 +00:00
|
|
|
export class Targetting {
|
2015-03-06 00:00:00 +00:00
|
|
|
// Initial target (as pointed by the user)
|
|
|
|
target_initial: Game.Target;
|
|
|
|
line_initial: Phaser.Graphics;
|
|
|
|
|
|
|
|
// Corrected target (applying action rules)
|
|
|
|
target_corrected: Game.Target;
|
|
|
|
line_corrected: Phaser.Graphics;
|
2015-01-08 00:00:00 +00:00
|
|
|
|
2015-03-06 00:00:00 +00:00
|
|
|
// Circle for effect radius
|
|
|
|
blast_radius: number;
|
|
|
|
blast: Phaser.Graphics;
|
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
// Signal to receive hovering events
|
|
|
|
targetHovered: Phaser.Signal;
|
|
|
|
|
|
|
|
// Signal to receive targetting events
|
|
|
|
targetSelected: Phaser.Signal;
|
|
|
|
|
2015-01-07 00:00:00 +00:00
|
|
|
// Access to the parent battle view
|
|
|
|
private battleview: BattleView;
|
|
|
|
|
|
|
|
// Source of the targetting
|
2015-01-21 00:00:00 +00:00
|
|
|
private source: PIXI.DisplayObject;
|
2015-01-07 00:00:00 +00:00
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
// Create a default targetting mode
|
2014-12-31 00:00:00 +00:00
|
|
|
constructor(battleview: BattleView) {
|
|
|
|
this.battleview = battleview;
|
2015-01-02 00:00:00 +00:00
|
|
|
this.targetHovered = new Phaser.Signal();
|
|
|
|
this.targetSelected = new Phaser.Signal();
|
2015-01-06 00:00:00 +00:00
|
|
|
|
|
|
|
// Visual effects
|
|
|
|
if (battleview) {
|
2015-03-06 00:00:00 +00:00
|
|
|
this.blast = new Phaser.Graphics(battleview.game, 0, 0);
|
|
|
|
this.blast.visible = false;
|
|
|
|
battleview.arena.add(this.blast);
|
2015-03-06 00:00:00 +00:00
|
|
|
this.line_initial = new Phaser.Graphics(battleview.game, 0, 0);
|
2015-03-06 00:00:00 +00:00
|
|
|
this.line_initial.visible = false;
|
2015-03-06 00:00:00 +00:00
|
|
|
battleview.arena.add(this.line_initial);
|
|
|
|
this.line_corrected = new Phaser.Graphics(battleview.game, 0, 0);
|
2015-03-06 00:00:00 +00:00
|
|
|
this.line_corrected.visible = false;
|
2015-03-06 00:00:00 +00:00
|
|
|
battleview.arena.add(this.line_corrected);
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.source = null;
|
2015-03-06 00:00:00 +00:00
|
|
|
this.target_initial = null;
|
|
|
|
this.target_corrected = null;
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
destroy(): void {
|
|
|
|
this.targetHovered.dispose();
|
|
|
|
this.targetSelected.dispose();
|
2015-03-06 00:00:00 +00:00
|
|
|
if (this.line_initial) {
|
|
|
|
this.line_initial.destroy();
|
|
|
|
}
|
|
|
|
if (this.line_corrected) {
|
|
|
|
this.line_corrected.destroy();
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
2015-03-06 00:00:00 +00:00
|
|
|
if (this.blast) {
|
|
|
|
this.blast.destroy();
|
|
|
|
}
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update visual effects for current targetting
|
|
|
|
update(): void {
|
|
|
|
if (this.battleview) {
|
2015-03-06 00:00:00 +00:00
|
|
|
if (this.source && this.target_initial) {
|
|
|
|
this.line_initial.clear();
|
|
|
|
this.line_initial.lineStyle(2, 0xFF0000);
|
|
|
|
this.line_initial.moveTo(this.source.x, this.source.y);
|
|
|
|
this.line_initial.lineTo(this.target_initial.x, this.target_initial.y);
|
|
|
|
this.line_initial.visible = true;
|
|
|
|
} else {
|
|
|
|
this.line_initial.visible = false;
|
|
|
|
}
|
2015-03-06 00:00:00 +00:00
|
|
|
|
2015-03-06 00:00:00 +00:00
|
|
|
if (this.source && this.target_corrected) {
|
|
|
|
this.line_corrected.clear();
|
|
|
|
this.line_corrected.lineStyle(3, 0x00FF00);
|
|
|
|
this.line_corrected.moveTo(this.source.x, this.source.y);
|
|
|
|
this.line_corrected.lineTo(this.target_corrected.x, this.target_corrected.y);
|
|
|
|
this.line_corrected.visible = true;
|
2015-01-06 00:00:00 +00:00
|
|
|
} else {
|
2015-03-06 00:00:00 +00:00
|
|
|
this.line_corrected.visible = false;
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
2015-03-06 00:00:00 +00:00
|
|
|
|
|
|
|
if (this.target_corrected && this.blast_radius) {
|
|
|
|
this.blast.clear();
|
|
|
|
this.blast.lineStyle(5, 0x208620, 0.4);
|
|
|
|
this.blast.beginFill(0x60D860, 0.2);
|
|
|
|
this.blast.drawCircle(this.target_corrected.x, this.target_corrected.y, this.blast_radius * 2);
|
|
|
|
this.blast.endFill();
|
|
|
|
this.blast.visible = true;
|
|
|
|
} else {
|
|
|
|
this.blast.visible = false;
|
|
|
|
}
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the source sprite for the targetting (for visual effects)
|
2015-01-21 00:00:00 +00:00
|
|
|
setSource(sprite: PIXI.DisplayObject) {
|
2015-01-06 00:00:00 +00:00
|
|
|
this.source = sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a target from a target object
|
2015-03-06 00:00:00 +00:00
|
|
|
setTarget(target: Game.Target, dispatch: boolean = true, blast_radius: number = 0): void {
|
2015-03-06 00:00:00 +00:00
|
|
|
this.target_corrected = target;
|
2015-03-06 00:00:00 +00:00
|
|
|
this.blast_radius = blast_radius;
|
2015-01-06 00:00:00 +00:00
|
|
|
if (dispatch) {
|
2015-03-06 00:00:00 +00:00
|
|
|
this.target_initial = target ? Game.Tools.copyObject(target) : null;
|
|
|
|
this.targetHovered.dispatch(this.target_corrected);
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set no target
|
|
|
|
unsetTarget(dispatch: boolean = true): void {
|
|
|
|
this.setTarget(null, dispatch);
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current target ship (when hovered)
|
2015-01-06 00:00:00 +00:00
|
|
|
setTargetShip(ship: Game.Ship, dispatch: boolean = true): void {
|
|
|
|
this.setTarget(Game.Target.newFromShip(ship), dispatch);
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current target in space (when hovered)
|
2015-01-06 00:00:00 +00:00
|
|
|
setTargetSpace(x: number, y: number, dispatch: boolean = true): void {
|
|
|
|
this.setTarget(Game.Target.newFromLocation(x, y));
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the current target (when clicked)
|
|
|
|
// This will broadcast the targetSelected signal
|
|
|
|
validate(): void {
|
2015-03-06 00:00:00 +00:00
|
|
|
this.targetSelected.dispatch(this.target_corrected);
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|