/KTYD/KTYD/gArray.cs

https://github.com/gegg2/Zombie-Wars · C# · 320 lines · 178 code · 84 blank · 58 comment · 20 complexity · a22040b2ad6c7cef63a0ff595d12d262 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using Microsoft.Xna.Framework;
  7. using System.Diagnostics;
  8. namespace KTYD
  9. {
  10. /// <summary>
  11. /// Splits the playing area up into small squares of n*n pixels with each entity being stored in their corresponding square
  12. /// This should make collision detection faster
  13. /// </summary>
  14. public class gArray
  15. {
  16. Dictionary<Entity, intVector> eLocs;
  17. List<Entity>[,] containers;
  18. int xGrids = 0; //Number of grids in the xDirection
  19. int yGrids = 0; //Number of grids in the yDirection
  20. int pSize;
  21. int iterX = 0;
  22. int iterY = 0;
  23. /// <summary>
  24. /// Constructor
  25. /// </summary>
  26. /// <param name="x">x-size of playing area</param>
  27. /// <param name="y">y-size of playing area</param>
  28. public gArray(int xSize, int ySize)
  29. {
  30. int p = GameConfig.GRID_PARTITION_SIZE;
  31. eLocs = new Dictionary<Entity, intVector>();
  32. xGrids = 12 + xSize / p;
  33. yGrids = 12 + Math.Abs(ySize) / p;
  34. pSize = p;
  35. containers = new List<Entity>[xGrids, yGrids];
  36. for (int x = 0; x < xGrids; x++)
  37. {
  38. for (int y = 0; y < yGrids; y++)
  39. {
  40. List<Entity> add = new List<Entity>();
  41. containers[x, y] = add;
  42. //containers[x,y].Add(null);
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// Returns the grid location of a vector
  48. /// </summary>
  49. /// <param name="v">Location vector</param>
  50. public intVector gridLoc(Vector2 v)
  51. {
  52. return new intVector(v.X / pSize, v.Y / pSize);
  53. }
  54. /// <summary>
  55. /// Adds an entity to the gArray
  56. /// </summary>
  57. /// <param name="e">Entity to add to map</param>
  58. public void Add(Entity e)
  59. {
  60. intVector gLoc = gridLoc(e.Center);
  61. eLocs.Add(e, gLoc);
  62. if (containers[gLoc.getX(), gLoc.getY()] == null)
  63. {
  64. List<Entity> a = new List<Entity>();
  65. a.Add(e);
  66. containers[gLoc.getX(), gLoc.getY()] = a;
  67. }
  68. else
  69. {
  70. containers[gLoc.getX(), gLoc.getY()].Add(e);
  71. }
  72. }
  73. /// <summary>
  74. /// Removes an entity from the map
  75. /// </summary>
  76. /// <param name="e">Entity to be removed</param>
  77. public void Remove(Entity e)
  78. {
  79. intVector eLoc = eLocs[e];
  80. containers[eLoc.getX(), eLoc.getY()].Remove(e);
  81. eLocs.Remove(e);
  82. }
  83. /// <summary>
  84. /// Starts the iteration
  85. /// </summary>
  86. public void startIteration()
  87. {
  88. iterX = 0;
  89. iterY = 0;
  90. }
  91. /// <summary>
  92. /// Can the array iterate over more entities
  93. /// </summary>
  94. public Boolean hasNext()
  95. {
  96. if (iterY >= yGrids)
  97. {
  98. return false;
  99. }
  100. return true;
  101. }
  102. /// <summary>
  103. /// Moves the iterator to the next grid and returns the entities in it
  104. /// </summary>
  105. public List<Entity> returnNext()
  106. {
  107. List<Entity> returnList = containers[(int)iterX, (int)iterY];
  108. iterX = iterX + 1;
  109. if (iterX == xGrids)
  110. {
  111. iterX = 0;
  112. iterY = iterY + 1;
  113. }
  114. return returnList;
  115. }
  116. /// <summary>
  117. /// Adds a list of entities to the array
  118. /// </summary>
  119. /// <param name="addList">Entities to be added</param>
  120. public void AddRange(List<Entity> addList)
  121. {
  122. foreach (Entity e in addList)
  123. {
  124. Add(e);
  125. }
  126. }
  127. /// <summary>
  128. /// Gets the xLocation of the current iterator
  129. /// Im not sure if this is necessary.
  130. /// </summary>
  131. public int getIterationX()
  132. {
  133. return iterX;
  134. }
  135. /// <summary>
  136. /// Gets the yLocation of the current iterator
  137. /// Im not sure if this is necessary.
  138. /// </summary>
  139. public int getIterationY()
  140. {
  141. return iterY;
  142. }
  143. /// <summary>
  144. /// Does the array contain an entity
  145. /// </summary>
  146. /// <param name="e">An entity</param>
  147. public bool Contains(Entity e)
  148. {
  149. return eLocs.ContainsKey(e);
  150. }
  151. /// <summary>
  152. /// Is the entity in the correct grid
  153. /// </summary>
  154. /// <param name="e">An entity</param>
  155. public bool inCorrectPlace(Entity e)
  156. {
  157. int x = (int)(e.Center.X) / pSize;
  158. int y = (int)(e.Center.Y) / pSize;
  159. if (containers[x, y].Contains(e) == false)
  160. {
  161. return false;
  162. }
  163. return true;
  164. }
  165. /// <summary>
  166. /// Makes sure that entities are in the correct location
  167. /// </summary>
  168. public void updateGridLocs()
  169. {
  170. startIteration();
  171. List<Entity> toBeMoved = new List<Entity>();
  172. while (true)
  173. {
  174. if (hasNext() == false)
  175. {
  176. break;
  177. }
  178. List<Entity> eList = returnNext();
  179. foreach (Entity e in eList)
  180. {
  181. if (inCorrectPlace(e) == false)
  182. {
  183. toBeMoved.Add(e);
  184. }
  185. }
  186. }
  187. foreach (Entity e in toBeMoved)
  188. {
  189. intVector eLoc = eLocs[e];
  190. containers[eLoc.getX(), eLoc.getY()].Remove(e);
  191. eLocs.Remove(e);
  192. intVector newLoc = gridLoc(e.Center);
  193. eLocs.Add(e, newLoc);
  194. containers[newLoc.getX(), newLoc.getY()].Add(e);
  195. }
  196. }
  197. /// <summary>
  198. /// Gets a list of entities close to a particular entitiy
  199. /// </summary>
  200. /// <param name="addList">Any entity</param>
  201. public List<Entity> getNearbycontainers(Entity e)
  202. {
  203. intVector gLoc = gridLoc(e.Center);
  204. int x = gLoc.getX();
  205. int y = gLoc.getY();
  206. List<Entity> nearbycontainers = new List<Entity>();
  207. for (int i = -1; i < 2; ++i)
  208. {
  209. if (x + i >= xGrids)
  210. {
  211. break;
  212. }
  213. while (x + i < 0)
  214. {
  215. i++;
  216. }
  217. for (int j = -1; j < 2; j++)
  218. {
  219. while (y + j < 0)
  220. {
  221. j++;
  222. }
  223. if (y + j >= yGrids)
  224. {
  225. break;
  226. }
  227. foreach (Entity en in containers[x + i, y + j])
  228. {
  229. nearbycontainers.Add(en);
  230. }
  231. }
  232. }
  233. return nearbycontainers;
  234. }
  235. }
  236. }