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

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