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

68 lines
2.2 KiB
TypeScript

module TS.SpaceTac.Game {
// Action to move to a given location
export class MoveAction extends BaseAction {
// Safety distance from other ships
safety_distance: number;
constructor(equipment: Equipment) {
super("move", "Move", true, equipment);
this.safety_distance = 50;
}
canBeUsed(battle: Battle, ship: Ship, remaining_ap: number = null): boolean {
if (battle && battle.playing_ship !== ship) {
return false;
}
// Check AP usage
if (remaining_ap === null) {
remaining_ap = ship.ap_current.current;
}
return remaining_ap > 0.0001;
}
getActionPointsUsage(battle: Battle, ship: Ship, target: Target): number {
if (target === null) {
return 0;
}
var distance = Target.newFromShip(ship).getDistanceTo(target);
return Math.ceil(this.equipment.ap_usage * distance / this.equipment.distance);
}
getRangeRadius(ship: Ship): number {
return ship.ap_current.current * this.equipment.distance / this.equipment.ap_usage;
}
/**
* Get the distance that may be traveled with 1 action point
*/
getDistanceByActionPoint(ship: Ship): number {
return this.equipment.distance / this.equipment.ap_usage;
}
checkLocationTarget(battle: Battle, 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
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;
}
protected customApply(battle: Battle, ship: Ship, target: Target): boolean {
ship.moveTo(target.x, target.y);
return true;
}
}
}