2017-03-05 17:48:13 +00:00
|
|
|
module TS.SpaceTac.UI {
|
2017-03-05 23:29:02 +00:00
|
|
|
/**
|
2017-03-22 21:16:59 +00:00
|
|
|
* Interface for any graphical area that may contain or receive an equipment
|
2017-03-05 23:29:02 +00:00
|
|
|
*/
|
2017-03-22 21:16:59 +00:00
|
|
|
export interface CharacterEquipmentContainer {
|
|
|
|
/**
|
|
|
|
* Check if a point in the character sheet is inside the container
|
|
|
|
*/
|
|
|
|
isInside(x: number, y: number): boolean
|
|
|
|
/**
|
|
|
|
* Get a centric anchor point and scaling to snap the equipment
|
|
|
|
*/
|
|
|
|
getEquipmentAnchor(): { x: number, y: number, scale: number }
|
|
|
|
/**
|
|
|
|
* Add an equipment to the container
|
|
|
|
*/
|
|
|
|
addEquipment(equipment: CharacterEquipment, source: CharacterEquipmentContainer | null, test: boolean): boolean
|
|
|
|
/**
|
|
|
|
* Remove an equipment from the container
|
|
|
|
*/
|
|
|
|
removeEquipment(equipment: CharacterEquipment, destination: CharacterEquipmentContainer | null, test: boolean): boolean
|
2017-03-05 23:29:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 17:48:13 +00:00
|
|
|
/**
|
|
|
|
* Display a ship equipment, either attached to a slot, in cargo, or being dragged down
|
|
|
|
*/
|
2017-03-15 21:40:19 +00:00
|
|
|
export class CharacterEquipment extends Phaser.Button {
|
2017-03-22 21:16:59 +00:00
|
|
|
sheet: CharacterSheet
|
|
|
|
item: Equipment
|
|
|
|
container: CharacterEquipmentContainer
|
|
|
|
tooltip: string
|
2017-03-14 17:48:04 +00:00
|
|
|
|
2017-03-22 21:16:59 +00:00
|
|
|
constructor(sheet: CharacterSheet, equipment: Equipment, container: CharacterEquipmentContainer) {
|
2017-03-09 19:11:19 +00:00
|
|
|
let icon = sheet.game.cache.checkImageKey(`equipment-${equipment.code}`) ? `equipment-${equipment.code}` : `battle-actions-${equipment.action.code}`;
|
|
|
|
super(sheet.game, 0, 0, icon);
|
2017-03-05 17:48:13 +00:00
|
|
|
|
2017-03-22 21:16:59 +00:00
|
|
|
this.sheet = sheet;
|
|
|
|
this.item = equipment;
|
|
|
|
this.container = container;
|
|
|
|
this.tooltip = equipment.name;
|
|
|
|
|
|
|
|
this.container.addEquipment(this, null, false);
|
2017-03-14 17:48:04 +00:00
|
|
|
|
2017-03-05 17:48:13 +00:00
|
|
|
this.anchor.set(0.5, 0.5);
|
2017-03-05 23:29:02 +00:00
|
|
|
|
2017-03-14 17:48:04 +00:00
|
|
|
this.setupDragDrop(sheet);
|
2017-03-22 21:16:59 +00:00
|
|
|
this.snapToContainer();
|
2017-03-15 21:40:19 +00:00
|
|
|
|
2017-03-22 21:16:59 +00:00
|
|
|
// TODO better tooltip (with equipment characteristics)
|
|
|
|
sheet.view.tooltip.bindDynamicText(this, () => this.tooltip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the container under a specific screen location
|
|
|
|
*/
|
|
|
|
findContainerAt(x: number, y: number): CharacterEquipmentContainer | null {
|
|
|
|
return ifirst(this.sheet.iEquipmentContainers(), container => container.isInside(x, y));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Snap in place to its current container
|
|
|
|
*/
|
|
|
|
snapToContainer() {
|
|
|
|
let info = this.container.getEquipmentAnchor();
|
|
|
|
this.position.set(info.x, info.y);
|
|
|
|
this.scale.set(0.5 * info.scale, 0.5 * info.scale);
|
|
|
|
this.alpha = 1.0;
|
2017-03-14 17:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable dragging to another slot
|
|
|
|
*/
|
|
|
|
setupDragDrop(sheet: CharacterSheet) {
|
2017-03-05 23:29:02 +00:00
|
|
|
this.inputEnabled = true;
|
|
|
|
this.input.enableDrag(false, true);
|
|
|
|
|
|
|
|
this.events.onDragStart.add(() => {
|
2017-03-14 17:48:04 +00:00
|
|
|
this.scale.set(0.5, 0.5);
|
2017-03-05 23:29:02 +00:00
|
|
|
this.alpha = 0.8;
|
|
|
|
});
|
|
|
|
this.events.onDragUpdate.add(() => {
|
2017-03-22 21:16:59 +00:00
|
|
|
let destination = this.findContainerAt(this.x, this.y);
|
|
|
|
if (destination) {
|
|
|
|
this.applyDragDrop(this.container, destination, true);
|
|
|
|
}
|
2017-03-05 23:29:02 +00:00
|
|
|
});
|
|
|
|
this.events.onDragStop.add(() => {
|
2017-03-22 21:16:59 +00:00
|
|
|
let destination = this.findContainerAt(this.x, this.y);
|
|
|
|
if (destination) {
|
|
|
|
this.applyDragDrop(this.container, destination, false);
|
2017-03-05 23:29:02 +00:00
|
|
|
sheet.refresh();
|
|
|
|
} else {
|
2017-03-22 21:16:59 +00:00
|
|
|
this.snapToContainer();
|
2017-03-05 23:29:02 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-05 17:48:13 +00:00
|
|
|
}
|
2017-03-14 17:48:04 +00:00
|
|
|
|
|
|
|
/**
|
2017-03-22 21:16:59 +00:00
|
|
|
* Apply drag and drop between two containers
|
2017-03-14 17:48:04 +00:00
|
|
|
*/
|
2017-03-22 21:16:59 +00:00
|
|
|
applyDragDrop(source: CharacterEquipmentContainer, destination: CharacterEquipmentContainer, hold: boolean) {
|
|
|
|
if (source.removeEquipment(this, destination, true) && destination.addEquipment(this, source, true)) {
|
|
|
|
if (!hold) {
|
|
|
|
if (source.removeEquipment(this, destination, false)) {
|
|
|
|
if (!destination.addEquipment(this, source, false)) {
|
|
|
|
console.error("Destination container refused to accept equipment", this, source, destination);
|
|
|
|
// Go back to source
|
|
|
|
if (!source.addEquipment(this, null, true)) {
|
|
|
|
console.error("Equipment lost in bad exchange !", this, source, destination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error("Source container refused to give away equipment", this, source, destination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 17:48:04 +00:00
|
|
|
}
|
2017-03-05 17:48:13 +00:00
|
|
|
}
|
|
|
|
}
|