imperfect-space/scripts/Sector.cs

114 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using Godot;
public class Sector
{
public Vector3I Coordinates;
public FastUniqueList<GameObject> GameObjects = new();
public Sector(Vector3I coordinates)
{
Coordinates = coordinates;
}
public void Simulate(double delta, HashSet<GameObject> ignoreObjects = null)
{
GameObjects.ForEach(gameObject =>
{
if (ignoreObjects == null || !ignoreObjects.Contains(gameObject))
{
gameObject.Simulate(delta);
}
});
}
public void NetworkSync(long id)
{
GameObjects.ForEach(gameObject =>
{
gameObject.NetworkWrite(id, false);
});
}
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 bool RemoveObjectById(Guid id)
{
foreach (GameObject gameObject in GameObjects)
{
if (gameObject.UUID.Equals(id))
{
GameObjects.Remove(gameObject);
return true;
}
}
return false;
}
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;
}
public override bool Equals(object obj)
{
Sector sector = (Sector)obj;
return sector.Coordinates == Coordinates;
}
public override int GetHashCode()
{
return Coordinates.GetHashCode();
}
}