Implement RPC Node
This commit is contained in:
parent
f7ee533d5a
commit
605e43273e
13 changed files with 524 additions and 206 deletions
|
|
@ -4,13 +4,107 @@ using Godot;
|
|||
|
||||
public abstract class GameObject
|
||||
{
|
||||
public Guid UUID { get; set; }
|
||||
[Flags]
|
||||
public enum DirtyFlags : ulong
|
||||
{
|
||||
None = 0,
|
||||
UUID = 1UL << 0,
|
||||
Sector = 1UL << 1,
|
||||
LocalCoordinates = 1UL << 2,
|
||||
Rotation = 1UL << 3,
|
||||
Velocity = 1UL << 4,
|
||||
AngularVelocity = 1UL << 5
|
||||
}
|
||||
public DirtyFlags DirtyBits { get; protected set; } = DirtyFlags.None;
|
||||
|
||||
protected Guid _uuid;
|
||||
public Guid UUID
|
||||
{
|
||||
get => _uuid;
|
||||
set
|
||||
{
|
||||
if (_uuid != value)
|
||||
{
|
||||
_uuid = value;
|
||||
DirtyBits |= DirtyFlags.UUID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Vector3I _currentSectorCoordinates;
|
||||
protected Sector _currentSector;
|
||||
public Sector CurrentSector
|
||||
{
|
||||
get => _currentSector;
|
||||
protected set
|
||||
{
|
||||
if (_currentSector != value)
|
||||
{
|
||||
_currentSector = value;
|
||||
_currentSectorCoordinates = value.Coordinates;
|
||||
DirtyBits |= DirtyFlags.Sector;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Vector3 _localCoordinates;
|
||||
public Vector3 LocalCoordinates
|
||||
{
|
||||
get => _localCoordinates;
|
||||
protected set
|
||||
{
|
||||
if (_localCoordinates != value)
|
||||
{
|
||||
_localCoordinates = value;
|
||||
DirtyBits |= DirtyFlags.LocalCoordinates;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Vector3 _rotation;
|
||||
public Vector3 Rotation
|
||||
{
|
||||
get => _rotation;
|
||||
protected set
|
||||
{
|
||||
if (_rotation != value)
|
||||
{
|
||||
_rotation = value;
|
||||
DirtyBits |= DirtyFlags.Rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Vector3 _velocity;
|
||||
public Vector3 Velocity
|
||||
{
|
||||
get => _velocity;
|
||||
protected set
|
||||
{
|
||||
if (_velocity != value)
|
||||
{
|
||||
_velocity = value;
|
||||
DirtyBits |= DirtyFlags.Velocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Vector3 _angularVelocity;
|
||||
public Vector3 AngularVelocity
|
||||
{
|
||||
get => _angularVelocity;
|
||||
protected set
|
||||
{
|
||||
if (_angularVelocity != value)
|
||||
{
|
||||
_angularVelocity = value;
|
||||
DirtyBits |= DirtyFlags.AngularVelocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Sector CurrentSector { get; protected set; }
|
||||
public Vector3 LocalCoordinates { get; protected set; }
|
||||
public Vector3Dec GlobalCoordinates { get; protected set; }
|
||||
|
||||
public Vector3 SectorOffset { get; set; }
|
||||
public Vector3 SectorOffset { get; protected set; }
|
||||
|
||||
protected bool reassigning = false;
|
||||
|
||||
|
|
@ -21,6 +115,10 @@ public abstract class GameObject
|
|||
CurrentSector = sector;
|
||||
LocalCoordinates = localCoordinates;
|
||||
|
||||
Velocity = new(0, 0, 1);
|
||||
AngularVelocity = Vector3.Zero;
|
||||
Rotation = Vector3.Zero;
|
||||
|
||||
GlobalCoordinates = CalculateGlobalCoordinates(sector.GlobalCenterCoordinates, localCoordinates);
|
||||
}
|
||||
|
||||
|
|
@ -46,6 +144,12 @@ public abstract class GameObject
|
|||
);
|
||||
}
|
||||
|
||||
public void ApplyVelocity(double delta)
|
||||
{
|
||||
SetCoordinatesFromLocal(LocalCoordinates + Velocity * delta);
|
||||
Rotation += AngularVelocity * delta;
|
||||
}
|
||||
|
||||
public bool IsInCurrentSector()
|
||||
{
|
||||
return Helpers.IsInsideArea(CurrentSector.Size / 2, LocalCoordinates);
|
||||
|
|
@ -87,15 +191,15 @@ public abstract class GameObject
|
|||
|
||||
UpdateSectorOffsetToMainPlayer();
|
||||
|
||||
List<Sector> neighbours = GameManager.Singleton.GetCurrentSector().GetNeighbouringSectors();
|
||||
List<Sector> neighbours = GameManager.Instance.GetCurrentSector().GetNeighbouringSectors();
|
||||
|
||||
if (neighbours.Contains(sector))
|
||||
{
|
||||
GameManager.Singleton.Spawn(this);
|
||||
GameManager.Instance.Spawn(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameManager.Singleton.Despawn(this);
|
||||
GameManager.Instance.Despawn(this);
|
||||
}
|
||||
|
||||
reassigning = false;
|
||||
|
|
@ -114,7 +218,7 @@ public abstract class GameObject
|
|||
|
||||
public void UpdateSectorOffsetToMainPlayer()
|
||||
{
|
||||
UpdateSectorOffset(GameManager.Singleton.GetCurrentSector());
|
||||
UpdateSectorOffset(GameManager.Instance.GetCurrentSector());
|
||||
}
|
||||
|
||||
public void SetCoordinatesFromGlobal(Vector3Dec globalCoordinates)
|
||||
|
|
@ -142,6 +246,8 @@ public abstract class GameObject
|
|||
|
||||
public virtual void Simulate(double delta)
|
||||
{
|
||||
ApplyVelocity(delta);
|
||||
|
||||
if (!reassigning && !IsInCurrentSector())
|
||||
{
|
||||
UpdateSector();
|
||||
|
|
@ -157,4 +263,70 @@ public abstract class GameObject
|
|||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public virtual Godot.Collections.Dictionary NetworkWrite(long id, bool full)
|
||||
{
|
||||
Godot.Collections.Dictionary gameObjectData = [];
|
||||
|
||||
if (full)
|
||||
gameObjectData.Add("type", GetType().ToString());
|
||||
|
||||
if (DirtyBits.HasFlag(DirtyFlags.Sector) || full)
|
||||
gameObjectData.Add("sectorCoordinates", _currentSectorCoordinates);
|
||||
|
||||
if (DirtyBits.HasFlag(DirtyFlags.LocalCoordinates) || full)
|
||||
gameObjectData.Add("localCoordinates", _localCoordinates);
|
||||
|
||||
if (DirtyBits.HasFlag(DirtyFlags.Rotation) || full)
|
||||
gameObjectData.Add("rotation", _rotation);
|
||||
|
||||
if (DirtyBits.HasFlag(DirtyFlags.Velocity) || full)
|
||||
gameObjectData.Add("velocity", _velocity);
|
||||
|
||||
if (DirtyBits.HasFlag(DirtyFlags.AngularVelocity) || full)
|
||||
gameObjectData.Add("angularVelocity", _angularVelocity);
|
||||
|
||||
if (gameObjectData.Count > 0)
|
||||
{
|
||||
gameObjectData.Add("uuid", UUID.ToString());
|
||||
return gameObjectData;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual void NetworkRead(Godot.Collections.Dictionary gameObjectData)
|
||||
{
|
||||
if (gameObjectData.TryGetValue("sectorCoordinates", out var sectorCoordinatesData))
|
||||
{
|
||||
Sector newSector = GameManager.GameUniverse.GetSector((Vector3I)sectorCoordinatesData);
|
||||
CurrentSector.GameObjects.Remove(this);
|
||||
newSector.GameObjects.Add(this);
|
||||
|
||||
AssignSector(newSector);
|
||||
}
|
||||
|
||||
if (gameObjectData.TryGetValue("localCoordinates", out var localCoordinatesData))
|
||||
LocalCoordinates = (Vector3)localCoordinatesData;
|
||||
|
||||
if (gameObjectData.TryGetValue("rotation", out var rotationData))
|
||||
Rotation = (Vector3)rotationData;
|
||||
|
||||
if (gameObjectData.TryGetValue("velocity", out var velocityData))
|
||||
Velocity = (Vector3)velocityData;
|
||||
|
||||
if (gameObjectData.TryGetValue("angularVelocity", out var angularVelocityData))
|
||||
AngularVelocity = (Vector3)angularVelocityData;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
GameObject gameObj = (GameObject)obj;
|
||||
return gameObj.UUID == UUID;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return UUID.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue