コード例 #1
0
ファイル: Vector3UtilitiesTests.cs プロジェクト: bmjoy/Osnowa
        public void LerpThereAndBack_ReturnsCorrectResult(Vector3 first, Vector3 second, float progress, Vector3 expectedResult)
        {
            Vector3 result = Vector3Utilities.LerpThereAndBack(first, second, progress);

            result.Should().Be(expectedResult);
        }
コード例 #2
0
        void Update()
        {
            if (_animationState == EntityAnimationState.Inactive)
            {
                return;
            }
            if (_animationState == EntityAnimationState.Moving)
            {
                _timeSinceStartedAnimation += Time.smoothDeltaTime;                 // deltaTime could be unnaturally big
                // because of calculations done when player performs an action

                Vector3 previousWorldPosition   = _gridInfoProvider.GetCellCenterWorld(_previousLogicalPosition);
                Vector3 animationTargetPosition = _gridInfoProvider.GetCellCenterWorld(_animationTargetPosition);
                Vector3 interpolatedPosition
                    = Vector3.Lerp(previousWorldPosition, animationTargetPosition, _timeSinceStartedAnimation / DefaultAnimationDuration);
                transform.position = interpolatedPosition;
                bool finished = _timeSinceStartedAnimation > DefaultAnimationDuration;
                if (finished)
                {
                    _animationState            = EntityAnimationState.Inactive;
                    _timeSinceStartedAnimation = 0f;
                }
            }
            else if (_animationState == EntityAnimationState.FallingIn)
            {
                _timeSinceStartedAnimation += Time.smoothDeltaTime;                 // deltaTime could be unnaturally big
                // because of calculations done when player performs an action

                Vector3 previousWorldPosition   = _gridInfoProvider.GetCellCenterWorld(_previousLogicalPosition);
                Vector3 animationTargetPosition = _gridInfoProvider.GetCellCenterWorld(_animationTargetPosition);
                float   progress = _timeSinceStartedAnimation / FallInAnimationDuration;
                Vector3 interpolatedPosition
                    = Vector3.Lerp(previousWorldPosition, animationTargetPosition, progress);
                Vector3    interpolatedScale    = Vector3.Lerp(new Vector3(1, 1, 1), new Vector3(0.5f, 0.5f, 0.5f), progress);
                Quaternion interpolatedRotation = Quaternion.Lerp(_animationStartRotation, Quaternion.Euler(0, 0, 180), progress);
                transform.position   = interpolatedPosition;
                transform.localScale = interpolatedScale;
                transform.rotation   = interpolatedRotation;
                bool finished = progress >= 1;
                if (finished)
                {
                    transform.parent           = _entityToBecomeParent.transform;
                    _animationState            = EntityAnimationState.Inactive;
                    _timeSinceStartedAnimation = 0f;
                    _entityToBecomeParent      = null;
                }
            }
            else if (_animationState == EntityAnimationState.FallingOut)
            {
                _timeSinceStartedAnimation += Time.smoothDeltaTime;                 // deltaTime could be unnaturally big
                // because of calculations done when player performs an action

                Vector3 previousWorldPosition   = _gridInfoProvider.GetCellCenterWorld(_previousLogicalPosition);
                Vector3 animationTargetPosition = _gridInfoProvider.GetCellCenterWorld(_animationTargetPosition);
                float   progress = _timeSinceStartedAnimation / FallInAnimationDuration;
                Vector3 interpolatedPosition
                    = Vector3.Lerp(previousWorldPosition, animationTargetPosition, progress);
                Vector3    interpolatedScale    = Vector3.Lerp(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(1, 1, 1), progress);
                Quaternion interpolatedRotation = Quaternion.Lerp(_animationStartRotation, Quaternion.identity, progress);
                transform.position   = interpolatedPosition;
                transform.localScale = interpolatedScale;
                transform.rotation   = interpolatedRotation;
                bool finished = progress >= 1;
                if (finished)
                {
                    _animationState = EntityAnimationState.Inactive;
                    GetComponent <SpriteRenderer>().sortingOrder -= 1;
                    _timeSinceStartedAnimation = 0f;
                    _entityToBecomeParent      = null;
                }
            }
            else if (_animationState == EntityAnimationState.Bumping)
            {
                _timeSinceStartedAnimation += Time.smoothDeltaTime;
                Vector3 previousWorldPosition = _gridInfoProvider.GetCellCenterWorld(_previousLogicalPosition);
                Vector3 middleWayToTarget     = (previousWorldPosition + _gridInfoProvider.GetCellCenterWorld(_animationAffectedPosition)) / 2;
                Vector3 interpolatedPosition
                    = Vector3Utilities.LerpThereAndBack(previousWorldPosition, middleWayToTarget, _timeSinceStartedAnimation / DefaultAnimationDuration);
                transform.position = interpolatedPosition;
                bool finished = _timeSinceStartedAnimation > DefaultAnimationDuration;
                if (finished)
                {
                    _animationState            = EntityAnimationState.Inactive;
                    _timeSinceStartedAnimation = 0f;
                }
            }
            else if (_animationState == EntityAnimationState.BeingKnockedOut)
            {
                _timeSinceStartedAnimation += Time.smoothDeltaTime;                 // deltaTime could be unnaturally big
                // because of calculations done when player performs an action
                float      progress             = _timeSinceStartedAnimation / DefaultAnimationDuration;
                Quaternion interpolatedRotation = Quaternion.Lerp(_animationStartRotation, Quaternion.Euler(0, 0, 180), progress);
                transform.rotation = interpolatedRotation;
                bool finished = progress >= 1;
                if (finished)
                {
                    _animationState            = EntityAnimationState.Inactive;
                    _timeSinceStartedAnimation = 0f;
                }
            }
            else if (_animationState == EntityAnimationState.StandingUp)
            {
                _timeSinceStartedAnimation += Time.smoothDeltaTime;                 // deltaTime could be unnaturally big
                // because of calculations done when player performs an action
                float      progress             = _timeSinceStartedAnimation / DefaultAnimationDuration;
                Quaternion interpolatedRotation = Quaternion.Lerp(_animationStartRotation, Quaternion.identity, progress);
                transform.rotation = interpolatedRotation;
                bool finished = progress >= 1;
                if (finished)
                {
                    _animationState            = EntityAnimationState.Inactive;
                    _timeSinceStartedAnimation = 0f;
                }
            }
        }