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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 bool RemoveJustFromMap(GameObj gameObj)
  93. {
  94. GameObjLockDict[gameObj.Type].EnterWriteLock();
  95. try
  96. {
  97. return GameObjDict[gameObj.Type].Remove(gameObj);
  98. }
  99. finally
  100. {
  101. GameObjLockDict[gameObj.Type].ExitWriteLock();
  102. }
  103. }
  104. public void Add(GameObj gameObj)
  105. {
  106. GameObjLockDict[gameObj.Type].EnterWriteLock();
  107. try
  108. {
  109. GameObjDict[gameObj.Type].Add(gameObj);
  110. }
  111. finally
  112. {
  113. GameObjLockDict[gameObj.Type].ExitWriteLock();
  114. }
  115. }
  116. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  117. {
  118. GameObj? GameObjForInteract = null;
  119. GameObjLockDict[gameObjType].EnterReadLock();
  120. try
  121. {
  122. foreach (GameObj gameObj in GameObjDict[gameObjType])
  123. {
  124. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  125. {
  126. GameObjForInteract = gameObj;
  127. break;
  128. }
  129. }
  130. }
  131. finally
  132. {
  133. GameObjLockDict[gameObjType].ExitReadLock();
  134. }
  135. return GameObjForInteract;
  136. }
  137. public Map(uint[,] mapResource)
  138. {
  139. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  140. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  141. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  142. {
  143. if (idx != GameObjType.Null)
  144. {
  145. gameObjDict.Add(idx, new List<IGameObj>());
  146. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  147. }
  148. }
  149. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  150. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  151. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  152. for (int i = 0; i < GameData.rows; ++i)
  153. {
  154. for (int j = 0; j < GameData.cols; ++j)
  155. {
  156. switch (mapResource[i, j])
  157. {
  158. case (uint)PlaceType.Wall:
  159. {
  160. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  161. break;
  162. }
  163. case (uint)PlaceType.Doorway:
  164. {
  165. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  166. break;
  167. }
  168. case (uint)PlaceType.EmergencyExit:
  169. {
  170. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  171. break;
  172. }
  173. case (uint)PlaceType.Generator:
  174. {
  175. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  176. break;
  177. }
  178. case (uint)PlaceType.Chest:
  179. {
  180. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  181. break;
  182. }
  183. case (uint)PlaceType.BirthPoint1:
  184. case (uint)PlaceType.BirthPoint2:
  185. case (uint)PlaceType.BirthPoint3:
  186. case (uint)PlaceType.BirthPoint4:
  187. case (uint)PlaceType.BirthPoint5:
  188. {
  189. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }