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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 void WhenStudentNumChange()
  15. {
  16. if (numOfDeceasedStudent + numOfEscapedStudent == GameData.numOfStudent)
  17. {
  18. Timer.IsGaming = false;
  19. }
  20. if (GameData.numOfStudent - NumOfDeceasedStudent - NumOfEscapedStudent == 1)
  21. {
  22. GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  23. try
  24. {
  25. foreach (EmergencyExit emergencyExit in GameObjDict[GameObjType.EmergencyExit])
  26. if (emergencyExit.CanOpen)
  27. {
  28. emergencyExit.IsOpen = true;
  29. break;
  30. }
  31. }
  32. finally
  33. {
  34. GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  35. }
  36. }
  37. }
  38. private uint numOfRepairedGenerators = 0;
  39. public uint NumOfRepairedGenerators
  40. {
  41. get => numOfRepairedGenerators;
  42. set
  43. {
  44. lock (lockForNum)
  45. {
  46. numOfRepairedGenerators = value;
  47. if (NumOfRepairedGenerators == GameData.numOfGeneratorRequiredForEmergencyExit)
  48. {
  49. GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  50. try
  51. {
  52. Random r = new Random(Environment.TickCount);
  53. EmergencyExit emergencyExit = (EmergencyExit)(GameObjDict[GameObjType.EmergencyExit][r.Next(0, GameObjDict[GameObjType.EmergencyExit].Count)]);
  54. emergencyExit.CanOpen = true;
  55. Preparation.Utility.Debugger.Output(emergencyExit, emergencyExit.Position.ToString());
  56. }
  57. finally
  58. {
  59. GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  60. }
  61. }
  62. else
  63. if (NumOfRepairedGenerators == GameData.numOfGeneratorRequiredForRepair)
  64. {
  65. GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  66. try
  67. {
  68. foreach (Doorway doorway in GameObjDict[GameObjType.Doorway])
  69. doorway.PowerSupply = true;
  70. }
  71. finally
  72. {
  73. GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  74. }
  75. }
  76. }
  77. }
  78. }
  79. private uint numOfDeceasedStudent = 0;
  80. public uint NumOfDeceasedStudent
  81. {
  82. get => numOfDeceasedStudent;
  83. set
  84. {
  85. lock (lockForNum)
  86. {
  87. numOfDeceasedStudent = value;
  88. WhenStudentNumChange();
  89. }
  90. }
  91. }
  92. private uint numOfEscapedStudent = 0;
  93. public uint NumOfEscapedStudent
  94. {
  95. get => numOfEscapedStudent;
  96. set
  97. {
  98. lock (lockForNum)
  99. {
  100. numOfEscapedStudent = value;
  101. WhenStudentNumChange();
  102. }
  103. }
  104. }
  105. private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
  106. public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
  107. private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
  108. public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
  109. public readonly uint[,] protoGameMap;
  110. public uint[,] ProtoGameMap => protoGameMap;
  111. public PlaceType GetPlaceType(IGameObj obj)
  112. {
  113. try
  114. {
  115. return (PlaceType)protoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  116. }
  117. catch
  118. {
  119. return PlaceType.Null;
  120. }
  121. }
  122. public PlaceType GetPlaceType(XY pos)
  123. {
  124. try
  125. {
  126. return (PlaceType)protoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
  127. }
  128. catch
  129. {
  130. return PlaceType.Null;
  131. }
  132. }
  133. public bool IsOutOfBound(IGameObj obj)
  134. {
  135. 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;
  136. }
  137. public IOutOfBound GetOutOfBound(XY pos)
  138. {
  139. return new OutOfBoundBlock(pos);
  140. }
  141. public Character? FindPlayer(long playerID)
  142. {
  143. Character? player = null;
  144. gameObjLockDict[GameObjType.Character].EnterReadLock();
  145. try
  146. {
  147. foreach (Character person in gameObjDict[GameObjType.Character])
  148. {
  149. if (playerID == person.ID)
  150. {
  151. player = person;
  152. break;
  153. }
  154. }
  155. }
  156. finally
  157. {
  158. gameObjLockDict[GameObjType.Character].ExitReadLock();
  159. }
  160. return player;
  161. }
  162. public Character? FindPlayerToAction(long playerID)
  163. {
  164. Character? player = null;
  165. gameObjLockDict[GameObjType.Character].EnterReadLock();
  166. try
  167. {
  168. foreach (Character person in gameObjDict[GameObjType.Character])
  169. {
  170. if (playerID == person.ID)
  171. {
  172. if (person.CharacterType == CharacterType.TechOtaku && person.FindIActiveSkill(ActiveSkillType.UseRobot).IsBeingUsed)
  173. {
  174. Debugger.Output(person, person.PlayerID.ToString());
  175. foreach (Character character in gameObjDict[GameObjType.Character])
  176. {
  177. Debugger.Output(character, character.PlayerID.ToString());
  178. if (person.PlayerID + GameData.numOfPeople == character.PlayerID)
  179. {
  180. player = character;
  181. break;
  182. }
  183. }
  184. }
  185. else player = person;
  186. break;
  187. }
  188. }
  189. }
  190. finally
  191. {
  192. gameObjLockDict[GameObjType.Character].ExitReadLock();
  193. }
  194. return player;
  195. }
  196. public bool Remove(GameObj gameObj)
  197. {
  198. GameObj? ToDel = null;
  199. GameObjLockDict[gameObj.Type].EnterWriteLock();
  200. try
  201. {
  202. foreach (GameObj obj in GameObjDict[gameObj.Type])
  203. {
  204. if (gameObj.ID == obj.ID)
  205. {
  206. ToDel = obj;
  207. break;
  208. }
  209. }
  210. }
  211. finally
  212. {
  213. GameObjLockDict[gameObj.Type].ExitWriteLock();
  214. }
  215. if (ToDel == null) return false;
  216. GameObjDict[gameObj.Type].Remove(ToDel);
  217. return true;
  218. }
  219. public bool RemoveJustFromMap(GameObj gameObj)
  220. {
  221. GameObjLockDict[gameObj.Type].EnterWriteLock();
  222. bool flag;
  223. try
  224. {
  225. flag = GameObjDict[gameObj.Type].Remove(gameObj);
  226. }
  227. finally
  228. {
  229. GameObjLockDict[gameObj.Type].ExitWriteLock();
  230. }
  231. return flag;
  232. }
  233. public void Add(GameObj gameObj)
  234. {
  235. GameObjLockDict[gameObj.Type].EnterWriteLock();
  236. try
  237. {
  238. GameObjDict[gameObj.Type].Add(gameObj);
  239. }
  240. finally
  241. {
  242. GameObjLockDict[gameObj.Type].ExitWriteLock();
  243. }
  244. }
  245. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  246. {
  247. GameObj? GameObjForInteract = null;
  248. GameObjLockDict[gameObjType].EnterReadLock();
  249. try
  250. {
  251. foreach (GameObj gameObj in GameObjDict[gameObjType])
  252. {
  253. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  254. {
  255. GameObjForInteract = gameObj;
  256. break;
  257. }
  258. }
  259. }
  260. finally
  261. {
  262. GameObjLockDict[gameObjType].ExitReadLock();
  263. }
  264. return GameObjForInteract;
  265. }
  266. public Student? StudentForInteract(XY Pos)
  267. {
  268. Student? GameObjForInteract = null;
  269. GameObjLockDict[GameObjType.Character].EnterReadLock();
  270. try
  271. {
  272. foreach (Character character in GameObjDict[GameObjType.Character])
  273. {
  274. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  275. {
  276. GameObjForInteract = (Student)character;
  277. break;
  278. }
  279. }
  280. }
  281. finally
  282. {
  283. GameObjLockDict[GameObjType.Character].ExitReadLock();
  284. }
  285. return GameObjForInteract;
  286. }
  287. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  288. {
  289. GameObj? GameObjForInteract = null;
  290. GameObjLockDict[gameObjType].EnterReadLock();
  291. try
  292. {
  293. foreach (GameObj gameObj in GameObjDict[gameObjType])
  294. {
  295. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  296. {
  297. GameObjForInteract = gameObj;
  298. break;
  299. }
  300. }
  301. }
  302. finally
  303. {
  304. GameObjLockDict[gameObjType].ExitReadLock();
  305. }
  306. return GameObjForInteract;
  307. }
  308. public GameObj? PartInTheSameCell(XY Pos, GameObjType gameObjType)
  309. {
  310. GameObj? GameObjForInteract = null;
  311. GameObjLockDict[gameObjType].EnterReadLock();
  312. try
  313. {
  314. foreach (GameObj gameObj in GameObjDict[gameObjType])
  315. {
  316. if (GameData.PartInTheSameCell(gameObj.Position, Pos))
  317. {
  318. GameObjForInteract = gameObj;
  319. break;
  320. }
  321. }
  322. }
  323. finally
  324. {
  325. GameObjLockDict[gameObjType].ExitReadLock();
  326. }
  327. return GameObjForInteract;
  328. }
  329. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  330. {
  331. GameObj? GameObjForInteract = null;
  332. GameObjLockDict[gameObjType].EnterReadLock();
  333. try
  334. {
  335. foreach (GameObj gameObj in GameObjDict[gameObjType])
  336. {
  337. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  338. {
  339. GameObjForInteract = gameObj;
  340. break;
  341. }
  342. }
  343. }
  344. finally
  345. {
  346. GameObjLockDict[gameObjType].ExitReadLock();
  347. }
  348. return GameObjForInteract;
  349. }
  350. public bool CanSee(Character player, GameObj gameObj)
  351. {
  352. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  353. XY pos1 = player.Position;
  354. XY pos2 = gameObj.Position;
  355. XY del = pos1 - pos2;
  356. if (del * del > player.ViewRange * player.ViewRange) return false;
  357. if (del.x > del.y)
  358. {
  359. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  360. {
  361. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  362. {
  363. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  364. return false;
  365. }
  366. }
  367. else
  368. {
  369. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  370. {
  371. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  372. return false;
  373. }
  374. }
  375. }
  376. else
  377. {
  378. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  379. {
  380. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  381. {
  382. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  383. return false;
  384. }
  385. }
  386. else
  387. {
  388. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  389. {
  390. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  391. return false;
  392. }
  393. }
  394. }
  395. return true;
  396. }
  397. public Map(uint[,] mapResource)
  398. {
  399. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  400. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  401. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  402. {
  403. if (idx != GameObjType.Null)
  404. {
  405. gameObjDict.Add(idx, new List<IGameObj>());
  406. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  407. }
  408. }
  409. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  410. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  411. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  412. for (int i = 0; i < GameData.rows; ++i)
  413. {
  414. for (int j = 0; j < GameData.cols; ++j)
  415. {
  416. switch (mapResource[i, j])
  417. {
  418. case (uint)PlaceType.Wall:
  419. {
  420. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  421. break;
  422. }
  423. case (uint)PlaceType.Doorway:
  424. {
  425. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  426. break;
  427. }
  428. case (uint)PlaceType.EmergencyExit:
  429. {
  430. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  431. break;
  432. }
  433. case (uint)PlaceType.Generator:
  434. {
  435. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  436. break;
  437. }
  438. case (uint)PlaceType.Chest:
  439. {
  440. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  441. break;
  442. }
  443. case (uint)PlaceType.Door3:
  444. {
  445. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  446. break;
  447. }
  448. case (uint)PlaceType.Door5:
  449. {
  450. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  451. break;
  452. }
  453. case (uint)PlaceType.Door6:
  454. {
  455. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  456. break;
  457. }
  458. case (uint)PlaceType.Window:
  459. {
  460. Add(new Window(GameData.GetCellCenterPos(i, j)));
  461. break;
  462. }
  463. case (uint)PlaceType.BirthPoint1:
  464. case (uint)PlaceType.BirthPoint2:
  465. case (uint)PlaceType.BirthPoint3:
  466. case (uint)PlaceType.BirthPoint4:
  467. case (uint)PlaceType.BirthPoint5:
  468. {
  469. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  470. break;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }