49 lines
2.0 KiB
C#
49 lines
2.0 KiB
C#
using System.Numerics;
|
|
using Encompass;
|
|
using PongFE.Components;
|
|
using PongFE.Extensions;
|
|
using PongFE.Messages;
|
|
|
|
namespace PongFE.Engines
|
|
{
|
|
[Reads(
|
|
typeof(PositionComponent),
|
|
typeof(VelocityComponent),
|
|
typeof(ScaleComponent),
|
|
typeof(BounceResponseComponent)
|
|
)]
|
|
[Receives(typeof(AngledBounceMessage))]
|
|
[Sends(typeof(UpdateVelocityMessage))]
|
|
public class AngledBounceEngine : Engine
|
|
{
|
|
public override void Update(double dt)
|
|
{
|
|
foreach (ref readonly var message in ReadMessages<AngledBounceMessage>())
|
|
{
|
|
if (HasComponent<BounceResponseComponent>(message.Bounced))
|
|
{
|
|
ref readonly var bouncedPositionComponent = ref GetComponent<PositionComponent>(message.Bounced);
|
|
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(message.Bounced);
|
|
ref readonly var bouncedScaleComponent = ref GetComponent<ScaleComponent>(message.Bounced);
|
|
|
|
ref readonly var bouncerPositionComponent = ref GetComponent<PositionComponent>(message.Bouncer);
|
|
ref readonly var bouncerScaleComponent = ref GetComponent<ScaleComponent>(message.Bouncer);
|
|
|
|
var bouncedY = bouncedPositionComponent.Position.Y + bouncedScaleComponent.Height / 2;
|
|
var bouncerY = bouncerPositionComponent.Position.Y + bouncerScaleComponent.Height / 2;
|
|
|
|
var speed = velocityComponent.Velocity.Length();
|
|
var horizontal = velocityComponent.Velocity.X < 0 ? 1 : -1;
|
|
var diff = bouncedY - bouncerY;
|
|
var scale = (float)diff / (bouncerScaleComponent.Height / 2);
|
|
var rotation = scale * System.Math.PI / 4;
|
|
|
|
Vector2 newVelocity = new Vector2(speed, 0).Rotate((float)rotation) * new Vector2(horizontal, 1);
|
|
|
|
SendMessage(new UpdateVelocityMessage(message.Bounced, newVelocity));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|