1
0
Fork 0
spacetac/src/core/actions/MoveAction.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac {
2014-12-31 00:00:00 +00:00
// Action to move to a given location
export class MoveAction extends BaseAction {
// Distance allowed for each power point
distance_per_power: number;
// Safety distance from other ships
safety_distance: number;
2017-03-09 17:11:00 +00:00
// Equipment cannot be null (engine)
equipment: Equipment;
constructor(equipment: Equipment, distance_per_power = 0) {
2017-01-08 22:42:53 +00:00
super("move", "Move", true, equipment);
this.distance_per_power = distance_per_power;
this.safety_distance = 50;
2014-12-31 00:00:00 +00:00
}
2017-03-09 17:11:00 +00:00
checkCannotBeApplied(ship: Ship, remaining_ap: number | null = null): string | null {
let base = super.checkCannotBeApplied(ship, Infinity);
if (base) {
return base;
}
// Check AP usage
if (remaining_ap === null) {
remaining_ap = ship.values.power.get();
}
if (remaining_ap > 0.0001) {
return null;
} else {
return "not enough power";
}
2014-12-31 00:00:00 +00:00
}
getActionPointsUsage(ship: Ship, target: Target): number {
2015-02-26 00:00:00 +00:00
if (target === null) {
return 0;
}
var distance = Target.newFromShip(ship).getDistanceTo(target);
return Math.ceil(distance / this.distance_per_power);
2015-02-26 00:00:00 +00:00
}
2015-03-03 00:00:00 +00:00
getRangeRadius(ship: Ship): number {
return ship.values.power.get() * this.distance_per_power;
2015-03-03 00:00:00 +00:00
}
/**
* Get the distance that may be traveled with 1 action point
*/
getDistanceByActionPoint(ship: Ship): number {
return this.distance_per_power;
}
checkLocationTarget(ship: Ship, target: Target): Target {
// Apply maximal distance
var max_distance = this.getRangeRadius(ship);
target = target.constraintInRange(ship.arena_x, ship.arena_y, max_distance);
// Apply collision prevention
let battle = ship.getBattle();
if (battle) {
battle.play_order.forEach((iship: Ship) => {
if (iship !== ship) {
target = target.moveOutOfCircle(iship.arena_x, iship.arena_y, this.safety_distance,
ship.arena_x, ship.arena_y);
}
});
}
return target;
2014-12-31 00:00:00 +00:00
}
protected customApply(ship: Ship, target: Target) {
ship.moveTo(target.x, target.y);
}
2014-12-31 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}