Implement basic networking
This commit is contained in:
parent
c320d9ddcb
commit
994823a9c3
25 changed files with 643 additions and 217 deletions
|
|
@ -1,157 +1,160 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public abstract class GameObject
|
||||
{
|
||||
public Sector CurrentSector { get; protected set; }
|
||||
public Vector3 LocalCoordinates { get; protected set; }
|
||||
public Vector3Dec GlobalCoordinates { get; protected set; }
|
||||
public Guid UUID { get; set; }
|
||||
|
||||
public Vector3 SectorOffset { get; set; }
|
||||
public Sector CurrentSector { get; protected set; }
|
||||
public Vector3 LocalCoordinates { get; protected set; }
|
||||
public Vector3Dec GlobalCoordinates { get; protected set; }
|
||||
|
||||
private bool reassigning = false;
|
||||
public Vector3 SectorOffset { get; set; }
|
||||
|
||||
public GameObject(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = localCoordinates;
|
||||
protected bool reassigning = false;
|
||||
|
||||
GlobalCoordinates = new
|
||||
(
|
||||
sector.GlobalCenterCoordinates.X + (decimal)localCoordinates.X,
|
||||
sector.GlobalCenterCoordinates.Y + (decimal)localCoordinates.Y,
|
||||
sector.GlobalCenterCoordinates.Z + (decimal)localCoordinates.Z
|
||||
);
|
||||
}
|
||||
public GameObject(Sector sector, Vector3 localCoordinates)
|
||||
{
|
||||
UUID = Guid.NewGuid();
|
||||
|
||||
public GameObject(Sector sector, double localX, double localY, double localZ)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = new(localX, localY, localZ);
|
||||
CurrentSector = sector;
|
||||
LocalCoordinates = localCoordinates;
|
||||
|
||||
GlobalCoordinates = new
|
||||
(
|
||||
sector.GlobalStartCoordinates.X + (decimal)localX,
|
||||
sector.GlobalStartCoordinates.Y + (decimal)localY,
|
||||
sector.GlobalStartCoordinates.Z + (decimal)localZ
|
||||
);
|
||||
}
|
||||
GlobalCoordinates = CalculateGlobalCoordinates(sector.GlobalCenterCoordinates, localCoordinates);
|
||||
}
|
||||
|
||||
public GameObject(Vector3Dec coordinates)
|
||||
{
|
||||
GlobalCoordinates = coordinates;
|
||||
UpdateSector();
|
||||
}
|
||||
public GameObject(Vector3Dec coordinates)
|
||||
{
|
||||
GlobalCoordinates = coordinates;
|
||||
UpdateSector();
|
||||
}
|
||||
|
||||
public GameObject(decimal x, decimal y, decimal z)
|
||||
{
|
||||
GlobalCoordinates = new(x, y, z);
|
||||
UpdateSector();
|
||||
}
|
||||
public GameObject(decimal x, decimal y, decimal z)
|
||||
{
|
||||
GlobalCoordinates = new(x, y, z);
|
||||
UpdateSector();
|
||||
}
|
||||
|
||||
public bool IsInCurrentSector()
|
||||
{
|
||||
return Helpers.IsInsideArea(CurrentSector.Size / 2, LocalCoordinates);
|
||||
}
|
||||
public Vector3Dec CalculateGlobalCoordinates(Vector3Dec sectorCenter, Vector3 local)
|
||||
{
|
||||
return new
|
||||
(
|
||||
sectorCenter.X + (decimal)local.X,
|
||||
sectorCenter.Y + (decimal)local.Y,
|
||||
sectorCenter.Z + (decimal)local.Z
|
||||
);
|
||||
}
|
||||
|
||||
public void UpdateSector()
|
||||
{
|
||||
List<Sector> neighbours = CurrentSector.GetNeighbouringSectors();
|
||||
foreach (Sector sector in neighbours)
|
||||
{
|
||||
if (sector.IsObjectInSector(this))
|
||||
{
|
||||
sector.AssignObject(this);
|
||||
reassigning = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
public bool IsInCurrentSector()
|
||||
{
|
||||
return Helpers.IsInsideArea(CurrentSector.Size / 2, LocalCoordinates);
|
||||
}
|
||||
|
||||
foreach (Sector sector in GameManager.Singleton.GameUniverse.Sectors)
|
||||
{
|
||||
if (sector.IsObjectInSector(this))
|
||||
{
|
||||
sector.AssignObject(this);
|
||||
reassigning = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void UpdateSector()
|
||||
{
|
||||
List<Sector> neighbours = CurrentSector.GetNeighbouringSectors();
|
||||
foreach (Sector sector in neighbours)
|
||||
{
|
||||
if (sector.IsObjectInSector(this))
|
||||
{
|
||||
reassigning = true;
|
||||
sector.AssignObject(this);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void AssignSector(Sector sector)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
ResetLocalCoordinates();
|
||||
if (!GameManager.GameUniverse.IsInside(CurrentSector.Coordinates, LocalCoordinates))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SectorOffset = GetSectorOffset(GameManager.Singleton.GetCurrentSector());
|
||||
foreach (Sector sector in GameManager.GameUniverse.Sectors)
|
||||
{
|
||||
if (sector.IsObjectInSector(this))
|
||||
{
|
||||
reassigning = true;
|
||||
sector.AssignObject(this);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this is Character)
|
||||
{
|
||||
GameManager.Singleton.ApplyOrigin();
|
||||
}
|
||||
public virtual void AssignSector(Sector sector)
|
||||
{
|
||||
CurrentSector = sector;
|
||||
ResetLocalCoordinates();
|
||||
|
||||
if (this is not Character)
|
||||
{
|
||||
List<Sector> neighbours = GameManager.Singleton.GetCurrentSector().GetNeighbouringSectors();
|
||||
UpdateSectorOffsetToMainPlayer();
|
||||
|
||||
if (neighbours.Contains(sector))
|
||||
{
|
||||
GameManager.Singleton.Spawn(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameManager.Singleton.Despawn(this);
|
||||
}
|
||||
}
|
||||
List<Sector> neighbours = GameManager.Singleton.GetCurrentSector().GetNeighbouringSectors();
|
||||
|
||||
reassigning = false;
|
||||
}
|
||||
if (neighbours.Contains(sector))
|
||||
{
|
||||
GameManager.Singleton.Spawn(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameManager.Singleton.Despawn(this);
|
||||
}
|
||||
|
||||
public Vector3 GetSectorOffset(Sector sector)
|
||||
{
|
||||
Vector3Dec relative = CurrentSector.GlobalCenterCoordinates - sector.GlobalCenterCoordinates;
|
||||
return relative.ToVector3();
|
||||
}
|
||||
reassigning = false;
|
||||
}
|
||||
|
||||
public virtual void UpdateSectorOffset(Sector sector)
|
||||
{
|
||||
SectorOffset = GetSectorOffset(sector);
|
||||
}
|
||||
public Vector3 GetSectorOffset(Sector sector)
|
||||
{
|
||||
Vector3Dec relative = CurrentSector.GlobalCenterCoordinates - sector.GlobalCenterCoordinates;
|
||||
return relative.ToVector3();
|
||||
}
|
||||
|
||||
public void SetCoordinatesFromGlobal(Vector3Dec globalCoordinates)
|
||||
{
|
||||
GlobalCoordinates = globalCoordinates;
|
||||
LocalCoordinates = (globalCoordinates - CurrentSector.GlobalCenterCoordinates).ToVector3();
|
||||
public void UpdateSectorOffset(Sector sector)
|
||||
{
|
||||
SectorOffset = GetSectorOffset(sector);
|
||||
}
|
||||
|
||||
UpdateNodePosition();
|
||||
}
|
||||
public void UpdateSectorOffsetToMainPlayer()
|
||||
{
|
||||
UpdateSectorOffset(GameManager.Singleton.GetCurrentSector());
|
||||
}
|
||||
|
||||
public void SetCoordinatesFromLocal(Vector3 localCoordinates)
|
||||
{
|
||||
LocalCoordinates = localCoordinates;
|
||||
GlobalCoordinates = Vector3Dec.FromVector3(localCoordinates) + CurrentSector.GlobalCenterCoordinates;
|
||||
public void SetCoordinatesFromGlobal(Vector3Dec globalCoordinates)
|
||||
{
|
||||
GlobalCoordinates = globalCoordinates;
|
||||
LocalCoordinates = (globalCoordinates - CurrentSector.GlobalCenterCoordinates).ToVector3();
|
||||
|
||||
UpdateNodePosition();
|
||||
}
|
||||
UpdateNodePosition();
|
||||
}
|
||||
|
||||
public void ResetLocalCoordinates()
|
||||
{
|
||||
SetCoordinatesFromGlobal(GlobalCoordinates);
|
||||
}
|
||||
public void SetCoordinatesFromLocal(Vector3 localCoordinates)
|
||||
{
|
||||
LocalCoordinates = localCoordinates;
|
||||
GlobalCoordinates = Vector3Dec.FromVector3(localCoordinates) + CurrentSector.GlobalCenterCoordinates;
|
||||
|
||||
public virtual void UpdateNodePosition() { }
|
||||
UpdateNodePosition();
|
||||
}
|
||||
|
||||
public virtual void Simulate(double delta)
|
||||
{
|
||||
if (!reassigning && !IsInCurrentSector())
|
||||
{
|
||||
UpdateSector();
|
||||
}
|
||||
}
|
||||
public void ResetLocalCoordinates()
|
||||
{
|
||||
SetCoordinatesFromGlobal(GlobalCoordinates);
|
||||
}
|
||||
|
||||
public virtual Node3D Instantiate(Sector sector)
|
||||
{
|
||||
PackedScene modulePrefab = ResourceLoader.Load<PackedScene>("res://prefabs/gameObjects/node.tscn");
|
||||
return modulePrefab.Instantiate<Node3D>();
|
||||
}
|
||||
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");
|
||||
Node3D instance = modulePrefab.Instantiate<Node3D>();
|
||||
|
||||
instance.Name = $"GameObject-{UUID}";
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue