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

157 lines
4.8 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
container: Phaser.Image
// 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
2017-10-25 22:45:53 +00:00
info_button: Phaser.Button
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);
this.container = builder.image("battle-shiplist-background", x, y);
2017-05-14 21:03:03 +00:00
2017-10-25 22:45:53 +00:00
this.view = view;
// TODO Should use an UI game state, not the actual game state
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;
2017-10-25 22:45:53 +00:00
this.info_button = new Phaser.Button(view.game, 0, 0, "battle-shiplist-info-button");
this.view.inputs.setHoverClick(this.info_button,
() => tactical_mode.manipulate("shiplist")(true),
() => tactical_mode.manipulate("shiplist")(false),
() => null);
2017-10-25 22:45:53 +00:00
this.container.addChild(this.info_button);
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);
}
/**
* Bind to a log processor, to watch for events
*/
bindToLog(log: LogProcessor): void {
log.watchForShipChange(ship => {
return {
foreground: async (animate) => {
this.refresh(animate)
}
}
});
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);
this.container.addChild(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(animate = true): void {
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.moveTo(-18, 962, animate ? 1000 : 0);
} else {
item.moveTo(2, 942 - position * 99, animate ? 1000 : 0);
}
item.visible = true;
this.container.setChildIndex(item, position);
}
} else {
2017-10-25 22:45:53 +00:00
item.moveTo(200, item.y, animate ? 1000 : 0);
}
});
}
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
}
}
}
}