refactor: network entity packets
This commit is contained in:
@@ -3,11 +3,12 @@ using System;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Network;
|
||||
using Syntriax.Engine.Systems.Input;
|
||||
|
||||
namespace Pong.Behaviours;
|
||||
|
||||
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : Behaviour2D
|
||||
public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Speed) : Behaviour2D, INetworkEntity
|
||||
{
|
||||
private Keys Up { get; } = Up;
|
||||
private Keys Down { get; } = Down;
|
||||
@@ -19,6 +20,8 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
private bool isDownPressed = false;
|
||||
|
||||
private IButtonInputs<Keys> inputs = null!;
|
||||
private INetworkCommunicatorClient networkClient = null!;
|
||||
private INetworkCommunicatorServer? networkServer = null;
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
@@ -26,9 +29,15 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
return;
|
||||
|
||||
if (isUpPressed)
|
||||
{
|
||||
Transform.Position = Transform.Position + Vector2D.Up * Universe.Time.DeltaTime * Speed;
|
||||
networkClient.SendToServer(new EntityPaddlePositionPacket(this));
|
||||
}
|
||||
else if (isDownPressed)
|
||||
{
|
||||
Transform.Position = Transform.Position + -Vector2D.Up * Universe.Time.DeltaTime * Speed;
|
||||
networkClient.SendToServer(new EntityPaddlePositionPacket(this));
|
||||
}
|
||||
|
||||
Transform.Position = new Vector2D(Transform.Position.X, MathF.Max(MathF.Min(Transform.Position.Y, High), Low));
|
||||
}
|
||||
@@ -36,7 +45,10 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
protected override void OnFirstActiveFrame()
|
||||
{
|
||||
inputs = Universe.FindRequiredBehaviour<IButtonInputs<Keys>>();
|
||||
|
||||
networkClient = Universe.GetRequiredUniverseObject<INetworkCommunicatorClient>();
|
||||
networkServer = Universe.GetRequiredUniverseObject<INetworkCommunicatorServer>();
|
||||
networkClient.SubscribeToPackets<EntityPaddlePositionPacket>(ReceiveDataClient);
|
||||
networkServer.SubscribeToPackets<EntityPaddlePositionPacket>(ReceiveDataServer);
|
||||
inputs.RegisterOnPress(Up, OnUpPressed);
|
||||
inputs.RegisterOnRelease(Up, OnUpReleased);
|
||||
|
||||
@@ -57,4 +69,44 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
||||
private void OnUpReleased(IButtonInputs<Keys> inputs, Keys keys) => isUpPressed = false;
|
||||
private void OnDownPressed(IButtonInputs<Keys> inputs, Keys keys) => isDownPressed = true;
|
||||
private void OnDownReleased(IButtonInputs<Keys> inputs, Keys keys) => isDownPressed = false;
|
||||
|
||||
public void ReceiveDataClient<T>(T data, string _)
|
||||
{
|
||||
if (data is not EntityPaddlePositionPacket position)
|
||||
return;
|
||||
|
||||
Transform.Position = position.Position;
|
||||
}
|
||||
|
||||
public void ReceiveDataServer<T>(T data, string fromClientId)
|
||||
{
|
||||
if (data is not EntityPaddlePositionPacket position)
|
||||
return;
|
||||
|
||||
Transform.Position = position.Position;
|
||||
networkServer?.SendToClient("*", position);
|
||||
}
|
||||
|
||||
public void ReceiveDataClient<T>(T data)
|
||||
{
|
||||
if (data is not EntityPaddlePositionPacket position)
|
||||
return;
|
||||
|
||||
Transform.Position = position.Position;
|
||||
}
|
||||
|
||||
public class EntityPaddlePositionPacket : IEntityNetworkPacket
|
||||
{
|
||||
public string EntityId = default!;
|
||||
public Vector2D Position = default!;
|
||||
|
||||
string IEntityNetworkPacket.EntityId => EntityId;
|
||||
|
||||
public EntityPaddlePositionPacket() { }
|
||||
public EntityPaddlePositionPacket(PaddleBehaviour paddleBehaviour)
|
||||
{
|
||||
EntityId = paddleBehaviour.Id;
|
||||
Position = paddleBehaviour.Transform.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user