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.

PropManager.cs 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System.Collections.Generic;
  2. using GameClass.GameObj;
  3. using System.Threading;
  4. using Preparation.Utility;
  5. using System;
  6. using Timothy.FrameRateTask;
  7. using GameEngine;
  8. namespace Gaming
  9. {
  10. public partial class Game
  11. {
  12. private readonly PropManager propManager;
  13. private class PropManager
  14. {
  15. private readonly Map gameMap;
  16. private MoveEngine moveEngine;
  17. private bool isProducingProp = false;
  18. private readonly List<XY> availableCellForGenerateProp;
  19. public void StartProducing()
  20. {
  21. if (isProducingProp)
  22. return;
  23. isProducingProp = true;
  24. ProduceProp();
  25. }
  26. public void UseProp(Character player)
  27. {
  28. if (player.IsResetting)
  29. return;
  30. Prop? prop = player.UseProp();
  31. switch (prop?.GetPropType())
  32. {
  33. case PropType.Spear:
  34. player.AddSpear(GameData.PropDuration);
  35. break;
  36. case PropType.Shield:
  37. player.AddShield(GameData.PropDuration);
  38. break;
  39. case PropType.addLIFE:
  40. player.AddLIFE(GameData.PropDuration);
  41. break;
  42. case PropType.addSpeed:
  43. player.AddMoveSpeed(GameData.PropDuration);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. /// <param name="player"></param>
  53. /// <param name="propType">若不指定,则自动判断可捡起什么道具</param>
  54. /// <returns></returns>
  55. public bool PickProp(Character player, PropType propType = PropType.Null)
  56. {
  57. if (player.IsResetting)
  58. return false;
  59. Prop? pickProp = null;
  60. if (propType == PropType.Null) // 自动检查有无道具可捡
  61. {
  62. gameMap.GameObjLockDict[GameObjType.Prop].EnterReadLock();
  63. try
  64. {
  65. foreach (Prop prop in gameMap.GameObjDict[GameObjType.Prop])
  66. {
  67. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  68. {
  69. pickProp = prop;
  70. }
  71. }
  72. }
  73. finally
  74. {
  75. gameMap.GameObjLockDict[GameObjType.Prop].ExitReadLock();
  76. }
  77. }
  78. else
  79. {
  80. gameMap.GameObjLockDict[GameObjType.Prop].EnterReadLock();
  81. try
  82. {
  83. foreach (Prop prop in gameMap.GameObjDict[GameObjType.Prop])
  84. {
  85. if (prop.GetPropType() == propType)
  86. {
  87. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  88. {
  89. pickProp = prop;
  90. }
  91. }
  92. }
  93. }
  94. finally
  95. {
  96. gameMap.GameObjLockDict[GameObjType.Prop].ExitReadLock();
  97. }
  98. }
  99. if (pickProp != null)
  100. {
  101. // pickProp.CanMove = false;
  102. Prop? dropProp = null;
  103. if (player.PropInventory != null) // 若角色原来有道具,则原始道具掉落在原地
  104. {
  105. dropProp = player.PropInventory;
  106. XY res = GameData.GetCellCenterPos(player.Position.x / GameData.numOfPosGridPerCell, player.Position.y / GameData.numOfPosGridPerCell);
  107. dropProp.ReSetPos(res, gameMap.GetPlaceType(res));
  108. }
  109. player.PropInventory = pickProp;
  110. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  111. try
  112. {
  113. gameMap.GameObjDict[GameObjType.Prop].Remove((Preparation.Interface.IGameObj)pickProp);
  114. if (dropProp != null)
  115. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)dropProp);
  116. }
  117. finally
  118. {
  119. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  120. }
  121. gameMap.GameObjLockDict[GameObjType.PickedProp].EnterWriteLock();
  122. try
  123. {
  124. gameMap.GameObjDict[GameObjType.PickedProp].Add(new PickedProp(pickProp));
  125. }
  126. finally
  127. {
  128. gameMap.GameObjLockDict[GameObjType.PickedProp].ExitWriteLock();
  129. }
  130. return true;
  131. }
  132. else
  133. return false;
  134. }
  135. public void ThrowProp(Character player, int timeInMilliseconds, double angle)
  136. {
  137. if (!gameMap.Timer.IsGaming)
  138. return;
  139. if (player.IsResetting) // 移动中也能扔,但由于“惯性”,可能初始位置会有点变化
  140. return;
  141. Prop? prop = player.UseProp();
  142. if (prop == null)
  143. return;
  144. prop.CanMove = true;
  145. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  146. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  147. try
  148. {
  149. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)prop);
  150. }
  151. finally
  152. {
  153. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  154. }
  155. timeInMilliseconds = timeInMilliseconds < GameData.PropMaxMoveDistance / prop.MoveSpeed * 1000 ? timeInMilliseconds : GameData.PropMaxMoveDistance / prop.MoveSpeed * 1000;
  156. moveEngine.MoveObj(prop, timeInMilliseconds, angle);
  157. }
  158. private void ProduceProp()
  159. {
  160. int len = availableCellForGenerateProp.Count;
  161. Random r = new Random(Environment.TickCount);
  162. new Thread
  163. (
  164. () =>
  165. {
  166. while (!gameMap.Timer.IsGaming)
  167. Thread.Sleep(1000);
  168. new FrameRateTaskExecutor<int>(
  169. () => gameMap.Timer.IsGaming,
  170. () =>
  171. {
  172. int rand = r.Next(0, len);
  173. XY randPos = availableCellForGenerateProp[rand];
  174. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  175. try
  176. {
  177. switch (r.Next(0, 4))
  178. {
  179. case 0:
  180. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new AddLIFE(randPos, gameMap.GetPlaceType(randPos)));
  181. break;
  182. case 1:
  183. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new AddSpeed(randPos, gameMap.GetPlaceType(randPos)));
  184. break;
  185. case 2:
  186. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new Shield(randPos, gameMap.GetPlaceType(randPos)));
  187. break;
  188. case 3:
  189. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new Spear(randPos, gameMap.GetPlaceType(randPos)));
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. finally
  196. {
  197. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  198. }
  199. },
  200. GameData.PropProduceTime,
  201. () => 0
  202. )
  203. .Start();
  204. }
  205. )
  206. { IsBackground = true }.Start();
  207. }
  208. public PropManager(Map gameMap) // 道具不能扔过墙
  209. {
  210. this.gameMap = gameMap;
  211. this.moveEngine = new MoveEngine(
  212. gameMap: gameMap,
  213. OnCollision: (obj, collision, moveVec) =>
  214. { return MoveEngine.AfterCollision.MoveMax; },
  215. EndMove: obj =>
  216. {
  217. // obj.Place = gameMap.GetPlaceType((GameObj)obj);
  218. obj.CanMove = false;
  219. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  220. }
  221. );
  222. availableCellForGenerateProp = new List<XY>();
  223. for (int i = 0; i < gameMap.protoGameMap.GetLength(0); i++)
  224. {
  225. for (int j = 0; j < gameMap.protoGameMap.GetLength(1); j++)
  226. {
  227. if (gameMap.protoGameMap[i, j] == (int)PlaceType.Null)
  228. {
  229. availableCellForGenerateProp.Add(GameData.GetCellCenterPos(i, j));
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }