PageRenderTime 30ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/Object/GridObject.cs

http://github.com/Concliff/Maze
C# | 257 lines | 119 code | 26 blank | 112 comment | 13 complexity | e12f25c2da516fe557d7e21a6f4a9ac6 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Maze.Classes
  6. {
  7. /// <summary>
  8. /// Specifies the type of object (or derived class) that an instance of the <see cref="GridObject"/> class represents.
  9. /// </summary>
  10. public enum GridObjectTypes
  11. {
  12. /// <summary>
  13. /// Default type. An <see cref="GridObject"/> instance is possibly had bad initialization.
  14. /// </summary>
  15. GridObject,
  16. /// <summary>
  17. /// An <see cref="GridObject"/> instance is <see cref="OozeDrop"/>.
  18. /// </summary>
  19. OozeDrop,
  20. /// <summary>
  21. /// An <see cref="GridObject"/> instance is <see cref="Portal"/>.
  22. /// </summary>
  23. Portal,
  24. /// <summary>
  25. /// An <see cref="GridObject"/> instance is <see cref="OozeBomb"/>.
  26. /// </summary>
  27. Bomb,
  28. /// <summary>
  29. /// An <see cref="GridObject"/> instance is <see cref="Slime"/>.
  30. /// </summary>
  31. Slime,
  32. /// <summary>
  33. /// An <see cref="GridObject"/> instance is <see cref="Bonus"/>.
  34. /// </summary>
  35. Bonus,
  36. /// <summary>
  37. /// An <see cref="GridObject"/> instance is <see cref="SmokeCloud"/>.
  38. /// </summary>
  39. SmokeCloud,
  40. };
  41. /// <summary>
  42. /// Defines the determinate state in which an instance of the <see cref="GridObject"/> class can be.
  43. /// </summary>
  44. public enum GridObjectStates : byte
  45. {
  46. /// <summary>
  47. /// An GridObject is "turned off". It is not located on map and cannot interact with units.
  48. /// </summary>
  49. Off,
  50. /// <summary>
  51. /// An GridObject is on the Map and ready to interact with units.
  52. /// </summary>
  53. Active,
  54. /// <summary>
  55. /// An GridObkect is temporary "turned off" and after the specified time (the activation time) the gridobject will be active again.
  56. /// </summary>
  57. Inactive,
  58. };
  59. /// <summary>
  60. /// Specified GridObject different behaviours.
  61. /// </summary>
  62. [Flags]
  63. public enum GridObjectFlags : uint
  64. {
  65. /// <summary>
  66. /// Units can interact with a GridObject (apply <see cref="GridObject.User"/> method).
  67. /// </summary>
  68. Usable = 0x001,
  69. /// <summary>
  70. /// Units can interact with a GridObject only once. After that the gridobject is marked as <see cref="ObjectStates.Removed"/>
  71. /// </summary>
  72. Disposable = 0x002,
  73. /// <summary>
  74. /// A GridObject applies effect at the nearest units within specified range.
  75. /// </summary>
  76. AreaEffect = 0x004,
  77. /// <summary>
  78. /// A GridObject cannot be switched to <see cref="GridObjectStates.Inactive"/> state. This gridobject is unlimited usable.
  79. /// </summary>
  80. AlwaysActive = 0x008,
  81. /// <summary>
  82. /// A GridIbject has its lifetime. When time has expired, object is marked as <see cref="ObjectStates.Removed"/>.
  83. /// </summary>
  84. Temporal = 0x010,
  85. };
  86. /// <summary>
  87. /// Contains information about <see cref="Effect"/> that a gridobject applies.
  88. /// </summary>
  89. public struct AreaEffect
  90. {
  91. /// <summary>
  92. /// <see cref="EffectEntry.ID"/>
  93. /// </summary>
  94. public ushort ID;
  95. /// <summary>
  96. /// A gridobject applies effect on unit within this range value.
  97. /// </summary>
  98. public int Range;
  99. }
  100. /// <summary>
  101. /// Specifies the base class for static objects on Map that are bound to the specific point. Provides a method for interacting units with these objects.
  102. /// </summary>
  103. public abstract class GridObject : Object
  104. {
  105. /// <summary>
  106. /// The GridObject lifetime remains. Uses only with <see cref="GridObjectFlags.Temporal"/>
  107. /// </summary>
  108. protected int timeToLive;
  109. /// <summary>
  110. /// The time that must elapsed before changing the gridobject state from <see cref="GridObjectStates.Inactive"/> to <see cref="GridObjectStates.Active"/>.
  111. /// </summary>
  112. protected int activationTime;
  113. /// <summary>
  114. /// Remaining time until the gridobject becomes active.
  115. /// </summary>
  116. protected int activationTimer;
  117. /// <summary>
  118. /// Indicates that the gridobject is in inactive state because of its recent interaction with an unit.
  119. /// </summary>
  120. protected bool recentlyUsed;
  121. /// <summary>
  122. /// Contains information about the effect that the gridObject is applying to the nearest units.
  123. /// </summary>
  124. protected AreaEffect areaEffect;
  125. /// <summary>
  126. /// GridObject flags set.
  127. /// </summary>
  128. protected GridObjectFlags gridObjectsFlags;
  129. /// <summary>
  130. /// GridObject type.
  131. /// </summary>
  132. protected GridObjectTypes gridObjectType;
  133. /// <summary>
  134. /// Initialized a new instance of the GridObject class.
  135. /// </summary>
  136. public GridObject()
  137. {
  138. pr_GridObjectState = GridObjectStates.Active;
  139. ObjectType = ObjectTypes.GridObject;
  140. this.gridObjectType = GridObjectTypes.GridObject;
  141. // Always in center of the cell
  142. Position = new GPS(Position, 25, 25);
  143. // Flags by default
  144. this.gridObjectsFlags = GridObjectFlags.Usable;
  145. }
  146. protected GridObjectStates pr_GridObjectState;
  147. /// <summary>
  148. /// Gets or sets the gridobject current state
  149. /// </summary>
  150. public GridObjectStates GridObjectState
  151. {
  152. get
  153. {
  154. return this.pr_GridObjectState;
  155. }
  156. set
  157. {
  158. // May not change state of disabled gridobjects
  159. if (this.pr_GridObjectState != GridObjectStates.Off)
  160. this.pr_GridObjectState = value;
  161. }
  162. }
  163. /// <summary>
  164. /// Gets the gridObject type.
  165. /// </summary>
  166. public GridObjectTypes GridObjectType
  167. {
  168. get
  169. {
  170. return this.gridObjectType;
  171. }
  172. }
  173. /// <summary>
  174. /// Gets a value indicating that the gridobject is in <see cref="GridObjectStates.Active"/> state.
  175. /// </summary>
  176. public bool IsActive
  177. {
  178. get
  179. {
  180. return GridObjectState == GridObjectStates.Active;
  181. }
  182. }
  183. /// <summary>
  184. /// Inidicating whether the gridobject has the specified flags.
  185. /// </summary>
  186. /// <param name="flags">Set of the flags to check. Multiple flags should be write as 'Flag1 | Flag2 | Flag3 ...'.</param>
  187. /// <returns><c>true</c> if the gridobject has all the checked flags; otherwise, <c>false</c>.</returns>
  188. public bool HasFlags(GridObjectFlags flags)
  189. {
  190. return (flags & this.gridObjectsFlags) == flags;
  191. }
  192. public override void UpdateState(int timeP)
  193. {
  194. // Update life timer for Temporal GO
  195. if (HasFlags(GridObjectFlags.Temporal))
  196. {
  197. if (timeToLive < timeP)
  198. ObjectState = ObjectStates.Removed;
  199. else
  200. timeToLive -= timeP;
  201. }
  202. // Update activation timer for inactive GO
  203. if (this.recentlyUsed && !HasFlags(GridObjectFlags.Disposable))
  204. {
  205. if (activationTimer < timeP)
  206. GridObjectState = GridObjectStates.Active;
  207. else
  208. activationTimer -= timeP;
  209. }
  210. // AreaEffect GO
  211. // Apply effect on the nearest units
  212. if (HasFlags(GridObjectFlags.AreaEffect) && areaEffect.ID != 0)
  213. {
  214. List<Unit> units = GetUnitsWithinRange(areaEffect.Range);
  215. foreach (Unit unit in units)
  216. unit.CastEffect(areaEffect.ID, unit);
  217. }
  218. base.UpdateState(timeP);
  219. }
  220. public virtual void Use(Unit user)
  221. {
  222. // Deactivate if needed
  223. if (!HasFlags(GridObjectFlags.AlwaysActive))
  224. {
  225. GridObjectState = GridObjectStates.Inactive;
  226. this.recentlyUsed = true;
  227. activationTimer = activationTime;
  228. }
  229. }
  230. }
  231. }