2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac.UI {
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Side bar with all playing ships, sorted by play order
|
|
|
|
*/
|
|
|
|
export class ShipList {
|
|
|
|
// Link to the parent view
|
|
|
|
view: BaseView
|
2015-01-14 00:00:00 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
// Current battle
|
|
|
|
battle: Battle
|
|
|
|
|
|
|
|
// Current player
|
|
|
|
player: Player
|
|
|
|
|
|
|
|
// Interface for acting as ship button
|
|
|
|
ship_buttons: IShipButton
|
2015-01-14 00:00:00 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
// Container
|
2018-05-15 14:57:45 +00:00
|
|
|
container: UIContainer
|
2017-10-25 22:45:53 +00:00
|
|
|
|
|
|
|
// List of ship items
|
|
|
|
items: ShipListItem[]
|
2015-02-03 00:00:00 +00:00
|
|
|
|
2015-02-04 00:00:00 +00:00
|
|
|
// Hovered ship
|
2017-10-25 22:45:53 +00:00
|
|
|
hovered: ShipListItem | null
|
2015-02-04 00:00:00 +00:00
|
|
|
|
2017-05-14 21:03:03 +00:00
|
|
|
// Info button
|
2018-05-15 14:57:45 +00:00
|
|
|
info_button: UIButton
|
2017-10-25 22:45:53 +00:00
|
|
|
|
|
|
|
constructor(view: BaseView, battle: Battle, player: Player, tactical_mode: Toggle, ship_buttons: IShipButton, parent?: UIContainer, x = 0, y = 0) {
|
|
|
|
let builder = new UIBuilder(view, parent);
|
2018-05-15 14:57:45 +00:00
|
|
|
this.container = builder.container("shiplist", x, y);
|
|
|
|
|
|
|
|
builder = builder.in(this.container);
|
|
|
|
builder.image("battle-shiplist-background", 0, 0);
|
2017-05-14 21:03:03 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
this.view = view;
|
|
|
|
this.battle = battle;
|
|
|
|
this.player = player;
|
|
|
|
this.ship_buttons = ship_buttons;
|
2016-10-26 21:15:04 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
this.items = [];
|
2015-02-04 00:00:00 +00:00
|
|
|
this.hovered = null;
|
2015-01-14 00:00:00 +00:00
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
// FIXME
|
|
|
|
this.info_button = builder.button("battle-shiplist-info-button", 0, 0, () => null, "Tactical display", on => tactical_mode.manipulate("shiplist")(on));
|
2017-05-14 21:03:03 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
this.setShipsFromBattle(battle);
|
2015-04-26 19:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Clear all ship cards
|
|
|
|
*/
|
2015-01-14 00:00:00 +00:00
|
|
|
clearAll(): void {
|
2017-10-25 22:45:53 +00:00
|
|
|
this.items.forEach(ship => ship.destroy());
|
|
|
|
this.items = [];
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Rebuild the ship list from an ongoing battle
|
|
|
|
*/
|
|
|
|
setShipsFromBattle(battle: Battle, animate = true): void {
|
2015-01-14 00:00:00 +00:00
|
|
|
this.clearAll();
|
2017-10-25 22:45:53 +00:00
|
|
|
iforeach(battle.iships(true), ship => this.addShip(ship));
|
2018-06-10 22:58:42 +00:00
|
|
|
this.refresh(animate ? 1 : 0);
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 18:12:06 +00:00
|
|
|
/**
|
|
|
|
* Bind to a log processor, to watch for events
|
|
|
|
*/
|
|
|
|
bindToLog(log: LogProcessor): void {
|
|
|
|
log.watchForShipChange(ship => {
|
|
|
|
return {
|
2018-06-10 22:58:42 +00:00
|
|
|
foreground: async (speed: number) => {
|
|
|
|
this.refresh(speed);
|
2017-12-04 18:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
log.register(diff => {
|
|
|
|
if (diff instanceof ShipDamageDiff) {
|
|
|
|
return {
|
|
|
|
background: async () => {
|
|
|
|
let item = this.findItem(diff.ship_id);
|
|
|
|
if (item) {
|
|
|
|
item.setDamageHit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Add a ship card
|
|
|
|
*/
|
2017-02-09 00:00:35 +00:00
|
|
|
addShip(ship: Ship): ShipListItem {
|
2017-11-14 00:07:06 +00:00
|
|
|
var owned = ship.isPlayedBy(this.player);
|
2017-10-25 22:45:53 +00:00
|
|
|
var result = new ShipListItem(this, 200, this.container.height / 2, ship, owned, this.ship_buttons);
|
|
|
|
this.items.push(result);
|
2018-05-15 14:57:45 +00:00
|
|
|
this.container.add(result);
|
2015-01-14 00:00:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-01-28 00:00:00 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Find the item (card) that displays a given ship
|
|
|
|
*/
|
2017-11-14 00:07:06 +00:00
|
|
|
findItem(ship: Ship | RObjectId | null): ShipListItem | null {
|
|
|
|
return first(this.items, item => item.ship.is(ship));
|
2015-01-28 00:00:00 +00:00
|
|
|
}
|
2015-02-03 00:00:00 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Update the locations of all items
|
|
|
|
*/
|
2018-06-10 22:58:42 +00:00
|
|
|
refresh(speed = 1): void {
|
|
|
|
let duration = speed ? 1000 / speed : 0;
|
2017-10-25 22:45:53 +00:00
|
|
|
this.items.forEach(item => {
|
|
|
|
if (item.ship.alive) {
|
|
|
|
let position = this.battle.getPlayOrder(item.ship);
|
|
|
|
if (position < 0) {
|
|
|
|
item.visible = false;
|
|
|
|
} else {
|
|
|
|
if (position == 0) {
|
2018-06-10 22:58:42 +00:00
|
|
|
item.moveAt(-14, 962, duration);
|
2017-10-25 22:45:53 +00:00
|
|
|
} else {
|
2018-06-10 22:58:42 +00:00
|
|
|
item.moveAt(2, 942 - position * 99, duration);
|
2017-10-25 22:45:53 +00:00
|
|
|
}
|
|
|
|
item.visible = true;
|
2018-05-15 14:57:45 +00:00
|
|
|
item.setZ(99 - position);
|
2017-10-25 22:45:53 +00:00
|
|
|
}
|
2015-04-26 17:15:45 +00:00
|
|
|
} else {
|
2018-05-15 14:57:45 +00:00
|
|
|
item.setZ(100);
|
2018-06-10 22:58:42 +00:00
|
|
|
item.moveAt(200, item.y, duration);
|
2015-04-26 17:15:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
/**
|
|
|
|
* Set the currently hovered ship
|
|
|
|
*/
|
2017-03-09 17:11:00 +00:00
|
|
|
setHovered(ship: Ship | null): void {
|
2015-02-04 00:00:00 +00:00
|
|
|
if (this.hovered) {
|
|
|
|
this.hovered.setHovered(false);
|
2017-03-09 17:11:00 +00:00
|
|
|
this.hovered = null;
|
2015-02-04 00:00:00 +00:00
|
|
|
}
|
2017-03-09 17:11:00 +00:00
|
|
|
if (ship) {
|
|
|
|
this.hovered = this.findItem(ship);
|
|
|
|
if (this.hovered) {
|
|
|
|
this.hovered.setHovered(true);
|
|
|
|
}
|
2015-02-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|