Added dynamic spawning and despawning of objects
This commit is contained in:
parent
adbb208436
commit
e0cf8d9755
23 changed files with 794 additions and 350 deletions
|
|
@ -1,13 +1,12 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
public interface IGenerator
|
||||
{
|
||||
public Vector3I GetUniverseSize();
|
||||
public Vector3 GetSectorSizeEachDirection();
|
||||
public Vector3 GetSectorSize();
|
||||
|
||||
public Task GenerateUniverse(Action<Universe> callback);
|
||||
public Universe GenerateUniverse();
|
||||
public Sector GenerateSector(Vector3I coordinates);
|
||||
|
||||
public Star GenerateStar(Sector sector, Vector3 localCoordinates);
|
||||
|
|
|
|||
|
|
@ -1,134 +1,169 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
public class TestGenerator : IGenerator
|
||||
{
|
||||
public static Vector3I UNIVERSE_SIZE = new(3, 3, 3);
|
||||
public static Vector3 SECTOR_SIZE_EACH_DIRECTION = new(1000, 1000, 1000);
|
||||
public static int STARS_PER_SECTOR = 5;
|
||||
public static int STARS_PER_SECTOR_VARIANCE = 2;
|
||||
public static int SHIPS_PER_SECTOR = 0;
|
||||
public static int SHIPS_PER_SECTOR_VARIANCE = 0;
|
||||
public static int STATIONS_PER_SECTOR = 0;
|
||||
public static int STATIONS_PER_SECTOR_VARIANCE = 0;
|
||||
public static Vector3I UNIVERSE_SIZE = new(100, 100, 100);
|
||||
public static Vector3 SECTOR_SIZE = new(5000, 5000, 5000);
|
||||
public static int STARS_PER_SECTOR = 100;
|
||||
public static int STARS_PER_SECTOR_VARIANCE = 5;
|
||||
public static int SHIPS_PER_SECTOR = 0;
|
||||
public static int SHIPS_PER_SECTOR_VARIANCE = 0;
|
||||
public static int STATIONS_PER_SECTOR = 0;
|
||||
public static int STATIONS_PER_SECTOR_VARIANCE = 0;
|
||||
|
||||
private RandomNumberGenerator rng;
|
||||
private readonly int threads = 16;
|
||||
|
||||
public Vector3I GetUniverseSize()
|
||||
{
|
||||
return UNIVERSE_SIZE;
|
||||
}
|
||||
private RandomNumberGenerator rng;
|
||||
|
||||
public Vector3 GetSectorSizeEachDirection()
|
||||
{
|
||||
return SECTOR_SIZE_EACH_DIRECTION;
|
||||
}
|
||||
public Vector3I GetUniverseSize()
|
||||
{
|
||||
return UNIVERSE_SIZE;
|
||||
}
|
||||
|
||||
public async Task GenerateUniverse(Action<Universe> callback)
|
||||
{
|
||||
rng = new();
|
||||
public Vector3I GetSectorOffset()
|
||||
{
|
||||
return new(
|
||||
(int)Mathf.Floor(UNIVERSE_SIZE.X / 2),
|
||||
(int)Mathf.Floor(UNIVERSE_SIZE.X / 2),
|
||||
(int)Mathf.Floor(UNIVERSE_SIZE.X / 2)
|
||||
);
|
||||
}
|
||||
|
||||
Universe universe = new(UNIVERSE_SIZE);
|
||||
for (int x = 0; x < UNIVERSE_SIZE.X; x++)
|
||||
{
|
||||
for (int y = 0; y < UNIVERSE_SIZE.Y; y++)
|
||||
{
|
||||
for (int z = 0; z < UNIVERSE_SIZE.Z; z++)
|
||||
{
|
||||
universe.Sectors[x, y, z] = GenerateSector(new(x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
public Vector3 GetSectorSize()
|
||||
{
|
||||
return SECTOR_SIZE;
|
||||
}
|
||||
|
||||
callback(universe);
|
||||
}
|
||||
public Universe GenerateUniverse()
|
||||
{
|
||||
rng = new();
|
||||
|
||||
public Sector GenerateSector(Vector3I coordinates)
|
||||
{
|
||||
Sector sector = new(coordinates, SECTOR_SIZE_EACH_DIRECTION);
|
||||
List<Task> tasks = [];
|
||||
|
||||
int starCount = rng.RandiRange(
|
||||
STARS_PER_SECTOR - STARS_PER_SECTOR_VARIANCE,
|
||||
STARS_PER_SECTOR + STARS_PER_SECTOR_VARIANCE
|
||||
);
|
||||
starCount = Mathf.Clamp(starCount, 0, int.MaxValue);
|
||||
Universe universe = new(UNIVERSE_SIZE);
|
||||
int countX = Mathf.Clamp(UNIVERSE_SIZE.X / threads, 1, int.MaxValue);
|
||||
|
||||
for (int i = 0; i < starCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Star star = GenerateStar(sector, localCoordinates);
|
||||
for (int x = 0; x < UNIVERSE_SIZE.X; x += countX)
|
||||
{
|
||||
int taskStartX = x;
|
||||
int taskCountX = countX;
|
||||
|
||||
sector.GameObjects.Add(star);
|
||||
}
|
||||
Task clusteredTask = Task.Run(() =>
|
||||
{
|
||||
GenerateClustered(universe, taskStartX, taskCountX);
|
||||
});
|
||||
|
||||
int shipCount = rng.RandiRange(
|
||||
SHIPS_PER_SECTOR - SHIPS_PER_SECTOR_VARIANCE,
|
||||
SHIPS_PER_SECTOR + SHIPS_PER_SECTOR_VARIANCE
|
||||
);
|
||||
shipCount = Mathf.Clamp(shipCount, 0, int.MaxValue);
|
||||
tasks.Add(clusteredTask);
|
||||
}
|
||||
|
||||
for (int i = 0; i < shipCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vessel ship = GenerateShip(sector, localCoordinates);
|
||||
Task.WaitAll([.. tasks]);
|
||||
|
||||
sector.GameObjects.Add(ship);
|
||||
}
|
||||
return universe;
|
||||
}
|
||||
|
||||
int stationCount = rng.RandiRange(
|
||||
STATIONS_PER_SECTOR - STATIONS_PER_SECTOR_VARIANCE,
|
||||
STATIONS_PER_SECTOR + STATIONS_PER_SECTOR_VARIANCE
|
||||
);
|
||||
stationCount = Mathf.Clamp(stationCount, 0, int.MaxValue);
|
||||
private void GenerateClustered(Universe universe, int startX, int countX)
|
||||
{
|
||||
int endX = Mathf.Clamp(startX + countX, 0, UNIVERSE_SIZE.X);
|
||||
|
||||
for (int i = 0; i < stationCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vessel station = GenerateStation(sector, localCoordinates);
|
||||
for (int x = startX; x < endX; x++)
|
||||
{
|
||||
for (int y = 0; y < UNIVERSE_SIZE.Y; y++)
|
||||
{
|
||||
for (int z = 0; z < UNIVERSE_SIZE.Z; z++)
|
||||
{
|
||||
universe.Sectors[x, y, z] = GenerateSector(new(x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sector.GameObjects.Add(station);
|
||||
}
|
||||
public Sector GenerateSector(Vector3I coordinates)
|
||||
{
|
||||
Sector sector = new(coordinates, GetSectorOffset(), SECTOR_SIZE);
|
||||
|
||||
return sector;
|
||||
}
|
||||
int starCount = rng.RandiRange(
|
||||
STARS_PER_SECTOR - STARS_PER_SECTOR_VARIANCE,
|
||||
STARS_PER_SECTOR + STARS_PER_SECTOR_VARIANCE
|
||||
);
|
||||
starCount = Mathf.Clamp(starCount, 0, int.MaxValue);
|
||||
|
||||
public Star GenerateStar(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
return new Star(sector, localCoordinates);
|
||||
}
|
||||
for (int i = 0; i < starCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Star star = GenerateStar(sector, localCoordinates);
|
||||
|
||||
public Star GenerateStar(Sector sector)
|
||||
{
|
||||
return GenerateStar(sector, GenerateLocalCoordinates());
|
||||
}
|
||||
sector.GameObjects.Add(star);
|
||||
}
|
||||
|
||||
public Vessel GenerateShip(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
return new Vessel(sector, localCoordinates);
|
||||
}
|
||||
int shipCount = rng.RandiRange(
|
||||
SHIPS_PER_SECTOR - SHIPS_PER_SECTOR_VARIANCE,
|
||||
SHIPS_PER_SECTOR + SHIPS_PER_SECTOR_VARIANCE
|
||||
);
|
||||
shipCount = Mathf.Clamp(shipCount, 0, int.MaxValue);
|
||||
|
||||
public Vessel GenerateShip(Sector sector)
|
||||
{
|
||||
return GenerateShip(sector, GenerateLocalCoordinates());
|
||||
}
|
||||
for (int i = 0; i < shipCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vessel ship = GenerateShip(sector, localCoordinates);
|
||||
|
||||
public Vessel GenerateStation(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
return new Vessel(sector, localCoordinates);
|
||||
}
|
||||
sector.GameObjects.Add(ship);
|
||||
}
|
||||
|
||||
public Vessel GenerateStation(Sector sector)
|
||||
{
|
||||
return GenerateStation(sector, GenerateLocalCoordinates());
|
||||
}
|
||||
int stationCount = rng.RandiRange(
|
||||
STATIONS_PER_SECTOR - STATIONS_PER_SECTOR_VARIANCE,
|
||||
STATIONS_PER_SECTOR + STATIONS_PER_SECTOR_VARIANCE
|
||||
);
|
||||
stationCount = Mathf.Clamp(stationCount, 0, int.MaxValue);
|
||||
|
||||
public Vector3 GenerateLocalCoordinates()
|
||||
{
|
||||
double x = rng.Randf() * SECTOR_SIZE_EACH_DIRECTION.X;
|
||||
double y = rng.Randf() * SECTOR_SIZE_EACH_DIRECTION.Y;
|
||||
double z = rng.Randf() * SECTOR_SIZE_EACH_DIRECTION.Z;
|
||||
for (int i = 0; i < stationCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vessel station = GenerateStation(sector, localCoordinates);
|
||||
|
||||
return new(x, y, z);
|
||||
}
|
||||
sector.GameObjects.Add(station);
|
||||
}
|
||||
|
||||
return sector;
|
||||
}
|
||||
|
||||
public Star GenerateStar(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
return new Star(sector, localCoordinates);
|
||||
}
|
||||
|
||||
public Star GenerateStar(Sector sector)
|
||||
{
|
||||
return GenerateStar(sector, GenerateLocalCoordinates());
|
||||
}
|
||||
|
||||
public Vessel GenerateShip(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
return new Vessel(sector, localCoordinates);
|
||||
}
|
||||
|
||||
public Vessel GenerateShip(Sector sector)
|
||||
{
|
||||
return GenerateShip(sector, GenerateLocalCoordinates());
|
||||
}
|
||||
|
||||
public Vessel GenerateStation(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
return new Vessel(sector, localCoordinates);
|
||||
}
|
||||
|
||||
public Vessel GenerateStation(Sector sector)
|
||||
{
|
||||
return GenerateStation(sector, GenerateLocalCoordinates());
|
||||
}
|
||||
|
||||
public Vector3 GenerateLocalCoordinates()
|
||||
{
|
||||
double x = (rng.Randf() - 0.5) * SECTOR_SIZE.X;
|
||||
double y = (rng.Randf() - 0.5) * SECTOR_SIZE.Y;
|
||||
double z = (rng.Randf() - 0.5) * SECTOR_SIZE.Z;
|
||||
|
||||
return new(x, y, z);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue