feat: added NetworkBehaviour for oth client & server communication
This commit is contained in:
40
Engine.Systems/Network/NetworkBehaviour.cs
Normal file
40
Engine.Systems/Network/NetworkBehaviour.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using Engine.Core;
|
||||||
|
|
||||||
|
namespace Engine.Systems.Network;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Basic network behaviour that supports both client & server behaviour. Finds both
|
||||||
|
/// the <see cref="INetworkCommunicatorClient"/> and the <see cref="INetworkCommunicatorServer"/>
|
||||||
|
/// in the universe in it's first active frame. Recommended to use <see cref="ClientBehaviour"/> or <see cref="ServerBehaviour"/>
|
||||||
|
/// <br/>
|
||||||
|
/// Disclaimer: It implements <see cref="IFirstFrameUpdate"/> and <see cref="ILastFrameUpdate"/> in virtual methods.
|
||||||
|
/// </summary>
|
||||||
|
public class NetworkBehaviour : Behaviour, IFirstFrameUpdate, ILastFrameUpdate
|
||||||
|
{
|
||||||
|
protected INetworkCommunicatorServer? server = null!;
|
||||||
|
protected INetworkCommunicatorClient? client = null!;
|
||||||
|
|
||||||
|
protected bool IsServer { get; private set; } = false;
|
||||||
|
protected bool IsClient { get; private set; } = false;
|
||||||
|
|
||||||
|
public INetworkCommunicatorServer Server => server ?? throw new Core.Exceptions.NotFoundException($"Universe does not contain a {nameof(INetworkCommunicatorServer)}");
|
||||||
|
public INetworkCommunicatorClient Client => client ?? throw new Core.Exceptions.NotFoundException($"Universe does not contain a {nameof(INetworkCommunicatorClient)}");
|
||||||
|
|
||||||
|
public virtual void FirstActiveFrame()
|
||||||
|
{
|
||||||
|
client = Universe.FindBehaviour<INetworkCommunicatorClient>();
|
||||||
|
server = Universe.FindBehaviour<INetworkCommunicatorServer>();
|
||||||
|
|
||||||
|
IsServer = server is not null;
|
||||||
|
IsClient = client is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void LastActiveFrame()
|
||||||
|
{
|
||||||
|
client = null!;
|
||||||
|
server = null!;
|
||||||
|
|
||||||
|
IsServer = false;
|
||||||
|
IsClient = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user