From 83d4b0f97fd1c0d57182b6f2db01b4fbc864cd7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 25 Mar 2015 01:00:00 +0100 Subject: [PATCH] Generate one warp location for each link to other star systems --- src/app/game/Star.ts | 19 ++++++++++++++++--- src/app/game/Universe.ts | 8 ++++++++ src/app/game/specs/Star.spec.ts | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/app/game/specs/Star.spec.ts diff --git a/src/app/game/Star.ts b/src/app/game/Star.ts index c630637..8bb825d 100644 --- a/src/app/game/Star.ts +++ b/src/app/game/Star.ts @@ -50,10 +50,10 @@ module SpaceTac.Game { result.push(new StarLocation(this, StarLocationType.STAR, 0, 0)); // Add warp locations around the star - var warps = 3; - while (warps--) { + var links = this.getLinks(); + links.forEach((link: StarLink) => { result.push(this.generateOneLocation(StarLocationType.WARP, result, this.radius * 0.3, random)); - } + }); // Add random locations while (count--) { @@ -63,6 +63,19 @@ module SpaceTac.Game { return result; } + // Get the number of links to other stars + getLinks(): StarLink[] { + var result: StarLink[] = []; + + this.universe.starlinks.forEach((link: StarLink) => { + if (link.first === this || link.second === this) { + result.push(link); + } + }); + + return result; + } + private generateOneLocation(type: StarLocationType, others: StarLocation[], radius: number, random: RandomGenerator): StarLocation { var x = (random.throw(2) - 1) * radius; var y = (random.throw(2) - 1) * radius; diff --git a/src/app/game/Universe.ts b/src/app/game/Universe.ts index cca91e7..08b2d65 100644 --- a/src/app/game/Universe.ts +++ b/src/app/game/Universe.ts @@ -24,6 +24,7 @@ module SpaceTac.Game { super(); this.stars = []; + this.starlinks = []; this.radius = 50; } @@ -157,5 +158,12 @@ module SpaceTac.Game { }); return result; } + + // Add a link between two stars + addLink(first: Star, second: Star): void { + if (!this.areLinked(first, second)) { + this.starlinks.push(new StarLink(first, second)); + } + } } } diff --git a/src/app/game/specs/Star.spec.ts b/src/app/game/specs/Star.spec.ts new file mode 100644 index 0000000..ad63bac --- /dev/null +++ b/src/app/game/specs/Star.spec.ts @@ -0,0 +1,22 @@ +/// + +module SpaceTac.Game.Specs { + "use strict"; + + describe("Star", () => { + it("lists links to other stars", () => { + var universe = new Universe(); + universe.stars.push(new Star(universe, 0, 0)); + universe.stars.push(new Star(universe, 1, 0)); + universe.stars.push(new Star(universe, 0, 1)); + universe.stars.push(new Star(universe, 1, 1)); + universe.addLink(universe.stars[0], universe.stars[1]); + universe.addLink(universe.stars[0], universe.stars[3]); + + var result = universe.stars[0].getLinks(); + expect(result.length).toBe(2); + expect(result[0]).toEqual(new StarLink(universe.stars[0], universe.stars[1])); + expect(result[1]).toEqual(new StarLink(universe.stars[0], universe.stars[3])); + }); + }); +}