PongFE/PongFE/Engines/BounceEngine.cs

43 lines
1.4 KiB
C#

using System.Numerics;
using Encompass;
using PongFE.Components;
using PongFE.Enums;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(
typeof(BounceResponseComponent),
typeof(VelocityComponent)
)]
[Receives(typeof(BounceMessage))]
[Sends(typeof(UpdateVelocityMessage))]
public class BounceEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var message in ReadMessages<BounceMessage>())
{
if (HasComponent<BounceResponseComponent>(message.Entity) && HasComponent<VelocityComponent>(message.Entity))
{
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(message.Entity);
Vector2 newVelocity;
if (message.HitOrientation == HitOrientation.Horizontal)
{
newVelocity =
new Vector2(-velocityComponent.Velocity.X, velocityComponent.Velocity.Y);
}
else
{
newVelocity =
new Vector2(velocityComponent.Velocity.X, -velocityComponent.Velocity.Y);
}
SendMessage(new UpdateVelocityMessage(message.Entity, newVelocity));
}
}
}
}
}