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 10 kB

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