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.6 kB

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