public void Create <T>(T value, Vector3 position) where T : struct, IComparable <T> { if (!UtilGeneric.IsZero(value) || createWhenValueIsZero) { GameObject obj = Instantiate(prefabRisingText, transform); obj.transform.position = position; ValueChangeText vct = obj.GetComponent <ValueChangeText>(); vct.SetValue(value); } }
// Retrieve a value from the array based on a key. public bool TryGetValue(TKey key, out TValue value) { foreach (TPair pair in array) { if (UtilGeneric.IsEqualTo(pair.key, key)) { value = pair.value; return(true); } } value = default(TValue); return(false); }
public void SetValue <T>(T value) where T : struct, IComparable <T> { // Whether the damage value is negative or not. bool negative = false; // Check if the value is negative. if (UtilGeneric.IsNegative(value)) { negative = true; // Remove the minus sign since we don't want that to confuse the string. value = UtilGeneric.Negate(value); } string sign = negative ? signMinus : signPlus; text.text = sign + prefix + value + postfix; }
void Start() { EnterNewSuite("Angles"); Angle a = Angle.FromDegrees(20.0f); Angle b = Angle.FromDegrees(310.0f); TestEqual(Angle.GetLargerDistance(a, b).GetDegreesUnsigned(), 290.0f); TestEqual(Angle.GetSmallerDistance(b, a).GetDegreesUnsigned(), 70.0f); TestEqual(Angle.FromHeadingVector(1.0f, 0.0f).GetDegreesUnsigned(), 0.0f); TestEqual(Angle.FromHeadingVector(0.0f, 1.0f).GetDegreesUnsigned(), 90.0f); TestEqual(Angle.FromHeadingVector(-1.0f, 0.0f).GetDegreesUnsigned(), 180.0f); TestEqual(Angle.FromHeadingVector(0.0f, -1.0f).GetDegreesUnsigned(), 270.0f); Test(Angle.FromDegrees(30.0f).IsCoterminal(Angle.FromDegrees(-330.0f))); Test(Angle.FromDegrees(30.0f).IsCoterminal(Angle.FromDegrees(390.0f))); EnterNewSuite("Generics"); TestEqual(UtilGeneric.Add <int, int, int>(5, 7), 12); Vector3 start = new Vector3(1, 0, 0); Vector3 end = new Vector3(16, 0, 0); Vector3 velocity = new Vector3(5, 0, 0); TestEqual(UtilVelocity.GetVelocity(start, end, 3.0f), velocity); TestEqual(UtilVelocity.GetFuturePosition(2.0f, 3.0f, 6.0f), 20.0f); }
// Gets the first key that has the given value. // Returns false if the given value doesn't exist in the dictionary. public static bool TryGetKey <TKey, TValue>(this Dictionary <TKey, TValue> dict, TValue value, out TKey key) { key = dict.FirstOrDefault(x => UtilGeneric.IsEqualTo(x.Value, value)).Key; return(dict.ContainsValue(value)); }
// Returns true if a tie exists in the number 1 spot. // TODO: This function is broken?? Fix it! public static bool IsLargestElementTied <T>(IEnumerable <T> collection) { List <T> ordered = collection.OrderByDescending(x => x).ToList(); return(UtilGeneric.IsEqualTo(ordered[0], ordered[1])); }
void TestEqual <T>(T lhs, T rhs) { Test(UtilGeneric.IsEqualTo(lhs, rhs), "LHS = " + lhs + "; RHS = " + rhs); }
// Returns true if the given result is the default result. private bool IsDefaultResult(T result) { return(UtilGeneric.IsEqualTo(result, resultDefault)); }
// Returns the velocity at which an object will have to move to get from the start position to // the end position in the given number of seconds. This assumes the object will move in a // straight line with a constant velocity. public static T GetVelocity <T>(T startPos, T endPos, float seconds) { T difference = UtilGeneric.Subtract <T, T, T>(endPos, startPos); return(UtilGeneric.Divide <T, float, T>(difference, seconds)); }
// Gets the predicted position of an object with a given start position and velocity after a // given number of seconds. Does not take potential collisions or external forces into account. public static T GetFuturePosition <T>(T startPos, T velocity, float seconds) { T distanceTraveled = UtilGeneric.Multiply <T, float, T>(velocity, seconds); return(UtilGeneric.Add <T, T, T>(startPos, distanceTraveled)); }