Files
brick-framework/GameCore/Movement/RotationSystem.cs

48 lines
1.6 KiB
C#

using GameCore.ECS;
using GameCore.ECS.Interfaces;
using GameCore.Input;
using GameCore.Player;
namespace GameCore.Movement;
public class RotationSystem : ISystem
{
private const float MinPitch = -(float)System.Math.PI / 2f;
private const float MaxPitch = (float)System.Math.PI / 2f;
public void Update(World world, float deltaTime)
{
var entities = world.GetEntitiesWith<InputStateComponent>();
foreach (var entity in entities)
{
var input = world.GetComponent<InputStateComponent>(entity);
var rotation = world.GetComponent<RotationComponent>(entity);
if (input == null || rotation == null)
{
world.Logger.Warn("RotationSystem: Missing InputStateComponent or RotationComponent.");
continue;
}
if (world.GetComponent<PlayerComponent>(entity) != null)
{
rotation.Rotation.Y += input.LookDirection.Y;
rotation.Rotation.X += input.LookDirection.X;
}
else
{
if (input.LookDirection.Length() > 0.001f)
{
var lookDir = input.LookDirection;
rotation.Rotation.Y = (float)System.Math.Atan2(lookDir.X, lookDir.Z);
var horizontalDist = (float)System.Math.Sqrt(lookDir.X * lookDir.X + lookDir.Z * lookDir.Z);
rotation.Rotation.X = -(float)System.Math.Atan2(lookDir.Y, horizontalDist);
}
}
rotation.Rotation.X = System.Math.Clamp(rotation.Rotation.X, MinPitch, MaxPitch);
}
}
}