188 lines
5.9 KiB
C#
188 lines
5.9 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
public struct GeneratorSettings
|
|
{
|
|
public CoordinateVector SpaceSize;
|
|
public CoordinateVector GalaxySizeMultiplier;
|
|
|
|
public long Galaxies;
|
|
public long MaxNebulasPerGalaxy;
|
|
public long MaxStarsPerGalaxy;
|
|
public long MaxPlanetsPerStar;
|
|
public long MaxMoonsPerPlanet;
|
|
}
|
|
|
|
public partial class Generator
|
|
{
|
|
private GeneratorSettings GeneratorSettings;
|
|
private Random RandomGenerator;
|
|
|
|
public Generator(GeneratorSettings generatorSettings)
|
|
{
|
|
this.GeneratorSettings = generatorSettings;
|
|
this.RandomGenerator = new Random();
|
|
}
|
|
|
|
public Universe GenerateUniverse()
|
|
{
|
|
Universe universe = new Universe();
|
|
|
|
Task[] galaxyGenerationTasks = new Task[this.GeneratorSettings.Galaxies];
|
|
|
|
for (long i = 0; i < this.GeneratorSettings.Galaxies; i++)
|
|
{
|
|
galaxyGenerationTasks[i] = new Task(() =>
|
|
{
|
|
Galaxy galaxy = GenerateGalaxy("Galaxy " + i);
|
|
GenerateLife(galaxy);
|
|
|
|
universe.GameObjects.Add(galaxy);
|
|
});
|
|
galaxyGenerationTasks[i].Start();
|
|
}
|
|
|
|
Task.WaitAll(galaxyGenerationTasks);
|
|
|
|
return universe;
|
|
}
|
|
|
|
public Galaxy GenerateGalaxy(string name)
|
|
{
|
|
CoordinateVector galaxyPosition = Helpers.GetRandomCoordinates(
|
|
0M, this.GeneratorSettings.SpaceSize.X,
|
|
0M, this.GeneratorSettings.SpaceSize.Y
|
|
);
|
|
CoordinateVector galaxySize = Helpers.GetRandomCoordinatesFromLY(
|
|
50000, 250000,
|
|
50000, 250000,
|
|
this.GeneratorSettings.GalaxySizeMultiplier.X
|
|
);
|
|
|
|
Galaxy galaxy = new Galaxy(name, galaxyPosition, galaxySize);
|
|
|
|
long nebulaCount = Helpers.GetRandomNumber(0, this.GeneratorSettings.MaxNebulasPerGalaxy);
|
|
long starCount = Helpers.GetRandomNumber(10000, this.GeneratorSettings.MaxStarsPerGalaxy);
|
|
|
|
for (long i = 0; i < nebulaCount; i++)
|
|
{
|
|
Nebula nebula = GenerateNebula(name + ", Nebula " + i, galaxy);
|
|
galaxy.GameObjects.Add(nebula);
|
|
}
|
|
|
|
for (long i = 0; i < starCount; i++)
|
|
{
|
|
Star star = GenerateStar(name + ", Star " + i, galaxy);
|
|
galaxy.GameObjects.Add(star);
|
|
}
|
|
|
|
return galaxy;
|
|
}
|
|
|
|
public Nebula GenerateNebula(string name, GameObject parent)
|
|
{
|
|
CoordinateVector nebulaPosition = Helpers.GetRandomCoordinates(
|
|
parent.Position.X, parent.Position.X + parent.Size.X,
|
|
parent.Position.Y, parent.Position.Y + parent.Size.Y
|
|
);
|
|
CoordinateVector nebulaSize = Helpers.GetRandomCoordinatesFromLY(
|
|
1, 100,
|
|
1, 100
|
|
);
|
|
|
|
Nebula nebula = new Nebula(name, nebulaPosition, nebulaSize, parent);
|
|
|
|
decimal nebulaAreaLY = Helpers.GetCoordinateInLY(nebulaSize.X) * Helpers.GetCoordinateInLY(nebulaSize.Y);
|
|
long starCount = Helpers.GetRandomNumber(1, (long)(Math.Sqrt((double)nebulaAreaLY) * 2));
|
|
|
|
for (long i = 0; i < starCount; i++)
|
|
{
|
|
Star star = GenerateStar(name + ", Star " + i, nebula);
|
|
nebula.GameObjects.Add(star);
|
|
}
|
|
|
|
return nebula;
|
|
}
|
|
|
|
public Star GenerateStar(string name, GameObject parent)
|
|
{
|
|
CoordinateVector starPosition = Helpers.GetRandomCoordinates(
|
|
parent.Position.X, parent.Position.X + parent.Size.X,
|
|
parent.Position.Y, parent.Position.Y + parent.Size.Y
|
|
);
|
|
|
|
Star.SpectralClassType starSpectralClass = (Star.SpectralClassType)(this.RandomGenerator.Next() % 7);
|
|
long starDiameter = Helpers.GetRandomNumber(1000000, 100000000000);
|
|
|
|
Star star = new Star(name, starPosition, parent, starSpectralClass, starDiameter);
|
|
|
|
long planetCount = Helpers.GetRandomNumber(0, this.GeneratorSettings.MaxPlanetsPerStar);
|
|
|
|
for (long i = 0; i < planetCount; i++)
|
|
{
|
|
Planet planet = GeneratePlanet(name + " - " + i, star);
|
|
star.GameObjects.Add(planet);
|
|
}
|
|
|
|
return star;
|
|
}
|
|
|
|
public Planet GeneratePlanet(string name, GameObject parent)
|
|
{
|
|
decimal offset = Helpers.GetCoordinateFromAU(10);
|
|
CoordinateVector planetPosition = Helpers.GetRandomCoordinates(
|
|
parent.Position.X - offset, parent.Position.X + offset,
|
|
parent.Position.Y - offset, parent.Position.Y + offset
|
|
);
|
|
|
|
long planetDiameter = Helpers.GetRandomNumber(1000, 10000);
|
|
|
|
Planet planet = new Planet(name, planetPosition, parent, false, planetDiameter);
|
|
|
|
long moonCount = Helpers.GetRandomNumber(0, this.GeneratorSettings.MaxMoonsPerPlanet);
|
|
|
|
for (long i = 0; i < moonCount; i++)
|
|
{
|
|
Planet moon = GenerateMoon(name + " (" + i + ")", planet, planetDiameter);
|
|
planet.OrbitingGameObjects.Add(moon);
|
|
}
|
|
|
|
return planet;
|
|
}
|
|
|
|
public Planet GenerateMoon(string name, GameObject parent, long parentDiameter)
|
|
{
|
|
decimal offset = Helpers.GetCoordinateFromKM(1000);
|
|
CoordinateVector moonPosition = Helpers.GetRandomCoordinates(
|
|
parent.Position.X - offset, parent.Position.X + offset,
|
|
parent.Position.Y - offset, parent.Position.Y + offset
|
|
);
|
|
|
|
long moonDiameter = Helpers.GetRandomNumber(100, (parentDiameter / 2));
|
|
|
|
Planet planet = new Planet(name, moonPosition, parent, true, moonDiameter);
|
|
|
|
return planet;
|
|
}
|
|
|
|
public void GenerateLife(Galaxy galaxy)
|
|
{
|
|
|
|
}
|
|
|
|
public Ship GeneratePlayer(GameObject parent)
|
|
{
|
|
decimal positionX = Helpers.GetRandomCoordinate(parent.Position.X, parent.Size.X, 1);
|
|
decimal positionY = Helpers.GetRandomCoordinate(parent.Position.Y, parent.Size.Y, 1);
|
|
|
|
Ship playerShip = new Ship(
|
|
"Player",
|
|
new CoordinateVector(positionX, positionY),
|
|
parent,
|
|
20,
|
|
true
|
|
);
|
|
|
|
return playerShip;
|
|
}
|
|
}
|