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

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