You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Map.cs 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using Preparation.Interface;
  4. using Preparation.Utility;
  5. using System;
  6. using GameClass.GameObj;
  7. namespace GameClass.GameObj
  8. {
  9. public partial class Map : IMap
  10. {
  11. private readonly Dictionary<uint, XY> birthPointList; // 出生点列表
  12. public Dictionary<uint, XY> BirthPointList => birthPointList;
  13. private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
  14. public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
  15. private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
  16. public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
  17. public readonly uint[,] protoGameMap;
  18. public uint[,] ProtoGameMap => protoGameMap;
  19. public PlaceType GetPlaceType(IGameObj obj)
  20. {
  21. try
  22. {
  23. return (PlaceType)protoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  24. }
  25. catch
  26. {
  27. return PlaceType.Null;
  28. }
  29. }
  30. public PlaceType GetPlaceType(XY pos)
  31. {
  32. try
  33. {
  34. return (PlaceType)protoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
  35. }
  36. catch
  37. {
  38. return PlaceType.Null;
  39. }
  40. }
  41. public bool IsOutOfBound(IGameObj obj)
  42. {
  43. return obj.Position.x >= GameData.lengthOfMap - obj.Radius || obj.Position.x <= obj.Radius || obj.Position.y >= GameData.lengthOfMap - obj.Radius || obj.Position.y <= obj.Radius;
  44. }
  45. public IOutOfBound GetOutOfBound(XY pos)
  46. {
  47. return new OutOfBoundBlock(pos);
  48. }
  49. public Character? FindPlayer(long playerID)
  50. {
  51. Character? player = null;
  52. gameObjLockDict[GameObjType.Character].EnterReadLock();
  53. try
  54. {
  55. foreach (Character person in gameObjDict[GameObjType.Character])
  56. {
  57. if (playerID == person.ID)
  58. {
  59. player = person;
  60. break;
  61. }
  62. }
  63. }
  64. finally
  65. {
  66. gameObjLockDict[GameObjType.Character].ExitReadLock();
  67. }
  68. return player;
  69. }
  70. public bool Remove(GameObj gameObj)
  71. {
  72. bool flag = false;
  73. GameObjLockDict[gameObj.Type].EnterWriteLock();
  74. try
  75. {
  76. foreach (GameObj obj in GameObjDict[gameObj.Type])
  77. {
  78. if (gameObj.ID == obj.ID)
  79. {
  80. GameObjDict[gameObj.Type].Remove(obj);
  81. flag = true;
  82. break;
  83. }
  84. }
  85. }
  86. finally
  87. {
  88. GameObjLockDict[gameObj.Type].ExitWriteLock();
  89. }
  90. return flag;
  91. }
  92. public void Add(GameObj gameObj)
  93. {
  94. GameObjLockDict[gameObj.Type].EnterWriteLock();
  95. try
  96. {
  97. GameObjDict[gameObj.Type].Add(gameObj);
  98. }
  99. finally
  100. {
  101. GameObjLockDict[gameObj.Type].ExitWriteLock();
  102. }
  103. }
  104. public Map(uint[,] mapResource)
  105. {
  106. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  107. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  108. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  109. {
  110. if (idx != GameObjType.Null)
  111. {
  112. gameObjDict.Add(idx, new List<IGameObj>());
  113. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  114. }
  115. }
  116. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  117. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  118. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  119. for (int i = 0; i < GameData.rows; ++i)
  120. {
  121. for (int j = 0; j < GameData.cols; ++j)
  122. {
  123. switch (mapResource[i, j])
  124. {
  125. case (uint)PlaceType.Wall:
  126. {
  127. GameObjLockDict[GameObjType.Wall].EnterWriteLock();
  128. try
  129. {
  130. GameObjDict[GameObjType.Wall].Add(new Wall(GameData.GetCellCenterPos(i, j)));
  131. }
  132. finally
  133. {
  134. GameObjLockDict[GameObjType.Wall].ExitWriteLock();
  135. }
  136. break;
  137. }
  138. case (uint)PlaceType.Doorway:
  139. {
  140. GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  141. try
  142. {
  143. GameObjDict[GameObjType.Doorway].Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  144. }
  145. finally
  146. {
  147. GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  148. }
  149. break;
  150. }
  151. case (uint)PlaceType.EmergencyExit:
  152. {
  153. GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  154. try
  155. {
  156. GameObjDict[GameObjType.EmergencyExit].Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  157. }
  158. finally
  159. {
  160. GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  161. }
  162. break;
  163. }
  164. case (uint)PlaceType.Generator:
  165. {
  166. GameObjLockDict[GameObjType.Generator].EnterWriteLock();
  167. try
  168. {
  169. GameObjDict[GameObjType.Generator].Add(new Generator(GameData.GetCellCenterPos(i, j)));
  170. }
  171. finally
  172. {
  173. GameObjLockDict[GameObjType.Generator].ExitWriteLock();
  174. }
  175. break;
  176. }
  177. case (uint)PlaceType.BirthPoint1:
  178. case (uint)PlaceType.BirthPoint2:
  179. case (uint)PlaceType.BirthPoint3:
  180. case (uint)PlaceType.BirthPoint4:
  181. case (uint)PlaceType.BirthPoint5:
  182. {
  183. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }