diff --git a/src/app/game/Player.ts b/src/app/game/Player.ts index 93fc488..02c95eb 100644 --- a/src/app/game/Player.ts +++ b/src/app/game/Player.ts @@ -71,5 +71,11 @@ module SpaceTac.Game { setBattle(battle: Battle): void { this.fleet.setBattle(battle); } + + // Exit the current battle unconditionally, if any + // This does not apply retreat penalties, or battle outcome, only unbind the battle from current session + exitBattle(): void { + this.setBattle(null); + } } } diff --git a/src/app/game/Star.ts b/src/app/game/Star.ts index 6061a81..3f7c35e 100644 --- a/src/app/game/Star.ts +++ b/src/app/game/Star.ts @@ -79,10 +79,10 @@ module SpaceTac.Game { // Base level for encounters in this system level: number; - constructor(universe: Universe, x: number, y: number) { + constructor(universe: Universe = null, x: number = 0, y: number = 0) { super(); - this.universe = universe; + this.universe = universe || new Universe(); this.x = x; this.y = y; this.radius = 0.1; diff --git a/src/app/game/StarLocation.ts b/src/app/game/StarLocation.ts index c43d3c1..b1b2203 100644 --- a/src/app/game/StarLocation.ts +++ b/src/app/game/StarLocation.ts @@ -30,10 +30,10 @@ module SpaceTac.Game { encounter: Fleet; encounter_gen: boolean; - constructor(star: Star, type: StarLocationType, x: number, y: number) { + constructor(star: Star, type: StarLocationType, x: number = 0, y: number = 0) { super(); - this.star = star; + this.star = star || new Star(); this.type = type; this.x = x; this.y = y; @@ -74,6 +74,15 @@ module SpaceTac.Game { var encounter = this.tryGenerateEncounter(random); if (encounter) { var battle = new Battle(fleet, encounter); + battle.log.subscribe((event: BaseLogEvent) => { + if (event.code === "endbattle") { + var endbattle = event; + if (!endbattle.outcome.draw && endbattle.outcome.winner !== encounter) { + // The encounter fleet lost, remove it + this.encounter = null; + } + } + }); battle.start(); return battle; } else { diff --git a/src/app/game/specs/StarLocation.spec.ts b/src/app/game/specs/StarLocation.spec.ts new file mode 100644 index 0000000..6c17d1e --- /dev/null +++ b/src/app/game/specs/StarLocation.spec.ts @@ -0,0 +1,35 @@ +/// + +module SpaceTac.Game.Specs { + "use strict"; + + describe("StarLocation", () => { + it("removes generated encounters that lose", function () { + var location = new StarLocation(null, StarLocationType.PLANET, 0, 0); + var fleet = new Fleet(); + var random = new RandomGenerator(0); + var battle = location.enterLocation(fleet, random); + + expect(location.encounter).not.toBeNull(); + expect(battle).not.toBeNull(); + + battle.endBattle(fleet); + + expect(location.encounter).toBeNull(); + }); + + it("leaves generated encounters that win", function () { + var location = new StarLocation(null, StarLocationType.PLANET, 0, 0); + var fleet = new Fleet(); + var random = new RandomGenerator(0); + var battle = location.enterLocation(fleet, random); + + expect(location.encounter).not.toBeNull(); + expect(battle).not.toBeNull(); + + battle.endBattle(location.encounter); + + expect(location.encounter).not.toBeNull(); + }); + }); +} diff --git a/src/app/view/BaseView.ts b/src/app/view/BaseView.ts index 841f8cd..f6bb265 100644 --- a/src/app/view/BaseView.ts +++ b/src/app/view/BaseView.ts @@ -4,7 +4,7 @@ module SpaceTac.View { // Base class for all game views export class BaseView extends Phaser.State { // Link to the root UI - protected gameui: GameUI; + gameui: GameUI; // Message notifications messages: Messages; diff --git a/src/app/view/battle/LogProcessor.ts b/src/app/view/battle/LogProcessor.ts index 654d120..c803fac 100644 --- a/src/app/view/battle/LogProcessor.ts +++ b/src/app/view/battle/LogProcessor.ts @@ -52,7 +52,7 @@ module SpaceTac.View { this.processFireEvent(event); break; case "endbattle": - this.view.setInteractionEnabled(false); + this.processEndBattleEvent(event); break; } } @@ -127,5 +127,19 @@ module SpaceTac.View { var effect = new WeaponEffect(this.view.arena, source, destination, event.weapon.code); effect.start(); } + + // Battle ended (victory or defeat) + private processEndBattleEvent(event: Game.EndBattleEvent): void { + this.view.setInteractionEnabled(false); + + if (event.outcome.winner.player === this.view.player) { + // Victory ! + // TODO Loot screen + this.view.player.exitBattle(); + this.view.game.state.start("router"); + } else { + // TODO Game over ? + } + } } }