OhiraKyou

Wrap functions

Apr 17th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using OhiraKyou.Extensions;
  6. using System;
  7.  
  8. namespace OhiraKyou {
  9.  
  10.     public class CustomMath {
  11.  
  12.         public static float Wrap01 (float value) {
  13.             return Wrap(value, 0f, 1f);
  14.         }
  15.  
  16.         public static float Wrap (float value, float min, float max) {
  17.             if (min == 0f && max == 0f) return 0f;
  18.             return (((value - min) % (max - min)) + (max - min)) % (max - min) + min;
  19.         }
  20.  
  21.         public static int Wrap (int value, int min, int max) {
  22.             if (min == 0 && max == 0) return 0;
  23.             max++; // makes max inclusive
  24.             return (((value - min) % (max - min)) + (max - min)) % (max - min) + min;
  25.         }
  26.  
  27.         public static float GetWrappedSeconds (float currentSeconds, float maxTime) {
  28.             return Wrap(currentSeconds + Time.deltaTime, 0f, maxTime);
  29.         }
  30.  
  31.         public static float GetWrappedSeconds (float currentSeconds, float deltaTime, float maxTime) {
  32.             return Wrap(currentSeconds + deltaTime, 0f, maxTime);
  33.         }
  34.  
  35.         public static T Clamp<T> (T value, T min, T max) where T : IComparable<T> {
  36.             if (value.CompareTo(min) < 0) return min;
  37.             else if (value.CompareTo(max) > 0) return max;
  38.             else return value;
  39.         }
  40.  
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment