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

85 lines
3.4 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac.UI {
2017-01-26 00:01:31 +00:00
// Group to display a star system
export class StarSystemDisplay extends Phaser.Image {
2017-02-09 00:00:35 +00:00
starsystem: Star;
player: Player;
2017-01-30 00:40:33 +00:00
fleet_display: FleetDisplay;
2017-02-09 00:00:35 +00:00
locations: [StarLocation, Phaser.Image, Phaser.Image][] = [];
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-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
this.addCircle(starsystem.radius);
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-01-30 00:40:33 +00:00
let fleet_move = () => this.fleet_display.moveToLocation(location);
2017-01-29 18:34:38 +00:00
2017-02-09 00:00:35 +00:00
if (location.type == StarLocationType.STAR) {
2017-01-30 00:40:33 +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-01-30 00:40:33 +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-01-26 23:01:04 +00:00
this.addCircle(Math.sqrt(location.x * location.x + location.y * location.y), 1);
2017-02-09 00:00:35 +00:00
} else if (location.type == StarLocationType.WARP) {
2017-01-30 00:40:33 +00:00
location_sprite = this.addImage(location.x, location.y, "map-location-warp", fleet_move);
2017-01-29 18:34:38 +00:00
}
if (location_sprite) {
2017-01-30 00:40:33 +00:00
let key = this.getVisitedKey(location);
let status_badge = this.addImage(location.x + 0.005, location.y + 0.005, key);
this.locations.push([location, location_sprite, status_badge]);
2017-01-26 00:01:31 +00:00
}
});
}
2017-01-30 00:40:33 +00:00
addImage(x: number, y: number, key: string, onclick: Function | null = null): Phaser.Image {
x /= this.scale.x;
y /= this.scale.y;
let image = onclick ? this.game.add.button(x, y, key, onclick) : this.game.add.image(x, y, key);
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
addCircle(radius, width = 3, color = 0x424242): Phaser.Graphics {
let circle = this.game.add.graphics(0, 0);
circle.lineStyle(width, color);
circle.drawCircle(0, 0, radius * 2 / this.scale.x);
this.addChild(circle);
return circle;
}
2017-01-30 00:40:33 +00:00
/**
* Return the sprite code to use for visited status.
*/
2017-02-09 00:00:35 +00:00
getVisitedKey(location: StarLocation) {
2017-01-30 00:40:33 +00:00
return this.player.hasVisitedLocation(location) ? (location.encounter ? "map-state-enemy" : "map-state-clear") : "map-state-unknown";
}
/**
* 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 => {
info[2].loadTexture(this.getVisitedKey(info[0]));
});
2017-03-09 23:21:34 +00:00
// LOD
let detailed = focus && level == 2;
this.children.forEach(child => Animation.setVisibility(this.game, child, detailed, 300));
2017-01-30 00:40:33 +00:00
}
2017-01-26 00:01:31 +00:00
}
}