Range.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. namespace DunGen
  3. {
  4. /**
  5. * A series of classes for getting a random value between a given range
  6. */
  7. [Serializable]
  8. public class IntRange
  9. {
  10. public int Min;
  11. public int Max;
  12. public IntRange() { }
  13. public IntRange(int min, int max)
  14. {
  15. Min = min;
  16. Max = max;
  17. }
  18. public int GetRandom(RandomStream random)
  19. {
  20. if (Min > Max)
  21. Max = Min;
  22. return random.Next(Min, Max + 1);
  23. }
  24. public override string ToString()
  25. {
  26. return Min + " - " + Max;
  27. }
  28. }
  29. [Serializable]
  30. public class FloatRange
  31. {
  32. public float Min;
  33. public float Max;
  34. public FloatRange() { }
  35. public FloatRange(float min, float max)
  36. {
  37. Min = min;
  38. Max = max;
  39. }
  40. public float GetRandom(RandomStream random)
  41. {
  42. if (Min > Max)
  43. {
  44. float temp = Min;
  45. Min = Max;
  46. Max = temp;
  47. }
  48. float range = Max - Min;
  49. return Min + ((float)random.NextDouble() * range);
  50. }
  51. }
  52. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  53. public sealed class FloatRangeLimitAttribute : Attribute
  54. {
  55. public float MinLimit { get; private set; }
  56. public float MaxLimit { get; private set; }
  57. public FloatRangeLimitAttribute(float minLimit, float maxLimit)
  58. {
  59. MinLimit = minLimit;
  60. MaxLimit = maxLimit;
  61. }
  62. }
  63. }