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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. {
  22. numOfRepairedGenerators = value;
  23. }
  24. }
  25. }
  26. private uint numOfDeceasedStudent = 0;
  27. public uint NumOfDeceasedStudent
  28. {
  29. get => numOfDeceasedStudent;
  30. set
  31. {
  32. lock (lockForNum)
  33. {
  34. numOfDeceasedStudent = value;
  35. if (numOfDeceasedStudent + numOfEscapedStudent == GameData.numOfStudent)
  36. {
  37. Timer.IsGaming = false;
  38. }
  39. }
  40. }
  41. }
  42. private uint numOfEscapedStudent = 0;
  43. public uint NumOfEscapedStudent
  44. {
  45. get => numOfEscapedStudent;
  46. set
  47. {
  48. lock (lockForNum)
  49. {
  50. numOfEscapedStudent = value;
  51. if (numOfDeceasedStudent + numOfEscapedStudent == GameData.numOfStudent)
  52. {
  53. Timer.IsGaming = false;
  54. }
  55. }
  56. }
  57. }
  58. private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
  59. public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
  60. private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
  61. public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
  62. public readonly uint[,] protoGameMap;
  63. public uint[,] ProtoGameMap => protoGameMap;
  64. public PlaceType GetPlaceType(IGameObj obj)
  65. {
  66. try
  67. {
  68. return (PlaceType)protoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  69. }
  70. catch
  71. {
  72. return PlaceType.Null;
  73. }
  74. }
  75. public PlaceType GetPlaceType(XY pos)
  76. {
  77. try
  78. {
  79. return (PlaceType)protoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
  80. }
  81. catch
  82. {
  83. return PlaceType.Null;
  84. }
  85. }
  86. public bool IsOutOfBound(IGameObj obj)
  87. {
  88. 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;
  89. }
  90. public IOutOfBound GetOutOfBound(XY pos)
  91. {
  92. return new OutOfBoundBlock(pos);
  93. }
  94. public Character? FindPlayer(long playerID)
  95. {
  96. Character? player = null;
  97. gameObjLockDict[GameObjType.Character].EnterReadLock();
  98. try
  99. {
  100. foreach (Character person in gameObjDict[GameObjType.Character])
  101. {
  102. if (playerID == person.ID)
  103. {
  104. player = person;
  105. break;
  106. }
  107. }
  108. }
  109. finally
  110. {
  111. gameObjLockDict[GameObjType.Character].ExitReadLock();
  112. }
  113. return player;
  114. }
  115. public bool Remove(GameObj gameObj)
  116. {
  117. bool flag = false;
  118. GameObjLockDict[gameObj.Type].EnterWriteLock();
  119. try
  120. {
  121. foreach (GameObj obj in GameObjDict[gameObj.Type])
  122. {
  123. if (gameObj.ID == obj.ID)
  124. {
  125. GameObjDict[gameObj.Type].Remove(obj);
  126. flag = true;
  127. break;
  128. }
  129. }
  130. }
  131. finally
  132. {
  133. GameObjLockDict[gameObj.Type].ExitWriteLock();
  134. }
  135. return flag;
  136. }
  137. public bool RemoveJustFromMap(GameObj gameObj)
  138. {
  139. GameObjLockDict[gameObj.Type].EnterWriteLock();
  140. try
  141. {
  142. return GameObjDict[gameObj.Type].Remove(gameObj);
  143. }
  144. finally
  145. {
  146. GameObjLockDict[gameObj.Type].ExitWriteLock();
  147. }
  148. }
  149. public void Add(GameObj gameObj)
  150. {
  151. GameObjLockDict[gameObj.Type].EnterWriteLock();
  152. try
  153. {
  154. GameObjDict[gameObj.Type].Add(gameObj);
  155. }
  156. finally
  157. {
  158. GameObjLockDict[gameObj.Type].ExitWriteLock();
  159. }
  160. }
  161. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  162. {
  163. GameObj? GameObjForInteract = null;
  164. GameObjLockDict[gameObjType].EnterReadLock();
  165. try
  166. {
  167. foreach (GameObj gameObj in GameObjDict[gameObjType])
  168. {
  169. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  170. {
  171. GameObjForInteract = gameObj;
  172. break;
  173. }
  174. }
  175. }
  176. finally
  177. {
  178. GameObjLockDict[gameObjType].ExitReadLock();
  179. }
  180. return GameObjForInteract;
  181. }
  182. public Student? StudentForInteract(XY Pos)
  183. {
  184. Student? GameObjForInteract = null;
  185. GameObjLockDict[GameObjType.Character].EnterReadLock();
  186. try
  187. {
  188. foreach (Character character in GameObjDict[GameObjType.Character])
  189. {
  190. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  191. {
  192. GameObjForInteract = (Student)character;
  193. break;
  194. }
  195. }
  196. }
  197. finally
  198. {
  199. GameObjLockDict[GameObjType.Character].ExitReadLock();
  200. }
  201. return GameObjForInteract;
  202. }
  203. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  204. {
  205. GameObj? GameObjForInteract = null;
  206. GameObjLockDict[gameObjType].EnterReadLock();
  207. try
  208. {
  209. foreach (GameObj gameObj in GameObjDict[gameObjType])
  210. {
  211. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  212. {
  213. GameObjForInteract = gameObj;
  214. break;
  215. }
  216. }
  217. }
  218. finally
  219. {
  220. GameObjLockDict[gameObjType].ExitReadLock();
  221. }
  222. return GameObjForInteract;
  223. }
  224. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  225. {
  226. GameObj? GameObjForInteract = null;
  227. GameObjLockDict[gameObjType].EnterReadLock();
  228. try
  229. {
  230. foreach (GameObj gameObj in GameObjDict[gameObjType])
  231. {
  232. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  233. {
  234. GameObjForInteract = gameObj;
  235. break;
  236. }
  237. }
  238. }
  239. finally
  240. {
  241. GameObjLockDict[gameObjType].ExitReadLock();
  242. }
  243. return GameObjForInteract;
  244. }
  245. public bool CanSee(XY pos1, XY pos2, int viewRange)
  246. {
  247. XY del = pos1 - pos2;
  248. if (del * del > viewRange * viewRange) return false;
  249. if (del.x > del.y)
  250. {
  251. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  252. {
  253. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  254. {
  255. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  256. return false;
  257. }
  258. }
  259. else
  260. {
  261. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  262. {
  263. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  264. return false;
  265. }
  266. }
  267. }
  268. else
  269. {
  270. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  271. {
  272. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  273. {
  274. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  275. return false;
  276. }
  277. }
  278. else
  279. {
  280. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  281. {
  282. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  283. return false;
  284. }
  285. }
  286. }
  287. return true;
  288. }
  289. public Map(uint[,] mapResource)
  290. {
  291. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  292. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  293. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  294. {
  295. if (idx != GameObjType.Null)
  296. {
  297. gameObjDict.Add(idx, new List<IGameObj>());
  298. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  299. }
  300. }
  301. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  302. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  303. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  304. for (int i = 0; i < GameData.rows; ++i)
  305. {
  306. for (int j = 0; j < GameData.cols; ++j)
  307. {
  308. switch (mapResource[i, j])
  309. {
  310. case (uint)PlaceType.Wall:
  311. {
  312. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  313. break;
  314. }
  315. case (uint)PlaceType.Doorway:
  316. {
  317. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  318. break;
  319. }
  320. case (uint)PlaceType.EmergencyExit:
  321. {
  322. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  323. break;
  324. }
  325. case (uint)PlaceType.Generator:
  326. {
  327. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  328. break;
  329. }
  330. case (uint)PlaceType.Chest:
  331. {
  332. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  333. break;
  334. }
  335. case (uint)PlaceType.Door3:
  336. {
  337. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  338. break;
  339. }
  340. case (uint)PlaceType.Door5:
  341. {
  342. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  343. break;
  344. }
  345. case (uint)PlaceType.Door6:
  346. {
  347. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  348. break;
  349. }
  350. case (uint)PlaceType.Window:
  351. {
  352. Add(new Window(GameData.GetCellCenterPos(i, j)));
  353. break;
  354. }
  355. case (uint)PlaceType.BirthPoint1:
  356. case (uint)PlaceType.BirthPoint2:
  357. case (uint)PlaceType.BirthPoint3:
  358. case (uint)PlaceType.BirthPoint4:
  359. case (uint)PlaceType.BirthPoint5:
  360. {
  361. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  362. break;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }