예제 #1
0
        /// <summary>
        /// Gets the closest gravity point out of all the ones stored in the GravityPoints array
        /// </summary>
        /// <returns>The closest gravity point.</returns>
        protected virtual GravityPoint GetClosestGravityPoint()
        {
            if (_gravityPoints.Count == 0)
            {
                return(null);
            }

            GravityPoint closestGravityPoint = null;
            float        closestDistanceSqr  = Mathf.Infinity;
            Vector3      currentPosition     = _controller.ColliderCenterPosition;

            foreach (GravityPoint gravityPoint in _gravityPoints)
            {
                Vector3 directionToTarget = gravityPoint.transform.position - currentPosition;
                float   dSqrToTarget      = directionToTarget.sqrMagnitude;

                // if we're outside of this point's zone of effect, we do nothing and exit
                if (directionToTarget.magnitude > gravityPoint.DistanceOfEffect)
                {
                    continue;
                }

                if (dSqrToTarget < closestDistanceSqr)
                {
                    closestDistanceSqr  = dSqrToTarget;
                    closestGravityPoint = gravityPoint;
                }
            }

            return(closestGravityPoint);
        }
예제 #2
0
        /// <summary>
        /// Finds the closest gravity point and changes the gravity if needed
        /// </summary>
        protected virtual void ComputeGravityPoints()
        {
            // if we're not affected by gravity points, we do nothing and exit
            if (!SubjectToGravityPoints)
            {
                return;
            }
            // if we're in a gravity zone, we do nothing and exit
            if (_inAGravityZone)
            {
                return;
            }

            // we grab the closest gravity point
            _closestGravityPoint = GetClosestGravityPoint();

            // if it's not null
            if (_closestGravityPoint != null)
            {
                // our new gravity point becomes the closest if we didn't have one already, otherwise we stay on the last gravity point met for now
                _newGravityPoint = (_lastGravityPoint == null) ? _closestGravityPoint : _lastGravityPoint;
                // if we've got a new gravity point
                if ((_lastGravityPoint != _closestGravityPoint) && (_lastGravityPoint != null))
                {
                    // if we haven't entered a new gravity point in a while, we switch to that new gravity point
                    if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                    {
                        _entryTimeStampPoints = Time.time;
                        _newGravityPoint      = _closestGravityPoint;
                        Transition(true, _newGravityPoint.transform.position - _controller.ColliderCenterPosition);
                        StartRotating();
                    }
                }
                // if we didn't have a gravity point last time, we switch to this new one
                if (_lastGravityPoint == null)
                {
                    if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                    {
                        _entryTimeStampPoints = Time.time;
                        _newGravityPoint      = _closestGravityPoint;
                        Transition(true, _newGravityPoint.transform.position - _controller.ColliderCenterPosition);
                        StartRotating();
                    }
                }
                // we override our gravity
                _gravityPointDirection = _newGravityPoint.transform.position - _controller.ColliderCenterPosition;
                float gravityAngle = 180 - MMMaths.AngleBetween(Vector2.up, _gravityPointDirection);
                _gravityOverridden    = true;
                _overrideGravityAngle = gravityAngle;
                _lastGravityPoint     = _newGravityPoint;
            }
            else
            {
                // if we don't have a gravity point in range, our gravity is not overridden
                if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                {
                    if (_lastGravityPoint != null)
                    {
                        Transition(false, _newGravityPoint.transform.position - _controller.ColliderCenterPosition);
                        StartRotating();
                    }
                    _entryTimeStampPoints = Time.time;
                    _gravityOverridden    = false;
                    _lastGravityPoint     = null;
                }
            }
        }