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