diff --git a/src/scripts/game/Target.ts b/src/scripts/game/Target.ts index 4489c3e..a7d7674 100644 --- a/src/scripts/game/Target.ts +++ b/src/scripts/game/Target.ts @@ -27,5 +27,27 @@ module SpaceTac.Game { static newFromLocation(x: number, y: number): Target { 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); + } + } } } diff --git a/src/scripts/game/actions/BaseAction.ts b/src/scripts/game/actions/BaseAction.ts index c0b9d5f..ad8305e 100644 --- a/src/scripts/game/actions/BaseAction.ts +++ b/src/scripts/game/actions/BaseAction.ts @@ -9,10 +9,14 @@ module SpaceTac.Game { // Boolean at true if the action needs a target needs_target: boolean; + // Equipment that triggers this action + equipment: Equipment; + // Create the action - constructor(code: string, needs_target: boolean) { + constructor(code: string, needs_target: boolean, equipment: Equipment = null) { this.code = code; this.needs_target = needs_target; + this.equipment = equipment; } // Check basic conditions to know if the ship can use this action at all diff --git a/src/scripts/game/actions/FireWeaponAction.ts b/src/scripts/game/actions/FireWeaponAction.ts index 6baeee0..ec3cab1 100644 --- a/src/scripts/game/actions/FireWeaponAction.ts +++ b/src/scripts/game/actions/FireWeaponAction.ts @@ -5,8 +5,13 @@ module SpaceTac.Game { // Action to fire a weapon on another ship, or in space export class FireWeaponAction extends BaseAction { - constructor() { - super("fire", true); + // Boolean set to true if the weapon can target space + 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 { @@ -14,8 +19,12 @@ module SpaceTac.Game { } checkLocationTarget(battle: Battle, ship: Ship, target: Target): Target { - // TODO In space targetting - return null; + if (this.can_target_space) { + 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 { @@ -23,14 +32,29 @@ module SpaceTac.Game { // No friendly fire return null; } else { - // TODO Limit by weapon range - return target; + // Check if target is in range + 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 { - // TODO - return false; + if (target.ship) { + // 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; + } + } } } diff --git a/src/scripts/game/actions/MoveAction.ts b/src/scripts/game/actions/MoveAction.ts index 2ddee6b..cfa7493 100644 --- a/src/scripts/game/actions/MoveAction.ts +++ b/src/scripts/game/actions/MoveAction.ts @@ -3,8 +3,8 @@ module SpaceTac.Game { // Action to move to a given location export class MoveAction extends BaseAction { - constructor() { - super("move", true); + constructor(equipment: Equipment) { + super("move", true, equipment); } canBeUsed(battle: Battle, ship: Ship): boolean { diff --git a/src/scripts/game/effects/BaseEffect.ts b/src/scripts/game/effects/BaseEffect.ts index 0bb176c..ca03f3b 100644 --- a/src/scripts/game/effects/BaseEffect.ts +++ b/src/scripts/game/effects/BaseEffect.ts @@ -11,5 +11,11 @@ module SpaceTac.Game { constructor(code: string) { 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; + } } } diff --git a/src/scripts/game/effects/DamageEffect.ts b/src/scripts/game/effects/DamageEffect.ts index cebb6b5..c4e5444 100644 --- a/src/scripts/game/effects/DamageEffect.ts +++ b/src/scripts/game/effects/DamageEffect.ts @@ -13,5 +13,10 @@ module SpaceTac.Game { this.value = value; } + + applyOnShip(ship: Ship): boolean { + // TODO + return true; + } } } diff --git a/src/scripts/game/equipments/AbstractWeapon.ts b/src/scripts/game/equipments/AbstractWeapon.ts index 377d227..d7dd680 100644 --- a/src/scripts/game/equipments/AbstractWeapon.ts +++ b/src/scripts/game/equipments/AbstractWeapon.ts @@ -5,14 +5,26 @@ module SpaceTac.Game.Equipments { // Base convenience class for weapons 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); + this.can_target_space = false; + 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 { - var result = new FireWeaponAction(); + var result = new FireWeaponAction(equipment, this.can_target_space); return result; } } diff --git a/src/scripts/game/equipments/ConventionalEngine.ts b/src/scripts/game/equipments/ConventionalEngine.ts index 1c9402f..d83455a 100644 --- a/src/scripts/game/equipments/ConventionalEngine.ts +++ b/src/scripts/game/equipments/ConventionalEngine.ts @@ -13,7 +13,7 @@ module SpaceTac.Game.Equipments { } protected getActionForEquipment(equipment: Equipment): BaseAction { - return new MoveAction(); + return new MoveAction(equipment); } } } diff --git a/src/scripts/game/equipments/GatlingGun.ts b/src/scripts/game/equipments/GatlingGun.ts index 7e9ef56..de806e3 100644 --- a/src/scripts/game/equipments/GatlingGun.ts +++ b/src/scripts/game/equipments/GatlingGun.ts @@ -8,7 +8,8 @@ module SpaceTac.Game.Equipments { constructor() { super("Gatling Gun", 10, 20); - this.distance = new Range(20, 30); + this.setRange(80, 150, false); + this.ap_usage = new Range(3, 4); this.min_level = new IntegerRange(1, 3); } diff --git a/src/scripts/game/specs/MoveAction.spec.ts b/src/scripts/game/specs/MoveAction.spec.ts index 5e5bdfa..5ef3d37 100644 --- a/src/scripts/game/specs/MoveAction.spec.ts +++ b/src/scripts/game/specs/MoveAction.spec.ts @@ -11,7 +11,7 @@ module SpaceTac.Game { ship.movement_cost = 2; ship.arena_x = 0; ship.arena_y = 0; - var action = new MoveAction(); + var action = new MoveAction(null); var result = action.checkTarget(null, ship, Target.newFromLocation(0, 2)); expect(result).toEqual(Target.newFromLocation(0, 2)); @@ -27,7 +27,7 @@ module SpaceTac.Game { it("forbids targetting a ship", function () { var ship1 = new Ship(null, "Test1"); var ship2 = new Ship(null, "Test2"); - var action = new MoveAction(); + var action = new MoveAction(null); var result = action.checkTarget(null, ship1, Target.newFromShip(ship1)); expect(result).toBeNull(); @@ -44,7 +44,7 @@ module SpaceTac.Game { ship.movement_cost = 1; ship.arena_x = 0; ship.arena_y = 0; - var action = new MoveAction(); + var action = new MoveAction(null); var result = action.apply(battle, ship, Target.newFromLocation(10, 10)); expect(result).toBe(true); diff --git a/src/scripts/game/specs/Ship.spec.ts b/src/scripts/game/specs/Ship.spec.ts index 698c46b..1eb0ef4 100644 --- a/src/scripts/game/specs/Ship.spec.ts +++ b/src/scripts/game/specs/Ship.spec.ts @@ -76,7 +76,7 @@ module SpaceTac.Game { slot = ship.addSlot(SlotType.Engine); equipment = new Equipment(); - equipment.action = new MoveAction(); + equipment.action = new MoveAction(equipment); slot.attach(equipment); actions = ship.getAvailableActions();