Exemple #1
0
        /// <summary>
        /// Computes the next Vector3 value for the specified property
        /// </summary>
        /// <param name="valueActive"></param>
        /// <param name="properties"></param>
        /// <param name="internalProperties"></param>
        /// <returns></returns>
        protected virtual bool UpdateValue(bool valueActive, WiggleProperties properties, ref InternalWiggleProperties internalProperties)
        {
            if (!valueActive)
            {
                return(false);
            }
            if (!properties.WigglePermitted)
            {
                return(false);
            }

            // handle limited time
            if ((properties.LimitedTime) && (properties.LimitedTimeTotal > 0f))
            {
                float timeSave = properties.LimitedTimeLeft;
                properties.LimitedTimeLeft -= properties.GetDeltaTime();
                if (properties.LimitedTimeLeft <= 0)
                {
                    if (timeSave > 0f)
                    {
                        if (properties.LimitedTimeResetValue)
                        {
                            internalProperties.returnVector = internalProperties.limitedTimeValueSave;
                            properties.LimitedTimeLeft      = 0;
                            properties.WigglePermitted      = false;
                            return(true);
                        }
                    }
                    return(false);
                }
            }

            switch (properties.WiggleType)
            {
            case WiggleTypes.PingPong:
                return(MoveVector3TowardsTarget(ref internalProperties.returnVector, properties, ref internalProperties.startValue, internalProperties.initialValue,
                                                ref internalProperties.newValue, ref internalProperties.timeSinceLastPause,
                                                ref internalProperties.timeSinceLastChange, ref internalProperties.randomAmplitude,
                                                ref internalProperties.randomFrequency,
                                                ref internalProperties.pauseDuration, internalProperties.randomFrequency));


            case WiggleTypes.Random:
                return(MoveVector3TowardsTarget(ref internalProperties.returnVector, properties, ref internalProperties.startValue, internalProperties.initialValue,
                                                ref internalProperties.newValue, ref internalProperties.timeSinceLastPause,
                                                ref internalProperties.timeSinceLastChange, ref internalProperties.randomAmplitude,
                                                ref internalProperties.randomFrequency,
                                                ref internalProperties.pauseDuration, internalProperties.randomFrequency));

            case WiggleTypes.Noise:
                internalProperties.returnVector = AnimateNoiseValue(ref internalProperties, properties);
                return(true);

            case WiggleTypes.Curve:
                internalProperties.returnVector = AnimateCurveValue(ref internalProperties, properties);
                return(true);
            }
            return(false);
        }
Exemple #2
0
 protected virtual void WiggleValue(ref WiggleProperties property, ref InternalWiggleProperties internalProperties, float duration)
 {
     InitializeRandomValues(ref property, ref internalProperties);
     internalProperties.limitedTimeValueSave = internalProperties.initialValue;
     property.LimitedTime      = true;
     property.LimitedTimeLeft  = duration;
     property.LimitedTimeTotal = duration;
     property.WigglePermitted  = true;
 }
Exemple #3
0
 /// <summary>
 /// Applies a falloff to the computed value based on time spent and a falloff animation curve
 /// </summary>
 /// <param name="newValue"></param>
 /// <param name="properties"></param>
 /// <returns></returns>
 protected Vector3 ApplyFalloff(Vector3 newValue, WiggleProperties properties)
 {
     if ((properties.LimitedTime) && (properties.LimitedTimeTotal > 0f))
     {
         float curveProgress = (properties.LimitedTimeTotal - properties.LimitedTimeLeft) / properties.LimitedTimeTotal;
         float curvePercent  = properties.LimitedTimeFalloff.Evaluate(curveProgress);
         newValue = newValue * curvePercent; //Vector3.LerpUnclamped(startValue, destinationValue, curvePercent);
     }
     return(newValue);
 }
Exemple #4
0
        /// <summary>
        /// Applies a falloff to the computed value based on time spent and a falloff animation curve
        /// </summary>
        /// <param name="newValue"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected float ApplyFalloff(WiggleProperties properties)
        {
            float newValue = 1f;

            if ((properties.LimitedTime) && (properties.LimitedTimeTotal > 0f))
            {
                float curveProgress = (properties.LimitedTimeTotal - properties.LimitedTimeLeft) / properties.LimitedTimeTotal;
                newValue = properties.LimitedTimeFalloff.Evaluate(curveProgress);
            }
            return(newValue);
        }
Exemple #5
0
        /// <summary>
        /// Moves a vector3's values towards a target
        /// </summary>
        /// <param name="movedValue"></param>
        /// <param name="properties"></param>
        /// <param name="startValue"></param>
        /// <param name="initialValue"></param>
        /// <param name="destinationValue"></param>
        /// <param name="timeSinceLastPause"></param>
        /// <param name="timeSinceLastValueChange"></param>
        /// <param name="randomAmplitude"></param>
        /// <param name="randomFrequency"></param>
        /// <param name="pauseDuration"></param>
        /// <param name="frequency"></param>
        /// <returns></returns>
        protected virtual bool MoveVector3TowardsTarget(ref Vector3 movedValue, WiggleProperties properties, ref Vector3 startValue, Vector3 initialValue,
                                                        ref Vector3 destinationValue, ref float timeSinceLastPause, ref float timeSinceLastValueChange,
                                                        ref Vector3 randomAmplitude, ref float randomFrequency,
                                                        ref float pauseDuration, float frequency)
        {
            timeSinceLastPause       += properties.GetDeltaTime();
            timeSinceLastValueChange += properties.GetDeltaTime();

            // handle pause
            if (timeSinceLastPause < pauseDuration)
            {
                return(false);
            }

            // if we're just out of a pause
            if (timeSinceLastPause == timeSinceLastValueChange)
            {
                timeSinceLastValueChange = 0f;
            }

            // if we've reached the end
            if (frequency > 0)
            {
                float curveProgress = (timeSinceLastValueChange) / frequency;

                if (!properties.UseSpeedCurve)
                {
                    movedValue = Vector3.Lerp(startValue, destinationValue, curveProgress);
                }
                else
                {
                    float curvePercent = properties.SpeedCurve.Evaluate(curveProgress);
                    movedValue = Vector3.LerpUnclamped(startValue, destinationValue, curvePercent);
                }


                if (timeSinceLastValueChange > frequency)
                {
                    timeSinceLastValueChange = 0f;
                    timeSinceLastPause       = 0f;
                    movedValue       = destinationValue;
                    destinationValue = DetermineNewValue(properties, movedValue, initialValue, ref startValue,
                                                         ref randomAmplitude, ref randomFrequency, ref pauseDuration);
                }
            }
            return(true);
        }
Exemple #6
0
        /// <summary>
        /// Picks a new target value
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="newValue"></param>
        /// <param name="initialValue"></param>
        /// <param name="startValue"></param>
        /// <param name="randomAmplitude"></param>
        /// <param name="randomFrequency"></param>
        /// <param name="pauseDuration"></param>
        /// <returns></returns>
        protected virtual Vector3 DetermineNewValue(WiggleProperties properties, Vector3 newValue, Vector3 initialValue, ref Vector3 startValue,
                                                    ref Vector3 randomAmplitude, ref float randomFrequency, ref float pauseDuration)
        {
            switch (properties.WiggleType)
            {
            case WiggleTypes.PingPong:

                if (properties.RelativeAmplitude)
                {
                    if (newValue == properties.AmplitudeMin + initialValue)
                    {
                        newValue   = properties.AmplitudeMax;
                        startValue = properties.AmplitudeMin;
                    }
                    else
                    {
                        newValue   = properties.AmplitudeMin;
                        startValue = properties.AmplitudeMax;
                    }
                    startValue += initialValue;
                    newValue   += initialValue;
                }
                else
                {
                    newValue   = (newValue == properties.AmplitudeMin) ? properties.AmplitudeMax : properties.AmplitudeMin;
                    startValue = (newValue == properties.AmplitudeMin) ? properties.AmplitudeMax : properties.AmplitudeMin;
                }
                RandomizeFloat(ref randomFrequency, properties.FrequencyMin, properties.FrequencyMax);
                RandomizeFloat(ref pauseDuration, properties.PauseMin, properties.PauseMax);
                return(newValue);

            case WiggleTypes.Random:
                startValue = newValue;
                RandomizeFloat(ref randomFrequency, properties.FrequencyMin, properties.FrequencyMax);
                RandomizeVector3(ref randomAmplitude, properties.AmplitudeMin, properties.AmplitudeMax);
                RandomizeFloat(ref pauseDuration, properties.PauseMin, properties.PauseMax);
                newValue = randomAmplitude;
                if (properties.RelativeAmplitude)
                {
                    newValue += initialValue;
                }
                return(newValue);
            }
            return(Vector3.zero);
        }
Exemple #7
0
        /// <summary>
        /// Initializes internal properties of the specified wiggle value
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="internalProperties"></param>
        protected virtual void InitializeRandomValues(ref WiggleProperties properties, ref InternalWiggleProperties internalProperties)
        {
            internalProperties.newValue             = Vector3.zero;
            internalProperties.timeSinceLastChange  = 0;
            internalProperties.returnVector         = Vector3.zero;
            internalProperties.randomFrequency      = 0f;
            internalProperties.randomNoiseFrequency = Vector3.zero;
            internalProperties.randomAmplitude      = Vector3.zero;
            internalProperties.timeSinceLastPause   = 0;
            internalProperties.pauseDuration        = 0;
            internalProperties.noiseElapsedTime     = 0;
            properties.LimitedTimeLeft = properties.LimitedTimeTotal;

            RandomizeVector3(ref internalProperties.randomAmplitude, properties.AmplitudeMin, properties.AmplitudeMax);
            RandomizeVector3(ref internalProperties.randomNoiseFrequency, properties.NoiseFrequencyMin, properties.NoiseFrequencyMax);
            RandomizeVector3(ref internalProperties.randomNoiseShift, properties.NoiseShiftMin, properties.NoiseShiftMax);

            internalProperties.newValue = DetermineNewValue(properties, internalProperties.newValue, internalProperties.initialValue, ref internalProperties.startValue,
                                                            ref internalProperties.randomAmplitude, ref internalProperties.randomFrequency, ref internalProperties.pauseDuration);
        }
Exemple #8
0
        /// <summary>
        /// Initializes internal properties of the specified wiggle value
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="internalProperties"></param>
        protected virtual void InitializeRandomValues(ref WiggleProperties properties, ref InternalWiggleProperties internalProperties)
        {
            internalProperties.newValue             = this.transform.localPosition;
            internalProperties.timeSinceLastChange  = 0;
            internalProperties.returnVector         = Vector3.zero;
            internalProperties.randomFrequency      = UnityEngine.Random.Range(properties.FrequencyMin, properties.FrequencyMax);
            internalProperties.randomNoiseFrequency = Vector3.zero;
            internalProperties.randomAmplitude      = Vector3.zero;
            internalProperties.timeSinceLastPause   = 0;
            internalProperties.pauseDuration        = 0;
            internalProperties.noiseElapsedTime     = 0;
            internalProperties.curveDirection       = 1f;
            properties.LimitedTimeLeft = properties.LimitedTimeTotal;

            RandomizeVector3(ref internalProperties.randomAmplitude, properties.AmplitudeMin, properties.AmplitudeMax);
            RandomizeVector3(ref internalProperties.randomNoiseFrequency, properties.NoiseFrequencyMin, properties.NoiseFrequencyMax);
            RandomizeVector3(ref internalProperties.randomNoiseShift, properties.NoiseShiftMin, properties.NoiseShiftMax);

            RandomizeVector3(ref internalProperties.remapZero, properties.RemapCurveZeroMin, properties.RemapCurveZeroMax);
            RandomizeVector3(ref internalProperties.remapOne, properties.RemapCurveOneMin, properties.RemapCurveOneMax);

            internalProperties.newValue = DetermineNewValue(properties, internalProperties.newValue, internalProperties.initialValue, ref internalProperties.startValue,
                                                            ref internalProperties.randomAmplitude, ref internalProperties.randomFrequency, ref internalProperties.pauseDuration);
        }
Exemple #9
0
        /// <summary>
        /// Animates a Vector3 value along a specified curve
        /// </summary>
        /// <param name="internalProperties"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected virtual Vector3 AnimateCurveValue(ref InternalWiggleProperties internalProperties, WiggleProperties properties)
        {
            internalProperties.timeSinceLastPause  += properties.GetDeltaTime();
            internalProperties.timeSinceLastChange += properties.GetDeltaTime();

            // handle pause
            if (internalProperties.timeSinceLastPause < internalProperties.pauseDuration)
            {
                float curveProgress = (internalProperties.curveDirection == 1f) ? 1f : 0f;

                EvaluateCurve(properties.Curve, curveProgress, internalProperties.remapZero, internalProperties.remapOne, ref internalProperties.newValue);
                if (properties.RelativeCurveAmplitude)
                {
                    internalProperties.newValue += internalProperties.initialValue;
                }
            }

            // if we're just out of a pause
            if (internalProperties.timeSinceLastPause == internalProperties.timeSinceLastChange)
            {
                internalProperties.timeSinceLastChange = 0f;
            }

            // if we've reached the end
            if (internalProperties.randomFrequency > 0)
            {
                float curveProgress = (internalProperties.timeSinceLastChange) / internalProperties.randomFrequency;
                if (internalProperties.curveDirection < 0f)
                {
                    curveProgress = 1 - curveProgress;
                }

                EvaluateCurve(properties.Curve, curveProgress, internalProperties.remapZero, internalProperties.remapOne, ref internalProperties.newValue);

                if (internalProperties.timeSinceLastChange > internalProperties.randomFrequency)
                {
                    internalProperties.timeSinceLastChange = 0f;
                    internalProperties.timeSinceLastPause  = 0f;
                    if (properties.CurvePingPong)
                    {
                        internalProperties.curveDirection = -internalProperties.curveDirection;
                    }

                    RandomizeFloat(ref internalProperties.randomFrequency, properties.FrequencyMin, properties.FrequencyMax);
                }
            }

            if (properties.RelativeCurveAmplitude)
            {
                internalProperties.newValue = internalProperties.initialValue + internalProperties.newValue;
            }

            return(internalProperties.newValue);
        }
Exemple #10
0
        /// <summary>
        /// Animates a Vector3 value along a perlin noise
        /// </summary>
        /// <param name="internalProperties"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected virtual Vector3 AnimateNoiseValue(ref InternalWiggleProperties internalProperties, WiggleProperties properties)
        {
            internalProperties.noiseElapsedTime += properties.GetDeltaTime();

            internalProperties.newValue.x = (Mathf.PerlinNoise(internalProperties.randomNoiseFrequency.x * internalProperties.noiseElapsedTime, internalProperties.randomNoiseShift.x) * 2.0f - 1.0f) * internalProperties.randomAmplitude.x;
            internalProperties.newValue.y = (Mathf.PerlinNoise(internalProperties.randomNoiseFrequency.y * internalProperties.noiseElapsedTime, internalProperties.randomNoiseShift.y) * 2.0f - 1.0f) * internalProperties.randomAmplitude.y;
            internalProperties.newValue.z = (Mathf.PerlinNoise(internalProperties.randomNoiseFrequency.z * internalProperties.noiseElapsedTime, internalProperties.randomNoiseShift.z) * 2.0f - 1.0f) * internalProperties.randomAmplitude.z;

            if (properties.RelativeAmplitude)
            {
                internalProperties.newValue += internalProperties.initialValue;
            }

            return(internalProperties.newValue);
        }
Exemple #11
0
 protected virtual void StopWiggle(ref WiggleProperties property)
 {
     property.WigglePermitted = false;
 }