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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 object lockForNum = new();
  14. private uint numOfRepairedGenerators = 0;
  15. public uint NumOfRepairedGenerators
  16. {
  17. get => numOfRepairedGenerators;
  18. set
  19. {
  20. lock (lockForNum)
  21. numOfRepairedGenerators = value;
  22. }
  23. }
  24. private uint numOfSurvivingStudent = GameData.numOfStudent;
  25. public uint NumOfSurvivingStudent
  26. {
  27. get => numOfSurvivingStudent;
  28. set
  29. {
  30. lock (lockForNum)
  31. numOfSurvivingStudent = value;
  32. }
  33. }
  34. private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
  35. public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
  36. private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
  37. public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
  38. public readonly uint[,] protoGameMap;
  39. public uint[,] ProtoGameMap => protoGameMap;
  40. public PlaceType GetPlaceType(IGameObj obj)
  41. {
  42. try
  43. {
  44. return (PlaceType)protoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  45. }
  46. catch
  47. {
  48. return PlaceType.Null;
  49. }
  50. }
  51. public PlaceType GetPlaceType(XY pos)
  52. {
  53. try
  54. {
  55. return (PlaceType)protoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
  56. }
  57. catch
  58. {
  59. return PlaceType.Null;
  60. }
  61. }
  62. public bool IsOutOfBound(IGameObj obj)
  63. {
  64. 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;
  65. }
  66. public IOutOfBound GetOutOfBound(XY pos)
  67. {
  68. return new OutOfBoundBlock(pos);
  69. }
  70. public Character? FindPlayer(long playerID)
  71. {
  72. Character? player = null;
  73. gameObjLockDict[GameObjType.Character].EnterReadLock();
  74. try
  75. {
  76. foreach (Character person in gameObjDict[GameObjType.Character])
  77. {
  78. if (playerID == person.ID)
  79. {
  80. player = person;
  81. break;
  82. }
  83. }
  84. }
  85. finally
  86. {
  87. gameObjLockDict[GameObjType.Character].ExitReadLock();
  88. }
  89. return player;
  90. }
  91. public bool Remove(GameObj gameObj)
  92. {
  93. bool flag = false;
  94. GameObjLockDict[gameObj.Type].EnterWriteLock();
  95. try
  96. {
  97. foreach (GameObj obj in GameObjDict[gameObj.Type])
  98. {
  99. if (gameObj.ID == obj.ID)
  100. {
  101. GameObjDict[gameObj.Type].Remove(obj);
  102. flag = true;
  103. break;
  104. }
  105. }
  106. }
  107. finally
  108. {
  109. GameObjLockDict[gameObj.Type].ExitWriteLock();
  110. }
  111. return flag;
  112. }
  113. public bool RemoveJustFromMap(GameObj gameObj)
  114. {
  115. GameObjLockDict[gameObj.Type].EnterWriteLock();
  116. try
  117. {
  118. return GameObjDict[gameObj.Type].Remove(gameObj);
  119. }
  120. finally
  121. {
  122. GameObjLockDict[gameObj.Type].ExitWriteLock();
  123. }
  124. }
  125. public void Add(GameObj gameObj)
  126. {
  127. GameObjLockDict[gameObj.Type].EnterWriteLock();
  128. try
  129. {
  130. GameObjDict[gameObj.Type].Add(gameObj);
  131. }
  132. finally
  133. {
  134. GameObjLockDict[gameObj.Type].ExitWriteLock();
  135. }
  136. }
  137. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  138. {
  139. GameObj? GameObjForInteract = null;
  140. GameObjLockDict[gameObjType].EnterReadLock();
  141. try
  142. {
  143. foreach (GameObj gameObj in GameObjDict[gameObjType])
  144. {
  145. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  146. {
  147. GameObjForInteract = gameObj;
  148. break;
  149. }
  150. }
  151. }
  152. finally
  153. {
  154. GameObjLockDict[gameObjType].ExitReadLock();
  155. }
  156. return GameObjForInteract;
  157. }
  158. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  159. {
  160. GameObj? GameObjForInteract = null;
  161. GameObjLockDict[gameObjType].EnterReadLock();
  162. try
  163. {
  164. foreach (GameObj gameObj in GameObjDict[gameObjType])
  165. {
  166. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  167. {
  168. GameObjForInteract = gameObj;
  169. break;
  170. }
  171. }
  172. }
  173. finally
  174. {
  175. GameObjLockDict[gameObjType].ExitReadLock();
  176. }
  177. return GameObjForInteract;
  178. }
  179. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  180. {
  181. GameObj? GameObjForInteract = null;
  182. GameObjLockDict[gameObjType].EnterReadLock();
  183. try
  184. {
  185. foreach (GameObj gameObj in GameObjDict[gameObjType])
  186. {
  187. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  188. {
  189. GameObjForInteract = gameObj;
  190. break;
  191. }
  192. }
  193. }
  194. finally
  195. {
  196. GameObjLockDict[gameObjType].ExitReadLock();
  197. }
  198. return GameObjForInteract;
  199. }
  200. public Map(uint[,] mapResource)
  201. {
  202. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  203. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  204. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  205. {
  206. if (idx != GameObjType.Null)
  207. {
  208. gameObjDict.Add(idx, new List<IGameObj>());
  209. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  210. }
  211. }
  212. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  213. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  214. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  215. for (int i = 0; i < GameData.rows; ++i)
  216. {
  217. for (int j = 0; j < GameData.cols; ++j)
  218. {
  219. switch (mapResource[i, j])
  220. {
  221. case (uint)PlaceType.Wall:
  222. {
  223. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  224. break;
  225. }
  226. case (uint)PlaceType.Doorway:
  227. {
  228. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  229. break;
  230. }
  231. case (uint)PlaceType.EmergencyExit:
  232. {
  233. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  234. break;
  235. }
  236. case (uint)PlaceType.Generator:
  237. {
  238. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  239. break;
  240. }
  241. case (uint)PlaceType.Chest:
  242. {
  243. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  244. break;
  245. }
  246. case (uint)PlaceType.Door3:
  247. {
  248. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  249. break;
  250. }
  251. case (uint)PlaceType.Door5:
  252. {
  253. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  254. break;
  255. }
  256. case (uint)PlaceType.Door6:
  257. {
  258. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  259. break;
  260. }
  261. case (uint)PlaceType.Window:
  262. {
  263. Add(new Window(GameData.GetCellCenterPos(i, j)));
  264. break;
  265. }
  266. case (uint)PlaceType.BirthPoint1:
  267. case (uint)PlaceType.BirthPoint2:
  268. case (uint)PlaceType.BirthPoint3:
  269. case (uint)PlaceType.BirthPoint4:
  270. case (uint)PlaceType.BirthPoint5:
  271. {
  272. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  273. break;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }