Int32Range.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Int32Range.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Text;
  12. using Microsoft.Xna.Framework.Content;
  13. #endregion
  14. namespace RolePlayingGameData
  15. {
  16. /// <summary>
  17. /// Defines a range of values, useful for generating values in that range.
  18. /// </summary>
  19. #if WINDOWS
  20. [Serializable]
  21. #endif
  22. public struct Int32Range
  23. {
  24. /// <summary>
  25. /// The minimum value for the range, inclusively.
  26. /// </summary>
  27. public int Minimum;
  28. /// <summary>
  29. /// The maximum value for the range, inclusively.
  30. /// </summary>
  31. public int Maximum;
  32. /// <summary>
  33. /// Calculate the average value returned by this range.
  34. /// </summary>
  35. public Int32 Average
  36. {
  37. get { return Minimum + Range / 2; }
  38. }
  39. /// <summary>
  40. /// Calculate the size of this range.
  41. /// </summary>
  42. public Int32 Range
  43. {
  44. get { return (Maximum - Minimum); }
  45. }
  46. #region Initialization
  47. /// <summary>
  48. /// Construct a new Int32Range object, from the given minimum and maximums.
  49. /// </summary>
  50. public Int32Range(Int32 minimum, Int32 maximum)
  51. {
  52. // check the parameters
  53. if (maximum > minimum)
  54. {
  55. throw new ArgumentException(
  56. "The minimum must be less than or equal to the maximum.");
  57. }
  58. // assign the parameters
  59. this.Minimum = minimum;
  60. this.Maximum = maximum;
  61. }
  62. #endregion
  63. #region Value Generation
  64. /// <summary>
  65. /// Generate a random value between the minimum and maximum, inclusively.
  66. /// </summary>
  67. /// <param name="random">The Random object used to generate the value.</param>
  68. public Int32 GenerateValue(Random random)
  69. {
  70. // check the parameters
  71. Random usedRandom = random;
  72. if (usedRandom == null)
  73. {
  74. usedRandom = new Random();
  75. }
  76. return usedRandom.Next(Minimum, Maximum);
  77. }
  78. #endregion
  79. #region Operator: Int32Range + Int32Range
  80. /// <summary>
  81. /// Add one range to another, piecewise, and return the result.
  82. /// </summary>
  83. public static Int32Range Add(Int32Range range1, Int32Range range2)
  84. {
  85. Int32Range outputRange;
  86. outputRange.Minimum = range1.Minimum + range2.Minimum;
  87. outputRange.Maximum = range1.Maximum + range2.Maximum;
  88. return outputRange;
  89. }
  90. /// <summary>
  91. /// Add one range to another, piecewise, and return the result.
  92. /// </summary>
  93. public static Int32Range operator +(Int32Range range1, Int32Range range2)
  94. {
  95. return Add(range1, range2);
  96. }
  97. #endregion
  98. #region Operator: Int32Range + Int32
  99. /// <summary>
  100. /// Add an Int32 to both the minimum and maximum values of the range.
  101. /// </summary>
  102. public static Int32Range Add(Int32Range range, Int32 amount)
  103. {
  104. Int32Range outputRange = range;
  105. outputRange.Minimum += amount;
  106. outputRange.Maximum += amount;
  107. return outputRange;
  108. }
  109. /// <summary>
  110. /// Add an Int32 to both the minimum and maximum values of the range.
  111. /// </summary>
  112. public static Int32Range operator +(Int32Range range, Int32 amount)
  113. {
  114. return Add(range, amount);
  115. }
  116. #endregion
  117. #region Operator: Int32Range - Int32Range
  118. /// <summary>
  119. /// Subtract one range from another, piecewise, and return the result.
  120. /// </summary>
  121. public static Int32Range Subtract(Int32Range range1, Int32Range range2)
  122. {
  123. Int32Range outputRange;
  124. outputRange.Minimum = range1.Minimum - range2.Minimum;
  125. outputRange.Maximum = range1.Maximum - range2.Maximum;
  126. return outputRange;
  127. }
  128. /// <summary>
  129. /// Subtract one range from another, piecewise, and return the result.
  130. /// </summary>
  131. public static Int32Range operator -(Int32Range range1, Int32Range range2)
  132. {
  133. return Subtract(range1, range2);
  134. }
  135. #endregion
  136. #region Operator: Int32Range - Int32
  137. /// <summary>
  138. /// Subtract an Int32 from both the minimum and maximum values of the range.
  139. /// </summary>
  140. public static Int32Range Subtract(Int32Range range, Int32 amount)
  141. {
  142. Int32Range outputRange = range;
  143. outputRange.Minimum -= amount;
  144. outputRange.Maximum -= amount;
  145. return outputRange;
  146. }
  147. /// <summary>
  148. /// Subtract an Int32 from both the minimum and maximum values of the range.
  149. /// </summary>
  150. public static Int32Range operator -(Int32Range range, Int32 amount)
  151. {
  152. return Subtract(range, amount);
  153. }
  154. #endregion
  155. // Compound assignment (+=, etc.) operators use the overloaded binary operators,
  156. // so there is no need in this case to override them explicitly
  157. #region String Output
  158. /// <summary>
  159. /// Builds a string that describes this object.
  160. /// </summary>
  161. public override string ToString()
  162. {
  163. StringBuilder sb = new StringBuilder("(");
  164. sb.Append(Minimum);
  165. sb.Append(',');
  166. sb.Append(Maximum);
  167. sb.Append(')');
  168. return sb.ToString();
  169. }
  170. #endregion
  171. #region Content Type Reader
  172. /// <summary>
  173. /// Read an Int32Range object from the content pipeline.
  174. /// </summary>
  175. public class Int32RangeReader : ContentTypeReader<Int32Range>
  176. {
  177. protected override Int32Range Read(ContentReader input,
  178. Int32Range existingInstance)
  179. {
  180. Int32Range output = existingInstance;
  181. output.Minimum = input.ReadInt32();
  182. output.Maximum = input.ReadInt32();
  183. return output;
  184. }
  185. }
  186. #endregion
  187. }
  188. }