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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. pickProp = player.PropInventory[indexing] = ((Prop?)gameMap.OneInTheSameCell(player.Position, GameObjType.Prop)) ?? new NullProp();
  60. }
  61. else
  62. {
  63. gameMap.GameObjLockDict[GameObjType.Prop].EnterReadLock();
  64. try
  65. {
  66. foreach (Prop prop in gameMap.GameObjDict[GameObjType.Prop])
  67. {
  68. if (prop.GetPropType() == propType)
  69. {
  70. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  71. {
  72. pickProp = player.PropInventory[indexing] = prop;
  73. }
  74. }
  75. }
  76. }
  77. finally
  78. {
  79. gameMap.GameObjLockDict[GameObjType.Prop].ExitReadLock();
  80. }
  81. }
  82. if (pickProp.GetPropType() != PropType.Null)
  83. {
  84. gameMap.Remove(pickProp);
  85. gameMap.Add(new PickedProp(pickProp));
  86. return true;
  87. }
  88. else
  89. return false;
  90. }
  91. public void ThrowProp(Character player, int indexing)
  92. {
  93. if (!gameMap.Timer.IsGaming || player.IsResetting)
  94. return;
  95. Prop prop = player.UseProp(indexing);
  96. if (prop.GetPropType() == PropType.Null)
  97. return;
  98. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  99. gameMap.Add(prop);
  100. }
  101. private Prop ProduceOnePropNotKey(Random r, XY Pos)
  102. {
  103. switch (r.Next(0, GameData.numOfPropTypeNotKey))
  104. {
  105. case 0:
  106. return new AddLIFE(Pos, gameMap.GetPlaceType(Pos));
  107. case 1:
  108. return new AddSpeed(Pos, gameMap.GetPlaceType(Pos));
  109. case 2:
  110. return new Shield(Pos, gameMap.GetPlaceType(Pos));
  111. case 3:
  112. return new Spear(Pos, gameMap.GetPlaceType(Pos));
  113. default:
  114. return null;
  115. }
  116. }
  117. private Chest GetChest(Random r)
  118. {
  119. int index = r.Next(0, GameData.numOfChest);
  120. while (((Chest)(gameMap.GameObjDict[GameObjType.Chest][index])).PropInChest[0].GetPropType() != PropType.Null) index = (index + 1) % GameData.numOfChest;
  121. return (Chest)(gameMap.GameObjDict[GameObjType.Chest][index]);
  122. }
  123. public void StartProducing()
  124. {
  125. int len = availableCellForGenerateProp.Count;
  126. Random r = new Random(Environment.TickCount);
  127. gameMap.GameObjLockDict[GameObjType.Chest].EnterWriteLock();
  128. try
  129. {
  130. int cou = 0;
  131. while (cou < GameData.numOfKeyEachArea)
  132. {
  133. ++cou;
  134. Chest chest = GetChest(r);
  135. chest.PropInChest[1] = new Key3(chest.Position, PlaceType.Chest);
  136. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  137. }
  138. cou = 0;
  139. while (cou < GameData.numOfKeyEachArea)
  140. {
  141. ++cou;
  142. Chest chest = GetChest(r);
  143. chest.PropInChest[1] = new Key5(chest.Position, PlaceType.Chest);
  144. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  145. }
  146. cou = 0;
  147. while (cou < GameData.numOfKeyEachArea)
  148. {
  149. ++cou;
  150. Chest chest = GetChest(r);
  151. chest.PropInChest[1] = new Key6(chest.Position, PlaceType.Chest);
  152. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  153. }
  154. foreach (Chest chest in gameMap.GameObjDict[GameObjType.Chest])
  155. {
  156. if (chest.PropInChest[0].GetPropType() == PropType.Null)
  157. {
  158. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  159. chest.PropInChest[1] = ProduceOnePropNotKey(r, chest.Position);
  160. }
  161. }
  162. }
  163. finally
  164. {
  165. gameMap.GameObjLockDict[GameObjType.Chest].ExitWriteLock();
  166. }
  167. new Thread
  168. (
  169. () =>
  170. {
  171. while (!gameMap.Timer.IsGaming)
  172. Thread.Sleep(1000);
  173. new FrameRateTaskExecutor<int>(
  174. () => gameMap.Timer.IsGaming,
  175. () =>
  176. {
  177. int rand = r.Next(0, len);
  178. XY randPos = availableCellForGenerateProp[rand];
  179. gameMap.Add(ProduceOnePropNotKey(r, randPos));
  180. },
  181. GameData.PropProduceTime,
  182. () => 0
  183. )
  184. .Start();
  185. }
  186. )
  187. { IsBackground = true }.Start();
  188. }
  189. public PropManager(Map gameMap) // 道具不能扔过墙
  190. {
  191. this.gameMap = gameMap;
  192. /* this.moveEngine = new MoveEngine(
  193. gameMap: gameMap,
  194. OnCollision: (obj, collision, moveVec) =>
  195. { return MoveEngine.AfterCollision.MoveMax; },
  196. EndMove: obj =>
  197. {
  198. // obj.Place = gameMap.GetPlaceType((GameObj)obj);
  199. obj.CanMove = false;
  200. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  201. }
  202. );*/
  203. availableCellForGenerateProp = new List<XY>();
  204. for (int i = 0; i < gameMap.protoGameMap.GetLength(0); i++)
  205. {
  206. for (int j = 0; j < gameMap.protoGameMap.GetLength(1); j++)
  207. {
  208. if (gameMap.protoGameMap[i, j] == (int)PlaceType.Null)
  209. {
  210. availableCellForGenerateProp.Add(GameData.GetCellCenterPos(i, j));
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }