diff --git a/Game/Network/Abstract/INetworkEntity.cs b/Game/Network/Abstract/INetworkEntity.cs new file mode 100644 index 0000000..05aa4f5 --- /dev/null +++ b/Game/Network/Abstract/INetworkEntity.cs @@ -0,0 +1,8 @@ +namespace Syntriax.Engine.Network.Abstract; + +internal interface INetworkEntity +{ + int NetworkId { get; set; } + + void SetNetworkId(int id); +} diff --git a/Game/Network/Abstract/INetworkManager.cs b/Game/Network/Abstract/INetworkManager.cs new file mode 100644 index 0000000..a5320b2 --- /dev/null +++ b/Game/Network/Abstract/INetworkManager.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using Syntriax.Engine.Core.Abstract; + +namespace Syntriax.Engine.Network.Abstract; + +public interface INetworkManager +{ + Action? OnNetworkGameObjectInstantiated { get; set; } + + INetworkCommunicator Communicator { get; } + + Task Instantiate(params object?[]? args) where T : class, IGameObject; +} diff --git a/Game/Network/NetworkBehaviour.cs b/Game/Network/NetworkBehaviour.cs index 2d97e6b..4ba8658 100644 --- a/Game/Network/NetworkBehaviour.cs +++ b/Game/Network/NetworkBehaviour.cs @@ -3,7 +3,7 @@ using Syntriax.Engine.Network.Abstract; namespace Syntriax.Engine.Network; -public class NetworkBehaviour : BehaviourOverride, INetworkBehaviour +public class NetworkBehaviour : BehaviourOverride, INetworkBehaviour, INetworkEntity { public int NetworkId { get; private set; } = 0; @@ -16,18 +16,22 @@ public class NetworkBehaviour : BehaviourOverride, INetworkBehaviour protected override void OnInitialize() { - if ((BehaviourController.GetBehaviourInParent() ?? GameObject.GameManager.FindBehaviour()) is not NetworkCommunicator) - throw new System.Exception($"Could not find an {nameof(INetworkCommunicator)}."); + NetworkCommunicator = BehaviourController.GetBehaviourInParent() + ?? GameObject.GameManager.FindBehaviour() + ?? 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; LocalAssigned = true; // TODO Set and send Network Id - } - else - IsClient = true; } + int INetworkEntity.NetworkId { get => NetworkId; set => NetworkId = value; } + void INetworkEntity.SetNetworkId(int id) => NetworkId = id; } diff --git a/Game/Network/NetworkManager.cs b/Game/Network/NetworkManager.cs new file mode 100644 index 0000000..e493d2c --- /dev/null +++ b/Game/Network/NetworkManager.cs @@ -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? OnNetworkGameObjectInstantiated { get; set; } = null; + + public INetworkCommunicator Communicator { get; private set; } = null!; + private BehaviourCollector entities = null!; + + protected override void OnInitialize() + { + base.OnInitialize(); + entities = new(GameObject.GameManager); + } + + public Task Instantiate(params object?[]? args) where T : class, IGameObject + { + return Task.Run(() => + { + if (IsServer) + return GameObject.GameManager.InstantiateGameObject(args); + + // Request Instantiation From Server + int id = 0; + return GameObject.GameManager.InstantiateGameObject(args); + }); + } +}