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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System.Collections.Generic;
  2. using GameClass.GameObj;
  3. using System.Threading;
  4. using Preparation.GameData;
  5. using Preparation.Utility;
  6. using System;
  7. using Timothy.FrameRateTask;
  8. using GameEngine;
  9. namespace Gaming
  10. {
  11. public partial class Game
  12. {
  13. private readonly PropManager propManager;
  14. private class PropManager
  15. {
  16. private readonly Map gameMap;
  17. private MoveEngine moveEngine;
  18. private bool isProducingProp = false;
  19. private readonly List<XY> availableCellForGenerateProp;
  20. public void StartProducing()
  21. {
  22. if (isProducingProp)
  23. return;
  24. isProducingProp = true;
  25. ProduceProp();
  26. }
  27. public void UseProp(Character player)
  28. {
  29. if (player.IsResetting)
  30. return;
  31. Prop? prop = player.UseProp();
  32. switch (prop?.GetPropType())
  33. {
  34. case PropType.Spear:
  35. player.AddSpear(GameData.PropDuration);
  36. break;
  37. case PropType.Shield:
  38. player.AddShield(GameData.PropDuration);
  39. break;
  40. case PropType.addLIFE:
  41. player.AddLIFE(GameData.PropDuration);
  42. break;
  43. case PropType.addSpeed:
  44. player.AddMoveSpeed(GameData.PropDuration);
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. /// <param name="player"></param>
  54. /// <param name="propType">若不指定,则自动判断可捡起什么道具</param>
  55. /// <returns></returns>
  56. public bool PickProp(Character player, PropType propType = PropType.Null)
  57. {
  58. if (player.IsResetting)
  59. return false;
  60. Prop? pickProp = null;
  61. if (propType == PropType.Null) // 自动检查有无道具可捡
  62. {
  63. gameMap.GameObjLockDict[GameObjIdx.Prop].EnterReadLock();
  64. try
  65. {
  66. foreach (Prop prop in gameMap.GameObjDict[GameObjIdx.Prop])
  67. {
  68. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  69. {
  70. pickProp = prop;
  71. }
  72. }
  73. }
  74. finally
  75. {
  76. gameMap.GameObjLockDict[GameObjIdx.Prop].ExitReadLock();
  77. }
  78. }
  79. else
  80. {
  81. gameMap.GameObjLockDict[GameObjIdx.Prop].EnterReadLock();
  82. try
  83. {
  84. foreach (Prop prop in gameMap.GameObjDict[GameObjIdx.Prop])
  85. {
  86. if (prop.GetPropType() == propType)
  87. {
  88. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  89. {
  90. pickProp = prop;
  91. }
  92. }
  93. }
  94. }
  95. finally
  96. {
  97. gameMap.GameObjLockDict[GameObjIdx.Prop].ExitReadLock();
  98. }
  99. }
  100. if (pickProp != null)
  101. {
  102. // pickProp.CanMove = false;
  103. Prop? dropProp = null;
  104. if (player.PropInventory != null) // 若角色原来有道具,则原始道具掉落在原地
  105. {
  106. dropProp = player.PropInventory;
  107. dropProp.SetNewPos(GameData.GetCellCenterPos(player.Position.x / GameData.numOfPosGridPerCell, player.Position.y / GameData.numOfPosGridPerCell));
  108. }
  109. player.PropInventory = pickProp;
  110. gameMap.GameObjLockDict[GameObjIdx.Prop].EnterWriteLock();
  111. try
  112. {
  113. gameMap.GameObjDict[GameObjIdx.Prop].Remove(pickProp);
  114. if (dropProp != null)
  115. gameMap.GameObjDict[GameObjIdx.Prop].Add(dropProp);
  116. }
  117. finally
  118. {
  119. gameMap.GameObjLockDict[GameObjIdx.Prop].ExitWriteLock();
  120. }
  121. gameMap.GameObjLockDict[GameObjIdx.PickedProp].EnterWriteLock();
  122. try
  123. {
  124. gameMap.GameObjDict[GameObjIdx.PickedProp].Add(new PickedProp(pickProp));
  125. }
  126. finally
  127. {
  128. gameMap.GameObjLockDict[GameObjIdx.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.SetNewPos(player.Position);
  146. gameMap.GameObjLockDict[GameObjIdx.Prop].EnterWriteLock();
  147. try
  148. {
  149. gameMap.GameObjDict[GameObjIdx.Prop].Add(prop);
  150. }
  151. finally
  152. {
  153. gameMap.GameObjLockDict[GameObjIdx.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[GameObjIdx.Prop].EnterWriteLock();
  175. try
  176. {
  177. switch (r.Next(0, 4))
  178. {
  179. case 0:
  180. gameMap.GameObjDict[GameObjIdx.Prop].Add(new AddLIFE(randPos));
  181. break;
  182. case 1:
  183. gameMap.GameObjDict[GameObjIdx.Prop].Add(new AddSpeed(randPos));
  184. break;
  185. case 2:
  186. gameMap.GameObjDict[GameObjIdx.Prop].Add(new Shield(randPos));
  187. break;
  188. case 3:
  189. gameMap.GameObjDict[GameObjIdx.Prop].Add(new Spear(randPos));
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. finally
  196. {
  197. gameMap.GameObjLockDict[GameObjIdx.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. 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)MapInfo.MapInfoObjType.Null)
  227. {
  228. availableCellForGenerateProp.Add(GameData.GetCellCenterPos(i, j));
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }