PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/unity/Assets/Futile/Rix/RXUtils.cs

https://bitbucket.org/ironpencil/tankfight
C# | 505 lines | 396 code | 93 blank | 16 comment | 37 complexity | 599092374961ca00b8aee42fcd269edb MD5 | raw file
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. public static class RXUtils
  5. {
  6. public static float GetAngle(this Vector2 vector)
  7. {
  8. return Mathf.Atan2(-vector.y, vector.x) * RXMath.RTOD;
  9. }
  10. public static float GetRadians(this Vector2 vector)
  11. {
  12. return Mathf.Atan2(-vector.y, vector.x);
  13. }
  14. public static Rect ExpandRect(Rect rect, float paddingX, float paddingY)
  15. {
  16. return new Rect(rect.x - paddingX, rect.y - paddingY, rect.width + paddingX*2, rect.height+paddingY*2);
  17. }
  18. public static void LogRect(string name, Rect rect)
  19. {
  20. Debug.Log (name+": ("+rect.x+","+rect.y+","+rect.width+","+rect.height+")");
  21. }
  22. public static void LogVectors(string name, params Vector2[] args)
  23. {
  24. string result = name + ": " + args.Length + " Vector2 "+ args[0].ToString()+"";
  25. for(int a = 1; a<args.Length; ++a)
  26. {
  27. Vector2 arg = args[a];
  28. result = result + ", "+ arg.ToString()+"";
  29. }
  30. Debug.Log(result);
  31. }
  32. public static void LogVectors(string name, params Vector3[] args)
  33. {
  34. string result = name + ": " + args.Length + " Vector3 "+args[0].ToString()+"";
  35. for(int a = 1; a<args.Length; ++a)
  36. {
  37. Vector3 arg = args[a];
  38. result = result + ", "+ arg.ToString()+"";
  39. }
  40. Debug.Log(result);
  41. }
  42. public static void LogVectorsDetailed(string name, params Vector2[] args)
  43. {
  44. string result = name + ": " + args.Length + " Vector2 "+ VectorDetailedToString(args[0])+"";
  45. for(int a = 1; a<args.Length; ++a)
  46. {
  47. Vector2 arg = args[a];
  48. result = result + ", "+ VectorDetailedToString(arg)+"";
  49. }
  50. Debug.Log(result);
  51. }
  52. public static string VectorDetailedToString(Vector2 vector)
  53. {
  54. return "("+vector.x + "," + vector.y +")";
  55. }
  56. public static Color GetColorFromHex(uint hex)
  57. {
  58. uint red = hex >> 16;
  59. uint greenBlue = hex - (red<<16);
  60. uint green = greenBlue >> 8;
  61. uint blue = greenBlue - (green << 8);
  62. return new Color(red/255.0f, green/255.0f, blue/255.0f);
  63. }
  64. public static Color GetColorFromHex(string hexString)
  65. {
  66. return GetColorFromHex(Convert.ToUInt32(hexString,16));
  67. }
  68. public static Vector2 GetVector2FromString(string input)
  69. {
  70. string[] parts = input.Split(new char[] {','});
  71. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  72. }
  73. }
  74. public class RXColorHSL
  75. {
  76. public float h = 0.0f;
  77. public float s = 0.0f;
  78. public float l = 0.0f;
  79. public RXColorHSL(float h, float s, float l)
  80. {
  81. this.h = h;
  82. this.s = s;
  83. this.l = l;
  84. }
  85. public RXColorHSL() : this(0.0f, 0.0f, 0.0f) {}
  86. }
  87. public class RXColor
  88. {
  89. //TODO: IMPLEMENT THIS
  90. public static Color ColorFromRGBString(string rgbString)
  91. {
  92. return Color.red;
  93. }
  94. //TODO: IMPLEMENT THIS
  95. public static Color ColorFromHSLString(string hslString)
  96. {
  97. return Color.green;
  98. }
  99. public static Color ColorFromHSL(RXColorHSL hsl)
  100. {
  101. return ColorFromHSL(hsl.h, hsl.s, hsl.l);
  102. }
  103. public static Color ColorFromHSL(float hue, float sat, float lum)
  104. {
  105. return ColorFromHSL(hue,sat,lum,1.0f);
  106. }
  107. //hue goes from 0 to 1
  108. public static Color ColorFromHSL(float hue, float sat, float lum, float alpha) //default - sat:1, lum:0.5
  109. {
  110. hue = (100000.0f+hue)%1f; //hue wraps around
  111. float r = lum;
  112. float g = lum;
  113. float b = lum;
  114. float v = (lum <= 0.5f) ? (lum * (1.0f + sat)) : (lum + sat - lum * sat);
  115. if (v > 0)
  116. {
  117. float m = lum + lum - v;
  118. float sv = (v - m ) / v;
  119. hue *= 6.0f;
  120. int sextant = (int) hue;
  121. float fract = hue - sextant;
  122. float vsf = v * sv * fract;
  123. float mid1 = m + vsf;
  124. float mid2 = v - vsf;
  125. switch (sextant)
  126. {
  127. case 0:
  128. r = v;
  129. g = mid1;
  130. b = m;
  131. break;
  132. case 1:
  133. r = mid2;
  134. g = v;
  135. b = m;
  136. break;
  137. case 2:
  138. r = m;
  139. g = v;
  140. b = mid1;
  141. break;
  142. case 3:
  143. r = m;
  144. g = mid2;
  145. b = v;
  146. break;
  147. case 4:
  148. r = mid1;
  149. g = m;
  150. b = v;
  151. break;
  152. case 5:
  153. r = v;
  154. g = m;
  155. b = mid2;
  156. break;
  157. }
  158. }
  159. return new Color(r,g,b,alpha);
  160. }
  161. //
  162. // Math for the conversion found here: http://www.easyrgb.com/index.php?X=MATH
  163. //
  164. public static RXColorHSL HSLFromColor(Color rgb)
  165. {
  166. RXColorHSL c = new RXColorHSL();
  167. float r = rgb.r;
  168. float g = rgb.g;
  169. float b = rgb.b;
  170. float minChan = Mathf.Min(r, g, b); //Min. value of RGB
  171. float maxChan = Mathf.Max(r, g, b); //Max. value of RGB
  172. float deltaMax = maxChan - minChan; //Delta RGB value
  173. c.l = (maxChan + minChan) * 0.5f;
  174. if (Mathf.Abs(deltaMax) <= 0.0001f) //This is a gray, no chroma...
  175. {
  176. c.h = 0; //HSL results from 0 to 1
  177. c.s = 0;
  178. }
  179. else //Chromatic data...
  180. {
  181. if ( c.l < 0.5f )
  182. c.s = deltaMax / (maxChan + minChan);
  183. else
  184. c.s = deltaMax / (2.0f - maxChan - minChan);
  185. float deltaR = (((maxChan - r) / 6.0f) + (deltaMax * 0.5f)) / deltaMax;
  186. float deltaG = (((maxChan - g) / 6.0f) + (deltaMax * 0.5f)) / deltaMax;
  187. float deltaB = (((maxChan - b) / 6.0f) + (deltaMax * 0.5f)) / deltaMax;
  188. if (Mathf.Approximately(r, maxChan))
  189. c.h = deltaB - deltaG;
  190. else if (Mathf.Approximately(g, maxChan))
  191. c.h = (1.0f / 3.0f) + deltaR - deltaB;
  192. else if (Mathf.Approximately(b, maxChan))
  193. c.h = (2.0f / 3.0f) + deltaG - deltaR;
  194. if (c.h < 0.0f)
  195. c.h += 1.0f;
  196. else if (c.h > 1.0f)
  197. c.h -= 1.0f;
  198. }
  199. return c;
  200. }
  201. public static Color GetColorFromHex(uint hex)
  202. {
  203. uint red = hex >> 16;
  204. uint greenBlue = hex - (red<<16);
  205. uint green = greenBlue >> 8;
  206. uint blue = greenBlue - (green << 8);
  207. return new Color(red/255.0f, green/255.0f, blue/255.0f);
  208. }
  209. }
  210. public class RXMath
  211. {
  212. public const float RTOD = 180.0f/Mathf.PI;
  213. public const float DTOR = Mathf.PI/180.0f;
  214. public const float DOUBLE_PI = Mathf.PI*2.0f;
  215. public const float HALF_PI = Mathf.PI/2.0f;
  216. public const float PI = Mathf.PI;
  217. public const float INVERSE_PI = 1.0f/Mathf.PI;
  218. public const float INVERSE_DOUBLE_PI = 1.0f/(Mathf.PI*2.0f);
  219. public static int Wrap(int input, int range)
  220. {
  221. return (input + (range*1000000)) % range;
  222. }
  223. public static float Wrap(float input, float range)
  224. {
  225. return (input + (range*1000000)) % range;
  226. }
  227. public static float GetDegreeDelta(float startAngle, float endAngle) //chooses the shortest angular distance
  228. {
  229. float delta = (endAngle - startAngle) % 360.0f;
  230. if (delta != delta % 180.0f)
  231. {
  232. delta = (delta < 0) ? delta + 360.0f : delta - 360.0f;
  233. }
  234. return delta;
  235. }
  236. public static float GetRadianDelta(float startAngle, float endAngle) //chooses the shortest angular distance
  237. {
  238. float delta = (endAngle - startAngle) % DOUBLE_PI;
  239. if (delta != delta % Mathf.PI)
  240. {
  241. delta = (delta < 0) ? delta + DOUBLE_PI : delta - DOUBLE_PI;
  242. }
  243. return delta;
  244. }
  245. //normalized ping pong (apparently Unity has this built in... so yeah) - Mathf.PingPong()
  246. public static float PingPong(float input, float range)
  247. {
  248. float first = ((input + (range*1000000.0f)) % range)/range; //0 to 1
  249. if(first < 0.5f) return first*2.0f;
  250. else return 1.0f - ((first - 0.5f)*2.0f);
  251. }
  252. }
  253. public static class RXRandom
  254. {
  255. private static System.Random _randomSource = new System.Random();
  256. public static float Float()
  257. {
  258. return (float)_randomSource.NextDouble();
  259. }
  260. public static float Float(int seed)
  261. {
  262. return (float)new System.Random(seed).NextDouble();
  263. }
  264. public static double Double()
  265. {
  266. return _randomSource.NextDouble();
  267. }
  268. public static float Float(float max)
  269. {
  270. return (float)_randomSource.NextDouble() * max;
  271. }
  272. public static int Int()
  273. {
  274. return _randomSource.Next();
  275. }
  276. public static int Int(int max)
  277. {
  278. if(max == 0) return 0;
  279. return _randomSource.Next() % max;
  280. }
  281. public static float Range(float low, float high)
  282. {
  283. return low + (high-low)*(float)_randomSource.NextDouble();
  284. }
  285. public static int Range(int low, int high)
  286. {
  287. int delta = high - low;
  288. if(delta == 0) return 0;
  289. return low + _randomSource.Next() % delta;
  290. }
  291. public static bool Bool()
  292. {
  293. return _randomSource.NextDouble() < 0.5;
  294. }
  295. //random item from all passed arguments/params - RXRandom.Select(one, two, three);
  296. public static object Select(params object[] objects)
  297. {
  298. return objects[_randomSource.Next() % objects.Length];
  299. }
  300. //random item from an array
  301. public static T AnyItem<T>(T[] items)
  302. {
  303. if(items.Length == 0) return default(T); //null
  304. return items[_randomSource.Next() % items.Length];
  305. }
  306. //random item from a list
  307. public static T AnyItem<T>(List<T> items)
  308. {
  309. if(items.Count == 0) return default(T); //null
  310. return items[_randomSource.Next() % items.Count];
  311. }
  312. //this isn't really perfectly randomized, but good enough for most purposes
  313. public static Vector2 Vector2Normalized()
  314. {
  315. return new Vector2(RXRandom.Range(-1.0f,1.0f),RXRandom.Range(-1.0f,1.0f)).normalized;
  316. }
  317. public static Vector3 Vector3Normalized()
  318. {
  319. return new Vector3(RXRandom.Range(-1.0f,1.0f),RXRandom.Range(-1.0f,1.0f),RXRandom.Range(-1.0f,1.0f)).normalized;
  320. }
  321. public static void ShuffleList<T>(List<T> list)
  322. {
  323. list.Sort(RandomComparison);
  324. }
  325. public static void Shuffle<T>(this List<T> list)
  326. {
  327. list.Sort(RandomComparison);
  328. }
  329. private static int RandomComparison<T>(T a, T b)
  330. {
  331. if(_randomSource.Next() % 2 == 0) return -1;
  332. return 1;
  333. }
  334. }
  335. public class RXCircle
  336. {
  337. public Vector2 center;
  338. public float radius;
  339. public float radiusSquared;
  340. public RXCircle(Vector2 center, float radius)
  341. {
  342. this.center = center;
  343. this.radius = radius;
  344. this.radiusSquared = radius * radius;
  345. }
  346. public bool CheckIntersectWithRect(Rect rect)
  347. {
  348. return rect.CheckIntersectWithCircle(this);
  349. }
  350. public bool CheckIntersectWithCircle(RXCircle circle)
  351. {
  352. Vector2 delta = circle.center - this.center;
  353. float radiusSumSquared = (circle.radius + this.radius) * (circle.radius + this.radius);
  354. return (delta.sqrMagnitude <= radiusSumSquared);
  355. }
  356. }
  357. //This class is incomplete, I just have to get around to converting all the equations to this simplified format
  358. public static class RXEase
  359. {
  360. //based on GoKit's easing equations: https://github.com/prime31/GoKit/tree/master/Assets/Plugins/GoKit/easing
  361. //but simplified to work with only normalized values (0..1)
  362. //t = current time, b = starting value, c = final value, d = duration
  363. //for our purposes, t = input, b = 0, d = 1, c = 1 :::: note that (t/d = input)
  364. public static float QuadOut(float input)
  365. {
  366. return -input * (input - 2.0f);
  367. }
  368. public static float QuadIn(float input)
  369. {
  370. return input * input;
  371. }
  372. public static float QuadInOut(float input)
  373. {
  374. if (input < 0.5f) return 2.0f * input * input;
  375. input = (input-0.5f) * 2.0f;
  376. return 0.5f - 0.5f * input * (input - 2.0f);
  377. }
  378. public static float ExpoOut(float input)
  379. {
  380. return -Mathf.Pow( 2.0f, -10.0f * input) + 1.0f;
  381. }
  382. public static float ExpoIn(float input)
  383. {
  384. return Mathf.Pow(2.0f,10.0f * (input - 1.0f));
  385. }
  386. public static float ExpoInOut(float input)
  387. {
  388. if (input < 0.5f) return Mathf.Pow(2.0f,10.0f * (input*2.0f - 1.0f)) * 0.5f;
  389. else return 0.5f + (-Mathf.Pow( 2.0f, -20.0f * (input-0.5f)) + 1.0f) * 0.5f;
  390. }
  391. public static float BackOut(float input) {return BackOut(input,1.7f);}
  392. public static float BackOut(float input, float backAmount)
  393. {
  394. input = input - 1.0f;
  395. return (input * input * ((backAmount + 1) * input + backAmount) + 1);
  396. }
  397. public static float BackIn(float input) {return BackIn(input,1.7f);}
  398. public static float BackIn(float input, float backAmount)
  399. {
  400. return input * input * ((backAmount + 1.0f) * input - backAmount);
  401. }
  402. public static float BackInOut(float input) {return BackInOut(input,1.7f);}
  403. public static float BackInOut(float input, float backAmount)
  404. {
  405. if (input < 0.5f) return BackIn(input*2.0f,backAmount)*0.5f;
  406. else return 0.5f + BackOut((input-0.5f)*2.0f,backAmount)*0.5f;
  407. }
  408. public static float SinInOut(float input)
  409. {
  410. return -0.5f * (Mathf.Cos(Mathf.PI*input) - 1.0f);
  411. }
  412. }