Apply weapon distance
This commit is contained in:
parent
e59d88f2c0
commit
32885d9651
|
@ -27,5 +27,27 @@ module SpaceTac.Game {
|
||||||
static newFromLocation(x: number, y: number): Target {
|
static newFromLocation(x: number, y: number): Target {
|
||||||
return new Target(x, y, null);
|
return new Target(x, y, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,14 @@ module SpaceTac.Game {
|
||||||
// Boolean at true if the action needs a target
|
// Boolean at true if the action needs a target
|
||||||
needs_target: boolean;
|
needs_target: boolean;
|
||||||
|
|
||||||
|
// Equipment that triggers this action
|
||||||
|
equipment: Equipment;
|
||||||
|
|
||||||
// Create the action
|
// Create the action
|
||||||
constructor(code: string, needs_target: boolean) {
|
constructor(code: string, needs_target: boolean, equipment: Equipment = null) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.needs_target = needs_target;
|
this.needs_target = needs_target;
|
||||||
|
this.equipment = equipment;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check basic conditions to know if the ship can use this action at all
|
// Check basic conditions to know if the ship can use this action at all
|
||||||
|
|
|
@ -5,8 +5,13 @@ module SpaceTac.Game {
|
||||||
|
|
||||||
// Action to fire a weapon on another ship, or in space
|
// Action to fire a weapon on another ship, or in space
|
||||||
export class FireWeaponAction extends BaseAction {
|
export class FireWeaponAction extends BaseAction {
|
||||||
constructor() {
|
// Boolean set to true if the weapon can target space
|
||||||
super("fire", true);
|
can_target_space: boolean;
|
||||||
|
|
||||||
|
constructor(equipment: Equipment, can_target_space: boolean = false) {
|
||||||
|
super("fire", true, equipment);
|
||||||
|
|
||||||
|
this.can_target_space = can_target_space;
|
||||||
}
|
}
|
||||||
|
|
||||||
canBeUsed(battle: Battle, ship: Ship): boolean {
|
canBeUsed(battle: Battle, ship: Ship): boolean {
|
||||||
|
@ -14,8 +19,12 @@ module SpaceTac.Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
checkLocationTarget(battle: Battle, ship: Ship, target: Target): Target {
|
checkLocationTarget(battle: Battle, ship: Ship, target: Target): Target {
|
||||||
// TODO In space targetting
|
if (this.can_target_space) {
|
||||||
return null;
|
target = target.constraintInRange(ship.arena_x, ship.arena_y, this.equipment.distance);
|
||||||
|
return target;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkShipTarget(battle: Battle, ship: Ship, target: Target): Target {
|
checkShipTarget(battle: Battle, ship: Ship, target: Target): Target {
|
||||||
|
@ -23,14 +32,29 @@ module SpaceTac.Game {
|
||||||
// No friendly fire
|
// No friendly fire
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
// TODO Limit by weapon range
|
// Check if target is in range
|
||||||
return target;
|
if (target.isInRange(ship.arena_x, ship.arena_y, this.equipment.distance)) {
|
||||||
|
return target;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected customApply(battle: Battle, ship: Ship, target: Target): boolean {
|
protected customApply(battle: Battle, ship: Ship, target: Target): boolean {
|
||||||
// TODO
|
if (target.ship) {
|
||||||
return false;
|
// Apply all target effects
|
||||||
|
var result = false;
|
||||||
|
this.equipment.target_effects.forEach((effect: BaseEffect) => {
|
||||||
|
var eff_result = effect.applyOnShip(target.ship);
|
||||||
|
result = result || eff_result;
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
// TODO target in space (=> apply blast radius)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,8 @@ module SpaceTac.Game {
|
||||||
|
|
||||||
// Action to move to a given location
|
// Action to move to a given location
|
||||||
export class MoveAction extends BaseAction {
|
export class MoveAction extends BaseAction {
|
||||||
constructor() {
|
constructor(equipment: Equipment) {
|
||||||
super("move", true);
|
super("move", true, equipment);
|
||||||
}
|
}
|
||||||
|
|
||||||
canBeUsed(battle: Battle, ship: Ship): boolean {
|
canBeUsed(battle: Battle, ship: Ship): boolean {
|
||||||
|
|
|
@ -11,5 +11,11 @@ module SpaceTac.Game {
|
||||||
constructor(code: string) {
|
constructor(code: string) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply ponctually the effect on a given ship
|
||||||
|
// Return true if the effect could be applied
|
||||||
|
applyOnShip(ship: Ship): boolean {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,5 +13,10 @@ module SpaceTac.Game {
|
||||||
|
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applyOnShip(ship: Ship): boolean {
|
||||||
|
// TODO
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,14 +5,26 @@ module SpaceTac.Game.Equipments {
|
||||||
|
|
||||||
// Base convenience class for weapons
|
// Base convenience class for weapons
|
||||||
export class AbstractWeapon extends LootTemplate {
|
export class AbstractWeapon extends LootTemplate {
|
||||||
constructor(name: string, min_damage: number, max_damage: number=null) {
|
// Boolean set to true if the weapon can target space
|
||||||
|
can_target_space: boolean;
|
||||||
|
|
||||||
|
constructor(name: string, min_damage: number, max_damage: number = null) {
|
||||||
super(SlotType.Weapon, name);
|
super(SlotType.Weapon, name);
|
||||||
|
|
||||||
|
this.can_target_space = false;
|
||||||
|
|
||||||
this.addDamageOnTargetEffect(min_damage, max_damage);
|
this.addDamageOnTargetEffect(min_damage, max_damage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the range for this weapon
|
||||||
|
// Pay attention that *min_distance* means the MAXIMAL reachable distance, but on a low-power loot
|
||||||
|
setRange(min_distance: number, max_distance: number = null, can_target_space: boolean = false): void {
|
||||||
|
this.distance = new Range(min_distance, max_distance);
|
||||||
|
this.can_target_space = can_target_space;
|
||||||
|
}
|
||||||
|
|
||||||
protected getActionForEquipment(equipment: Equipment): BaseAction {
|
protected getActionForEquipment(equipment: Equipment): BaseAction {
|
||||||
var result = new FireWeaponAction();
|
var result = new FireWeaponAction(equipment, this.can_target_space);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ module SpaceTac.Game.Equipments {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getActionForEquipment(equipment: Equipment): BaseAction {
|
protected getActionForEquipment(equipment: Equipment): BaseAction {
|
||||||
return new MoveAction();
|
return new MoveAction(equipment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,8 @@ module SpaceTac.Game.Equipments {
|
||||||
constructor() {
|
constructor() {
|
||||||
super("Gatling Gun", 10, 20);
|
super("Gatling Gun", 10, 20);
|
||||||
|
|
||||||
this.distance = new Range(20, 30);
|
this.setRange(80, 150, false);
|
||||||
|
|
||||||
this.ap_usage = new Range(3, 4);
|
this.ap_usage = new Range(3, 4);
|
||||||
this.min_level = new IntegerRange(1, 3);
|
this.min_level = new IntegerRange(1, 3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ module SpaceTac.Game {
|
||||||
ship.movement_cost = 2;
|
ship.movement_cost = 2;
|
||||||
ship.arena_x = 0;
|
ship.arena_x = 0;
|
||||||
ship.arena_y = 0;
|
ship.arena_y = 0;
|
||||||
var action = new MoveAction();
|
var action = new MoveAction(null);
|
||||||
|
|
||||||
var result = action.checkTarget(null, ship, Target.newFromLocation(0, 2));
|
var result = action.checkTarget(null, ship, Target.newFromLocation(0, 2));
|
||||||
expect(result).toEqual(Target.newFromLocation(0, 2));
|
expect(result).toEqual(Target.newFromLocation(0, 2));
|
||||||
|
@ -27,7 +27,7 @@ module SpaceTac.Game {
|
||||||
it("forbids targetting a ship", function () {
|
it("forbids targetting a ship", function () {
|
||||||
var ship1 = new Ship(null, "Test1");
|
var ship1 = new Ship(null, "Test1");
|
||||||
var ship2 = new Ship(null, "Test2");
|
var ship2 = new Ship(null, "Test2");
|
||||||
var action = new MoveAction();
|
var action = new MoveAction(null);
|
||||||
|
|
||||||
var result = action.checkTarget(null, ship1, Target.newFromShip(ship1));
|
var result = action.checkTarget(null, ship1, Target.newFromShip(ship1));
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
|
@ -44,7 +44,7 @@ module SpaceTac.Game {
|
||||||
ship.movement_cost = 1;
|
ship.movement_cost = 1;
|
||||||
ship.arena_x = 0;
|
ship.arena_x = 0;
|
||||||
ship.arena_y = 0;
|
ship.arena_y = 0;
|
||||||
var action = new MoveAction();
|
var action = new MoveAction(null);
|
||||||
|
|
||||||
var result = action.apply(battle, ship, Target.newFromLocation(10, 10));
|
var result = action.apply(battle, ship, Target.newFromLocation(10, 10));
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
|
|
|
@ -76,7 +76,7 @@ module SpaceTac.Game {
|
||||||
|
|
||||||
slot = ship.addSlot(SlotType.Engine);
|
slot = ship.addSlot(SlotType.Engine);
|
||||||
equipment = new Equipment();
|
equipment = new Equipment();
|
||||||
equipment.action = new MoveAction();
|
equipment.action = new MoveAction(equipment);
|
||||||
slot.attach(equipment);
|
slot.attach(equipment);
|
||||||
|
|
||||||
actions = ship.getAvailableActions();
|
actions = ship.getAvailableActions();
|
||||||
|
|
Loading…
Reference in a new issue