imperfect-space/scripts/Sector.cs
2026-02-02 12:17:04 -05:00

117 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using Godot;
public class Sector
{
public Vector3I Coordinates;
public Vector3Dec GlobalStartCoordinates;
public Vector3Dec GlobalCenterCoordinates;
public Vector3Dec GlobalEndCoordinates;
public Vector3 Size;
public FastUniqueList<GameObject> GameObjects = new();
public Sector(Vector3I coordinates, Vector3I offset, Vector3 size)
{
Coordinates = coordinates;
Vector3Dec sizeDec = Vector3Dec.FromVector3(size);
decimal startX = (coordinates.X - offset.X) * sizeDec.X;
decimal startY = (coordinates.Y - offset.Y) * sizeDec.Y;
decimal startZ = (coordinates.Z - offset.Z) * sizeDec.Z;
GlobalStartCoordinates = new(startX, startY, startZ);
GlobalCenterCoordinates = new(
startX + sizeDec.X / 2,
startY + sizeDec.Y / 2,
startZ + sizeDec.Z / 2
);
GlobalEndCoordinates = new(
startX + sizeDec.X,
startY + sizeDec.Y,
startZ + sizeDec.Z
);
Size = size;
}
public void Simulate(double delta)
{
GameObjects.ForEach(gameObject =>
{
gameObject.Simulate(delta);
});
}
public void NetworkSync(long id)
{
GameObjects.ForEach(gameObject =>
{
gameObject.NetworkWrite(id, false);
});
}
public bool IsObjectInSector(GameObject gameObject)
{
return Helpers.IsInsideGlobalArea(GlobalStartCoordinates, GlobalEndCoordinates, gameObject.GlobalCoordinates);
}
public void AssignObject(GameObject gameObject)
{
QueueManager.SectorReassignQueue.Enqueue((this, gameObject));
}
public void SpawnObjects()
{
GameObjects.ForEach(GameManager.Instance.Spawn);
}
public GameObject GetObjectById(Guid id)
{
foreach (GameObject gameObject in GameObjects)
{
if (gameObject.UUID.Equals(id))
{
return gameObject;
}
}
return null;
}
public List<Sector> GetNeighbouringSectors()
{
List<Sector> neighbours = [];
Sector[,,] allSectors = GameManager.GameUniverse.Sectors;
int sizeX = allSectors.GetLength(0);
int sizeY = allSectors.GetLength(1);
int sizeZ = allSectors.GetLength(2);
int startX = Mathf.Clamp(Coordinates.X - 1, 0, sizeX - 1);
int startY = Mathf.Clamp(Coordinates.Y - 1, 0, sizeY - 1);
int startZ = Mathf.Clamp(Coordinates.Z - 1, 0, sizeZ - 1);
int endX = Mathf.Clamp(Coordinates.X + 1, 0, sizeX - 1);
int endY = Mathf.Clamp(Coordinates.Y + 1, 0, sizeY - 1);
int endZ = Mathf.Clamp(Coordinates.Z + 1, 0, sizeZ - 1);
for (int x = startX; x <= endX; x++)
{
for (int y = startY; y <= endY; y++)
{
for (int z = startZ; z <= endZ; z++)
{
neighbours.Add(allSectors[x, y, z]);
}
}
}
return neighbours;
}
}