PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/Object/Slug.cs

http://github.com/Concliff/Maze
C# | 306 lines | 203 code | 39 blank | 64 comment | 27 complexity | 4593dd44e90d93b43dcb07e567722a1c 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. /// Represents the unit that is controlled by a player.
  9. /// </summary>
  10. public class Slug : Unit
  11. {
  12. /// <summary>
  13. /// Max value of energy the Slug may have.
  14. /// </summary>
  15. public const int MaxOozeEnergy = 100;
  16. private int downTime; // stand still time
  17. private int travelTime; // in motion time
  18. private bool isInMotion;
  19. /// <summary>
  20. /// Total game score of this Slug
  21. /// </summary>
  22. private int score;
  23. /// <summary>
  24. /// Count of Drops that Player has collected at current level.
  25. /// </summary>
  26. private int collectedDropsCount;
  27. /// <summary>
  28. /// Initialized a new instance of the Slug class.
  29. /// </summary>
  30. public Slug()
  31. {
  32. Name = "Noname";
  33. ObjectType = ObjectTypes.Slug;
  34. this.unitType = UnitTypes.Slug;
  35. this.unitSide = UnitSides.Good;
  36. // Home Poisition will be initialized after the Level loading.
  37. this.respawnTimer = 3000;
  38. OozeEnergy = MaxOozeEnergy;
  39. objectSize.Width = GlobalConstants.PLAYER_SIZE_WIDTH;
  40. objectSize.Height = GlobalConstants.PLAYER_SIZE_HEIGHT;
  41. BaseSpeed = 0.7d;
  42. this.motionMaster = new ManualMovement(this);
  43. }
  44. private String pr_Name;
  45. /// <summary>
  46. /// Gets or Sets Player's name
  47. /// </summary>
  48. public String Name
  49. {
  50. get { return pr_Name; }
  51. set { pr_Name = value; }
  52. }
  53. /// <summary>
  54. /// Gets total game score of this Slug.
  55. /// </summary>
  56. public int Score { get { return this.score; } }
  57. /// <summary>
  58. /// Gets Count of Drops that this Slug has collected at current Level.
  59. /// </summary>
  60. public int CollectedDropsCount { get { return this.collectedDropsCount; } }
  61. private int pr_oozeEnergy;
  62. /// <summary>
  63. /// Gets or Sets Slug's Energy supply.
  64. /// </summary>
  65. public int OozeEnergy
  66. {
  67. get { return pr_oozeEnergy; }
  68. set
  69. {
  70. if (value > MaxOozeEnergy)
  71. pr_oozeEnergy = MaxOozeEnergy;
  72. else if (value <= 0)
  73. {
  74. pr_oozeEnergy = 0;
  75. // Cast Deslimation when energy ends
  76. CastEffect(4, this);
  77. }
  78. else
  79. pr_oozeEnergy = value;
  80. }
  81. }
  82. /// <summary>
  83. /// Registers a Slug in <see cref="ObjectContainer"/>. (Overrides <see cref="Object.Create"/>.)
  84. /// </summary>
  85. public override void Create()
  86. {
  87. base.Create();
  88. // Bind Player's effect events to Game Form
  89. // Needed to display auras at AuraBar
  90. effectList.EffectApplied += new EffectCollection.EffectHandler(World.PlayForm.OnEffectApplied);
  91. effectList.EffectRemoved += new EffectCollection.EffectHandler(World.PlayForm.OnEffectRemoved);
  92. }
  93. public override void UpdateState(int timeP)
  94. {
  95. // Slug is moving:
  96. // take 2 OozeEnergy every second
  97. if (isInMotion && GetEffectsByType(EffectTypes.Replenishment).Count == 0) // or id under Replenishment effect
  98. {
  99. travelTime += timeP;
  100. if (travelTime > 1000) // 1 second of motion
  101. {
  102. travelTime -= 1000;
  103. OozeEnergy -= 2;
  104. }
  105. isInMotion = false;
  106. }
  107. // Slug is standing still:
  108. // give 1 (3 at Start Point) OozeEnergy every second
  109. else
  110. {
  111. downTime += timeP;
  112. if (downTime > 1000) // not in motion over 1 seconds
  113. {
  114. downTime -= 1000;
  115. OozeEnergy += IsAtHome ? 3 : 1;
  116. }
  117. }
  118. MovementAction(timeP);
  119. base.UpdateState(timeP);
  120. }
  121. public override void SetDeathState(DeathStates deathState)
  122. {
  123. if (deathState == DeathStates.Dead)
  124. {
  125. respawnTimer = 3000;
  126. }
  127. base.SetDeathState(deathState);
  128. }
  129. /// <summary>
  130. /// Player Moving Handler
  131. /// </summary>
  132. /// <param name="MoveType">Flags of direction</param>
  133. private void MovementAction(int timeP)
  134. {
  135. if (!IsAlive)
  136. return;
  137. if (HasEffectType(EffectTypes.Root))
  138. return;
  139. GPS previousPosition = Position;
  140. // Find a point in currectDirection + searchingStep
  141. // to determine whether Slug is affected by Slime speed-up boost
  142. GPS searchingPoint = Position;
  143. int searchingStep = 10;
  144. List<GridObject> slimeAround;
  145. bool slimePersist = false;
  146. switch (this.motionMaster.CurrentDirection.First)
  147. {
  148. case Directions.Right:
  149. searchingPoint.X = Position.X + searchingStep;
  150. if (searchingPoint.X > GlobalConstants.CELL_WIDTH)
  151. {
  152. searchingPoint.X -= GlobalConstants.CELL_WIDTH;
  153. ++searchingPoint.Location.X;
  154. }
  155. break;
  156. case Directions.Left:
  157. searchingPoint.X = Position.X - searchingStep;
  158. if (searchingPoint.X < 0)
  159. {
  160. searchingPoint.X -= GlobalConstants.CELL_WIDTH;
  161. --searchingPoint.Location.X;
  162. }
  163. break;
  164. case Directions.Up:
  165. searchingPoint.Y = Position.Y - searchingStep;
  166. if (searchingPoint.Y < 0)
  167. {
  168. searchingPoint.Y += GlobalConstants.CELL_HEIGHT;
  169. --searchingPoint.Location.Y;
  170. }
  171. break;
  172. case Directions.Down:
  173. searchingPoint.Y = Position.Y + searchingStep;
  174. if (searchingPoint.Y > GlobalConstants.CELL_HEIGHT)
  175. {
  176. searchingPoint.Y -= GlobalConstants.CELL_HEIGHT;
  177. ++searchingPoint.Location.Y;
  178. }
  179. break;
  180. }
  181. // try to find any Slime around that point
  182. slimeAround = ObjectSearcher.GetGridObjectsInArea(searchingPoint, searchingStep);
  183. foreach (GridObject slime in slimeAround)
  184. {
  185. if (slime.GridObjectType == GridObjectTypes.Slime)
  186. slimePersist = true;
  187. }
  188. // Increase speed with an Effect "Viscous Slime - Slug"
  189. if (slimePersist)
  190. CastEffect(15, this);
  191. this.motionMaster.UpdateState(timeP);
  192. // Check movement occurrence
  193. if (previousPosition != Position)
  194. {
  195. this.isInMotion = true;
  196. //create slime at old position
  197. Slime slime = new Slime();
  198. slime.Create(previousPosition);
  199. }
  200. else
  201. {
  202. this.isInMotion = false;
  203. }
  204. }
  205. public void LevelChanged()
  206. {
  207. this.respawnLocation = Map.Instance.StartLocation;
  208. Position = new GPS(Home, 25, 25);
  209. }
  210. /// <summary>
  211. /// Handles process of picking up a <see cref="OozeDrop"/> object.
  212. /// </summary>
  213. /// <param name="drop"></param>
  214. public void CollectDrop(OozeDrop drop)
  215. {
  216. AddPoints(10);
  217. OozeEnergy += 10;
  218. Map.Instance.CollectDrop(drop);
  219. ++collectedDropsCount;
  220. }
  221. /// <summary>
  222. /// Adds points to Total <see cref="Slug.Score"/>
  223. /// </summary>
  224. /// <param name="points"></param>
  225. public void AddPoints(int points) { this.score += points; }
  226. /// <summary>
  227. /// Handles process of picking up a hidden bonus on map.
  228. /// </summary>
  229. /// <param name="effectID"><see cref="EffectEntry.ID"/> of the picking effect.</param>
  230. public void CollectHiddenBonus(ushort effectID)
  231. {
  232. EffectEntry effectEntry = DBStores.EffectStore[effectID];
  233. if(effectEntry.HasAttribute(EffectAttributes.CanBeSpell))
  234. {
  235. World.PlayForm.AddSpell(effectEntry);
  236. }
  237. else
  238. {
  239. Effect effect = new Effect(effectEntry, this, this);
  240. effect.Cast();
  241. }
  242. }
  243. /// <summary>
  244. /// Create an exact copy of the Slug at the same position and moving in the same direction as the original.
  245. /// </summary>
  246. public void CreateClone()
  247. {
  248. SlugClone clone = new SlugClone();
  249. clone.Create(Position, this.motionMaster.CurrentDirection);
  250. }
  251. protected override void UnitCollisionEnds(Unit unit)
  252. {
  253. if (this.HasEffectType(EffectTypes.Shield))
  254. {
  255. EffectHolder holder = this.effectList.GetHolder(11);
  256. if (holder != null)
  257. RemoveEffect(holder);
  258. }
  259. }
  260. protected override void UnitCollision(Unit unit)
  261. {
  262. // Do not kill with shield effect
  263. if (!this.HasEffectType(EffectTypes.Shield))
  264. unit.KillUnit(this);
  265. }
  266. }
  267. }