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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 Character? FindPlayerToAction(long playerID)
  116. {
  117. Character? player = null;
  118. gameObjLockDict[GameObjType.Character].EnterReadLock();
  119. try
  120. {
  121. foreach (Character person in gameObjDict[GameObjType.Character])
  122. {
  123. if (playerID == person.ID)
  124. {
  125. if (person.CharacterType == CharacterType.TechOtaku && person.FindIActiveSkill(ActiveSkillType.UseRobot).IsBeingUsed)
  126. {
  127. foreach (Character character in gameObjDict[GameObjType.Character])
  128. {
  129. if (playerID + GameData.numOfPeople == character.ID)
  130. {
  131. player = character;
  132. break;
  133. }
  134. }
  135. }
  136. else player = person;
  137. break;
  138. }
  139. }
  140. }
  141. finally
  142. {
  143. gameObjLockDict[GameObjType.Character].ExitReadLock();
  144. }
  145. return player;
  146. }
  147. public bool Remove(GameObj gameObj)
  148. {
  149. bool flag = false;
  150. GameObjLockDict[gameObj.Type].EnterWriteLock();
  151. try
  152. {
  153. foreach (GameObj obj in GameObjDict[gameObj.Type])
  154. {
  155. if (gameObj.ID == obj.ID)
  156. {
  157. GameObjDict[gameObj.Type].Remove(obj);
  158. flag = true;
  159. break;
  160. }
  161. }
  162. }
  163. finally
  164. {
  165. GameObjLockDict[gameObj.Type].ExitWriteLock();
  166. }
  167. return flag;
  168. }
  169. public bool RemoveJustFromMap(GameObj gameObj)
  170. {
  171. GameObjLockDict[gameObj.Type].EnterWriteLock();
  172. try
  173. {
  174. return GameObjDict[gameObj.Type].Remove(gameObj);
  175. }
  176. finally
  177. {
  178. GameObjLockDict[gameObj.Type].ExitWriteLock();
  179. }
  180. }
  181. public void Add(GameObj gameObj)
  182. {
  183. GameObjLockDict[gameObj.Type].EnterWriteLock();
  184. try
  185. {
  186. GameObjDict[gameObj.Type].Add(gameObj);
  187. }
  188. finally
  189. {
  190. GameObjLockDict[gameObj.Type].ExitWriteLock();
  191. }
  192. }
  193. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  194. {
  195. GameObj? GameObjForInteract = null;
  196. GameObjLockDict[gameObjType].EnterReadLock();
  197. try
  198. {
  199. foreach (GameObj gameObj in GameObjDict[gameObjType])
  200. {
  201. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  202. {
  203. GameObjForInteract = gameObj;
  204. break;
  205. }
  206. }
  207. }
  208. finally
  209. {
  210. GameObjLockDict[gameObjType].ExitReadLock();
  211. }
  212. return GameObjForInteract;
  213. }
  214. public Student? StudentForInteract(XY Pos)
  215. {
  216. Student? GameObjForInteract = null;
  217. GameObjLockDict[GameObjType.Character].EnterReadLock();
  218. try
  219. {
  220. foreach (Character character in GameObjDict[GameObjType.Character])
  221. {
  222. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  223. {
  224. GameObjForInteract = (Student)character;
  225. break;
  226. }
  227. }
  228. }
  229. finally
  230. {
  231. GameObjLockDict[GameObjType.Character].ExitReadLock();
  232. }
  233. return GameObjForInteract;
  234. }
  235. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  236. {
  237. GameObj? GameObjForInteract = null;
  238. GameObjLockDict[gameObjType].EnterReadLock();
  239. try
  240. {
  241. foreach (GameObj gameObj in GameObjDict[gameObjType])
  242. {
  243. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  244. {
  245. GameObjForInteract = gameObj;
  246. break;
  247. }
  248. }
  249. }
  250. finally
  251. {
  252. GameObjLockDict[gameObjType].ExitReadLock();
  253. }
  254. return GameObjForInteract;
  255. }
  256. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  257. {
  258. GameObj? GameObjForInteract = null;
  259. GameObjLockDict[gameObjType].EnterReadLock();
  260. try
  261. {
  262. foreach (GameObj gameObj in GameObjDict[gameObjType])
  263. {
  264. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  265. {
  266. GameObjForInteract = gameObj;
  267. break;
  268. }
  269. }
  270. }
  271. finally
  272. {
  273. GameObjLockDict[gameObjType].ExitReadLock();
  274. }
  275. return GameObjForInteract;
  276. }
  277. public bool CanSee(Character player, GameObj gameObj)
  278. {
  279. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  280. XY pos1 = player.Position;
  281. XY pos2 = gameObj.Position;
  282. XY del = pos1 - pos2;
  283. if (del * del > player.ViewRange * player.ViewRange) return false;
  284. if (del.x > del.y)
  285. {
  286. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  287. {
  288. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  289. {
  290. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  291. return false;
  292. }
  293. }
  294. else
  295. {
  296. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  297. {
  298. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  299. return false;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  306. {
  307. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  308. {
  309. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  310. return false;
  311. }
  312. }
  313. else
  314. {
  315. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  316. {
  317. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  318. return false;
  319. }
  320. }
  321. }
  322. return true;
  323. }
  324. public Map(uint[,] mapResource)
  325. {
  326. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  327. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  328. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  329. {
  330. if (idx != GameObjType.Null)
  331. {
  332. gameObjDict.Add(idx, new List<IGameObj>());
  333. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  334. }
  335. }
  336. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  337. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  338. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  339. for (int i = 0; i < GameData.rows; ++i)
  340. {
  341. for (int j = 0; j < GameData.cols; ++j)
  342. {
  343. switch (mapResource[i, j])
  344. {
  345. case (uint)PlaceType.Wall:
  346. {
  347. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  348. break;
  349. }
  350. case (uint)PlaceType.Doorway:
  351. {
  352. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  353. break;
  354. }
  355. case (uint)PlaceType.EmergencyExit:
  356. {
  357. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  358. break;
  359. }
  360. case (uint)PlaceType.Generator:
  361. {
  362. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  363. break;
  364. }
  365. case (uint)PlaceType.Chest:
  366. {
  367. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  368. break;
  369. }
  370. case (uint)PlaceType.Door3:
  371. {
  372. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  373. break;
  374. }
  375. case (uint)PlaceType.Door5:
  376. {
  377. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  378. break;
  379. }
  380. case (uint)PlaceType.Door6:
  381. {
  382. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  383. break;
  384. }
  385. case (uint)PlaceType.Window:
  386. {
  387. Add(new Window(GameData.GetCellCenterPos(i, j)));
  388. break;
  389. }
  390. case (uint)PlaceType.BirthPoint1:
  391. case (uint)PlaceType.BirthPoint2:
  392. case (uint)PlaceType.BirthPoint3:
  393. case (uint)PlaceType.BirthPoint4:
  394. case (uint)PlaceType.BirthPoint5:
  395. {
  396. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }