Refactor; Optimize simulation
This commit is contained in:
parent
8581cf6fb8
commit
0ef5652cea
16 changed files with 508 additions and 651 deletions
|
|
@ -6,8 +6,6 @@ public partial class RPCNode : Node
|
|||
{
|
||||
public static RPCNode Instance { get; private set; }
|
||||
|
||||
private readonly HashSet<Guid> requestedFullGameObjects = [];
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
|
|
@ -18,6 +16,31 @@ public partial class RPCNode : Node
|
|||
Instance = null;
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.Authority)]
|
||||
public void RpcDespawnGameObject(string uuidData)
|
||||
{
|
||||
GD.Print("DESPAWNING: " + uuidData);
|
||||
|
||||
if (!GameManager.Instance.playerReady)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Guid uuid = Guid.Parse(uuidData);
|
||||
|
||||
List<Sector> sectors = GameManager.Instance.GetCurrentSector().GetNeighbouringSectors();
|
||||
foreach (Sector sector in sectors)
|
||||
{
|
||||
bool removed = sector.RemoveObjectById(uuid);
|
||||
if (removed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
GameManager.Instance.Despawn(uuid);
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.Authority)]
|
||||
public void RpcSyncGameObject(Godot.Collections.Dictionary gameObjectData)
|
||||
{
|
||||
|
|
@ -43,28 +66,22 @@ public partial class RPCNode : Node
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!TrySyncFullGameObject(gameObjectData))
|
||||
{
|
||||
if (requestedFullGameObjects.Contains(uuid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
requestedFullGameObjects.Add(uuid);
|
||||
RpcId(1, nameof(RequestFullGameObject), NetworkManager.Instance.LocalNetId, (string)uuidData);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TrySyncFullGameObject(Godot.Collections.Dictionary gameObjectData)
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.Authority)]
|
||||
public void RpcSpawnGameObject(Godot.Collections.Dictionary gameObjectData)
|
||||
{
|
||||
GD.Print("SPAWNING: " + gameObjectData);
|
||||
|
||||
if (!gameObjectData.TryGetValue("type", out var typeData))
|
||||
return false;
|
||||
return;
|
||||
if (!gameObjectData.TryGetValue("sectorCoordinates", out var sectorCoordinatesData))
|
||||
return false;
|
||||
return;
|
||||
if (!gameObjectData.TryGetValue("localCoordinates", out var localCoordinatesData))
|
||||
return false;
|
||||
return;
|
||||
if (!gameObjectData.TryGetValue("uuid", out var uuidData))
|
||||
return false;
|
||||
return;
|
||||
|
||||
string type = (string)typeData;
|
||||
Vector3I sectorCoordinates = (Vector3I)sectorCoordinatesData;
|
||||
|
|
@ -73,7 +90,7 @@ public partial class RPCNode : Node
|
|||
|
||||
Sector sector = GameManager.GameUniverse.GetSector(sectorCoordinates);
|
||||
if (sector == null)
|
||||
return false;
|
||||
return;
|
||||
|
||||
GameObject gameObject;
|
||||
switch (type)
|
||||
|
|
@ -83,38 +100,15 @@ public partial class RPCNode : Node
|
|||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
gameObject.UUID = uuid;
|
||||
gameObject.NetworkRead(gameObjectData);
|
||||
requestedFullGameObjects.Remove(uuid);
|
||||
|
||||
sector.AssignObject(gameObject);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer)]
|
||||
public void RequestFullGameObject(long id, string uuidString)
|
||||
{
|
||||
if (!Global.IsGameHost)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Guid uuid = Guid.Parse(uuidString);
|
||||
|
||||
List<Sector> sectors = GameManager.Instance.GetPlayer(id).PlayerData.CurrentSector.GetNeighbouringSectors();
|
||||
foreach (Sector sector in sectors)
|
||||
{
|
||||
GameObject gameObject = sector.GetObjectById(uuid);
|
||||
if (gameObject != null)
|
||||
{
|
||||
gameObject.NetworkWrite(id, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
GameManager.Instance.Spawn(gameObject);
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer)]
|
||||
|
|
@ -138,8 +132,7 @@ public partial class RPCNode : Node
|
|||
return;
|
||||
}
|
||||
|
||||
Vector3Dec newGlobal = playerData.CalculateGlobalCoordinates(sector.GlobalCenterCoordinates, position);
|
||||
playerData.SetCoordinatesFromGlobal(newGlobal);
|
||||
playerData.SetCoordinates(position);
|
||||
|
||||
if (playerData.CurrentSector.Coordinates != sectorCoordinates)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue