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
|
|
|
// Icon to activate a ship capability (move, fire...)
|
2014-12-31 00:00:00 +00:00
|
|
|
export class ActionIcon extends Phaser.Button {
|
2015-01-22 00:00:00 +00:00
|
|
|
// Link to the parent bar
|
|
|
|
bar: ActionBar;
|
2015-01-02 00:00:00 +00:00
|
|
|
|
|
|
|
// Link to the parent battle view
|
|
|
|
battleview: BattleView;
|
|
|
|
|
|
|
|
// Related ship
|
|
|
|
ship: Game.Ship;
|
|
|
|
|
|
|
|
// Related game action
|
|
|
|
action: Game.BaseAction;
|
|
|
|
|
2015-02-26 00:00:00 +00:00
|
|
|
// True if the action can be used
|
|
|
|
active: boolean;
|
|
|
|
|
|
|
|
// True if an action is currently selected, and this one won't be available after its use
|
|
|
|
fading: boolean;
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Current targetting
|
|
|
|
private targetting: Targetting;
|
|
|
|
|
2015-02-04 00:00:00 +00:00
|
|
|
// Action icon - image representing the action
|
|
|
|
private layer_icon: Phaser.Image;
|
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
// Layer applied when the action is active
|
2015-02-04 00:00:00 +00:00
|
|
|
private layer_active: Phaser.Image;
|
2015-01-29 00:00:00 +00:00
|
|
|
|
2015-02-26 00:00:00 +00:00
|
|
|
// Layer applied when the action will become unavailable if another action is played
|
|
|
|
private layer_fading: Phaser.Image;
|
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
// Create an icon for a single ship action
|
2015-01-06 00:00:00 +00:00
|
|
|
constructor(bar: ActionBar, x: number, y: number, ship: Game.Ship, action: Game.BaseAction) {
|
2015-01-22 00:00:00 +00:00
|
|
|
this.bar = bar;
|
2015-01-02 00:00:00 +00:00
|
|
|
this.battleview = bar.battleview;
|
|
|
|
this.ship = ship;
|
|
|
|
this.action = action;
|
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
super(bar.game, x, y, "battle-action-inactive");
|
2015-01-18 00:00:00 +00:00
|
|
|
bar.addChild(this);
|
2015-01-02 00:00:00 +00:00
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
// Active layer
|
2015-02-26 00:00:00 +00:00
|
|
|
this.active = false;
|
2015-02-04 00:00:00 +00:00
|
|
|
this.layer_active = new Phaser.Image(this.game, 0, 0, "battle-action-active", 0);
|
2015-02-26 00:00:00 +00:00
|
|
|
this.layer_active.visible = false;
|
2015-02-04 00:00:00 +00:00
|
|
|
this.addChild(this.layer_active);
|
|
|
|
|
|
|
|
// Icon layer
|
2015-02-23 00:00:00 +00:00
|
|
|
this.layer_icon = new Phaser.Image(this.game, 15, 18, "battle-actions-" + action.code, 0);
|
2015-02-04 00:00:00 +00:00
|
|
|
this.addChild(this.layer_icon);
|
2015-01-06 00:00:00 +00:00
|
|
|
|
2015-02-26 00:00:00 +00:00
|
|
|
// Fading layer
|
|
|
|
this.fading = false;
|
|
|
|
this.layer_fading = new Phaser.Image(this.game, 0, 0, "battle-action-fading", 0);
|
|
|
|
this.layer_fading.visible = false;
|
|
|
|
this.addChild(this.layer_fading);
|
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
// Click process
|
2015-01-02 00:00:00 +00:00
|
|
|
this.onInputUp.add(() => {
|
|
|
|
this.processClick();
|
2015-03-03 00:00:00 +00:00
|
|
|
});
|
|
|
|
|
2015-03-03 00:00:00 +00:00
|
|
|
// Information on hover
|
2015-03-03 00:00:00 +00:00
|
|
|
this.onInputOver.add(() => {
|
2015-03-03 00:00:00 +00:00
|
|
|
this.bar.tooltip.setAction(this);
|
2015-03-03 00:00:00 +00:00
|
|
|
this.battleview.arena.range_hint.setSecondary(this.ship, this.action);
|
|
|
|
});
|
|
|
|
this.onInputOut.add(() => {
|
2015-03-03 00:00:00 +00:00
|
|
|
this.bar.tooltip.setAction(null);
|
2015-03-03 00:00:00 +00:00
|
|
|
this.battleview.arena.range_hint.clearSecondary();
|
|
|
|
});
|
2015-01-29 00:00:00 +00:00
|
|
|
|
|
|
|
// Initialize
|
|
|
|
this.updateActiveStatus();
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process a click event on the action icon
|
2015-02-04 00:00:00 +00:00
|
|
|
processClick(): void {
|
2015-01-06 00:00:00 +00:00
|
|
|
if (!this.action.canBeUsed(this.battleview.battle, this.ship)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
console.log("Action started", this.action);
|
|
|
|
|
2015-02-04 00:00:00 +00:00
|
|
|
// End any previously selected action
|
|
|
|
this.bar.actionEnded();
|
2015-02-28 00:00:00 +00:00
|
|
|
this.bar.actionStarted();
|
2015-02-04 00:00:00 +00:00
|
|
|
|
2015-03-03 00:00:00 +00:00
|
|
|
// Update range hint
|
|
|
|
this.battleview.arena.range_hint.setPrimary(this.ship, this.action);
|
|
|
|
|
2015-02-26 00:00:00 +00:00
|
|
|
// Update fading statuses
|
2015-02-26 00:00:00 +00:00
|
|
|
this.bar.updateFadings(this.action.getActionPointsUsage(this.battleview.battle, this.ship, null));
|
2015-02-26 00:00:00 +00:00
|
|
|
|
2015-02-04 00:00:00 +00:00
|
|
|
// Set the lighting color to highlight
|
2015-02-26 00:00:00 +00:00
|
|
|
if (this.game.renderType !== Phaser.HEADLESS) {
|
|
|
|
// Tint doesn't work in headless renderer
|
|
|
|
this.layer_active.tint = 0xFFD060;
|
|
|
|
}
|
2015-02-04 00:00:00 +00:00
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
if (this.action.needs_target) {
|
2015-02-04 00:00:00 +00:00
|
|
|
// Switch to targetting mode (will apply action when a target is selected)
|
2015-01-06 00:00:00 +00:00
|
|
|
this.targetting = this.battleview.enterTargettingMode();
|
|
|
|
this.targetting.setSource(this.battleview.arena.findShipSprite(this.ship));
|
|
|
|
this.targetting.targetSelected.add(this.processSelection, this);
|
|
|
|
this.targetting.targetHovered.add(this.processHover, this);
|
|
|
|
} else {
|
2015-02-04 00:00:00 +00:00
|
|
|
// No target needed, apply action immediately
|
2015-01-06 00:00:00 +00:00
|
|
|
this.processSelection(null);
|
|
|
|
}
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when a target is hovered
|
|
|
|
// This will check the target against current action and adjust it if needed
|
2015-02-04 00:00:00 +00:00
|
|
|
processHover(target: Game.Target): void {
|
2015-01-06 00:00:00 +00:00
|
|
|
target = this.action.checkTarget(this.battleview.battle, this.ship, target);
|
2015-03-06 00:00:00 +00:00
|
|
|
this.targetting.setTarget(target, false, this.action.getBlastRadius(this.ship));
|
2015-02-26 00:00:00 +00:00
|
|
|
this.bar.updateFadings(this.action.getActionPointsUsage(this.battleview.battle, this.ship, target));
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Called when a target is selected
|
2015-02-04 00:00:00 +00:00
|
|
|
processSelection(target: Game.Target): void {
|
2015-01-02 00:00:00 +00:00
|
|
|
console.log("Action target", this.action, target);
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
if (this.action.apply(this.battleview.battle, this.ship, target)) {
|
2015-01-22 00:00:00 +00:00
|
|
|
this.bar.actionEnded();
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
2015-01-29 00:00:00 +00:00
|
|
|
|
2015-02-04 00:00:00 +00:00
|
|
|
// Called to clear the current state
|
|
|
|
resetState(): void {
|
|
|
|
if (this.targetting) {
|
|
|
|
this.targetting = null;
|
|
|
|
}
|
2015-02-04 00:00:00 +00:00
|
|
|
this.layer_active.tint = 0xFFFFFF;
|
2015-02-04 00:00:00 +00:00
|
|
|
this.updateActiveStatus();
|
2015-02-26 00:00:00 +00:00
|
|
|
this.updateFadingStatus(this.ship.ap_current.current);
|
2015-03-03 00:00:00 +00:00
|
|
|
this.battleview.arena.range_hint.clearPrimary();
|
2015-02-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
// Update the active status, from the action canBeUsed result
|
2015-02-04 00:00:00 +00:00
|
|
|
updateActiveStatus(): void {
|
2015-02-26 00:00:00 +00:00
|
|
|
this.active = this.action.canBeUsed(this.battleview.battle, this.ship);
|
|
|
|
Animation.setVisibility(this.game, this.layer_active, this.active, 500);
|
|
|
|
this.game.tweens.create(this.layer_icon).to({alpha: this.active ? 1 : 0.3}, 500).start();
|
|
|
|
this.input.useHandCursor = this.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the fading status, given an hypothetical remaining AP
|
|
|
|
updateFadingStatus(remaining_ap: number): void {
|
|
|
|
this.fading = !this.action.canBeUsed(this.battleview.battle, this.ship, remaining_ap);
|
2015-02-26 00:00:00 +00:00
|
|
|
Animation.setVisibility(this.game, this.layer_fading, this.fading && this.active, 200);
|
2015-01-29 00:00:00 +00:00
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|