1
0
Fork 0
spacetac/src/ui/map/StarSystemDisplay.ts

155 lines
6.7 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI {
2017-01-26 00:01:31 +00:00
// Group to display a star system
export class StarSystemDisplay extends Phaser.Image {
view: UniverseMapView
circles: Phaser.Group
starsystem: Star
player: Player
fleet_display: FleetDisplay
locations: [StarLocation, Phaser.Image, Phaser.Image][] = []
2017-06-05 17:53:27 +00:00
label: Phaser.Button
2017-01-26 00:01:31 +00:00
2017-02-09 00:00:35 +00:00
constructor(parent: UniverseMapView, starsystem: Star) {
2017-01-26 00:01:31 +00:00
super(parent.game, starsystem.x, starsystem.y, "map-starsystem-background");
2017-01-30 00:40:33 +00:00
2017-03-15 21:40:19 +00:00
this.view = parent;
2017-01-26 00:01:31 +00:00
this.anchor.set(0.5, 0.5);
let scale = this.width;
this.scale.set(starsystem.radius * 2 / scale);
this.starsystem = starsystem;
2017-01-30 00:40:33 +00:00
this.player = parent.player;
this.fleet_display = parent.player_fleet;
2017-01-26 00:01:31 +00:00
2017-01-26 23:01:04 +00:00
// Show boundary
2017-03-15 21:40:19 +00:00
this.circles = new Phaser.Group(this.game);
this.addChild(this.circles);
2017-06-05 17:53:27 +00:00
let boundaries = this.game.add.image(0, 0, "map-boundaries", 0, this.circles);
boundaries.anchor.set(0.5);
boundaries.scale.set(starsystem.radius / (this.scale.x * 256));
2017-01-26 00:01:31 +00:00
2017-01-26 23:01:04 +00:00
// Show locations
2017-01-30 00:40:33 +00:00
starsystem.locations.map(location => {
2017-01-29 18:34:38 +00:00
let location_sprite: Phaser.Image | null = null;
2017-06-05 22:05:34 +00:00
let fleet_move = () => this.view.moveToLocation(location);
2017-01-29 18:34:38 +00:00
2017-02-09 00:00:35 +00:00
if (location.type == StarLocationType.STAR) {
2017-08-21 22:41:44 +00:00
location_sprite = this.addImage(location.x, location.y, "map-location-star", fleet_move);
2017-02-09 00:00:35 +00:00
} else if (location.type == StarLocationType.PLANET) {
2017-08-21 22:41:44 +00:00
location_sprite = this.addImage(location.x, location.y, "map-location-planet", fleet_move);
2017-01-29 18:34:38 +00:00
location_sprite.rotation = Math.atan2(location.y, location.x);
2017-06-05 17:53:27 +00:00
this.addCircle(location.x, location.y);
2017-02-09 00:00:35 +00:00
} else if (location.type == StarLocationType.WARP) {
2017-08-21 22:41:44 +00:00
location_sprite = this.addImage(location.x, location.y, "map-location-warp", fleet_move);
2017-06-05 17:53:27 +00:00
location_sprite.rotation = Math.atan2(location.y, location.x);
2017-01-29 18:34:38 +00:00
}
2017-03-15 21:40:19 +00:00
this.view.tooltip.bindDynamicText(<Phaser.Button>location_sprite, () => {
2017-03-23 18:58:09 +00:00
let visited = this.player.hasVisitedLocation(location);
2017-06-02 08:06:06 +00:00
let shop = (visited && !location.encounter && location.shop) ? " (dockyard present)" : "";
2017-03-23 18:58:09 +00:00
if (location.is(this.player.fleet.location)) {
2017-03-23 18:58:09 +00:00
return `Current fleet location${shop}`;
2017-03-15 21:40:19 +00:00
} else {
let loctype = StarLocationType[location.type].toLowerCase();
let danger = (visited && location.encounter) ? " [enemy fleet detected !]" : "";
2017-03-23 18:58:09 +00:00
return `${visited ? "Visited" : "Unvisited"} ${loctype} - Move the fleet there${danger}${shop}`;
2017-03-15 21:40:19 +00:00
}
});
2017-01-29 18:34:38 +00:00
if (location_sprite) {
2017-09-12 22:37:56 +00:00
let status = this.getBadgeFrame(location);
let status_badge = this.addImage(location.x + 0.005, location.y + 0.005, `map-status-${status}`);
2017-01-30 00:40:33 +00:00
this.locations.push([location, location_sprite, status_badge]);
2017-01-26 00:01:31 +00:00
}
});
// Show name
2017-06-05 17:53:27 +00:00
this.label = new Phaser.Button(this.game, 0, 460, "map-name");
this.label.anchor.set(0.5, 0.5);
2017-06-05 17:53:27 +00:00
this.label.input.useHandCursor = false;
2017-08-15 22:30:19 +00:00
let label_content = new Phaser.Text(this.game, 0, 0, this.starsystem.name, { align: "center", font: `32pt SpaceTac`, fill: "#b8d2f1" });
2017-06-05 17:53:27 +00:00
label_content.anchor.set(0.6, 0.5);
this.label.addChild(label_content);
2017-08-15 22:30:19 +00:00
let label_level = new Phaser.Text(this.game, 243, 30, this.starsystem.level.toString(), { align: "center", font: `24pt SpaceTac`, fill: "#a0a0a0" });
2017-06-05 17:53:27 +00:00
label_level.anchor.set(0.6, 0.5);
this.label.addChild(label_level);
this.addChild(this.label);
2017-06-05 17:53:27 +00:00
this.view.tooltip.bindStaticText(this.label, `Level ${this.starsystem.level} starsystem`);
2017-01-26 00:01:31 +00:00
}
2017-08-21 22:41:44 +00:00
addImage(x: number, y: number, name: string, onclick: Function | null = null): Phaser.Image {
2017-01-30 00:40:33 +00:00
x /= this.scale.x;
y /= this.scale.y;
2017-08-21 22:41:44 +00:00
let info = this.view.getImageInfo(name);
let image = onclick ? this.game.add.button(x, y, info.key, onclick, undefined, info.frame, info.frame) : this.game.add.image(x, y, info.key, info.frame);
2017-01-26 00:01:31 +00:00
image.anchor.set(0.5, 0.5);
this.addChild(image);
return image;
}
2017-01-26 23:01:04 +00:00
2017-06-05 17:53:27 +00:00
/**
* Add an orbit marker
*/
addCircle(x: number, y: number): void {
let radius = Math.sqrt(x * x + y * y);
let angle = Math.atan2(y, x);
let circle = this.game.add.image(0, 0, "map-orbit", 0, this.circles);
circle.anchor.set(0.5);
circle.scale.set(radius / (this.scale.x * 198));
circle.rotation = angle - 0.01;
2017-03-15 21:40:19 +00:00
this.circles.add(circle);
2017-01-26 23:01:04 +00:00
}
2017-01-30 00:40:33 +00:00
/**
2017-06-05 17:53:27 +00:00
* Return the frame to use for info badge.
2017-01-30 00:40:33 +00:00
*/
2017-08-21 22:41:44 +00:00
getBadgeFrame(location: StarLocation): string {
2017-03-23 18:58:09 +00:00
if (this.player.hasVisitedLocation(location)) {
if (location.encounter) {
2017-08-21 22:41:44 +00:00
return "enemy";
2017-03-23 18:58:09 +00:00
} else if (location.shop) {
2017-08-21 22:41:44 +00:00
return "dockyard";
2017-03-23 18:58:09 +00:00
} else {
2017-08-21 22:41:44 +00:00
return "clear";
2017-03-23 18:58:09 +00:00
}
} else {
2017-08-21 22:41:44 +00:00
return "unvisited";
2017-03-23 18:58:09 +00:00
}
2017-01-30 00:40:33 +00:00
}
/**
* Update displayed information, and fog of war
*/
2017-03-09 23:21:34 +00:00
updateInfo(level: number, focus: boolean) {
2017-01-30 00:40:33 +00:00
this.locations.forEach(info => {
2017-09-12 22:37:56 +00:00
let status = this.getBadgeFrame(info[0]);
this.view.changeImage(info[2], `map-status-${status}`);
2017-01-30 00:40:33 +00:00
});
2017-03-09 23:21:34 +00:00
// LOD
let detailed = focus && level == 2;
this.children.filter(child => child !== this.label).forEach(child => this.view.animations.setVisible(child, detailed, 300));
this.updateLabel(level);
}
/**
* Update label position and scaling
*/
updateLabel(zoom: number) {
this.label.visible = this.player.hasVisitedSystem(this.starsystem);
let factor = (zoom == 2) ? 1 : (zoom == 1 ? 5 : 15);
this.view.tweens.create(this.label.scale).to({ x: factor, y: factor }, 500, Phaser.Easing.Cubic.InOut).start();
2017-06-05 17:53:27 +00:00
let position = (zoom == 2) ? { x: -560, y: 440 } : { x: 0, y: (zoom == 1 ? 180 : 100) * factor };
this.view.tweens.create(this.label.position).to(position, 500, Phaser.Easing.Cubic.InOut).start();
2017-01-30 00:40:33 +00:00
}
2017-01-26 00:01:31 +00:00
}
}