using GameCore.Attributes; using GameCore.ECS; using GameCore.ECS.Interfaces; using GameCore.Input; using GameCore.Math; using GameCore.Physics; using Attribute = GameCore.Attributes.Attribute; namespace GameCore.Movement; public class GroundMovementSystem : ISystem { public void Update(World world, float deltaTime) { var entities = world.GetEntitiesWith(); foreach (var entity in entities) { var input = world.GetComponent(entity); var velocity = world.GetComponent(entity); var attributes = world.GetComponent(entity); var rotation = world.GetComponent(entity); if (input == null || velocity == null || attributes == null || rotation == null) continue; var moveSpeed = attributes.GetValue(Attribute.MoveSpeed); var acceleration = attributes.GetValue(Attribute.Acceleration); var friction = attributes.GetValue(Attribute.Friction); var yaw = rotation.Rotation.Y; var rotatedDir = world.WorldQuery.RotateVectorByYaw(input.MoveDirection, yaw); var targetVelocity = new Vector3( rotatedDir.X * moveSpeed, velocity.DesiredVelocity.Y, rotatedDir.Z * moveSpeed ); if (rotatedDir.Length() >= 0.1f) { velocity.DesiredVelocity = MoveToward(velocity.DesiredVelocity, targetVelocity, acceleration * deltaTime); } else { var frictionVec = new Vector3(velocity.DesiredVelocity.X, 0f, velocity.DesiredVelocity.Z); frictionVec = MoveToward(frictionVec, Vector3.Zero, friction * deltaTime); velocity.DesiredVelocity.X = frictionVec.X; velocity.DesiredVelocity.Z = frictionVec.Z; } } } private Vector3 MoveToward(Vector3 from, Vector3 to, float delta) { var diff = to - from; if (diff.Length() <= delta) return to; return from + diff.Normalize() * delta; } }