-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.cs
More file actions
1566 lines (1281 loc) · 44.5 KB
/
Calc.cs
File metadata and controls
1566 lines (1281 loc) · 44.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Chip8
{
/// <summary>
/// Calc class that provides a ton of useful functions. Copied from Matt Thorsons Monocle Engine.
/// See `https://bitbucket.org/MattThorson/monocle-engine/src/default/Monocle/Util/Calc.cs`
/// </summary>
public static class Calc
{
#region Enums
public static int EnumLength(Type e)
{
return Enum.GetNames(e).Length;
}
public static T StringToEnum<T>(string str) where T : struct
{
if (Enum.IsDefined(typeof(T), str))
return (T)Enum.Parse(typeof(T), str);
else
throw new Exception("The string cannot be converted to the enum type.");
}
public static T[] StringsToEnums<T>(string[] strs) where T : struct
{
T[] ret = new T[strs.Length];
for (int i = 0; i < strs.Length; i++)
ret[i] = StringToEnum<T>(strs[i]);
return ret;
}
public static bool EnumHasString<T>(string str) where T : struct
{
return Enum.IsDefined(typeof(T), str);
}
#endregion
#region Strings
public static bool StartsWith(this string str, string match)
{
return str.IndexOf(match) == 0;
}
public static bool EndsWith(this string str, string match)
{
return str.LastIndexOf(match) == str.Length - match.Length;
}
public static bool IsIgnoreCase(this string str, params string[] matches)
{
if (string.IsNullOrEmpty(str))
return false;
foreach (var match in matches)
if (str.Equals(match, StringComparison.InvariantCultureIgnoreCase))
return true;
return false;
}
public static string ToString(this int num, int minDigits)
{
string ret = num.ToString();
while (ret.Length < minDigits)
ret = "0" + ret;
return ret;
}
public static string[] SplitLines(string text, SpriteFont font, int maxLineWidth, char newLine = '\n')
{
List<string> lines = new List<string>();
foreach (var forcedLine in text.Split(newLine))
{
string line = "";
foreach (string word in forcedLine.Split(' '))
{
if (font.MeasureString(line + " " + word).X > maxLineWidth)
{
lines.Add(line);
line = word;
}
else
{
if (line != "")
line += " ";
line += word;
}
}
lines.Add(line);
}
return lines.ToArray();
}
#endregion
#region Count
public static int Count<T>(T target, T a, T b)
{
int num = 0;
if (a.Equals(target))
num++;
if (b.Equals(target))
num++;
return num;
}
public static int Count<T>(T target, T a, T b, T c)
{
int num = 0;
if (a.Equals(target))
num++;
if (b.Equals(target))
num++;
if (c.Equals(target))
num++;
return num;
}
public static int Count<T>(T target, T a, T b, T c, T d)
{
int num = 0;
if (a.Equals(target))
num++;
if (b.Equals(target))
num++;
if (c.Equals(target))
num++;
if (d.Equals(target))
num++;
return num;
}
public static int Count<T>(T target, T a, T b, T c, T d, T e)
{
int num = 0;
if (a.Equals(target))
num++;
if (b.Equals(target))
num++;
if (c.Equals(target))
num++;
if (d.Equals(target))
num++;
if (e.Equals(target))
num++;
return num;
}
public static int Count<T>(T target, T a, T b, T c, T d, T e, T f)
{
int num = 0;
if (a.Equals(target))
num++;
if (b.Equals(target))
num++;
if (c.Equals(target))
num++;
if (d.Equals(target))
num++;
if (e.Equals(target))
num++;
if (f.Equals(target))
num++;
return num;
}
#endregion
#region Give Me
public static T GiveMe<T>(int index, T a, T b)
{
switch (index)
{
default:
throw new Exception("Index was out of range!");
case 0:
return a;
case 1:
return b;
}
}
public static T GiveMe<T>(int index, T a, T b, T c)
{
switch (index)
{
default:
throw new Exception("Index was out of range!");
case 0:
return a;
case 1:
return b;
case 2:
return c;
}
}
public static T GiveMe<T>(int index, T a, T b, T c, T d)
{
switch (index)
{
default:
throw new Exception("Index was out of range!");
case 0:
return a;
case 1:
return b;
case 2:
return c;
case 3:
return d;
}
}
public static T GiveMe<T>(int index, T a, T b, T c, T d, T e)
{
switch (index)
{
default:
throw new Exception("Index was out of range!");
case 0:
return a;
case 1:
return b;
case 2:
return c;
case 3:
return d;
case 4:
return e;
}
}
public static T GiveMe<T>(int index, T a, T b, T c, T d, T e, T f)
{
switch (index)
{
default:
throw new Exception("Index was out of range!");
case 0:
return a;
case 1:
return b;
case 2:
return c;
case 3:
return d;
case 4:
return e;
case 5:
return f;
}
}
#endregion
#region Random
public static Random Random = new Random();
private static Stack<Random> randomStack = new Stack<Random>();
public static void PushRandom(int newSeed)
{
randomStack.Push(Calc.Random);
Calc.Random = new Random(newSeed);
}
public static void PushRandom(Random random)
{
randomStack.Push(Calc.Random);
Calc.Random = random;
}
public static void PushRandom()
{
randomStack.Push(Calc.Random);
Calc.Random = new Random();
}
public static void PopRandom()
{
Calc.Random = randomStack.Pop();
}
#region Choose
public static T Choose<T>(this Random random, T a, T b)
{
return GiveMe<T>(random.Next(2), a, b);
}
public static T Choose<T>(this Random random, T a, T b, T c)
{
return GiveMe<T>(random.Next(3), a, b, c);
}
public static T Choose<T>(this Random random, T a, T b, T c, T d)
{
return GiveMe<T>(random.Next(4), a, b, c, d);
}
public static T Choose<T>(this Random random, T a, T b, T c, T d, T e)
{
return GiveMe<T>(random.Next(5), a, b, c, d, e);
}
public static T Choose<T>(this Random random, T a, T b, T c, T d, T e, T f)
{
return GiveMe<T>(random.Next(6), a, b, c, d, e, f);
}
public static T Choose<T>(this Random random, params T[] choices)
{
return choices[random.Next(choices.Length)];
}
public static T Choose<T>(this Random random, List<T> choices)
{
return choices[random.Next(choices.Count)];
}
#endregion
#region Range
/// <summary>
/// Returns a random integer between min (inclusive) and max (exclusive)
/// </summary>
/// <param name="random"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static int Range(this Random random, int min, int max)
{
return min + random.Next(max - min);
}
/// <summary>
/// Returns a random float between min (inclusive) and max (exclusive)
/// </summary>
/// <param name="random"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static float Range(this Random random, float min, float max)
{
return min + random.NextFloat(max - min);
}
/// <summary>
/// Returns a random Vector2, and x- and y-values of which are between min (inclusive) and max (exclusive)
/// </summary>
/// <param name="random"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static Vector2 Range(this Random random, Vector2 min, Vector2 max)
{
return min + new Vector2(random.NextFloat(max.X - min.X), random.NextFloat(max.Y - min.Y));
}
#endregion
public static int Facing(this Random random)
{
return (random.NextFloat() < 0.5f ? -1 : 1);
}
public static bool Chance(this Random random, float chance)
{
return random.NextFloat() < chance;
}
public static float NextFloat(this Random random)
{
return (float)random.NextDouble();
}
public static float NextFloat(this Random random, float max)
{
return random.NextFloat() * max;
}
public static float NextAngle(this Random random)
{
return random.NextFloat() * MathHelper.TwoPi;
}
private static int[] shakeVectorOffsets = new int[] { -1, -1, 0, 1, 1 };
public static Vector2 ShakeVector(this Random random)
{
return new Vector2(random.Choose(shakeVectorOffsets), random.Choose(shakeVectorOffsets));
}
#endregion
#region Lists
public static Vector2 ClosestTo(this List<Vector2> list, Vector2 to)
{
Vector2 best = list[0];
float distSq = Vector2.DistanceSquared(list[0], to);
for (int i = 1; i < list.Count; i++)
{
float d = Vector2.DistanceSquared(list[i], to);
if (d < distSq)
{
distSq = d;
best = list[i];
}
}
return best;
}
public static Vector2 ClosestTo(this Vector2[] list, Vector2 to)
{
Vector2 best = list[0];
float distSq = Vector2.DistanceSquared(list[0], to);
for (int i = 1; i < list.Length; i++)
{
float d = Vector2.DistanceSquared(list[i], to);
if (d < distSq)
{
distSq = d;
best = list[i];
}
}
return best;
}
public static Vector2 ClosestTo(this Vector2[] list, Vector2 to, out int index)
{
index = 0;
Vector2 best = list[0];
float distSq = Vector2.DistanceSquared(list[0], to);
for (int i = 1; i < list.Length; i++)
{
float d = Vector2.DistanceSquared(list[i], to);
if (d < distSq)
{
index = i;
distSq = d;
best = list[i];
}
}
return best;
}
public static void Shuffle<T>(this List<T> list, Random random)
{
int i = list.Count;
int j;
T t;
while (--i > 0)
{
t = list[i];
list[i] = list[j = random.Next(i + 1)];
list[j] = t;
}
}
public static void Shuffle<T>(this List<T> list)
{
list.Shuffle(Random);
}
public static void ShuffleSetFirst<T>(this List<T> list, Random random, T first)
{
int amount = 0;
while (list.Contains(first))
{
list.Remove(first);
amount++;
}
list.Shuffle(random);
for (int i = 0; i < amount; i++)
list.Insert(0, first);
}
public static void ShuffleSetFirst<T>(this List<T> list, T first)
{
list.ShuffleSetFirst(Random, first);
}
public static void ShuffleNotFirst<T>(this List<T> list, Random random, T notFirst)
{
int amount = 0;
while (list.Contains(notFirst))
{
list.Remove(notFirst);
amount++;
}
list.Shuffle(random);
for (int i = 0; i < amount; i++)
list.Insert(random.Next(list.Count - 1) + 1, notFirst);
}
public static void ShuffleNotFirst<T>(this List<T> list, T notFirst)
{
list.ShuffleNotFirst<T>(Random, notFirst);
}
#endregion
#region Colors
public static Color Invert(this Color color)
{
return new Color(255 - color.R, 255 - color.G, 255 - color.B, color.A);
}
public static Color HexToColor(string hex)
{
if (hex.Length >= 6)
{
float r = (HexToByte(hex[0]) * 16 + HexToByte(hex[1])) / 255.0f;
float g = (HexToByte(hex[2]) * 16 + HexToByte(hex[3])) / 255.0f;
float b = (HexToByte(hex[4]) * 16 + HexToByte(hex[5])) / 255.0f;
return new Color(r, g, b);
}
return Color.White;
}
#endregion
#region Time
public static string ShortGameplayFormat(this TimeSpan time)
{
if (time.TotalHours >= 1)
return ((int)time.Hours) + ":" + time.ToString(@"mm\:ss\.fff");
else
return time.ToString(@"m\:ss\.fff");
}
public static string LongGameplayFormat(this TimeSpan time)
{
StringBuilder str = new StringBuilder();
if (time.TotalDays >= 2)
{
str.Append((int)time.TotalDays);
str.Append(" days, ");
}
else if (time.TotalDays >= 1)
str.Append("1 day, ");
str.Append((time.TotalHours - ((int)time.TotalDays * 24)).ToString("0.0"));
str.Append(" hours");
return str.ToString();
}
#endregion
#region Math
public const float Right = 0;
public const float Up = -MathHelper.PiOver2;
public const float Left = MathHelper.Pi;
public const float Down = MathHelper.PiOver2;
public const float UpRight = -MathHelper.PiOver4;
public const float UpLeft = -MathHelper.PiOver4 - MathHelper.PiOver2;
public const float DownRight = MathHelper.PiOver4;
public const float DownLeft = MathHelper.PiOver4 + MathHelper.PiOver2;
public const float DegToRad = MathHelper.Pi / 180f;
public const float RadToDeg = 180f / MathHelper.Pi;
public const float DtR = DegToRad;
public const float RtD = RadToDeg;
public const float Circle = MathHelper.TwoPi;
public const float HalfCircle = MathHelper.Pi;
public const float QuarterCircle = MathHelper.PiOver2;
public const float EighthCircle = MathHelper.PiOver4;
private const string Hex = "0123456789ABCDEF";
public static int Digits(this int num)
{
int digits = 1;
int target = 10;
while (num >= target)
{
digits++;
target *= 10;
}
return digits;
}
public static byte HexToByte(char c)
{
return (byte)Hex.IndexOf(char.ToUpper(c));
}
public static float Percent(float num, float zeroAt, float oneAt)
{
return MathHelper.Clamp((num - zeroAt) / oneAt, 0, 1);
}
public static float SignThreshold(float value, float threshold)
{
if (Math.Abs(value) >= threshold)
return Math.Sign(value);
else
return 0;
}
public static float Min(params float[] values)
{
float min = values[0];
for (int i = 1; i < values.Length; i++)
min = MathHelper.Min(values[i], min);
return min;
}
public static float Max(params float[] values)
{
float max = values[0];
for (int i = 1; i < values.Length; i++)
max = MathHelper.Max(values[i], max);
return max;
}
public static float ToRad(this float f)
{
return f * DegToRad;
}
public static float ToDeg(this float f)
{
return f * RadToDeg;
}
public static int Axis(bool negative, bool positive, int both = 0)
{
if (negative)
{
if (positive)
return both;
else
return -1;
}
else if (positive)
return 1;
else
return 0;
}
public static int Clamp(int value, int min, int max)
{
return Math.Min(Math.Max(value, min), max);
}
public static float Clamp(float value, float min, float max)
{
return Math.Min(Math.Max(value, min), max);
}
public static float YoYo(float value)
{
if (value <= .5f)
return value * 2;
else
return 1 - ((value - .5f) * 2);
}
public static float Map(float val, float min, float max, float newMin = 0, float newMax = 1)
{
return ((val - min) / (max - min)) * (newMax - newMin) + newMin;
}
public static float SineMap(float counter, float newMin, float newMax)
{
return Calc.Map((float)Math.Sin(counter), 01, 1, newMin, newMax);
}
public static float ClampedMap(float val, float min, float max, float newMin = 0, float newMax = 1)
{
return MathHelper.Clamp((val - min) / (max - min), 0, 1) * (newMax - newMin) + newMin;
}
public static float LerpSnap(float value1, float value2, float amount, float snapThreshold = .1f)
{
float ret = MathHelper.Lerp(value1, value2, amount);
if (Math.Abs(ret - value2) < snapThreshold)
return value2;
else
return ret;
}
public static float LerpClamp(float value1, float value2, float lerp)
{
return MathHelper.Lerp(value1, value2, MathHelper.Clamp(lerp, 0, 1));
}
public static Vector2 LerpSnap(Vector2 value1, Vector2 value2, float amount, float snapThresholdSq = .1f)
{
Vector2 ret = Vector2.Lerp(value1, value2, amount);
if ((ret - value2).LengthSquared() < snapThresholdSq)
return value2;
else
return ret;
}
public static Vector2 Sign(this Vector2 vec)
{
return new Vector2(Math.Sign(vec.X), Math.Sign(vec.Y));
}
public static Vector2 SafeNormalize(this Vector2 vec)
{
return SafeNormalize(vec, Vector2.Zero);
}
public static Vector2 SafeNormalize(this Vector2 vec, float length)
{
return SafeNormalize(vec, Vector2.Zero, length);
}
public static Vector2 SafeNormalize(this Vector2 vec, Vector2 ifZero)
{
if (vec == Vector2.Zero)
return ifZero;
else
{
vec.Normalize();
return vec;
}
}
public static Vector2 SafeNormalize(this Vector2 vec, Vector2 ifZero, float length)
{
if (vec == Vector2.Zero)
return ifZero * length;
else
{
vec.Normalize();
return vec * length;
}
}
public static float ReflectAngle(float angle, float axis = 0)
{
return -(angle + axis) - axis;
}
public static float ReflectAngle(float angleRadians, Vector2 axis)
{
return ReflectAngle(angleRadians, axis.Angle());
}
public static Vector2 ClosestPointOnLine(Vector2 lineA, Vector2 lineB, Vector2 closestTo)
{
Vector2 v = lineB - lineA;
Vector2 w = closestTo - lineA;
float t = Vector2.Dot(w, v) / Vector2.Dot(v, v);
t = MathHelper.Clamp(t, 0, 1);
return lineA + v * t;
}
public static Vector2 Round(this Vector2 vec)
{
return new Vector2((float)Math.Round(vec.X), (float)Math.Round(vec.Y));
}
public static float Snap(float value, float increment)
{
return (float)Math.Round(value / increment) * increment;
}
public static float Snap(float value, float increment, float offset)
{
return ((float)Math.Round((value - offset) / increment) * increment) + offset;
}
public static float WrapAngleDeg(float angleDegrees)
{
return (((angleDegrees * Math.Sign(angleDegrees) + 180) % 360) - 180) * Math.Sign(angleDegrees);
}
public static float WrapAngle(float angleRadians)
{
return (((angleRadians * Math.Sign(angleRadians) + MathHelper.Pi) % (MathHelper.Pi * 2)) - MathHelper.Pi) * Math.Sign(angleRadians);
}
public static Vector2 AngleToVector(float angleRadians, float length)
{
return new Vector2((float)Math.Cos(angleRadians) * length, (float)Math.Sin(angleRadians) * length);
}
public static float AngleApproach(float val, float target, float maxMove)
{
var diff = AngleDiff(val, target);
if (Math.Abs(diff) < maxMove)
return target;
return val + MathHelper.Clamp(diff, -maxMove, maxMove);
}
public static float AngleLerp(float startAngle, float endAngle, float percent)
{
return startAngle + AngleDiff(startAngle, endAngle) * percent;
}
public static float Approach(float val, float target, float maxMove)
{
return val > target ? Math.Max(val - maxMove, target) : Math.Min(val + maxMove, target);
}
public static float AngleDiff(float radiansA, float radiansB)
{
float diff = radiansB - radiansA;
while (diff > MathHelper.Pi) { diff -= MathHelper.TwoPi; }
while (diff <= -MathHelper.Pi) { diff += MathHelper.TwoPi; }
return diff;
}
public static float AbsAngleDiff(float radiansA, float radiansB)
{
return Math.Abs(AngleDiff(radiansA, radiansB));
}
public static int SignAngleDiff(float radiansA, float radiansB)
{
return Math.Sign(AngleDiff(radiansA, radiansB));
}
public static float Angle(Vector2 from, Vector2 to)
{
return (float)Math.Atan2(to.Y - from.Y, to.X - from.X);
}
public static Color ToggleColors(Color current, Color a, Color b)
{
if (current == a)
return b;
else
return a;
}
public static float ShorterAngleDifference(float currentAngle, float angleA, float angleB)
{
if (Math.Abs(Calc.AngleDiff(currentAngle, angleA)) < Math.Abs(Calc.AngleDiff(currentAngle, angleB)))
return angleA;
else
return angleB;
}
public static float ShorterAngleDifference(float currentAngle, float angleA, float angleB, float angleC)
{
if (Math.Abs(Calc.AngleDiff(currentAngle, angleA)) < Math.Abs(Calc.AngleDiff(currentAngle, angleB)))
return ShorterAngleDifference(currentAngle, angleA, angleC);
else
return ShorterAngleDifference(currentAngle, angleB, angleC);
}
public static bool IsInRange<T>(this T[] array, int index)
{
return index >= 0 && index < array.Length;
}
public static bool IsInRange<T>(this List<T> list, int index)
{
return index >= 0 && index < list.Count;
}
public static T[] Array<T>(params T[] items)
{
return items;
}
public static T[] VerifyLength<T>(this T[] array, int length)
{
if (array == null)
return new T[length];
else if (array.Length != length)
{
T[] newArray = new T[length];
for (int i = 0; i < Math.Min(length, array.Length); i++)
newArray[i] = array[i];
return newArray;
}
else
return array;
}
public static T[][] VerifyLength<T>(this T[][] array, int length0, int length1)
{
array = VerifyLength<T[]>(array, length0);
for (int i = 0; i < array.Length; i++)
array[i] = VerifyLength<T>(array[i], length1);
return array;
}
public static bool BetweenInterval(float val, float interval)
{
return val % (interval * 2) > interval;
}
public static bool OnInterval(float val, float prevVal, float interval)
{
return (int)(prevVal / interval) != (int)(val / interval);
}
#endregion
#region Vector2
public static Vector2 Toward(Vector2 from, Vector2 to, float length)
{
if (from == to)
return Vector2.Zero;
else
return (to - from).SafeNormalize(length);
}
public static Vector2 Perpendicular(this Vector2 vector)
{
return new Vector2(-vector.Y, vector.X);
}
public static float Angle(this Vector2 vector)
{
return (float)Math.Atan2(vector.Y, vector.X);
}
public static Vector2 Clamp(this Vector2 val, float minX, float minY, float maxX, float maxY)
{
return new Vector2(MathHelper.Clamp(val.X, minX, maxX), MathHelper.Clamp(val.Y, minY, maxY));
}
public static Vector2 Floor(this Vector2 val)
{
return new Vector2((int)Math.Floor(val.X), (int)Math.Floor(val.Y));
}
public static Vector2 Ceiling(this Vector2 val)
{
return new Vector2((int)Math.Ceiling(val.X), (int)Math.Ceiling(val.Y));
}
public static Vector2 Abs(this Vector2 val)
{
return new Vector2(Math.Abs(val.X), Math.Abs(val.Y));
}