wip: INetworkEntity & INetworkManager

This commit is contained in:
Syntriax 2024-02-09 11:50:16 +03:00
parent 86ef57fb62
commit 6f3e7b4ae5
4 changed files with 67 additions and 7 deletions

View File

@ -0,0 +1,8 @@
namespace Syntriax.Engine.Network.Abstract;
internal interface INetworkEntity
{
int NetworkId { get; set; }
void SetNetworkId(int id);
}

View File

@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Network.Abstract;
public interface INetworkManager
{
Action<IGameObject>? OnNetworkGameObjectInstantiated { get; set; }
INetworkCommunicator Communicator { get; }
Task<T> Instantiate<T>(params object?[]? args) where T : class, IGameObject;
}

View File

@ -3,7 +3,7 @@ using Syntriax.Engine.Network.Abstract;
namespace Syntriax.Engine.Network; namespace Syntriax.Engine.Network;
public class NetworkBehaviour : BehaviourOverride, INetworkBehaviour public class NetworkBehaviour : BehaviourOverride, INetworkBehaviour, INetworkEntity
{ {
public int NetworkId { get; private set; } = 0; public int NetworkId { get; private set; } = 0;
@ -16,18 +16,22 @@ public class NetworkBehaviour : BehaviourOverride, INetworkBehaviour
protected override void OnInitialize() protected override void OnInitialize()
{ {
if ((BehaviourController.GetBehaviourInParent<INetworkCommunicator>() ?? GameObject.GameManager.FindBehaviour<INetworkCommunicator>()) is not NetworkCommunicator) NetworkCommunicator = BehaviourController.GetBehaviourInParent<INetworkCommunicator>()
throw new System.Exception($"Could not find an {nameof(INetworkCommunicator)}."); ?? GameObject.GameManager.FindBehaviour<INetworkCommunicator>()
?? throw new System.Exception($"Could not find an {nameof(INetworkCommunicator)}.");
if (NetworkCommunicator is INetworkServer server) if (NetworkCommunicator is INetworkClient client)
{ {
IsClient = true;
return;
}
IsServer = true; IsServer = true;
LocalAssigned = true; LocalAssigned = true;
// TODO Set and send Network Id // TODO Set and send Network Id
}
else
IsClient = true;
} }
int INetworkEntity.NetworkId { get => NetworkId; set => NetworkId = value; }
void INetworkEntity.SetNetworkId(int id) => NetworkId = id;
} }

View File

@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Network.Abstract;
namespace Syntriax.Engine.Network;
public class NetworkManager : NetworkBehaviour, INetworkManager
{
public Action<IGameObject>? OnNetworkGameObjectInstantiated { get; set; } = null;
public INetworkCommunicator Communicator { get; private set; } = null!;
private BehaviourCollector<INetworkEntity> entities = null!;
protected override void OnInitialize()
{
base.OnInitialize();
entities = new(GameObject.GameManager);
}
public Task<T> Instantiate<T>(params object?[]? args) where T : class, IGameObject
{
return Task.Run(() =>
{
if (IsServer)
return GameObject.GameManager.InstantiateGameObject<T>(args);
// Request Instantiation From Server
int id = 0;
return GameObject.GameManager.InstantiateGameObject<T>(args);
});
}
}