private void _UpdateSingleSetVelocity2D(SetVelocity2DData inputData, bool restoreOriginal = false) { if (inputData.cachedVelo2D == null) { throw new NullReferenceException("No SetVelocity2D Action found on the FSM " + inputData.FSMName + " in state " + inputData.FSMStateName); } float realFactor = (float)(((danceSpeed - 1.0) * (inputData.MagnitudeFactor - 1.0)) + 1.0); if (!active || !speedModActive || restoreOriginal) { realFactor = 1.0f; } // Stop divide by zero. else if (realFactor <= epsilon) { throw new Exception("To prevent stupid looking, and broken behavior," + " your relative magnitude must be greater than " + epsilon + ". But a dance speed of " + danceSpeed + " set it to " + realFactor); } inputData.cachedVelo2D.x.Value = (inputData.DefaultMagnitude.x * realFactor); inputData.cachedVelo2D.y.Value = (inputData.DefaultMagnitude.y * realFactor); }
private IEnumerator _WaitForSetVelo2DToBeLoaded(SetVelocity2DData inputData) { // after this much time it will assume the set velocity 2d actually in fact has a magnitude of 0 // at which point it WILL NOT add it to the list. because: simply put, you can't speed up or slow down // a zero vector by any amount. float maxWaitTime = 5f; Vector2 velocityVec = new Vector2(inputData.cachedVelo2D.x.Value, inputData.cachedVelo2D.y.Value); while (velocityVec.magnitude <= epsilon && maxWaitTime > 0.0f) { maxWaitTime -= Time.deltaTime; yield return(null); velocityVec = new Vector2(inputData.cachedVelo2D.x.Value, inputData.cachedVelo2D.y.Value); } if (maxWaitTime <= 0.0f) { Modding.Logger.LogError("[ModCommon] Unable to add setvelocity2d to CustomEnemySpeed because the velocity is a " + "zero vector which cannot have its speed modified."); yield break; } Vector2 origVec = new Vector2(inputData.cachedVelo2D.x.Value, inputData.cachedVelo2D.y.Value); inputData.SetDefaultMagnitude(origVec); speedModifyVelocity2D.Add(inputData); if (active && speedModActive) { _UpdateSingleSetVelocity2D(inputData); } }
// Removes the SetVelocity2D from the list, if it exists. Returns true if it found and removed it. public bool RemoveSetVelocity2DData(SetVelocity2DData inputData) { if (!speedModifyVelocity2D.Contains(inputData)) { return(false); } _UpdateSingleSetVelocity2D(inputData, true); speedModifyVelocity2D.Remove(inputData); return(true); }
// Adds a SetVelocity2D to the list, stored in the struct format SetVelocity2DData. Use a constructor to build it. public void AddSetVelocity2DData(SetVelocity2DData inputData) { if (inputData.CustomGameObject == null) { inputData.CustomGameObject = gameObject; } if (inputData.cachedVelo2D == null) { inputData.cachedVelo2D = _getOrCacheFSMSetVelocity2D(inputData.FSMStateName, inputData.FSMName, inputData.CustomGameObject, inputData.ElementIndex); } if (inputData.cachedVelo2D == null) { throw new System.NullReferenceException("No SetVelocity2D Action found on the FSM " + inputData.FSMName + " in state " + inputData.FSMStateName); } Vector2 origVec = new Vector2(inputData.cachedVelo2D.x.Value, inputData.cachedVelo2D.y.Value); float mag = origVec.magnitude; if (mag <= epsilon) { StartCoroutine(_WaitForSetVelo2DToBeLoaded(inputData)); } else { inputData.SetDefaultMagnitude(origVec); speedModifyVelocity2D.Add(inputData); if (active && speedModActive) { _UpdateSingleSetVelocity2D(inputData); } } }