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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 void Add(GameObj gameObj)
  170. {
  171. GameObjLockDict[gameObj.Type].EnterWriteLock();
  172. try
  173. {
  174. GameObjDict[gameObj.Type].Add(gameObj);
  175. }
  176. finally
  177. {
  178. GameObjLockDict[gameObj.Type].ExitWriteLock();
  179. }
  180. }
  181. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  182. {
  183. GameObj? GameObjForInteract = null;
  184. GameObjLockDict[gameObjType].EnterReadLock();
  185. try
  186. {
  187. foreach (GameObj gameObj in GameObjDict[gameObjType])
  188. {
  189. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  190. {
  191. GameObjForInteract = gameObj;
  192. break;
  193. }
  194. }
  195. }
  196. finally
  197. {
  198. GameObjLockDict[gameObjType].ExitReadLock();
  199. }
  200. return GameObjForInteract;
  201. }
  202. public Student? StudentForInteract(XY Pos)
  203. {
  204. Student? GameObjForInteract = null;
  205. GameObjLockDict[GameObjType.Character].EnterReadLock();
  206. try
  207. {
  208. foreach (Character character in GameObjDict[GameObjType.Character])
  209. {
  210. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  211. {
  212. GameObjForInteract = (Student)character;
  213. break;
  214. }
  215. }
  216. }
  217. finally
  218. {
  219. GameObjLockDict[GameObjType.Character].ExitReadLock();
  220. }
  221. return GameObjForInteract;
  222. }
  223. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  224. {
  225. GameObj? GameObjForInteract = null;
  226. GameObjLockDict[gameObjType].EnterReadLock();
  227. try
  228. {
  229. foreach (GameObj gameObj in GameObjDict[gameObjType])
  230. {
  231. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  232. {
  233. GameObjForInteract = gameObj;
  234. break;
  235. }
  236. }
  237. }
  238. finally
  239. {
  240. GameObjLockDict[gameObjType].ExitReadLock();
  241. }
  242. return GameObjForInteract;
  243. }
  244. public GameObj? OneForInteractInACross(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.ApproachToInteractInACross(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 bool CanSee(Character player, GameObj gameObj)
  266. {
  267. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  268. XY pos1 = player.Position;
  269. XY pos2 = gameObj.Position;
  270. XY del = pos1 - pos2;
  271. if (del * del > player.ViewRange * player.ViewRange) return false;
  272. if (del.x > del.y)
  273. {
  274. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  275. {
  276. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  277. {
  278. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  279. return false;
  280. }
  281. }
  282. else
  283. {
  284. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  285. {
  286. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  287. return false;
  288. }
  289. }
  290. }
  291. else
  292. {
  293. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  294. {
  295. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  296. {
  297. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  298. return false;
  299. }
  300. }
  301. else
  302. {
  303. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  304. {
  305. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  306. return false;
  307. }
  308. }
  309. }
  310. return true;
  311. }
  312. public Map(uint[,] mapResource)
  313. {
  314. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  315. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  316. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  317. {
  318. if (idx != GameObjType.Null)
  319. {
  320. gameObjDict.Add(idx, new List<IGameObj>());
  321. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  322. }
  323. }
  324. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  325. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  326. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  327. for (int i = 0; i < GameData.rows; ++i)
  328. {
  329. for (int j = 0; j < GameData.cols; ++j)
  330. {
  331. switch (mapResource[i, j])
  332. {
  333. case (uint)PlaceType.Wall:
  334. {
  335. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  336. break;
  337. }
  338. case (uint)PlaceType.Doorway:
  339. {
  340. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  341. break;
  342. }
  343. case (uint)PlaceType.EmergencyExit:
  344. {
  345. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  346. break;
  347. }
  348. case (uint)PlaceType.Generator:
  349. {
  350. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  351. break;
  352. }
  353. case (uint)PlaceType.Chest:
  354. {
  355. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  356. break;
  357. }
  358. case (uint)PlaceType.Door3:
  359. {
  360. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  361. break;
  362. }
  363. case (uint)PlaceType.Door5:
  364. {
  365. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  366. break;
  367. }
  368. case (uint)PlaceType.Door6:
  369. {
  370. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  371. break;
  372. }
  373. case (uint)PlaceType.Window:
  374. {
  375. Add(new Window(GameData.GetCellCenterPos(i, j)));
  376. break;
  377. }
  378. case (uint)PlaceType.BirthPoint1:
  379. case (uint)PlaceType.BirthPoint2:
  380. case (uint)PlaceType.BirthPoint3:
  381. case (uint)PlaceType.BirthPoint4:
  382. case (uint)PlaceType.BirthPoint5:
  383. {
  384. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  385. break;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }