2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac.UI {
|
2017-09-28 23:18:46 +00:00
|
|
|
/**
|
|
|
|
* Icon to activate a ship ability
|
|
|
|
*/
|
|
|
|
export class ActionIcon {
|
|
|
|
// Link to parents
|
2017-05-22 16:29:04 +00:00
|
|
|
bar: ActionBar
|
2017-09-28 23:18:46 +00:00
|
|
|
view: BattleView
|
2015-01-02 00:00:00 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// Container
|
|
|
|
container: Phaser.Button
|
2015-01-02 00:00:00 +00:00
|
|
|
|
|
|
|
// Related ship
|
2017-05-22 16:29:04 +00:00
|
|
|
ship: Ship
|
2015-01-02 00:00:00 +00:00
|
|
|
|
|
|
|
// Related game action
|
2017-05-22 16:29:04 +00:00
|
|
|
action: BaseAction
|
2015-01-02 00:00:00 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// Current state
|
|
|
|
fading = false
|
|
|
|
disabled = true
|
|
|
|
selected = false
|
|
|
|
toggled = false
|
2017-12-06 23:10:43 +00:00
|
|
|
targetting = false
|
2017-10-01 18:16:41 +00:00
|
|
|
cooldown = 0
|
2015-02-26 00:00:00 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// Images
|
|
|
|
img_targetting: Phaser.Image
|
2017-12-06 23:10:43 +00:00
|
|
|
img_top: Phaser.Image | null = null
|
2017-09-28 23:18:46 +00:00
|
|
|
img_bottom: Phaser.Image
|
|
|
|
img_power: Phaser.Image
|
|
|
|
img_sticky: Phaser.Image
|
|
|
|
img_action: Phaser.Image
|
2017-01-19 23:39:13 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// Indicators
|
|
|
|
text_power: Phaser.Text
|
2015-02-26 00:00:00 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
constructor(bar: ActionBar, ship: Ship, action: BaseAction, position: number) {
|
|
|
|
this.bar = bar;
|
|
|
|
this.view = bar.battleview;
|
2017-05-16 23:12:05 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
let info = this.view.getImageInfo("battle-actionbar-frame-disabled");
|
|
|
|
this.container = this.view.add.button(0, 0, info.key, () => this.processClick(), undefined, info.frame, info.frame);
|
|
|
|
this.container.anchor.set(0.5);
|
|
|
|
this.container.input.useHandCursor = false;
|
2017-01-12 23:57:28 +00:00
|
|
|
|
2015-01-02 00:00:00 +00:00
|
|
|
this.ship = ship;
|
|
|
|
this.action = action;
|
|
|
|
|
2017-12-06 23:10:43 +00:00
|
|
|
let builder = new UIBuilder(this.view, this.container);
|
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// Action icon
|
2017-12-06 23:10:43 +00:00
|
|
|
this.img_action = builder.image([`action-${action.code}`, `equipment-${action.equipment ? action.equipment.code : "---"}`]);
|
2017-09-28 23:18:46 +00:00
|
|
|
this.img_action.anchor.set(0.5);
|
|
|
|
this.img_action.scale.set(0.35);
|
|
|
|
this.img_action.alpha = 0.2;
|
2017-12-06 23:10:43 +00:00
|
|
|
|
|
|
|
// Hotkey indicator
|
|
|
|
if (!(action instanceof EndTurnAction)) {
|
|
|
|
this.img_top = builder.image("battle-actionbar-hotkey", 0, -47);
|
|
|
|
this.img_top.anchor.set(0.5);
|
|
|
|
builder.in(this.img_top, builder => {
|
|
|
|
builder.text(`${(position + 1) % 10}`, 0, 0, {
|
|
|
|
size: 12, color: "#d1d1d1", shadow: true, center: true, vcenter: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-09-28 23:18:46 +00:00
|
|
|
|
|
|
|
// Bottom indicator
|
2017-12-06 23:10:43 +00:00
|
|
|
this.img_bottom = builder.image("battle-actionbar-bottom-disabled", 0, 40);
|
2017-09-28 23:18:46 +00:00
|
|
|
this.img_bottom.anchor.set(0.5);
|
2017-12-06 23:10:43 +00:00
|
|
|
builder.in(this.img_bottom, builder => {
|
|
|
|
this.img_targetting = builder.image("battle-actionbar-bottom-targetting", 0, 12);
|
|
|
|
this.img_targetting.anchor.set(0.5);
|
|
|
|
this.img_targetting.visible = false;
|
|
|
|
});
|
2017-09-28 23:18:46 +00:00
|
|
|
|
|
|
|
// Left indicator
|
2017-01-19 23:39:13 +00:00
|
|
|
this.selected = false;
|
2017-12-06 23:10:43 +00:00
|
|
|
this.img_power = builder.image("battle-actionbar-consumption-disabled", -46);
|
2017-09-28 23:18:46 +00:00
|
|
|
this.img_power.anchor.set(0.5);
|
|
|
|
this.img_power.visible = false;
|
2017-12-06 23:10:43 +00:00
|
|
|
builder.in(this.img_power, builder => {
|
|
|
|
this.text_power = builder.text("", -2, 4, {
|
|
|
|
size: 16, color: "#ffdd4b", shadow: true, center: true, vcenter: true
|
|
|
|
});
|
|
|
|
});
|
2017-09-28 23:18:46 +00:00
|
|
|
|
|
|
|
// Right indicator
|
2017-12-06 23:10:43 +00:00
|
|
|
this.img_sticky = builder.image("battle-actionbar-sticky-untoggled", 46);
|
2017-09-28 23:18:46 +00:00
|
|
|
this.img_sticky.anchor.set(0.5);
|
|
|
|
this.img_sticky.visible = action instanceof ToggleAction;
|
2017-01-19 00:17:24 +00:00
|
|
|
|
|
|
|
// Events
|
2017-09-28 23:18:46 +00:00
|
|
|
this.view.tooltip.bind(this.container, filler => {
|
2017-05-16 23:12:05 +00:00
|
|
|
ActionTooltip.fill(filler, this.ship, this.action, position);
|
|
|
|
return true;
|
|
|
|
});
|
2015-01-29 00:00:00 +00:00
|
|
|
|
|
|
|
// Initialize
|
2017-09-28 23:18:46 +00:00
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the icon
|
|
|
|
*/
|
|
|
|
destroy(): void {
|
|
|
|
this.container.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move to a given layer and position
|
|
|
|
*/
|
|
|
|
moveTo(layer: Phaser.Group, x = 0, y = 0): void {
|
|
|
|
layer.add(this.container);
|
|
|
|
this.container.position.set(x, y);
|
2015-01-02 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 15:09:06 +00:00
|
|
|
/**
|
|
|
|
* Process a click event on the action icon
|
|
|
|
*
|
|
|
|
* This will enter the action's targetting mode, waiting for a target or confirmation to apply the action
|
|
|
|
*/
|
2015-02-04 00:00:00 +00:00
|
|
|
processClick(): void {
|
2017-02-12 18:54:09 +00:00
|
|
|
if (!this.bar.interactive) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-07 19:27:46 +00:00
|
|
|
if (this.action.checkCannotBeApplied(this.ship)) {
|
2015-01-06 00:00:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-19 23:22:18 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
this.view.audio.playOnce("ui-button-click");
|
2017-07-19 23:22:18 +00:00
|
|
|
|
2017-01-19 23:39:13 +00:00
|
|
|
if (this.selected) {
|
|
|
|
this.bar.actionEnded();
|
|
|
|
return;
|
|
|
|
}
|
2015-01-02 00:00:00 +00:00
|
|
|
|
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
|
|
|
|
2017-09-19 15:09:06 +00:00
|
|
|
let mode = this.action.getTargettingMode(this.ship);
|
|
|
|
if (mode == ActionTargettingMode.SELF || mode == ActionTargettingMode.SELF_CONFIRM) {
|
|
|
|
// Apply immediately on the ship
|
|
|
|
// TODO Handle confirm
|
|
|
|
this.processSelection(Target.newFromShip(this.ship));
|
|
|
|
} else {
|
2017-09-28 23:18:46 +00:00
|
|
|
// Switch to targetting mode (will apply action when a target is selected)
|
|
|
|
this.view.enterTargettingMode(this.action, mode);
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
2015-01-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 15:09:06 +00:00
|
|
|
/**
|
|
|
|
* Called when a target is selected
|
|
|
|
*
|
|
|
|
* This will effectively apply the action
|
|
|
|
*/
|
|
|
|
processSelection(target: Target): void {
|
2017-11-14 00:07:06 +00:00
|
|
|
if (this.view.applyAction(this.action, 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
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
/**
|
|
|
|
* Update the display elements
|
|
|
|
*
|
|
|
|
* A currently targetting action may be passed, with power usage, to display potential fading and cooldown.
|
|
|
|
*/
|
|
|
|
refresh(used: BaseAction | null = null, power_consumption = 0): void {
|
|
|
|
let disabled = bool(this.action.checkCannotBeApplied(this.ship));
|
|
|
|
let selected = (used === this.action);
|
|
|
|
let toggled = (this.action instanceof ToggleAction) && this.action.activated;
|
2017-10-01 16:33:48 +00:00
|
|
|
let fading = bool(this.action.checkCannotBeApplied(this.ship, this.ship.getValue("power") - power_consumption));
|
2017-10-01 18:16:41 +00:00
|
|
|
let cooldown = this.action.cooldown.heat;
|
2017-12-06 23:10:43 +00:00
|
|
|
let targetting = used !== null;
|
2017-10-01 16:33:48 +00:00
|
|
|
if (this.action == used && this.action.cooldown.willOverheat()) {
|
|
|
|
fading = true;
|
2017-10-01 18:16:41 +00:00
|
|
|
cooldown = this.action.cooldown.cooling;
|
2017-10-01 16:33:48 +00:00
|
|
|
}
|
2017-09-28 23:18:46 +00:00
|
|
|
|
|
|
|
// inputs
|
|
|
|
if (disabled != this.disabled) {
|
|
|
|
this.container.input.useHandCursor = !disabled;
|
2015-02-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// frame
|
2017-10-01 16:33:48 +00:00
|
|
|
if (disabled != this.disabled || fading != this.fading) {
|
|
|
|
let name = "battle-actionbar-frame-enabled";
|
|
|
|
if (disabled) {
|
|
|
|
name = "battle-actionbar-frame-disabled";
|
|
|
|
} else if (fading) {
|
|
|
|
name = "battle-actionbar-frame-fading";
|
|
|
|
}
|
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
let info = this.view.getImageInfo(name);
|
|
|
|
this.container.name = name;
|
|
|
|
this.container.loadTexture(info.key);
|
|
|
|
this.container.setFrames(info.frame, info.frame, info.frame, info.frame);
|
|
|
|
}
|
2017-01-19 23:39:13 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// action icon
|
|
|
|
if (disabled != this.disabled) {
|
|
|
|
this.img_action.alpha = disabled ? 0.2 : 1;
|
2017-05-16 23:12:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:10:43 +00:00
|
|
|
// top
|
|
|
|
if (this.img_top && (targetting != this.targetting || disabled != this.disabled)) {
|
|
|
|
this.view.animations.setVisible(this.img_top, !targetting, 200, disabled ? 0.2 : 1);
|
|
|
|
}
|
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// bottom
|
|
|
|
if (disabled != this.disabled || toggled != this.toggled) {
|
|
|
|
if (disabled) {
|
|
|
|
this.view.changeImage(this.img_bottom, "battle-actionbar-bottom-disabled");
|
|
|
|
} else if (toggled) {
|
|
|
|
this.view.changeImage(this.img_bottom, "battle-actionbar-bottom-toggled");
|
|
|
|
} else {
|
|
|
|
this.view.changeImage(this.img_bottom, "battle-actionbar-bottom-enabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (selected != this.selected) {
|
|
|
|
this.view.animations.setVisible(this.img_targetting, selected, 200);
|
2017-01-13 00:28:23 +00:00
|
|
|
}
|
2015-02-26 00:00:00 +00:00
|
|
|
|
2017-09-28 23:18:46 +00:00
|
|
|
// left
|
2017-12-04 19:01:50 +00:00
|
|
|
let cost = this.action.getActionPointsUsage(this.ship, null);
|
|
|
|
this.img_power.visible = bool(cost);
|
|
|
|
this.text_power.text = `${Math.abs(cost)}\n${cost < 0 ? "+" : "-"}`;
|
|
|
|
this.text_power.fill = (cost > 0) ? "#ffdd4b" : "#dbe748";
|
2017-11-29 22:50:40 +00:00
|
|
|
this.text_power.alpha = disabled ? 0.2 : 1;
|
2017-10-01 16:33:48 +00:00
|
|
|
if (disabled != this.disabled || selected != this.selected || toggled != this.toggled) {
|
2017-09-28 23:18:46 +00:00
|
|
|
if (disabled) {
|
|
|
|
this.view.changeImage(this.img_power, "battle-actionbar-consumption-disabled");
|
|
|
|
} else if (toggled) {
|
|
|
|
this.view.changeImage(this.img_power, "battle-actionbar-consumption-toggled");
|
|
|
|
} else if (selected) {
|
|
|
|
this.view.changeImage(this.img_power, "battle-actionbar-consumption-targetting");
|
|
|
|
} else {
|
|
|
|
this.view.changeImage(this.img_power, "battle-actionbar-consumption-enabled");
|
|
|
|
}
|
2017-01-13 00:28:23 +00:00
|
|
|
}
|
2017-09-28 23:18:46 +00:00
|
|
|
|
|
|
|
// right
|
2017-10-01 18:16:41 +00:00
|
|
|
if (toggled != this.toggled || disabled != this.disabled || cooldown != this.cooldown) {
|
|
|
|
destroyChildren(this.img_sticky);
|
2017-09-28 23:18:46 +00:00
|
|
|
if (this.action instanceof ToggleAction) {
|
|
|
|
if (toggled) {
|
|
|
|
this.view.changeImage(this.img_sticky, "battle-actionbar-sticky-toggled");
|
|
|
|
} else {
|
|
|
|
this.view.changeImage(this.img_sticky, "battle-actionbar-sticky-untoggled");
|
|
|
|
}
|
|
|
|
this.img_sticky.visible = !disabled;
|
2017-10-01 18:16:41 +00:00
|
|
|
} else if (cooldown) {
|
|
|
|
if (disabled) {
|
|
|
|
this.view.changeImage(this.img_sticky, "battle-actionbar-sticky-disabled");
|
|
|
|
} else {
|
|
|
|
this.view.changeImage(this.img_sticky, "battle-actionbar-sticky-overheat");
|
|
|
|
}
|
|
|
|
range(Math.min(cooldown - 1, 4)).forEach(i => {
|
2017-12-06 23:10:43 +00:00
|
|
|
this.img_sticky.addChild(this.view.newImage("battle-actionbar-cooldown-one", 0, 2 - i * 7));
|
2017-10-01 18:16:41 +00:00
|
|
|
});
|
2017-12-06 23:10:43 +00:00
|
|
|
this.img_sticky.addChild(this.view.newImage("battle-actionbar-cooldown-front", -4, -20));
|
2017-10-01 18:16:41 +00:00
|
|
|
this.img_sticky.visible = true;
|
2017-09-28 23:18:46 +00:00
|
|
|
} else {
|
|
|
|
this.img_sticky.visible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.disabled = disabled;
|
|
|
|
this.selected = selected;
|
2017-12-06 23:10:43 +00:00
|
|
|
this.targetting = targetting;
|
2017-09-28 23:18:46 +00:00
|
|
|
this.fading = fading;
|
|
|
|
this.toggled = toggled;
|
2017-10-01 18:16:41 +00:00
|
|
|
this.cooldown = cooldown;
|
2015-01-29 00:00:00 +00:00
|
|
|
}
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|