1
0
Fork 0
spacetac/src/ui/battle/ActionIcon.ts

259 lines
9.9 KiB
TypeScript
Raw Normal View History

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
2017-09-28 23:18:46 +00:00
// Container
container: Phaser.Button
// Related ship
2017-05-22 16:29:04 +00:00
ship: Ship
// Related game action
2017-05-22 16:29:04 +00:00
action: BaseAction
2017-09-28 23:18:46 +00:00
// Current state
fading = false
disabled = true
selected = false
toggled = false
cooldown = 0
2017-09-28 23:18:46 +00:00
// Images
img_targetting: Phaser.Image
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
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;
this.ship = ship;
this.action = action;
2017-09-28 23:18:46 +00:00
// Action icon
let icon = this.view.getFirstImage(`action-${action.code}`, `equipment-${action.equipment ? action.equipment.code : "---"}`);
2017-10-10 22:32:46 +00:00
this.img_action = this.view.newImage(icon);
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;
this.container.addChild(this.img_action);
// Bottom indicator
this.img_bottom = this.view.newImage("battle-actionbar-bottom-disabled", 0, 40);
this.img_bottom.anchor.set(0.5);
this.container.addChild(this.img_bottom);
this.img_targetting = this.view.newImage("battle-actionbar-bottom-targetting", 0, 12);
this.img_targetting.anchor.set(0.5);
this.img_targetting.visible = false;
this.img_bottom.addChild(this.img_targetting);
// Left indicator
2017-01-19 23:39:13 +00:00
this.selected = false;
2017-09-28 23:18:46 +00:00
this.img_power = this.view.newImage("battle-actionbar-consumption-disabled", -46);
this.img_power.anchor.set(0.5);
this.img_power.visible = false;
this.container.addChild(this.img_power);
this.text_power = new Phaser.Text(bar.game, 0, 0, "", { align: "left", font: "16pt SpaceTac", fill: "#ffdd4b" });
this.text_power.setShadow(1, 1, "#000000");
this.text_power.anchor.set(0.5);
this.img_power.addChild(this.text_power);
// Right indicator
this.img_sticky = this.view.newImage("battle-actionbar-sticky-untoggled", 46);
this.img_sticky.anchor.set(0.5);
this.img_sticky.visible = action instanceof ToggleAction;
this.container.addChild(this.img_sticky);
// 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;
});
// 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);
}
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
*/
processClick(): void {
2017-02-12 18:54:09 +00:00
if (!this.bar.interactive) {
return;
}
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;
}
// End any previously selected action
this.bar.actionEnded();
2015-02-28 00:00:00 +00:00
this.bar.actionStarted();
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
}
}
2017-09-19 15:09:06 +00:00
/**
* Called when a target is selected
*
* This will effectively apply the action
*/
processSelection(target: Target): void {
if (this.action.apply(this.ship, target)) {
this.bar.actionEnded();
2015-01-06 00:00:00 +00:00
}
2014-12-31 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));
let cooldown = this.action.cooldown.heat;
2017-10-01 16:33:48 +00:00
if (this.action == used && this.action.cooldown.willOverheat()) {
fading = true;
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;
}
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-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-09-28 23:18:46 +00:00
// left
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
let power = this.action.getActionPointsUsage(this.ship, null);
this.img_power.visible = toggled || (power > 0);
this.text_power.text = `${power}`;
this.text_power.alpha = disabled ? 0.2 : 1;
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-09-28 23:18:46 +00:00
// right
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;
} 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 => {
this.img_sticky.addChild(this.view.newImage("battle-actionbar-cooldown-one", -4, 2 - i * 7));
});
this.img_sticky.addChild(this.view.newImage("battle-actionbar-cooldown-front", -8, -20));
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;
this.fading = fading;
this.toggled = toggled;
this.cooldown = cooldown;
}
2014-12-31 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}