MoveSystem

Anonymous
csharp
07/18/2026 10:22 AM
3.0 KB
29
No Index
using Leopotam.EcsProto;
using Leopotam.EcsProto.QoL;
using UnityEngine;

namespace ShiftedRealms.IsoRift.GameWorld
{
    public sealed class MoveSystem : IProtoInitSystem, IProtoRunSystem
    {
        [DI] private MovementAspect movementAspect;
        [DI] private PhysicsAspect physicsAspect;

        private ProtoIt movableEntities;

        public void Init(IProtoSystems systems)
        {
            movableEntities = new ProtoIt(new[]
            {
                typeof(MovementStats),
                typeof(MoveCommand),
                typeof(Velocity)
            });

            movableEntities.Init(systems.World());
        }

        public void Run()
        {
            float fixedDeltaTime = Time.fixedDeltaTime;

            foreach (ProtoEntity entity in movableEntities)
            {
                ref MoveCommand moveCommand =
                    ref movementAspect.MoveCommandsPool.Get(entity);

                ref Velocity velocity =
                    ref physicsAspect.VelocitiesPool.Get(entity);

                ref MovementStats stats =
                    ref movementAspect.MovementStats.Get(entity);

                float desiredMoveSpeed = moveCommand.HaveMoveCommand
                    ? stats.MaxSpeed * Mathf.Clamp01(moveCommand.Intensity)
                    : 0.0f;

                Vector3 desiredHorizontalVelocity =
                    moveCommand.WorldDirection * desiredMoveSpeed;

                Vector3 currentVelocity = velocity.Value;

                Vector3 currentHorizontalVelocity = new Vector3(
                    currentVelocity.x,
                    0.0f,
                    currentVelocity.z);

                float speedChangeRate = moveCommand.HaveMoveCommand
                    ? stats.Acceleration
                    : stats.Deceleration;

                Vector3 newHorizontalVelocity = Vector3.MoveTowards(
                    currentHorizontalVelocity,
                    desiredHorizontalVelocity,
                    speedChangeRate * fixedDeltaTime);

                currentVelocity.x = newHorizontalVelocity.x;
                currentVelocity.z = newHorizontalVelocity.z;

                velocity.Value = currentVelocity;
            }
        }
    }
}
Editor is loading...
Leave a Comment