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.0 kB

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