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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, BirthPoint> birthPointList; // 出生点列表
  11. public Dictionary<uint, BirthPoint> 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. uint type = ProtoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  22. if (type == 2)
  23. return PlaceType.Grass1;
  24. else if (type == 3)
  25. return PlaceType.Grass2;
  26. else if (type == 4)
  27. return PlaceType.Grass3;
  28. else
  29. return PlaceType.Land; // 其他情况均返回land
  30. }
  31. catch
  32. {
  33. return PlaceType.Land;
  34. }
  35. }
  36. public PlaceType GetPlaceType(XY pos)
  37. {
  38. try
  39. {
  40. switch (ProtoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell])
  41. {
  42. case 2:
  43. return PlaceType.Grass1;
  44. case 3:
  45. return PlaceType.Grass2;
  46. case 4:
  47. return PlaceType.Grass3;
  48. default:
  49. return PlaceType.Land;
  50. }
  51. }
  52. catch
  53. {
  54. return PlaceType.Land;
  55. }
  56. }
  57. public bool IsOutOfBound(IGameObj obj)
  58. {
  59. 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;
  60. }
  61. public IOutOfBound GetOutOfBound(XY pos)
  62. {
  63. return new OutOfBoundBlock(pos);
  64. }
  65. public Character? FindPlayer(long playerID)
  66. {
  67. Character? player = null;
  68. gameObjLockDict[GameObjType.Character].EnterReadLock();
  69. try
  70. {
  71. foreach (Character person in gameObjDict[GameObjType.Character])
  72. {
  73. if (playerID == person.ID)
  74. {
  75. player = person;
  76. break;
  77. }
  78. }
  79. }
  80. finally
  81. {
  82. gameObjLockDict[GameObjType.Character].ExitReadLock();
  83. }
  84. return player;
  85. }
  86. public Map(uint[,] mapResource)
  87. {
  88. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  89. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  90. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  91. {
  92. if (idx != GameObjType.Null)
  93. {
  94. gameObjDict.Add(idx, new List<IGameObj>());
  95. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  96. }
  97. }
  98. ProtoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  99. Array.Copy(mapResource, ProtoGameMap, mapResource.Length);
  100. birthPointList = new Dictionary<uint, BirthPoint>(GameData.numOfBirthPoint);
  101. for (int i = 0; i < GameData.rows; ++i)
  102. {
  103. for (int j = 0; j < GameData.cols; ++j)
  104. {
  105. switch (mapResource[i, j])
  106. {
  107. case (uint)MapInfoObjType.Wall:
  108. {
  109. GameObjLockDict[GameObjType.Wall].EnterWriteLock();
  110. try
  111. {
  112. GameObjDict[GameObjType.Wall].Add(new Wall(GameData.GetCellCenterPos(i, j)));
  113. }
  114. finally
  115. {
  116. GameObjLockDict[GameObjType.Wall].ExitWriteLock();
  117. }
  118. break;
  119. }
  120. case (uint)MapInfoObjType.Doorway:
  121. {
  122. GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  123. try
  124. {
  125. GameObjDict[GameObjType.Doorway].Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  126. }
  127. finally
  128. {
  129. GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  130. }
  131. break;
  132. }
  133. case (uint)MapInfoObjType.EmergencyExit:
  134. {
  135. GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  136. try
  137. {
  138. GameObjDict[GameObjType.EmergencyExit].Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  139. }
  140. finally
  141. {
  142. GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  143. }
  144. break;
  145. }
  146. case (uint)MapInfoObjType.Generator:
  147. {
  148. GameObjLockDict[GameObjType.Generator].EnterWriteLock();
  149. try
  150. {
  151. GameObjDict[GameObjType.Generator].Add(new Generator(GameData.GetCellCenterPos(i, j)));
  152. }
  153. finally
  154. {
  155. GameObjLockDict[GameObjType.Generator].ExitWriteLock();
  156. }
  157. break;
  158. }
  159. case (uint)MapInfoObjType.BirthPoint1:
  160. case (uint)MapInfoObjType.BirthPoint2:
  161. case (uint)MapInfoObjType.BirthPoint3:
  162. case (uint)MapInfoObjType.BirthPoint4:
  163. case (uint)MapInfoObjType.BirthPoint5:
  164. {
  165. BirthPoint newBirthPoint = new BirthPoint(GameData.GetCellCenterPos(i, j));
  166. birthPointList.Add(mapResource[i, j], newBirthPoint);
  167. GameObjLockDict[GameObjType.BirthPoint].EnterWriteLock();
  168. try
  169. {
  170. GameObjDict[GameObjType.BirthPoint].Add(newBirthPoint);
  171. }
  172. finally
  173. {
  174. GameObjLockDict[GameObjType.BirthPoint].ExitWriteLock();
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }