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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 ConsumeProp(Character player, PropType propType)
  19. {
  20. if (player.CharacterType == CharacterType.Robot || player.IsRemoved)
  21. return;
  22. Gadget prop = player.ConsumeProp(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.HP.GetMaxV())
  46. {
  47. player.HP.AddPositiveV(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. foreach (Gadget prop in gameMap.GameObjDict[GameObjType.Gadget])
  83. {
  84. if (prop.GetPropType() == propType)
  85. {
  86. if (GameData.IsInTheSameCell(prop.Position, player.Position) && prop.CanMove == false)
  87. {
  88. pickProp = player.PropInventory[indexing] = prop;
  89. }
  90. }
  91. }
  92. }
  93. if (pickProp.GetPropType() != PropType.Null)
  94. {
  95. gameMap.RemoveJustFromMap(pickProp);
  96. //gameMap.Add(new Item(pickProp));
  97. return true;
  98. }
  99. else
  100. return false;
  101. }
  102. public void ThrowProp(Character player, PropType propType)
  103. {
  104. if (!gameMap.Timer.IsGaming || player.IsRemoved)
  105. return;
  106. Gadget prop = player.ConsumeProp(propType);
  107. if (prop.GetPropType() == PropType.Null)
  108. return;
  109. prop.ReSetPos(player.Position);
  110. gameMap.Add(prop);
  111. }
  112. private static Gadget ProduceOnePropNotKey(Random r, XY Pos)
  113. {
  114. return PropFactory.GetConsumables((PropType)r.Next(GameData.numOfTeachingBuilding + 1, GameData.numOfPropSpecies + 1), Pos);
  115. }
  116. private Chest GetChest(Random r)
  117. {
  118. int index = r.Next(0, GameData.numOfChest);
  119. while (((Chest)(gameMap.GameObjDict[GameObjType.Chest][index])).PropInChest[0].GetPropType() != PropType.Null) index = (index + 1) % GameData.numOfChest;
  120. return (Chest)(gameMap.GameObjDict[GameObjType.Chest][index]);
  121. }
  122. public void StartProducing()
  123. {
  124. int len = availableCellForGenerateProp.Count;
  125. Random r = new Random(Environment.TickCount);
  126. int cou = 0;
  127. while (cou < GameData.numOfKeyEachArea)
  128. {
  129. ++cou;
  130. Chest chest = GetChest(r);
  131. chest.PropInChest[1] = new Key3(chest.Position);
  132. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  133. }
  134. cou = 0;
  135. while (cou < GameData.numOfKeyEachArea)
  136. {
  137. ++cou;
  138. Chest chest = GetChest(r);
  139. chest.PropInChest[1] = new Key5(chest.Position);
  140. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  141. }
  142. cou = 0;
  143. while (cou < GameData.numOfKeyEachArea)
  144. {
  145. ++cou;
  146. Chest chest = GetChest(r);
  147. chest.PropInChest[1] = new Key6(chest.Position);
  148. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  149. }
  150. foreach (Chest chest in gameMap.GameObjDict[GameObjType.Chest])
  151. {
  152. if (chest.PropInChest[0].GetPropType() == PropType.Null)
  153. {
  154. chest.PropInChest[0] = ProduceOnePropNotKey(r, chest.Position);
  155. chest.PropInChest[1] = ProduceOnePropNotKey(r, chest.Position);
  156. }
  157. }
  158. /*
  159. new Thread
  160. (
  161. () =>
  162. {
  163. while (!gameMap.Timer.IsGaming)
  164. Thread.Sleep(1000);
  165. new FrameRateTaskExecutor<int>(
  166. () => gameMap.Timer.IsGaming,
  167. () =>
  168. {
  169. int rand = r.Next(0, len);
  170. XY randPos = availableCellForGenerateProp[rand];
  171. gameMap.Add(ProduceOnePropNotKey(r, randPos));
  172. },
  173. GameData.PropProduceTime,
  174. () => 0
  175. )
  176. .Start();
  177. }
  178. )
  179. { IsBackground = true }.Start();
  180. */
  181. }
  182. public PropManager(Map gameMap, CharacterManager characterManager) // 道具不能扔过墙
  183. {
  184. this.characterManager = characterManager;
  185. this.gameMap = gameMap;
  186. /* this.moveEngine = new MoveEngine(
  187. gameMap: gameMap,
  188. OnCollision: (obj, collision, moveVec) =>
  189. { return MoveEngine.AfterCollision.MoveMax; },
  190. EndMove: obj =>
  191. {
  192. // obj.Place = gameMap.GetPlaceType((GameObj)obj);
  193. obj.CanMove = false;
  194. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  195. }
  196. );*/
  197. availableCellForGenerateProp = new List<XY>();
  198. for (int i = 0; i < gameMap.protoGameMap.GetLength(0); i++)
  199. {
  200. for (int j = 0; j < gameMap.protoGameMap.GetLength(1); j++)
  201. {
  202. if (gameMap.protoGameMap[i, j] == (int)PlaceType.Null)
  203. {
  204. availableCellForGenerateProp.Add(GameData.GetCellCenterPos(i, j));
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }