1
0
Fork 0
spacetac/src/app/game/Target.ts

72 lines
2.2 KiB
TypeScript
Raw Normal View History

2015-03-03 00:00:00 +00:00
/// <reference path="Serializable.ts"/>
2014-12-31 00:00:00 +00:00
module SpaceTac.Game {
2015-01-07 00:00:00 +00:00
"use strict";
2014-12-31 00:00:00 +00:00
// Target for a capability
// This could be a location in space, or a ship
2015-03-03 00:00:00 +00:00
export class Target extends Serializable {
2014-12-31 00:00:00 +00:00
// Coordinates of the target
x: number;
y: number;
// If the target is a ship, this attribute will be set
ship: Ship;
// Standard constructor
constructor(x: number, y: number, ship: Ship) {
2015-03-03 00:00:00 +00:00
super();
2014-12-31 00:00:00 +00:00
this.x = x;
this.y = y;
this.ship = ship;
}
// 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
// Get distance to another target
getDistanceTo(other: Target): 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: Target): number {
var dx = other.x - this.x;
var dy = other.y - this.y;
return Math.atan2(dy, dx);
}
2015-01-28 00:00:00 +00:00
// Check if a target is in range from a specific point
isInRange(x: number, y: number, radius: number): boolean {
var dx = this.x - x;
var dy = this.y - y;
var length = Math.sqrt(dx * dx + dy * dy);
return (length <= radius);
}
// Constraint a target, to be in a given range from a specific point
// May return the original target if it's already in radius
constraintInRange(x: number, y: number, radius: number): Target {
var dx = this.x - x;
var dy = this.y - y;
var length = Math.sqrt(dx * dx + dy * dy);
if (length <= radius) {
return this;
} else {
var factor = radius / length;
return Target.newFromLocation(x + dx * factor, y + dy * factor);
}
}
2014-12-31 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}