1
0
Fork 0

Added handling of in-space targets for area effects

This commit is contained in:
Michaël Lemaire 2015-03-11 01:00:00 +01:00
parent cc6b6695f8
commit af82ffff7a
6 changed files with 72 additions and 30 deletions

View file

@ -106,6 +106,17 @@ module SpaceTac.Game {
return result;
}
// Collect all ships within a given radius of a target
collectShipsInCircle(center: Target, radius: number): Ship[] {
var result: Ship[] = [];
this.play_order.forEach((ship: Ship) => {
if (Target.newFromShip(ship).getDistanceTo(center) <= radius) {
result.push(ship);
}
});
return result;
}
// Ends a battle and sets the outcome
endBattle(winner: Fleet, log: boolean = true) {
this.ended = true;

View file

@ -39,6 +39,13 @@ module SpaceTac.Game {
return Math.sqrt(dx * dx + dy * dy);
}
// Get the normalized angle, in radians, to another target
getAngleTo(other: Target): number {
var dx = other.x - this.x;
var dy = other.y - this.y;
return Math.atan2(dy, dx);
}
// Check if a target is in range from a specific point
isInRange(x: number, y: number, radius: number): boolean {
var dx = this.x - x;

View file

@ -38,23 +38,27 @@ module SpaceTac.Game {
}
protected customApply(battle: Battle, ship: Ship, target: Target): boolean {
if (target.ship) {
// Fire event
ship.addBattleEvent(new FireEvent(ship, this.equipment, target));
var affected: Ship[] = [];
var blast = this.getBlastRadius(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;
// Collect affected ships
if (blast) {
affected = affected.concat(battle.collectShipsInCircle(target, blast));
} else if (target.ship) {
affected.push(target.ship);
}
// Fire event
ship.addBattleEvent(new FireEvent(ship, this.equipment, target));
// Apply all target effects
affected.forEach((affship: Ship) => {
this.equipment.target_effects.forEach((effect: BaseEffect) => {
effect.applyOnShip(affship);
});
});
return true;
}
}
}

View file

@ -198,5 +198,23 @@ module SpaceTac.Game {
expect(battle.log.events[0].code).toBe("endbattle");
expect((<EndBattleEvent>battle.log.events[0]).outcome.winner).toBeNull();
});
it("collects ships present in a circle", function () {
var fleet1 = new Fleet();
var ship1 = new Ship(fleet1, "F1S1");
ship1.setArenaPosition(0, 0);
var ship2 = new Ship(fleet1, "F1S2");
ship2.setArenaPosition(5, 8);
var ship3 = new Ship(fleet1, "F1S3");
ship3.setArenaPosition(6.5, 9.5);
var ship4 = new Ship(fleet1, "F1S4");
ship4.setArenaPosition(12, 12);
var battle = new Battle(fleet1);
battle.throwInitiative(new RandomGenerator(5, 4, 3, 2));
var result = battle.collectShipsInCircle(Target.newFromLocation(5, 8), 3);
expect(result).toEqual([ship2, ship3]);
});
});
}

View file

@ -116,17 +116,15 @@ module SpaceTac.View {
// Weapon used
private processFireEvent(event: Game.FireEvent): void {
// TODO Handle in-space target
var source = this.view.arena.findShipSprite(event.ship);
var destination = this.view.arena.findShipSprite(event.target.ship);
var source = Game.Target.newFromShip(event.ship);
var destination = event.target;
var dy = destination.y - source.y;
var dx = destination.x - source.x;
var angle = Math.atan2(dy, dx);
// Face the target
var attacker = this.view.arena.findShipSprite(event.ship);
var angle = source.getAngleTo(destination);
attacker.moveTo(source.x, source.y, angle, true);
source.moveTo(source.x, source.y, angle, true);
var effect = new WeaponEffect(source, destination, event.weapon.code);
var effect = new WeaponEffect(this.view.arena, source, destination, event.weapon.code);
effect.start();
}
}

View file

@ -11,11 +11,14 @@ module SpaceTac.View {
// Renderer of visual effects for a weapon fire
export class WeaponEffect {
// Arena in which to display the effect
private arena: Arena;
// Firing ship
private source: ArenaShip;
private source: Game.Target;
// Targetted ship
private destination: ArenaShip;
private destination: Game.Target;
// Weapon class code (e.g. GatlingGun, ...)
private weapon: string;
@ -23,7 +26,8 @@ module SpaceTac.View {
// Effect in use
private effect: Function;
constructor(source: ArenaShip, destination: ArenaShip, weapon: string) {
constructor(arena: Arena, source: Game.Target, destination: Game.Target, weapon: string) {
this.arena = arena;
this.source = source;
this.destination = destination;
this.weapon = weapon;
@ -44,15 +48,15 @@ module SpaceTac.View {
// Default firing effect (bullets)
private defaultEffect(): void {
this.source.game.sound.play("battle-weapon-bullets");
this.arena.game.sound.play("battle-weapon-bullets");
var source = this.source.sprite.toGlobal(new PIXI.Point(0, 0));
var destination = this.destination.sprite.toGlobal(new PIXI.Point(0, 0));
var source = this.arena.toGlobal(new PIXI.Point(this.source.x, this.source.y));
var destination = this.arena.toGlobal(new PIXI.Point(this.destination.x, this.destination.y));
var dx = destination.x - source.x;
var dy = destination.y - source.y;
var angle = Math.atan2(dy, dx);
var distance = Math.sqrt(dx * dx + dy * dy);
var emitter = new Phaser.Particles.Arcade.Emitter(this.source.game, source.x, source.y, 10);
var emitter = new Phaser.Particles.Arcade.Emitter(this.arena.game, source.x, source.y, 10);
var speed = 5000;
var scale = 0.1;
emitter.particleClass = BulletParticle;