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

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