2015-03-03 00:00:00 +00:00
|
|
|
/// <reference path="Serializable.ts"/>
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
module SpaceTac.Game {
|
2015-01-07 00:00:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// Main game universe
|
2015-03-03 00:00:00 +00:00
|
|
|
export class Universe extends Serializable {
|
2014-12-29 00:00:00 +00:00
|
|
|
// Current connected player
|
|
|
|
player: Player;
|
2015-02-27 00:00:00 +00:00
|
|
|
|
|
|
|
// Currently played battle
|
|
|
|
battle: Battle;
|
2015-03-03 00:00:00 +00:00
|
|
|
|
2015-03-19 00:00:00 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-03-03 00:00:00 +00:00
|
|
|
// Load a game state from a string
|
|
|
|
static loadFromString(serialized: string): Universe {
|
|
|
|
var serializer = new Serializer();
|
|
|
|
return <Universe>serializer.unserialize(serialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a new "quick battle" game
|
2015-03-06 00:00:00 +00:00
|
|
|
startQuickBattle(with_ai: boolean = false): void {
|
|
|
|
this.battle = Game.Battle.newQuickRandom(with_ai);
|
2015-03-03 00:00:00 +00:00
|
|
|
this.player = this.battle.fleets[0].player;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serializes the game state to a string
|
|
|
|
saveToString(): string {
|
|
|
|
var serializer = new Serializer();
|
|
|
|
return serializer.serialize(this);
|
|
|
|
}
|
2015-03-19 00:00:00 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|