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); } }
// 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); } } }