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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. dropProp.SetNewPos(GameData.GetCellCenterPos(player.Position.x / GameData.numOfPosGridPerCell, player.Position.y / GameData.numOfPosGridPerCell));
  107. }
  108. player.PropInventory = pickProp;
  109. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  110. try
  111. {
  112. gameMap.GameObjDict[GameObjType.Prop].Remove((Preparation.Interface.IGameObj)pickProp);
  113. if (dropProp != null)
  114. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)dropProp);
  115. }
  116. finally
  117. {
  118. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  119. }
  120. gameMap.GameObjLockDict[GameObjType.PickedProp].EnterWriteLock();
  121. try
  122. {
  123. gameMap.GameObjDict[GameObjType.PickedProp].Add(new PickedProp(pickProp));
  124. }
  125. finally
  126. {
  127. gameMap.GameObjLockDict[GameObjType.PickedProp].ExitWriteLock();
  128. }
  129. return true;
  130. }
  131. else
  132. return false;
  133. }
  134. public void ThrowProp(Character player, int timeInMilliseconds, double angle)
  135. {
  136. if (!gameMap.Timer.IsGaming)
  137. return;
  138. if (player.IsResetting) // 移动中也能扔,但由于“惯性”,可能初始位置会有点变化
  139. return;
  140. Prop? prop = player.UseProp();
  141. if (prop == null)
  142. return;
  143. prop.CanMove = true;
  144. prop.SetNewPos(player.Position);
  145. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  146. try
  147. {
  148. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)prop);
  149. }
  150. finally
  151. {
  152. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  153. }
  154. timeInMilliseconds = timeInMilliseconds < GameData.PropMaxMoveDistance / prop.MoveSpeed * 1000 ? timeInMilliseconds : GameData.PropMaxMoveDistance / prop.MoveSpeed * 1000;
  155. moveEngine.MoveObj(prop, timeInMilliseconds, angle);
  156. }
  157. private void ProduceProp()
  158. {
  159. int len = availableCellForGenerateProp.Count;
  160. Random r = new Random(Environment.TickCount);
  161. new Thread
  162. (
  163. () =>
  164. {
  165. while (!gameMap.Timer.IsGaming)
  166. Thread.Sleep(1000);
  167. new FrameRateTaskExecutor<int>(
  168. () => gameMap.Timer.IsGaming,
  169. () =>
  170. {
  171. int rand = r.Next(0, len);
  172. XY randPos = availableCellForGenerateProp[rand];
  173. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  174. try
  175. {
  176. switch (r.Next(0, 4))
  177. {
  178. case 0:
  179. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new AddLIFE(randPos));
  180. break;
  181. case 1:
  182. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new AddSpeed(randPos));
  183. break;
  184. case 2:
  185. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new Shield(randPos));
  186. break;
  187. case 3:
  188. gameMap.GameObjDict[GameObjType.Prop].Add((Preparation.Interface.IGameObj)new Spear(randPos));
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. finally
  195. {
  196. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  197. }
  198. },
  199. GameData.PropProduceTime,
  200. () => 0
  201. )
  202. .Start();
  203. }
  204. )
  205. { IsBackground = true }.Start();
  206. }
  207. public PropManager(Map gameMap) // 道具不能扔过墙
  208. {
  209. this.gameMap = gameMap;
  210. this.moveEngine = new MoveEngine(
  211. gameMap: gameMap,
  212. OnCollision: (obj, collision, moveVec) =>
  213. { return MoveEngine.AfterCollision.MoveMax; },
  214. EndMove: obj =>
  215. {
  216. // obj.Place = gameMap.GetPlaceType((GameObj)obj);
  217. obj.CanMove = false;
  218. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  219. }
  220. );
  221. availableCellForGenerateProp = new List<XY>();
  222. for (int i = 0; i < gameMap.ProtoGameMap.GetLength(0); i++)
  223. {
  224. for (int j = 0; j < gameMap.ProtoGameMap.GetLength(1); j++)
  225. {
  226. if (gameMap.ProtoGameMap[i, j] == (int)PlaceType.Null)
  227. {
  228. availableCellForGenerateProp.Add(GameData.GetCellCenterPos(i, j));
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }