61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
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<InputStateComponent>();
|
|
foreach (var entity in entities)
|
|
{
|
|
var input = world.GetComponent<InputStateComponent>(entity);
|
|
var velocity = world.GetComponent<VelocityComponent>(entity);
|
|
var attributes = world.GetComponent<AttributeComponent>(entity);
|
|
var rotation = world.GetComponent<RotationComponent>(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;
|
|
}
|
|
} |