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

70 lines
2.6 KiB
TypeScript
Raw Normal View History

/// <reference path="BaseAction.ts"/>
2017-02-09 00:00:35 +00:00
module TS.SpaceTac {
// Action to fire a weapon on another ship, or in space
export class FireWeaponAction extends BaseAction {
2015-01-28 00:00:00 +00:00
// Boolean set to true if the weapon can target space
can_target_space: boolean;
2017-03-09 17:11:00 +00:00
// Equipment cannot be null
equipment: Equipment;
2017-01-08 22:42:53 +00:00
constructor(equipment: Equipment, can_target_space = false, name = "Fire") {
super("fire-" + equipment.code, name, true, equipment);
2015-01-28 00:00:00 +00:00
this.can_target_space = can_target_space;
}
2017-03-09 17:11:00 +00:00
checkLocationTarget(ship: Ship, target: Target): Target | null {
if (target && this.can_target_space) {
2015-01-28 00:00:00 +00:00
target = target.constraintInRange(ship.arena_x, ship.arena_y, this.equipment.distance);
return target;
} else {
return null;
}
}
2017-03-09 17:11:00 +00:00
checkShipTarget(ship: Ship, target: Target): Target | null {
if (target.ship && ship.getPlayer() === target.ship.getPlayer()) {
// No friendly fire
return null;
} else {
2015-01-28 00:00:00 +00:00
// Check if target is in range
if (this.can_target_space) {
return this.checkLocationTarget(ship, new Target(target.x, target.y));
} else if (target.isInRange(ship.arena_x, ship.arena_y, this.equipment.distance)) {
2015-01-28 00:00:00 +00:00
return target;
} else {
return null;
}
}
}
/**
* Collect the effects applied by this action
*/
2017-03-09 17:11:00 +00:00
getEffects(ship: Ship, target: Target): [Ship, BaseEffect][] {
let result: [Ship, BaseEffect][] = [];
let blast = this.getBlastRadius(ship);
2017-03-09 17:11:00 +00:00
let battle = ship.getBattle();
let ships = (blast && battle) ? battle.collectShipsInCircle(target, blast, true) : ((target.ship && target.ship.alive) ? [target.ship] : []);
ships.forEach(ship => {
this.equipment.target_effects.forEach(effect => result.push([ship, effect]));
});
return result;
}
2015-02-20 00:00:00 +00:00
protected customApply(ship: Ship, target: Target) {
// Face the target
ship.rotate(Target.newFromShip(ship).getAngleTo(target));
// Fire event
ship.addBattleEvent(new FireEvent(ship, this.equipment, target));
// Apply effects
2017-03-09 17:11:00 +00:00
let effects = this.getEffects(ship, target);
effects.forEach(([ship, effect]) => effect.applyOnShip(ship));
}
}
}