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

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