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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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(Character player, GameObj gameObj)
  246. {
  247. if ((gameObj.Type == GameObjType.Character) && !((Character)gameObj).HasInvisible) return false;
  248. XY pos1 = player.Position;
  249. XY pos2 = gameObj.Position;
  250. XY del = pos1 - pos2;
  251. if (del * del > player.ViewRange * player.ViewRange) return false;
  252. if (del.x > del.y)
  253. {
  254. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  255. {
  256. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  257. {
  258. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  259. return false;
  260. }
  261. }
  262. else
  263. {
  264. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  265. {
  266. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  267. return false;
  268. }
  269. }
  270. }
  271. else
  272. {
  273. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  274. {
  275. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  276. {
  277. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  278. return false;
  279. }
  280. }
  281. else
  282. {
  283. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  284. {
  285. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  286. return false;
  287. }
  288. }
  289. }
  290. return true;
  291. }
  292. public Map(uint[,] mapResource)
  293. {
  294. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  295. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  296. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  297. {
  298. if (idx != GameObjType.Null)
  299. {
  300. gameObjDict.Add(idx, new List<IGameObj>());
  301. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  302. }
  303. }
  304. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  305. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  306. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  307. for (int i = 0; i < GameData.rows; ++i)
  308. {
  309. for (int j = 0; j < GameData.cols; ++j)
  310. {
  311. switch (mapResource[i, j])
  312. {
  313. case (uint)PlaceType.Wall:
  314. {
  315. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  316. break;
  317. }
  318. case (uint)PlaceType.Doorway:
  319. {
  320. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  321. break;
  322. }
  323. case (uint)PlaceType.EmergencyExit:
  324. {
  325. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  326. break;
  327. }
  328. case (uint)PlaceType.Generator:
  329. {
  330. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  331. break;
  332. }
  333. case (uint)PlaceType.Chest:
  334. {
  335. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  336. break;
  337. }
  338. case (uint)PlaceType.Door3:
  339. {
  340. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  341. break;
  342. }
  343. case (uint)PlaceType.Door5:
  344. {
  345. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  346. break;
  347. }
  348. case (uint)PlaceType.Door6:
  349. {
  350. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  351. break;
  352. }
  353. case (uint)PlaceType.Window:
  354. {
  355. Add(new Window(GameData.GetCellCenterPos(i, j)));
  356. break;
  357. }
  358. case (uint)PlaceType.BirthPoint1:
  359. case (uint)PlaceType.BirthPoint2:
  360. case (uint)PlaceType.BirthPoint3:
  361. case (uint)PlaceType.BirthPoint4:
  362. case (uint)PlaceType.BirthPoint5:
  363. {
  364. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. }