refactor: removed noise from class names
This commit is contained in:
94
Shared/Behaviours/Ball.cs
Normal file
94
Shared/Behaviours/Ball.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Network;
|
||||
using Syntriax.Engine.Physics2D;
|
||||
using Syntriax.Engine.Systems.Tween;
|
||||
|
||||
namespace Pong.Behaviours;
|
||||
|
||||
public class Ball : Behaviour2D, IFirstFrameUpdate, IPhysicsUpdate, INetworkEntity,
|
||||
IPacketListenerClient<Ball.BallUpdatePacket>,
|
||||
IPacketListenerClient<Ball.BallResetPacket>
|
||||
{
|
||||
public float Speed { get; set; } = 500f;
|
||||
public float SpeedUpMultiplier { get; set; } = .025f;
|
||||
|
||||
public IRigidBody2D RigidBody { get; private set; } = null!;
|
||||
|
||||
private IPhysicsEngine2D physicsEngine2D = null!;
|
||||
private ITweenManager tweenManager = null!;
|
||||
private INetworkCommunicatorServer? networkServer = null;
|
||||
private ITween? networkTween = null;
|
||||
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
BehaviourController.GetRequiredBehaviour<ICollider2D>().OnCollisionDetected.AddListener(OnCollisionDetected);
|
||||
physicsEngine2D = Universe.FindRequiredBehaviour<IPhysicsEngine2D>();
|
||||
tweenManager = Universe.FindRequiredBehaviour<ITweenManager>();
|
||||
RigidBody = BehaviourController.GetRequiredBehaviour<IRigidBody2D>();
|
||||
networkServer = Universe.FindBehaviour<INetworkCommunicatorServer>();
|
||||
}
|
||||
|
||||
public void LaunchBall(Vector2D launchDirection)
|
||||
{
|
||||
ResetBall();
|
||||
RigidBody.Velocity = launchDirection * Speed;
|
||||
networkServer?.SendToAll(new BallUpdatePacket(this));
|
||||
}
|
||||
|
||||
public void ResetBall()
|
||||
{
|
||||
if (networkTween is not null)
|
||||
tweenManager.CancelTween(networkTween);
|
||||
Transform.Position = Vector2D.Zero;
|
||||
RigidBody.Velocity = Vector2D.Zero;
|
||||
networkServer?.SendToAll(new BallResetPacket());
|
||||
}
|
||||
|
||||
public void PhysicsUpdate(float delta)
|
||||
{
|
||||
if (RigidBody.Velocity.MagnitudeSquared <= 0.01f)
|
||||
return;
|
||||
|
||||
Vector2D direction = RigidBody.Velocity.Normalized;
|
||||
RigidBody.Velocity += direction * delta * SpeedUpMultiplier;
|
||||
}
|
||||
|
||||
private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information)
|
||||
{
|
||||
if (Math.Abs(information.Normal.Dot(Vector2D.Right)) > .25)
|
||||
RigidBody.Velocity = information.Detected.Transform.Position.FromTo(information.Detector.Transform.Position).Normalized * RigidBody.Velocity.Magnitude;
|
||||
else
|
||||
RigidBody.Velocity = RigidBody.Velocity.Reflect(information.Normal);
|
||||
networkServer?.SendToAll(new BallUpdatePacket(this));
|
||||
}
|
||||
|
||||
public Ball() { }
|
||||
public Ball(float speed) => Speed = speed;
|
||||
|
||||
void IPacketListenerClient<BallResetPacket>.OnClientPacketArrived(IConnection sender, BallResetPacket packet) => ResetBall();
|
||||
void IPacketListenerClient<BallUpdatePacket>.OnClientPacketArrived(IConnection sender, BallUpdatePacket packet)
|
||||
{
|
||||
Vector2D localToServerPosition = Transform.Position.FromTo(packet.Position);
|
||||
if (localToServerPosition.MagnitudeSquared < 4f)
|
||||
networkTween = Transform.TweenPositionAdditive(tweenManager, .25f, localToServerPosition);
|
||||
else
|
||||
Transform.Position = packet.Position;
|
||||
|
||||
RigidBody.Velocity = packet.Velocity;
|
||||
physicsEngine2D.StepIndividual(RigidBody, sender.Ping);
|
||||
}
|
||||
|
||||
private class BallResetPacket : INetworkPacket;
|
||||
private class BallUpdatePacket : INetworkPacket
|
||||
{
|
||||
public Vector2D Position { get; set; } = Vector2D.Zero;
|
||||
public Vector2D Velocity { get; set; } = Vector2D.Zero;
|
||||
|
||||
public BallUpdatePacket() { }
|
||||
public BallUpdatePacket(Ball ballBehaviour)
|
||||
{
|
||||
Position = ballBehaviour.Transform.Position;
|
||||
Velocity = ballBehaviour.RigidBody.Velocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user