Exemplo n.º 1
0
        private void UpdateVelocity()
        {
            var acceleration = Vector2.Zero;

            if (Curve.GetPointCount() == 0)
            {
                Decelerate();
            }
            else
            {
                var destinationPoint = Curve.InterpolateBaked(_currentT);
                if (_owner.GlobalPosition.DistanceSquaredTo(destinationPoint) < MAX_AHEAD * MAX_AHEAD)
                {
                    _currentT += MAX_AHEAD_DELTA * GetProcessDeltaTime();
                }

                if (_currentT < (Curve.GetBakedLength()))
                {
                    acceleration = (destinationPoint - _owner.GlobalPosition).Normalized() * _acceleration;
                }
                else
                {
                    if (!_pathEndReached)
                    {
                        _pathEndReached = true;
                        EmitSignal(nameof(PathEndReached));
                    }
                    Decelerate();
                }
            }

            Velocity += acceleration * GetProcessDeltaTime();
            Velocity  = Velocity.Clamped(_maxSpeed);
            Velocity  = _owner.MoveAndSlide(Velocity);
        }