2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2015-03-19 00:00:00 +00:00
|
|
|
// An hyperspace link between two star systems
|
2017-02-07 18:54:53 +00:00
|
|
|
export class StarLink {
|
2015-03-19 00:00:00 +00:00
|
|
|
// Stars
|
|
|
|
first: Star;
|
|
|
|
second: Star;
|
|
|
|
|
|
|
|
constructor(first: Star, second: Star) {
|
|
|
|
this.first = first;
|
|
|
|
this.second = second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this links bounds the two stars together, in either way
|
|
|
|
isLinking(first: Star, second: Star) {
|
|
|
|
return (this.first === first && this.second === second) || (this.first === second && this.second === first);
|
|
|
|
}
|
2015-03-19 00:00:00 +00:00
|
|
|
|
|
|
|
// Get the length of a link
|
|
|
|
getLength(): number {
|
|
|
|
return this.first.getDistanceTo(this.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this link crosses another
|
|
|
|
isCrossing(other: StarLink): boolean {
|
|
|
|
if (this.first === other.first || this.second === other.first || this.first === other.second || this.second === other.second) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var ccw = (a: Star, b: Star, c: Star): boolean => {
|
|
|
|
return (c.y - a.y) * (b.x - a.x) > (b.y - a.y) * (c.x - a.x);
|
|
|
|
};
|
|
|
|
var cc1 = ccw(this.first, other.first, other.second);
|
|
|
|
var cc2 = ccw(this.second, other.first, other.second);
|
|
|
|
var cc3 = ccw(this.first, this.second, other.first);
|
|
|
|
var cc4 = ccw(this.first, this.second, other.second);
|
|
|
|
return cc1 !== cc2 && cc3 !== cc4;
|
|
|
|
}
|
2015-03-27 00:00:00 +00:00
|
|
|
|
|
|
|
// Get the other side of the link, for a given side
|
2017-03-09 17:11:00 +00:00
|
|
|
getPeer(star: Star): Star | null {
|
2015-03-27 00:00:00 +00:00
|
|
|
if (star === this.first) {
|
|
|
|
return this.second;
|
|
|
|
} else if (star === this.second) {
|
|
|
|
return this.first;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|