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

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