Beispiel #1
0
 public bool IsValid(Dog state) => _actionDestination.IsTraversable(state) && !_actionDestination.IsReached(state);
Beispiel #2
0
        public override IEnumerator ExecuteAction(float transitionIn, Func <float?> getTransitionOut, Action <float> setTransitionOut)
        {
            // Execute enter
            this.StopCoroutineIfExistsAndReplace(ref _enteringExiting, StartCoroutine(_Enter(transitionIn)));
            // Create out transition
            float?transitionOut;

            // Loop forever
            while (true)
            {
                // Set out transition
                transitionOut = getTransitionOut();
                // Check if out transition exists
                if (transitionOut.HasValue)
                {
                    // Stop loop
                    break;
                }
                // Check if destination not traversable
                if (!_actionDestination.IsTraversable())
                {
                    // No destination
                    break;
                }
                // Get delta time
                var deltaTime = Time.deltaTime;
                // Get position
                var position = new Vector2(Transform.position.x, Transform.position.z);
                // Get destination
                var destination = _actionDestination.GetPosition();
                // Get direction
                var direction = destination - position;
                // Get distance
                var distance = direction.magnitude;
                // Get speed
                var speed = Mathf.Clamp(_speed.Value + _speedAcceleration * deltaTime, _speedMinimum, _speedMaximum);
                // Check if slow on approach
                if (_actionDestination.SlowOnApproach)
                {
                    // Interpolate speed
                    speed = Mathf.Lerp(_speedMinimum, speed, distance / _distanceSlow);
                }
                // Set speed
                _speed.Value = speed;
                // Move position
                position = Vector2.MoveTowards(position, destination, speed * deltaTime);
                // Set position
                Transform.position = new Vector3(position.x, Transform.position.y, position.y);
                // Check if direction exists
                if (!direction.normalized.Equals(_zero))
                {
                    // Set rotation
                    Transform.rotation = Quaternion.LookRotation(new Vector3(direction.x, Transform.position.y, direction.y));
                }
                // Check if at destination or destination reached
                if (position.Equals(destination) || _actionDestination.IsReached())
                {
                    // Destination reached
                    break;
                }
                // Wait a frame
                yield return(null);
            }
            // Execute exit
            this.StopCoroutineIfExistsAndReplace(ref _enteringExiting, StartCoroutine(_Exit(transitionOut ?? 0.1f)));
        }