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,71 +1,157 @@
|
|||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public abstract class GameObject
|
||||
{
|
||||
public Sector CurrentSector { get; private set; }
|
||||
public Vector3 LocalCoordinates;
|
||||
public Vector3Dec GlobalCoordinates;
|
||||
public Sector CurrentSector { get; protected set; }
|
||||
public Vector3 LocalCoordinates { get; protected set; }
|
||||
public Vector3Dec GlobalCoordinates { get; protected set; }
|
||||
|
||||
public GameObject(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = localCoordinates;
|
||||
public Vector3 SectorOffset { get; set; }
|
||||
|
||||
GlobalCoordinates = new
|
||||
(
|
||||
sector.GlobalStartCoordinates.X + (decimal)localCoordinates.X,
|
||||
sector.GlobalStartCoordinates.Y + (decimal)localCoordinates.Y,
|
||||
sector.GlobalStartCoordinates.Z + (decimal)localCoordinates.Z
|
||||
);
|
||||
}
|
||||
private bool reassigning = false;
|
||||
|
||||
public GameObject(Sector sector, double localX, double localY, double localZ)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = new(localX, localY, localZ);
|
||||
public GameObject(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = localCoordinates;
|
||||
|
||||
GlobalCoordinates = new
|
||||
(
|
||||
sector.GlobalStartCoordinates.X + (decimal)localX,
|
||||
sector.GlobalStartCoordinates.Y + (decimal)localY,
|
||||
sector.GlobalStartCoordinates.Z + (decimal)localZ
|
||||
);
|
||||
}
|
||||
GlobalCoordinates = new
|
||||
(
|
||||
sector.GlobalCenterCoordinates.X + (decimal)localCoordinates.X,
|
||||
sector.GlobalCenterCoordinates.Y + (decimal)localCoordinates.Y,
|
||||
sector.GlobalCenterCoordinates.Z + (decimal)localCoordinates.Z
|
||||
);
|
||||
}
|
||||
|
||||
public GameObject(Vector3Dec coordinates)
|
||||
{
|
||||
GlobalCoordinates = coordinates;
|
||||
UpdateSector();
|
||||
}
|
||||
public GameObject(Sector sector, double localX, double localY, double localZ)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = new(localX, localY, localZ);
|
||||
|
||||
public GameObject(decimal x, decimal y, decimal z)
|
||||
{
|
||||
GlobalCoordinates = new(x, y, z);
|
||||
UpdateSector();
|
||||
}
|
||||
GlobalCoordinates = new
|
||||
(
|
||||
sector.GlobalStartCoordinates.X + (decimal)localX,
|
||||
sector.GlobalStartCoordinates.Y + (decimal)localY,
|
||||
sector.GlobalStartCoordinates.Z + (decimal)localZ
|
||||
);
|
||||
}
|
||||
|
||||
public Vector3 GetSectorCoordinates()
|
||||
{
|
||||
Vector3Dec relativeSectorCoordinates = GlobalCoordinates - CurrentSector.GlobalStartCoordinates;
|
||||
public GameObject(Vector3Dec coordinates)
|
||||
{
|
||||
GlobalCoordinates = coordinates;
|
||||
UpdateSector();
|
||||
}
|
||||
|
||||
return relativeSectorCoordinates.ToVector3();
|
||||
}
|
||||
public GameObject(decimal x, decimal y, decimal z)
|
||||
{
|
||||
GlobalCoordinates = new(x, y, z);
|
||||
UpdateSector();
|
||||
}
|
||||
|
||||
public bool IsInSector()
|
||||
{
|
||||
return Helpers.IsInsideArea(CurrentSector.Size, GetSectorCoordinates());
|
||||
}
|
||||
public bool IsInCurrentSector()
|
||||
{
|
||||
return Helpers.IsInsideArea(CurrentSector.Size / 2, LocalCoordinates);
|
||||
}
|
||||
|
||||
public void UpdateSector()
|
||||
{
|
||||
public void UpdateSector()
|
||||
{
|
||||
List<Sector> neighbours = CurrentSector.GetNeighbouringSectors();
|
||||
foreach (Sector sector in neighbours)
|
||||
{
|
||||
if (sector.IsObjectInSector(this))
|
||||
{
|
||||
sector.AssignObject(this);
|
||||
reassigning = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
foreach (Sector sector in GameManager.Singleton.GameUniverse.Sectors)
|
||||
{
|
||||
if (sector.IsObjectInSector(this))
|
||||
{
|
||||
sector.AssignObject(this);
|
||||
reassigning = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Simulate(double delta)
|
||||
{
|
||||
if (!IsInSector())
|
||||
{
|
||||
UpdateSector();
|
||||
}
|
||||
}
|
||||
public void AssignSector(Sector sector)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
ResetLocalCoordinates();
|
||||
|
||||
SectorOffset = GetSectorOffset(GameManager.Singleton.GetCurrentSector());
|
||||
|
||||
if (this is Character)
|
||||
{
|
||||
GameManager.Singleton.ApplyOrigin();
|
||||
}
|
||||
|
||||
if (this is not Character)
|
||||
{
|
||||
List<Sector> neighbours = GameManager.Singleton.GetCurrentSector().GetNeighbouringSectors();
|
||||
|
||||
if (neighbours.Contains(sector))
|
||||
{
|
||||
GameManager.Singleton.Spawn(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameManager.Singleton.Despawn(this);
|
||||
}
|
||||
}
|
||||
|
||||
reassigning = false;
|
||||
}
|
||||
|
||||
public Vector3 GetSectorOffset(Sector sector)
|
||||
{
|
||||
Vector3Dec relative = CurrentSector.GlobalCenterCoordinates - sector.GlobalCenterCoordinates;
|
||||
return relative.ToVector3();
|
||||
}
|
||||
|
||||
public virtual void UpdateSectorOffset(Sector sector)
|
||||
{
|
||||
SectorOffset = GetSectorOffset(sector);
|
||||
}
|
||||
|
||||
public void SetCoordinatesFromGlobal(Vector3Dec globalCoordinates)
|
||||
{
|
||||
GlobalCoordinates = globalCoordinates;
|
||||
LocalCoordinates = (globalCoordinates - CurrentSector.GlobalCenterCoordinates).ToVector3();
|
||||
|
||||
UpdateNodePosition();
|
||||
}
|
||||
|
||||
public void SetCoordinatesFromLocal(Vector3 localCoordinates)
|
||||
{
|
||||
LocalCoordinates = localCoordinates;
|
||||
GlobalCoordinates = Vector3Dec.FromVector3(localCoordinates) + CurrentSector.GlobalCenterCoordinates;
|
||||
|
||||
UpdateNodePosition();
|
||||
}
|
||||
|
||||
public void ResetLocalCoordinates()
|
||||
{
|
||||
SetCoordinatesFromGlobal(GlobalCoordinates);
|
||||
}
|
||||
|
||||
public virtual void UpdateNodePosition() { }
|
||||
|
||||
public virtual void Simulate(double delta)
|
||||
{
|
||||
if (!reassigning && !IsInCurrentSector())
|
||||
{
|
||||
UpdateSector();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Node3D Instantiate(Sector sector)
|
||||
{
|
||||
PackedScene modulePrefab = ResourceLoader.Load<PackedScene>("res://prefabs/gameObjects/node.tscn");
|
||||
return modulePrefab.Instantiate<Node3D>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue