imperfect-space/scripts/Universe.cs

93 lines
2.1 KiB
C#

using System;
using Godot;
public class Universe
{
public Sector[,,] Sectors;
public Vector3I Size;
public Universe(Vector3I size)
{
Sectors = new Sector[size.X, size.Y, size.Z];
Size = size;
}
public Universe(int sizeX, int sizeY, int sizeZ)
{
Sectors = new Sector[sizeX, sizeY, sizeZ];
Size = new(sizeX, sizeY, sizeZ);
}
public Sector GetSector(Vector3I coordinates)
{
return Sectors[coordinates.X, coordinates.Y, coordinates.Z];
}
public Sector GetSector(int x, int y, int z)
{
return Sectors[x, y, z];
}
public Sector GetNeighbouringSector(Sector sector, bool? x, bool? y, bool? z)
{
Vector3I coordinates = sector.Coordinates;
if (x == true) coordinates.X++;
else if (x == false) coordinates.X--;
if (y == true) coordinates.Y++;
else if (y == false) coordinates.Y--;
if (z == true) coordinates.Z++;
else if (z == false) coordinates.Z--;
coordinates.X = Math.Clamp(coordinates.X, 0, Size.X);
coordinates.Y = Math.Clamp(coordinates.Y, 0, Size.Y);
coordinates.Z = Math.Clamp(coordinates.Z, 0, Size.Z);
return GetSector(coordinates);
}
public bool IsInside(Vector3I sectorCoordinates, Vector3 localCoordinates)
{
Vector3 sectorSize = GameManager.Generator.GetSectorSize() / 2;
if (
sectorCoordinates.X == 0 && localCoordinates.X < -sectorSize.X ||
sectorCoordinates.Y == 0 && localCoordinates.Y < -sectorSize.Y ||
sectorCoordinates.Z == 0 && localCoordinates.Z < -sectorSize.Z
)
{
return false;
}
if (
sectorCoordinates.X == Size.X - 1 && localCoordinates.X >= sectorSize.X ||
sectorCoordinates.Y == Size.Y - 1 && localCoordinates.Y >= sectorSize.Y ||
sectorCoordinates.Z == Size.Z - 1 && localCoordinates.Z >= sectorSize.Z
)
{
return false;
}
return true;
}
public void ForEachSector(Action<Sector> action)
{
int sizeX = Sectors.GetLength(0);
int sizeY = Sectors.GetLength(1);
int sizeZ = Sectors.GetLength(2);
for (int x = 0; x < sizeX; x++)
{
for (int y = 0; y < sizeY; y++)
{
for (int z = 0; z < sizeZ; z++)
{
action(Sectors[x, y, z]);
}
}
}
}
}