113 lines
3.8 KiB
C#
113 lines
3.8 KiB
C#
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, INetworkEntity
|
|
{
|
|
private Keys Up { get; } = Up;
|
|
private Keys Down { get; } = Down;
|
|
public float High { get; } = High;
|
|
public float Low { get; } = Low;
|
|
public float Speed { get; set; } = Speed;
|
|
|
|
private bool isUpPressed = false;
|
|
private bool isDownPressed = false;
|
|
|
|
private IButtonInputs<Keys> inputs = null!;
|
|
private INetworkCommunicatorClient networkClient = null!;
|
|
private INetworkCommunicatorServer? networkServer = null;
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
if (isUpPressed && isDownPressed)
|
|
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));
|
|
}
|
|
|
|
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);
|
|
|
|
inputs.RegisterOnPress(Down, OnDownPressed);
|
|
inputs.RegisterOnRelease(Down, OnDownReleased);
|
|
}
|
|
|
|
protected override void OnFinalize()
|
|
{
|
|
inputs.UnregisterOnPress(Up, OnUpPressed);
|
|
inputs.UnregisterOnRelease(Up, OnUpReleased);
|
|
|
|
inputs.UnregisterOnPress(Down, OnDownPressed);
|
|
inputs.UnregisterOnRelease(Down, OnDownReleased);
|
|
}
|
|
|
|
private void OnUpPressed(IButtonInputs<Keys> inputs, Keys keys) => isUpPressed = true;
|
|
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;
|
|
}
|
|
}
|
|
}
|