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
|
|
|
|
*/
|
|
|
|
export class FleetDisplay extends Phaser.Group {
|
2017-07-09 21:18:05 +00:00
|
|
|
private map: UniverseMapView
|
|
|
|
private fleet: Fleet
|
|
|
|
private tween: Phaser.Tween
|
|
|
|
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) {
|
2017-01-30 00:40:33 +00:00
|
|
|
super(parent.game);
|
|
|
|
|
|
|
|
this.map = parent;
|
|
|
|
this.fleet = fleet;
|
|
|
|
|
2017-07-09 21:18:05 +00:00
|
|
|
this.updateShipSprites();
|
2017-01-30 00:40:33 +00:00
|
|
|
|
2018-01-16 00:08:24 +00:00
|
|
|
let location = this.map.universe.getLocation(fleet.location);
|
|
|
|
if (location) {
|
|
|
|
this.position.set(location.star.x + location.x, location.star.y + location.y);
|
2017-03-09 17:11:00 +00:00
|
|
|
}
|
2017-01-30 00:40:33 +00:00
|
|
|
this.scale.set(SCALING, SCALING);
|
|
|
|
|
|
|
|
this.tween = this.game.tweens.create(this);
|
|
|
|
this.loopOrbit();
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:18:05 +00:00
|
|
|
/**
|
|
|
|
* Update the ship sprites
|
|
|
|
*/
|
|
|
|
updateShipSprites() {
|
|
|
|
if (this.ship_count != this.fleet.ships.length) {
|
|
|
|
this.removeAll(true);
|
|
|
|
this.fleet.ships.forEach((ship, index) => {
|
|
|
|
let offset = LOCATIONS[index];
|
2017-07-26 22:54:56 +00:00
|
|
|
let sprite = this.map.newImage(`ship-${ship.model.code}-sprite`, offset[0], offset[1] + 150);
|
2017-07-09 21:18:05 +00:00
|
|
|
sprite.scale.set(64 / sprite.width);
|
|
|
|
sprite.anchor.set(0.5, 0.5);
|
2017-07-26 22:54:56 +00:00
|
|
|
this.add(sprite);
|
2017-07-09 21:18:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.ship_count = this.fleet.ships.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:05:34 +00:00
|
|
|
get location(): StarLocation {
|
2018-01-16 00:08:24 +00:00
|
|
|
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) {
|
2017-01-30 00:40:33 +00:00
|
|
|
this.tween.stop(false);
|
|
|
|
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;
|
2017-02-09 21:01:37 +00:00
|
|
|
this.tween = this.game.tweens.create(this).to({ rotation: target }, 30000 * distance / speed, ease ? Phaser.Easing.Cubic.In : Phaser.Easing.Linear.None);
|
2017-01-30 00:40:33 +00:00
|
|
|
if (then) {
|
|
|
|
this.tween.onComplete.addOnce(then);
|
|
|
|
}
|
|
|
|
this.tween.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-01-16 00:08:24 +00:00
|
|
|
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, () => {
|
2017-02-09 19:16:49 +00:00
|
|
|
let duration = 10000 * distance / speed;
|
|
|
|
if (on_leave) {
|
|
|
|
on_leave(duration);
|
|
|
|
}
|
|
|
|
let tween = this.game.tweens.create(this.position).to({ x: this.x + dx, y: this.y + dy }, duration, Phaser.Easing.Cubic.Out);
|
2017-01-30 00:40:33 +00:00
|
|
|
tween.onComplete.addOnce(() => {
|
|
|
|
if (this.fleet.battle) {
|
|
|
|
this.game.state.start("router");
|
|
|
|
} 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
|
|
|
});
|
|
|
|
tween.start();
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|