42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
using System;
|
|
using System.Numerics;
|
|
using MonoGameBlank2dStartKit.Core.Utils;
|
|
using MoonTools.ECS;
|
|
|
|
namespace MonoGameBlank2dStartKit.Core.Systems;
|
|
|
|
public class RotationSystem : MoonTools.ECS.System
|
|
{
|
|
private readonly Filter _filter;
|
|
|
|
public RotationSystem(World world) : base(world)
|
|
{
|
|
// Doesn't filter on Rotation, because it *should* blow up if RSpeed exists without Rotation.
|
|
_filter = FilterBuilder.Include<RotationSpeed>().Build();
|
|
}
|
|
|
|
public override void Update(TimeSpan delta)
|
|
{
|
|
foreach (var entity in _filter.Entities)
|
|
{
|
|
var currentRotation = World.Get<Rotation>(entity);
|
|
var rotationSpeed = World.Get<RotationSpeed>(entity);
|
|
float change = rotationSpeed.Speed * delta.SecFloat();
|
|
World.Set(entity, currentRotation with { Value = currentRotation.Value + change });
|
|
|
|
var newSpeed = rotationSpeed.Speed - (rotationSpeed.Drag * delta.SecFloat());
|
|
if (Math.Sign(newSpeed) != Math.Sign(rotationSpeed.Speed))
|
|
{
|
|
World.Remove<RotationSpeed>(entity);
|
|
}
|
|
else
|
|
{
|
|
World.Set(entity, rotationSpeed with
|
|
{
|
|
Speed = newSpeed,
|
|
Amount = rotationSpeed.Amount - change,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
} |