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.

ActionManager.cs 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. using GameClass.GameObj;
  5. using GameEngine;
  6. using Preparation.Utility;
  7. using Timothy.FrameRateTask;
  8. namespace Gaming
  9. {
  10. public partial class Game
  11. {
  12. private readonly ActionManager actionManager;
  13. private class ActionManager
  14. {
  15. // 人物移动
  16. public bool MovePlayer(Character playerToMove, int moveTimeInMilliseconds, double moveDirection)
  17. {
  18. if (playerToMove.PlayerState != PlayerStateType.Null) return false;
  19. moveEngine.MoveObj(playerToMove, moveTimeInMilliseconds, moveDirection);
  20. return true;
  21. }
  22. public bool Fix(Student player)// 自动检查有无发电机可修
  23. {
  24. if (player.PlayerState != PlayerStateType.Null || player.IsGhost())
  25. return false;
  26. Generator? generatorForFix = null;
  27. gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
  28. try
  29. {
  30. foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
  31. {
  32. if (GameData.ApproachToInteract(generator.Position, player.Position))
  33. {
  34. generatorForFix = generator;
  35. break;
  36. }
  37. }
  38. }
  39. finally
  40. {
  41. gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
  42. }
  43. if (generatorForFix == null || generatorForFix.DegreeOfFRepair == GameData.degreeOfFixedGenerator)
  44. return false;
  45. player.PlayerState = PlayerStateType.IsFixing;
  46. new Thread
  47. (
  48. () =>
  49. {
  50. new FrameRateTaskExecutor<int>(
  51. loopCondition: () => player.PlayerState == PlayerStateType.IsFixing && gameMap.Timer.IsGaming && generatorForFix.DegreeOfFRepair < GameData.degreeOfFixedGenerator && GameData.ApproachToInteract(player.Position, generatorForFix.Position),
  52. loopToDo: () =>
  53. {
  54. return !generatorForFix.Repair(player.FixSpeed * GameData.frameDuration);
  55. },
  56. timeInterval: GameData.frameDuration,
  57. finallyReturn: () => 0
  58. )
  59. .Start();
  60. if (generatorForFix.DegreeOfFRepair == GameData.degreeOfFixedGenerator)
  61. {
  62. gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
  63. try
  64. {
  65. Doorway exit = (Doorway)gameMap.GameObjDict[GameObjType.Doorway][1];
  66. if (!exit.PowerSupply)
  67. {
  68. int numOfFixedGenerator = 0;
  69. foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
  70. if (generator.DegreeOfFRepair == GameData.degreeOfFixedGenerator)
  71. ++numOfFixedGenerator;
  72. if (numOfFixedGenerator >= GameData.numOfGeneratorRequiredForRepair)
  73. {
  74. gameMap.GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  75. try
  76. {
  77. foreach (Doorway doorway in gameMap.GameObjDict[GameObjType.Doorway])
  78. doorway.PowerSupply = true;
  79. }
  80. finally
  81. {
  82. gameMap.GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  83. }
  84. }
  85. }
  86. }
  87. finally
  88. {
  89. gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
  90. }
  91. }
  92. }
  93. )
  94. { IsBackground = true }.Start();
  95. return true;
  96. }
  97. public bool Escape(Student player)
  98. {
  99. if (!(player.PlayerState == PlayerStateType.Null || player.PlayerState == PlayerStateType.IsMoving) || player.IsGhost())
  100. return false;
  101. Doorway? doorwayForEscape = null;
  102. gameMap.GameObjLockDict[GameObjType.Doorway].EnterReadLock();
  103. try
  104. {
  105. foreach (Doorway doorway in gameMap.GameObjDict[GameObjType.Doorway])
  106. {
  107. if (GameData.IsInTheSameCell(doorway.Position, player.Position))
  108. {
  109. doorwayForEscape = doorway;
  110. break;
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. gameMap.GameObjLockDict[GameObjType.Doorway].ExitReadLock();
  117. }
  118. if (doorwayForEscape != null && doorwayForEscape.IsOpen)
  119. {
  120. player.Die(PlayerStateType.IsEscaped);
  121. return true;
  122. }
  123. else
  124. return false;
  125. }
  126. public bool Treat(Student player, Student playerTreated)
  127. {
  128. if (playerTreated.PlayerState == PlayerStateType.Null || player.PlayerState == PlayerStateType.Null || playerTreated.HP == playerTreated.MaxHp || !GameData.ApproachToInteract(playerTreated.Position, player.Position))
  129. return false;
  130. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  131. {
  132. playerTreated.HP = playerTreated.MaxHp;
  133. playerTreated.DegreeOfTreatment = 0;
  134. return false;
  135. }
  136. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  137. {
  138. playerTreated.HP += GameData.basicTreatmentDegree;
  139. playerTreated.DegreeOfTreatment = 0;
  140. return false;
  141. }
  142. new Thread
  143. (
  144. () =>
  145. {
  146. new FrameRateTaskExecutor<int>(
  147. loopCondition: () => playerTreated.PlayerState == PlayerStateType.IsTreated && player.PlayerState == PlayerStateType.IsTreating && gameMap.Timer.IsGaming && playerTreated.HP + playerTreated.DegreeOfTreatment < playerTreated.MaxHp && playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree && GameData.ApproachToInteract(playerTreated.Position, player.Position),
  148. loopToDo: () =>
  149. {
  150. playerTreated.DegreeOfTreatment += GameData.frameDuration * player.TreatSpeed;
  151. },
  152. timeInterval: GameData.frameDuration,
  153. finallyReturn: () => 0
  154. )
  155. .Start();
  156. if (playerTreated.PlayerState == PlayerStateType.IsTreated) playerTreated.PlayerState = PlayerStateType.Null;
  157. if (player.PlayerState == PlayerStateType.IsTreating) player.PlayerState = PlayerStateType.Null;
  158. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  159. {
  160. playerTreated.HP = playerTreated.MaxHp;
  161. playerTreated.DegreeOfTreatment = 0;
  162. }
  163. else
  164. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  165. {
  166. playerTreated.HP += GameData.basicTreatmentDegree;
  167. playerTreated.DegreeOfTreatment = 0;
  168. }
  169. }
  170. )
  171. { IsBackground = true }.Start();
  172. return true;
  173. }
  174. public bool Rescue(Student player, Student playerRescued)
  175. {
  176. if (player.PlayerState != PlayerStateType.Null || playerRescued.PlayerState != PlayerStateType.IsAddicted || !GameData.ApproachToInteract(playerRescued.Position, player.Position))
  177. return false;
  178. player.PlayerState = PlayerStateType.IsRescuing;
  179. playerRescued.PlayerState = PlayerStateType.IsRescued;
  180. int rescuedDegree = 0;
  181. new Thread
  182. (
  183. () =>
  184. {
  185. new FrameRateTaskExecutor<int>(
  186. loopCondition: () => playerRescued.PlayerState == PlayerStateType.IsRescued && player.PlayerState == PlayerStateType.IsRescuing && gameMap.Timer.IsGaming && GameData.ApproachToInteract(playerRescued.Position, player.Position),
  187. loopToDo: () =>
  188. {
  189. rescuedDegree += GameData.frameDuration;
  190. },
  191. timeInterval: GameData.frameDuration,
  192. finallyReturn: () => 0,
  193. maxTotalDuration: 1000
  194. )
  195. .Start();
  196. if (rescuedDegree == 1000)
  197. {
  198. if (playerRescued.PlayerState == PlayerStateType.IsRescued) playerRescued.PlayerState = PlayerStateType.Null;
  199. if (player.PlayerState == PlayerStateType.IsRescuing) player.PlayerState = PlayerStateType.Null;
  200. }
  201. else
  202. {
  203. if (playerRescued.PlayerState == PlayerStateType.IsRescued) playerRescued.PlayerState = PlayerStateType.Null;
  204. if (player.PlayerState == PlayerStateType.IsRescuing) player.PlayerState = PlayerStateType.IsAddicted;
  205. }
  206. }
  207. )
  208. { IsBackground = true }.Start();
  209. return true;
  210. }
  211. /*
  212. private void ActivateMine(Character player, Mine mine)
  213. {
  214. gameMap.ObjListLock.EnterWriteLock();
  215. try { gameMap.ObjList.Remove(mine); }
  216. catch { }
  217. finally { gameMap.ObjListLock.ExitWriteLock(); }
  218. switch (mine.GetPropType())
  219. {
  220. case PropType.Dirt:
  221. player.AddMoveSpeed(Constant.dirtMoveSpeedDebuff, Constant.buffPropTime);
  222. break;
  223. case PropType.Attenuator:
  224. player.AddAP(Constant.attenuatorAtkDebuff, Constant.buffPropTime);
  225. break;
  226. case PropType.Divider:
  227. player.ChangeCD(Constant.dividerCdDiscount, Constant.buffPropTime);
  228. break;
  229. }
  230. }
  231. */
  232. private readonly Map gameMap;
  233. private readonly MoveEngine moveEngine;
  234. public ActionManager(Map gameMap)
  235. {
  236. this.gameMap = gameMap;
  237. this.moveEngine = new MoveEngine(
  238. gameMap: gameMap,
  239. OnCollision: (obj, collisionObj, moveVec) =>
  240. {
  241. //if (collisionObj is Mine)
  242. //{
  243. // ActivateMine((Character)obj, (Mine)collisionObj);
  244. // return MoveEngine.AfterCollision.ContinueCheck;
  245. //}
  246. return MoveEngine.AfterCollision.MoveMax;
  247. },
  248. EndMove: obj =>
  249. {
  250. // Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  251. }
  252. );
  253. }
  254. }
  255. }
  256. }