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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.ShieldOrSpear:
  28. if (player.IsGhost())
  29. player.AddSpear(GameData.PropDuration);
  30. else player.AddShield(GameData.PropDuration);
  31. break;
  32. case PropType.AddLifeOrAp:
  33. if (!player.IsGhost())
  34. player.AddLIFE(GameData.PropDuration);
  35. else player.AddAp(GameData.PropDuration);
  36. break;
  37. case PropType.AddSpeed:
  38. player.AddMoveSpeed(GameData.PropDuration);
  39. break;
  40. case PropType.AddHpOrAp:
  41. if (!player.IsGhost())
  42. player.HP += GameData.basicTreatmentDegree;
  43. else player.AddAp(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. int indexing = player.IndexingOfAddProp();
  60. if (indexing == GameData.maxNumOfPropInPropInventory)
  61. return false;
  62. Prop pickProp = new NullProp();
  63. if (propType == PropType.Null) // 自动检查有无道具可捡
  64. {
  65. pickProp = player.PropInventory[indexing] = ((Prop?)gameMap.OneInTheSameCell(player.Position, GameObjType.Prop)) ?? new NullProp();
  66. }
  67. else
  68. {
  69. gameMap.GameObjLockDict[GameObjType.Prop].EnterReadLock();
  70. try
  71. {
  72. foreach (Prop prop in gameMap.GameObjDict[GameObjType.Prop])
  73. {
  74. if (prop.GetPropType() == propType)
  75. {
  76. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  77. {
  78. pickProp = player.PropInventory[indexing] = prop;
  79. }
  80. }
  81. }
  82. }
  83. finally
  84. {
  85. gameMap.GameObjLockDict[GameObjType.Prop].ExitReadLock();
  86. }
  87. }
  88. if (pickProp.GetPropType() != PropType.Null)
  89. {
  90. gameMap.Remove(pickProp);
  91. gameMap.Add(new PickedProp(pickProp));
  92. return true;
  93. }
  94. else
  95. return false;
  96. }
  97. public void ThrowProp(Character player, int indexing)
  98. {
  99. if (!gameMap.Timer.IsGaming || player.IsResetting)
  100. return;
  101. Prop prop = player.UseProp(indexing);
  102. if (prop.GetPropType() == PropType.Null)
  103. return;
  104. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  105. gameMap.Add(prop);
  106. }
  107. private Prop ProduceOnePropNotKey(Random r, XY Pos)
  108. {
  109. return PropFactory.GetProp((PropType)r.Next(0, GameData.numOfPropTypeNotKey), Pos, gameMap.GetPlaceType(Pos));
  110. }
  111. private Chest GetChest(Random r)
  112. {
  113. int index = r.Next(0, GameData.numOfChest);
  114. while (((Chest)(gameMap.GameObjDict[GameObjType.Chest][index])).PropInChest[0].GetPropType() != PropType.Null) index = (index + 1) % GameData.numOfChest;
  115. return (Chest)(gameMap.GameObjDict[GameObjType.Chest][index]);
  116. }
  117. public void StartProducing()
  118. {
  119. int len = availableCellForGenerateProp.Count;
  120. Random r = new Random(Environment.TickCount);
  121. gameMap.GameObjLockDict[GameObjType.Chest].EnterWriteLock();
  122. try
  123. {
  124. int cou = 0;
  125. while (cou < GameData.numOfKeyEachArea)
  126. {
  127. ++cou;
  128. Chest chest = GetChest(r);
  129. chest.PropInChest[1] = new Key3(chest.Position, PlaceType.Chest);
  130. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  131. }
  132. cou = 0;
  133. while (cou < GameData.numOfKeyEachArea)
  134. {
  135. ++cou;
  136. Chest chest = GetChest(r);
  137. chest.PropInChest[1] = new Key5(chest.Position, PlaceType.Chest);
  138. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  139. }
  140. cou = 0;
  141. while (cou < GameData.numOfKeyEachArea)
  142. {
  143. ++cou;
  144. Chest chest = GetChest(r);
  145. chest.PropInChest[1] = new Key6(chest.Position, PlaceType.Chest);
  146. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  147. }
  148. foreach (Chest chest in gameMap.GameObjDict[GameObjType.Chest])
  149. {
  150. if (chest.PropInChest[0].GetPropType() == PropType.Null)
  151. {
  152. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  153. chest.PropInChest[1] = ProduceOnePropNotKey(r, chest.Position);
  154. }
  155. }
  156. }
  157. finally
  158. {
  159. gameMap.GameObjLockDict[GameObjType.Chest].ExitWriteLock();
  160. }
  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.Add(ProduceOnePropNotKey(r, randPos));
  174. },
  175. GameData.PropProduceTime,
  176. () => 0
  177. )
  178. .Start();
  179. }
  180. )
  181. { IsBackground = true }.Start();
  182. }
  183. public PropManager(Map gameMap) // 道具不能扔过墙
  184. {
  185. this.gameMap = gameMap;
  186. /* this.moveEngine = new MoveEngine(
  187. gameMap: gameMap,
  188. OnCollision: (obj, collision, moveVec) =>
  189. { return MoveEngine.AfterCollision.MoveMax; },
  190. EndMove: obj =>
  191. {
  192. // obj.Place = gameMap.GetPlaceType((GameObj)obj);
  193. obj.CanMove = false;
  194. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  195. }
  196. );*/
  197. availableCellForGenerateProp = new List<XY>();
  198. for (int i = 0; i < gameMap.protoGameMap.GetLength(0); i++)
  199. {
  200. for (int j = 0; j < gameMap.protoGameMap.GetLength(1); j++)
  201. {
  202. if (gameMap.protoGameMap[i, j] == (int)PlaceType.Null)
  203. {
  204. availableCellForGenerateProp.Add(GameData.GetCellCenterPos(i, j));
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }