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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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].EnterReadLock();
  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].ExitReadLock();
  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. bool flag = false;
  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. GameObjDict[gameObj.Type].Remove(obj);
  207. flag = true;
  208. break;
  209. }
  210. }
  211. }
  212. finally
  213. {
  214. GameObjLockDict[gameObj.Type].ExitWriteLock();
  215. }
  216. return flag;
  217. }
  218. public bool RemoveJustFromMap(GameObj gameObj)
  219. {
  220. GameObjLockDict[gameObj.Type].EnterWriteLock();
  221. bool flag;
  222. try
  223. {
  224. flag = GameObjDict[gameObj.Type].Remove(gameObj);
  225. }
  226. finally
  227. {
  228. GameObjLockDict[gameObj.Type].ExitWriteLock();
  229. }
  230. return flag;
  231. }
  232. public void Add(GameObj gameObj)
  233. {
  234. GameObjLockDict[gameObj.Type].EnterWriteLock();
  235. try
  236. {
  237. GameObjDict[gameObj.Type].Add(gameObj);
  238. }
  239. finally
  240. {
  241. GameObjLockDict[gameObj.Type].ExitWriteLock();
  242. }
  243. }
  244. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  245. {
  246. GameObj? GameObjForInteract = null;
  247. GameObjLockDict[gameObjType].EnterReadLock();
  248. try
  249. {
  250. foreach (GameObj gameObj in GameObjDict[gameObjType])
  251. {
  252. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  253. {
  254. GameObjForInteract = gameObj;
  255. break;
  256. }
  257. }
  258. }
  259. finally
  260. {
  261. GameObjLockDict[gameObjType].ExitReadLock();
  262. }
  263. return GameObjForInteract;
  264. }
  265. public Student? StudentForInteract(XY Pos)
  266. {
  267. Student? GameObjForInteract = null;
  268. GameObjLockDict[GameObjType.Character].EnterReadLock();
  269. try
  270. {
  271. foreach (Character character in GameObjDict[GameObjType.Character])
  272. {
  273. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  274. {
  275. GameObjForInteract = (Student)character;
  276. break;
  277. }
  278. }
  279. }
  280. finally
  281. {
  282. GameObjLockDict[GameObjType.Character].ExitReadLock();
  283. }
  284. return GameObjForInteract;
  285. }
  286. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  287. {
  288. GameObj? GameObjForInteract = null;
  289. GameObjLockDict[gameObjType].EnterReadLock();
  290. try
  291. {
  292. foreach (GameObj gameObj in GameObjDict[gameObjType])
  293. {
  294. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  295. {
  296. GameObjForInteract = gameObj;
  297. break;
  298. }
  299. }
  300. }
  301. finally
  302. {
  303. GameObjLockDict[gameObjType].ExitReadLock();
  304. }
  305. return GameObjForInteract;
  306. }
  307. public GameObj? PartInTheSameCell(XY Pos, GameObjType gameObjType)
  308. {
  309. GameObj? GameObjForInteract = null;
  310. GameObjLockDict[gameObjType].EnterReadLock();
  311. try
  312. {
  313. foreach (GameObj gameObj in GameObjDict[gameObjType])
  314. {
  315. if (GameData.PartInTheSameCell(gameObj.Position, Pos))
  316. {
  317. GameObjForInteract = gameObj;
  318. break;
  319. }
  320. }
  321. }
  322. finally
  323. {
  324. GameObjLockDict[gameObjType].ExitReadLock();
  325. }
  326. return GameObjForInteract;
  327. }
  328. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  329. {
  330. GameObj? GameObjForInteract = null;
  331. GameObjLockDict[gameObjType].EnterReadLock();
  332. try
  333. {
  334. foreach (GameObj gameObj in GameObjDict[gameObjType])
  335. {
  336. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  337. {
  338. GameObjForInteract = gameObj;
  339. break;
  340. }
  341. }
  342. }
  343. finally
  344. {
  345. GameObjLockDict[gameObjType].ExitReadLock();
  346. }
  347. return GameObjForInteract;
  348. }
  349. public bool CanSee(Character player, GameObj gameObj)
  350. {
  351. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  352. XY pos1 = player.Position;
  353. XY pos2 = gameObj.Position;
  354. XY del = pos1 - pos2;
  355. if (del * del > player.ViewRange * player.ViewRange) return false;
  356. if (del.x > del.y)
  357. {
  358. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  359. {
  360. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  361. {
  362. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  363. return false;
  364. }
  365. }
  366. else
  367. {
  368. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  369. {
  370. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  371. return false;
  372. }
  373. }
  374. }
  375. else
  376. {
  377. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  378. {
  379. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  380. {
  381. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  382. return false;
  383. }
  384. }
  385. else
  386. {
  387. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  388. {
  389. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  390. return false;
  391. }
  392. }
  393. }
  394. return true;
  395. }
  396. public Map(uint[,] mapResource)
  397. {
  398. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  399. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  400. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  401. {
  402. if (idx != GameObjType.Null)
  403. {
  404. gameObjDict.Add(idx, new List<IGameObj>());
  405. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  406. }
  407. }
  408. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  409. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  410. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  411. for (int i = 0; i < GameData.rows; ++i)
  412. {
  413. for (int j = 0; j < GameData.cols; ++j)
  414. {
  415. switch (mapResource[i, j])
  416. {
  417. case (uint)PlaceType.Wall:
  418. {
  419. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  420. break;
  421. }
  422. case (uint)PlaceType.Doorway:
  423. {
  424. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  425. break;
  426. }
  427. case (uint)PlaceType.EmergencyExit:
  428. {
  429. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  430. break;
  431. }
  432. case (uint)PlaceType.Generator:
  433. {
  434. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  435. break;
  436. }
  437. case (uint)PlaceType.Chest:
  438. {
  439. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  440. break;
  441. }
  442. case (uint)PlaceType.Door3:
  443. {
  444. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  445. break;
  446. }
  447. case (uint)PlaceType.Door5:
  448. {
  449. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  450. break;
  451. }
  452. case (uint)PlaceType.Door6:
  453. {
  454. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  455. break;
  456. }
  457. case (uint)PlaceType.Window:
  458. {
  459. Add(new Window(GameData.GetCellCenterPos(i, j)));
  460. break;
  461. }
  462. case (uint)PlaceType.BirthPoint1:
  463. case (uint)PlaceType.BirthPoint2:
  464. case (uint)PlaceType.BirthPoint3:
  465. case (uint)PlaceType.BirthPoint4:
  466. case (uint)PlaceType.BirthPoint5:
  467. {
  468. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  469. break;
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }