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,37 +1,95 @@
|
|||
using System.Threading;
|
||||
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, Vector3 size)
|
||||
public Sector(Vector3I coordinates, Vector3I offset, Vector3 size)
|
||||
{
|
||||
Coordinates = coordinates;
|
||||
|
||||
decimal startX = Coordinates.X * (decimal)size.X;
|
||||
decimal startY = Coordinates.Y * (decimal)size.Y;
|
||||
decimal startZ = Coordinates.Z * (decimal)size.Z;
|
||||
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 Sector(int x, int y, int z, Vector3 size) : this(new(x, y, z), size) { }
|
||||
|
||||
public void Simulate(double delta)
|
||||
{
|
||||
//GD.Print(Thread.CurrentThread.ManagedThreadId);
|
||||
|
||||
GameObjects.ForEach(gameObject =>
|
||||
{
|
||||
gameObject.Simulate(delta);
|
||||
});
|
||||
}
|
||||
|
||||
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.Singleton.Spawn);
|
||||
}
|
||||
|
||||
public List<Sector> GetNeighbouringSectors()
|
||||
{
|
||||
List<Sector> neighbours = [];
|
||||
|
||||
Sector[,,] allSectors = GameManager.Singleton.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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue