1
0
Fork 0
spacetac/src/core/Target.ts

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
2014-12-31 00:00:00 +00:00
// Target for a capability
// This could be a location in space, or a ship
export class Target {
2014-12-31 00:00:00 +00:00
// Coordinates of the target
x: number
y: number
2014-12-31 00:00:00 +00:00
// If the target is a ship, this attribute will be set
ship_id: RObjectId | null
2014-12-31 00:00:00 +00:00
// Standard constructor
constructor(x: number, y: number, ship: Ship | null = null) {
2014-12-31 00:00:00 +00:00
this.x = x;
this.y = y;
this.ship_id = ship ? ship.id : null;
2014-12-31 00:00:00 +00:00
}
jasmineToString() {
if (this.ship_id) {
return `(${this.x},${this.y}) ship_id=${this.ship_id}}`;
} else {
return `(${this.x},${this.y})`;
}
}
2014-12-31 00:00:00 +00:00
// Constructor to target a single ship
static newFromShip(ship: Ship): Target {
return new Target(ship.arena_x, ship.arena_y, ship);
}
// Constructor to target a location in space
static newFromLocation(x: number, y: number): Target {
return new Target(x, y, null);
}
2015-01-28 00:00:00 +00:00
2018-03-21 22:09:26 +00:00
/**
* Snap to battle grid
*/
2018-07-09 14:59:17 +00:00
snap(grid: ArenaGrid): Target {
2018-07-09 09:38:42 +00:00
if (this.ship_id) {
2018-03-21 22:09:26 +00:00
return this;
} else {
let location = grid.snap(this);
return Target.newFromLocation(location.x, location.y);
}
}
// Get distance to another target
getDistanceTo(other: { x: number, y: number }): number {
var dx = other.x - this.x;
var dy = other.y - this.y;
return Math.sqrt(dx * dx + dy * dy);
}
// Get the normalized angle, in radians, to another target
getAngleTo(other: { x: number, y: number }): number {
var dx = other.x - this.x;
var dy = other.y - this.y;
return Math.atan2(dy, dx);
}
/**
* Returns true if the target is a ship
2018-07-09 13:33:37 +00:00
*
* If a ship or ship id is passed, also check that it is the targetted one
*/
2018-07-09 13:33:37 +00:00
isShip(ship?: Ship | RObject): boolean {
return this.ship_id !== null && (typeof ship == "undefined" || rid(ship) == this.ship_id);
}
/**
* Get the targetted ship in a battle
*/
getShip(battle: Battle): Ship | null {
if (this.isShip()) {
return battle.getShip(this.ship_id);
} else {
return null;
}
}
2014-12-31 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}