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

157 lines
4.7 KiB
TypeScript
Raw Normal View History

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
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
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;
2018-05-15 14:57:45 +00:00
// FIXME
2018-06-11 16:22:56 +00:00
this.info_button = builder.button("battle-shiplist-info-button", 0, 0, undefined, "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
*/
clearAll(): void {
2017-10-25 22:45:53 +00:00
this.items.forEach(ship => ship.destroy());
this.items = [];
}
2017-10-25 22:45:53 +00:00
/**
* Rebuild the ship list from an ongoing battle
*/
setShipsFromBattle(battle: Battle, animate = true): void {
this.clearAll();
2017-10-25 22:45:53 +00:00
iforeach(battle.iships(true), ship => this.addShip(ship));
this.refresh(animate ? 1 : 0);
}
/**
* Bind to a log processor, to watch for events
*/
bindToLog(log: LogProcessor): void {
log.watchForShipChange(ship => {
return {
foreground: async (speed: number) => {
this.refresh(speed);
}
}
});
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 {
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);
return result;
}
2017-10-25 22:45:53 +00:00
/**
* Find the item (card) that displays a given ship
*/
findItem(ship: Ship | RObjectId | null): ShipListItem | null {
return first(this.items, item => item.ship.is(ship));
}
2015-02-03 00:00:00 +00:00
2017-10-25 22:45:53 +00:00
/**
* Update the locations of all items
*/
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) {
item.moveAt(-14, 962, duration);
2017-10-25 22:45:53 +00:00
} else {
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
}
} else {
2018-05-15 14:57:45 +00:00
item.setZ(100);
item.moveAt(200, item.y, duration);
}
});
}
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
}
}
}
}