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

121 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI {
2017-01-30 00:40:33 +00:00
const SCALING = 0.00005;
const LOCATIONS: [number, number][] = [
[80, 0],
[0, -50],
[0, 50],
[-80, 0],
[0, 0],
];
const PI2 = Math.PI * 2;
/**
* Group to display a fleet
*/
2018-05-15 14:57:45 +00:00
export class FleetDisplay extends UIContainer {
private map: UniverseMapView
private fleet: Fleet
private ship_count = 0
2017-01-30 00:40:33 +00:00
2017-02-09 00:00:35 +00:00
constructor(parent: UniverseMapView, fleet: Fleet) {
2018-05-15 14:57:45 +00:00
super(parent);
2017-01-30 00:40:33 +00:00
this.map = parent;
this.fleet = fleet;
this.updateShipSprites();
2017-01-30 00:40:33 +00:00
let location = this.map.universe.getLocation(fleet.location);
if (location) {
2018-05-15 14:57:45 +00:00
this.setPosition(location.star.x + location.x, location.star.y + location.y);
2017-03-09 17:11:00 +00:00
}
2018-05-15 14:57:45 +00:00
this.setScale(SCALING, SCALING);
2017-01-30 00:40:33 +00:00
this.loopOrbit();
}
/**
* Update the ship sprites
*/
updateShipSprites() {
if (this.ship_count != this.fleet.ships.length) {
2018-05-15 14:57:45 +00:00
let builder = new UIBuilder(this.map, this);
builder.clear();
this.fleet.ships.forEach((ship, index) => {
let offset = LOCATIONS[index];
2018-05-15 14:57:45 +00:00
let sprite = builder.image(`ship-${ship.model.code}-sprite`, offset[0], offset[1] + 150, true);
sprite.setScale(64 / sprite.width);
});
this.ship_count = this.fleet.ships.length;
}
}
2017-06-05 22:05:34 +00:00
get location(): StarLocation {
return this.map.universe.getLocation(this.fleet.location) || new StarLocation();
2017-06-05 22:05:34 +00:00
}
2017-01-30 00:40:33 +00:00
/**
* Animate to a given position in orbit of its current star location
*/
2017-02-09 21:01:37 +00:00
goToOrbitPoint(angle: number, speed = 1, fullturns = 0, then: Function | null = null, ease = false) {
2018-05-15 14:57:45 +00:00
this.map.animations.killPrevious(this);
2017-01-30 00:40:33 +00:00
this.rotation %= PI2;
let target = -angle;
while (target >= this.rotation) {
target -= PI2;
}
2017-02-09 21:01:37 +00:00
target -= PI2 * fullturns;
2017-01-30 00:40:33 +00:00
let distance = Math.abs(target - this.rotation) / PI2;
2018-05-15 14:57:45 +00:00
let tween = this.map.animations.addAnimation<UIContainer>(this, { rotation: target }, 30000 * distance / speed, ease ? "Cubic.easeIn" : "Linear");
2017-01-30 00:40:33 +00:00
if (then) {
2018-05-15 14:57:45 +00:00
tween.then(() => then());
2017-01-30 00:40:33 +00:00
}
}
/**
* Make the fleet loop in orbit
*/
loopOrbit() {
2017-02-09 21:01:37 +00:00
this.goToOrbitPoint(this.rotation + PI2, 1, 0, () => {
2017-01-30 00:40:33 +00:00
this.loopOrbit();
});
}
/**
* Make the fleet move to another location in the same system
*/
2017-06-05 22:05:34 +00:00
moveToLocation(location: StarLocation, speed = 1, on_leave: ((duration: number) => any) | null = null, on_finished: Function | null = null) {
let fleet_location = this.map.universe.getLocation(this.fleet.location);
if (fleet_location && this.fleet.move(location)) {
let dx = location.universe_x - fleet_location.universe_x;
let dy = location.universe_y - fleet_location.universe_y;
2017-01-30 00:40:33 +00:00
let distance = Math.sqrt(dx * dx + dy * dy);
let angle = Math.atan2(dx, dy);
2017-03-15 23:45:52 +00:00
this.map.current_location.setFleetMoving(true);
2017-02-09 21:01:37 +00:00
this.goToOrbitPoint(angle - Math.PI / 2, 40, 1, () => {
let duration = 10000 * distance / speed;
if (on_leave) {
on_leave(duration);
}
2018-05-15 14:57:45 +00:00
let tween = this.map.animations.addAnimation<UIContainer>(this, { x: this.x + dx, y: this.y + dy }, duration, "Cubic.easeOut");
tween.then(() => {
2017-01-30 00:40:33 +00:00
if (this.fleet.battle) {
2018-05-15 14:57:45 +00:00
this.map.backToRouter();
2017-01-30 00:40:33 +00:00
} else {
2017-03-15 23:45:52 +00:00
this.map.current_location.setFleetMoving(false);
2017-01-30 00:40:33 +00:00
this.loopOrbit();
}
2017-06-05 22:05:34 +00:00
if (on_finished) {
on_finished();
}
2017-01-30 00:40:33 +00:00
});
}, true);
}
}
}
}