2014-12-31 00:00:00 +00:00
|
|
|
module SpaceTac.View {
|
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-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-01-06 00:00:00 +00:00
|
|
|
// Current targetting
|
|
|
|
private targetting: Targetting;
|
|
|
|
|
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-02 00:00:00 +00:00
|
|
|
this.battleview = bar.battleview;
|
|
|
|
this.ship = ship;
|
|
|
|
this.action = action;
|
|
|
|
|
|
|
|
super(bar.game, x, y, 'action-' + action.code);
|
|
|
|
bar.add(this);
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// TODO Handle action.canBeUsed() result to enable/disable the button
|
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
this.onInputUp.add(() => {
|
|
|
|
this.processClick();
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process a click event on the action icon
|
|
|
|
processClick() {
|
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-01-06 00:00:00 +00:00
|
|
|
if (this.action.needs_target) {
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
processHover(target: Game.Target) {
|
|
|
|
target = this.action.checkTarget(this.battleview.battle, this.ship, target);
|
|
|
|
this.targetting.setTarget(target, false);
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
// Called when a target is selected
|
|
|
|
processSelection(target: Game.Target) {
|
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)) {
|
|
|
|
this.battleview.exitTargettingMode();
|
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|