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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using Preparation.Interface;
  4. using Preparation.Utility;
  5. using System;
  6. namespace GameClass.GameObj
  7. {
  8. public partial class Map : IMap
  9. {
  10. private readonly Dictionary<uint, XY> birthPointList; // 出生点列表
  11. public Dictionary<uint, XY> BirthPointList => birthPointList;
  12. private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
  13. public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
  14. private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
  15. public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
  16. public readonly uint[,] ProtoGameMap;
  17. public PlaceType GetPlaceType(GameObj obj)
  18. {
  19. try
  20. {
  21. return (PlaceType)ProtoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  22. }
  23. catch
  24. {
  25. return PlaceType.Null;
  26. }
  27. }
  28. public PlaceType GetPlaceType(XY pos)
  29. {
  30. try
  31. {
  32. return (PlaceType)ProtoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
  33. }
  34. catch
  35. {
  36. return PlaceType.Null;
  37. }
  38. }
  39. public bool IsOutOfBound(IGameObj obj)
  40. {
  41. 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;
  42. }
  43. public IOutOfBound GetOutOfBound(XY pos)
  44. {
  45. return new OutOfBoundBlock(pos);
  46. }
  47. public Character? FindPlayer(long playerID)
  48. {
  49. Character? player = null;
  50. gameObjLockDict[GameObjType.Character].EnterReadLock();
  51. try
  52. {
  53. foreach (Character person in gameObjDict[GameObjType.Character])
  54. {
  55. if (playerID == person.ID)
  56. {
  57. player = person;
  58. break;
  59. }
  60. }
  61. }
  62. finally
  63. {
  64. gameObjLockDict[GameObjType.Character].ExitReadLock();
  65. }
  66. return player;
  67. }
  68. public Map(uint[,] mapResource)
  69. {
  70. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  71. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  72. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  73. {
  74. if (idx != GameObjType.Null)
  75. {
  76. gameObjDict.Add(idx, new List<IGameObj>());
  77. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  78. }
  79. }
  80. ProtoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  81. Array.Copy(mapResource, ProtoGameMap, mapResource.Length);
  82. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  83. for (int i = 0; i < GameData.rows; ++i)
  84. {
  85. for (int j = 0; j < GameData.cols; ++j)
  86. {
  87. switch (mapResource[i, j])
  88. {
  89. case (uint)PlaceType.Wall:
  90. {
  91. GameObjLockDict[GameObjType.Wall].EnterWriteLock();
  92. try
  93. {
  94. GameObjDict[GameObjType.Wall].Add(new Wall(GameData.GetCellCenterPos(i, j)));
  95. }
  96. finally
  97. {
  98. GameObjLockDict[GameObjType.Wall].ExitWriteLock();
  99. }
  100. break;
  101. }
  102. case (uint)PlaceType.Doorway:
  103. {
  104. GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  105. try
  106. {
  107. GameObjDict[GameObjType.Doorway].Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  108. }
  109. finally
  110. {
  111. GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  112. }
  113. break;
  114. }
  115. case (uint)PlaceType.EmergencyExit:
  116. {
  117. GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  118. try
  119. {
  120. GameObjDict[GameObjType.EmergencyExit].Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  121. }
  122. finally
  123. {
  124. GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  125. }
  126. break;
  127. }
  128. case (uint)PlaceType.Generator:
  129. {
  130. GameObjLockDict[GameObjType.Generator].EnterWriteLock();
  131. try
  132. {
  133. GameObjDict[GameObjType.Generator].Add(new Generator(GameData.GetCellCenterPos(i, j)));
  134. }
  135. finally
  136. {
  137. GameObjLockDict[GameObjType.Generator].ExitWriteLock();
  138. }
  139. break;
  140. }
  141. case (uint)PlaceType.BirthPoint1:
  142. case (uint)PlaceType.BirthPoint2:
  143. case (uint)PlaceType.BirthPoint3:
  144. case (uint)PlaceType.BirthPoint4:
  145. case (uint)PlaceType.BirthPoint5:
  146. {
  147. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }