Added basic star systems generation and displaying
This commit is contained in:
parent
9cc228e47c
commit
86b20ffd2b
BIN
src/assets/images/map/star-icon.png
Normal file
BIN
src/assets/images/map/star-icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.9 KiB |
|
@ -18,6 +18,7 @@ module SpaceTac {
|
|||
this.state.add('mainmenu', View.MainMenu);
|
||||
this.state.add('router', View.Router);
|
||||
this.state.add('battle', View.BattleView);
|
||||
this.state.add('universe', View.UniverseMapView);
|
||||
|
||||
this.state.start('boot');
|
||||
}
|
||||
|
|
31
src/scripts/game/Star.ts
Normal file
31
src/scripts/game/Star.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
/// <reference path="Serializable.ts"/>
|
||||
|
||||
module SpaceTac.Game {
|
||||
"use strict";
|
||||
|
||||
// A star system
|
||||
export class Star extends Serializable {
|
||||
// Parent universe
|
||||
universe: Universe;
|
||||
|
||||
// Location in the universe
|
||||
x: number;
|
||||
y: number;
|
||||
|
||||
constructor(universe: Universe, x: number, y: number) {
|
||||
super();
|
||||
|
||||
this.universe = universe;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
// Get the distance to another star
|
||||
getDistanceTo(star: Star): number {
|
||||
var dx = this.x - star.x;
|
||||
var dy = this.y - star.y;
|
||||
|
||||
return Math.sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
}
|
||||
}
|
24
src/scripts/game/StarLink.ts
Normal file
24
src/scripts/game/StarLink.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
/// <reference path="Serializable.ts"/>
|
||||
|
||||
module SpaceTac.Game {
|
||||
"use strict";
|
||||
|
||||
// An hyperspace link between two star systems
|
||||
export class StarLink extends Serializable {
|
||||
// Stars
|
||||
first: Star;
|
||||
second: Star;
|
||||
|
||||
constructor(first: Star, second: Star) {
|
||||
super();
|
||||
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
// Check if this links bounds the two stars together, in either way
|
||||
isLinking(first: Star, second: Star) {
|
||||
return (this.first === first && this.second === second) || (this.first === second && this.second === first);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,6 +11,22 @@ module SpaceTac.Game {
|
|||
// Currently played battle
|
||||
battle: Battle;
|
||||
|
||||
// List of star systems
|
||||
stars: Star[];
|
||||
|
||||
// List of links between star systems
|
||||
starlinks: StarLink[];
|
||||
|
||||
// Radius of the universe
|
||||
radius: number;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.stars = [];
|
||||
this.radius = 50;
|
||||
}
|
||||
|
||||
// Load a game state from a string
|
||||
static loadFromString(serialized: string): Universe {
|
||||
var serializer = new Serializer();
|
||||
|
@ -28,5 +44,64 @@ module SpaceTac.Game {
|
|||
var serializer = new Serializer();
|
||||
return serializer.serialize(this);
|
||||
}
|
||||
|
||||
// Generates a universe, with star systems and such
|
||||
generate(starcount: number = 50, random: RandomGenerator = new RandomGenerator()): void {
|
||||
this.stars = [];
|
||||
this.starlinks = [];
|
||||
|
||||
while (starcount) {
|
||||
var x = random.throw() * this.radius * 2.0 - this.radius;
|
||||
var y = random.throw() * this.radius * 2.0 - this.radius;
|
||||
var star = new Star(this, x, y);
|
||||
|
||||
var nearest = this.getNearestTo(star);
|
||||
if (nearest && nearest.getDistanceTo(star) < this.radius * 0.1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.stars.push(star);
|
||||
|
||||
starcount--;
|
||||
}
|
||||
|
||||
this.stars.forEach((first: Star) => {
|
||||
var second = this.getNearestTo(first);
|
||||
if (!this.areLinked(first, second)) {
|
||||
this.starlinks.push(new StarLink(first, second));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get the star nearest to another
|
||||
getNearestTo(star: Star): Star {
|
||||
if (this.stars.length === 0) {
|
||||
return null;
|
||||
} else {
|
||||
var mindist = this.radius * 2.0;
|
||||
var nearest: Star = null;
|
||||
this.stars.forEach((istar: Star) => {
|
||||
if (istar !== star) {
|
||||
var dist = star.getDistanceTo(istar);
|
||||
if (dist < mindist) {
|
||||
nearest = istar;
|
||||
mindist = dist;
|
||||
}
|
||||
}
|
||||
});
|
||||
return nearest;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a link exists between two stars
|
||||
areLinked(first: Star, second: Star): boolean {
|
||||
var result = false;
|
||||
this.starlinks.forEach((link: StarLink) => {
|
||||
if (link.isLinking(first, second)) {
|
||||
result = true;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,5 +39,12 @@ module SpaceTac.Game.Specs {
|
|||
// Check equality after game steps
|
||||
expect(loaded_universe).toEqual(universe);
|
||||
});
|
||||
|
||||
it("generates star systems", () => {
|
||||
var universe = new Universe();
|
||||
universe.generate(31);
|
||||
|
||||
expect(universe.stars.length).toBe(31);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@ module SpaceTac.View {
|
|||
"use strict";
|
||||
|
||||
export class MainMenu extends Phaser.State {
|
||||
button_new_game: Phaser.Button;
|
||||
button_quick_battle: Phaser.Button;
|
||||
button_load_game: Phaser.Button;
|
||||
|
||||
preload() {
|
||||
this.button_quick_battle = this.addButton(1280 / 2 - 300, 400, "Quick Battle", this.onQuickBattle);
|
||||
this.button_load_game = this.addButton(1280 / 2 + 300, 400, "Load game", this.onLoadGame);
|
||||
this.button_new_game = this.addButton(1280 / 2 - 300, 400, "New Game", this.onNewGame);
|
||||
this.button_quick_battle = this.addButton(1280 / 2, 400, "Quick Battle", this.onQuickBattle);
|
||||
this.button_load_game = this.addButton(1280 / 2 + 300, 400, "Load Game", this.onLoadGame);
|
||||
}
|
||||
|
||||
addButton(x: number, y: number, caption: string, callback: Function): Phaser.Button {
|
||||
|
@ -23,6 +25,16 @@ module SpaceTac.View {
|
|||
return button;
|
||||
}
|
||||
|
||||
// Called when "New Game" is clicked
|
||||
onNewGame(): void {
|
||||
var gameui = <GameUI>this.game;
|
||||
|
||||
gameui.universe = new Game.Universe();
|
||||
gameui.universe.generate();
|
||||
|
||||
this.game.state.start("router");
|
||||
}
|
||||
|
||||
// Called when "Quick Battle" is clicked
|
||||
onQuickBattle(): void {
|
||||
var gameui = <GameUI>this.game;
|
||||
|
@ -33,7 +45,7 @@ module SpaceTac.View {
|
|||
this.game.state.start("router");
|
||||
}
|
||||
|
||||
// Called when "Load game" is clicked
|
||||
// Called when "Load Game" is clicked
|
||||
onLoadGame(): void {
|
||||
var gameui = <GameUI>this.game;
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ module SpaceTac.View {
|
|||
this.loadImage("ship/scout/portrait.png");
|
||||
this.loadImage("common/standard-bar-background.png");
|
||||
this.loadImage("common/standard-bar-foreground.png");
|
||||
this.loadImage("map/star-icon.png");
|
||||
|
||||
// Load sounds
|
||||
this.loadSound("battle/ship-change.wav");
|
||||
|
|
|
@ -8,10 +8,13 @@ module SpaceTac.View {
|
|||
|
||||
if (!universe) {
|
||||
// No universe, go back to main menu
|
||||
this.game.state.start("mainmenu");
|
||||
this.game.state.start("mainmenu", true, false);
|
||||
} else if (universe.battle) {
|
||||
// A battle is raging, go to it
|
||||
this.game.state.start("battle", true, false, universe.player, universe.battle);
|
||||
} else {
|
||||
// Go to the universe map
|
||||
this.game.state.start("universe", true, false, universe, universe.player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
49
src/scripts/view/map/UniverseMapView.ts
Normal file
49
src/scripts/view/map/UniverseMapView.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
module SpaceTac.View {
|
||||
"use strict";
|
||||
|
||||
// Interactive map of the universe
|
||||
export class UniverseMapView extends Phaser.State {
|
||||
|
||||
// Displayed universe
|
||||
universe: Game.Universe;
|
||||
|
||||
// Interacting player
|
||||
player: Game.Player;
|
||||
|
||||
// Group for the stars and links
|
||||
stars: Phaser.Group;
|
||||
|
||||
// Init the view, binding it to a universe
|
||||
init(universe: Game.Universe, player: Game.Player) {
|
||||
this.universe = universe;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
// Create view graphics
|
||||
create() {
|
||||
this.stars = this.add.group();
|
||||
|
||||
var scale = 720 / (this.universe.radius * 2);
|
||||
this.stars.position.set(this.universe.radius * scale, this.universe.radius * scale);
|
||||
this.stars.scale.set(scale);
|
||||
|
||||
this.universe.starlinks.forEach((link: Game.StarLink) => {
|
||||
var line = this.add.graphics(0, 0, this.stars);
|
||||
line.lineStyle(0.3, 0xFFFFFF);
|
||||
line.moveTo(link.first.x, link.first.y);
|
||||
line.lineTo(link.second.x, link.second.y);
|
||||
});
|
||||
this.universe.stars.forEach((star: Game.Star) => {
|
||||
var sprite = this.add.sprite(star.x, star.y, "map-star-icon", 0, this.stars);
|
||||
sprite.scale.set(0.1, 0.1);
|
||||
sprite.anchor.set(0.5, 0.5);
|
||||
});
|
||||
}
|
||||
|
||||
// Leaving the view, unbind and destroy
|
||||
shutdown() {
|
||||
this.universe = null;
|
||||
this.player = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue