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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using System;
  2. using System.Threading;
  3. using GameClass.GameObj;
  4. using GameEngine;
  5. using Preparation.Interface;
  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.Commandable()) return false;
  19. moveEngine.MoveObj(playerToMove, moveTimeInMilliseconds, moveDirection);
  20. return true;
  21. }
  22. public static bool Stop(Character player)
  23. {
  24. if (player.Commandable())
  25. {
  26. player.PlayerState = PlayerStateType.Null;
  27. return true;
  28. }
  29. return false;
  30. }
  31. public bool Fix(Student player)// 自动检查有无发电机可修
  32. {
  33. if ((!player.Commandable()) || player.PlayerState == PlayerStateType.Fixing)
  34. return false;
  35. Generator? generatorForFix = (Generator?)gameMap.OneForInteract(player.Position, GameObjType.Generator);
  36. if (generatorForFix == null || generatorForFix.DegreeOfRepair == GameData.degreeOfFixedGenerator)
  37. return false;
  38. player.PlayerState = PlayerStateType.Fixing;
  39. new Thread
  40. (
  41. () =>
  42. {
  43. int ScoreAdded = GameData.StudentScoreFix(generatorForFix.DegreeOfRepair);
  44. new FrameRateTaskExecutor<int>(
  45. loopCondition: () => player.PlayerState == PlayerStateType.Fixing && gameMap.Timer.IsGaming && generatorForFix.DegreeOfRepair < GameData.degreeOfFixedGenerator,
  46. loopToDo: () =>
  47. {
  48. generatorForFix.Repair(player.FixSpeed * GameData.frameDuration);
  49. player.AddScore(GameData.StudentScoreFix(generatorForFix.DegreeOfRepair - ScoreAdded));
  50. ScoreAdded = GameData.StudentScoreFix(generatorForFix.DegreeOfRepair);
  51. },
  52. timeInterval: GameData.frameDuration,
  53. finallyReturn: () => 0
  54. )
  55. .Start();
  56. if (generatorForFix.DegreeOfRepair >= GameData.degreeOfFixedGenerator)
  57. {
  58. gameMap.NumOfRepairedGenerators++;
  59. if (player.PlayerState == PlayerStateType.Fixing) player.PlayerState = PlayerStateType.Null;
  60. if (gameMap.NumOfRepairedGenerators == GameData.numOfGeneratorRequiredForEmergencyExit)
  61. {
  62. gameMap.GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  63. try
  64. {
  65. Random r = new Random(Environment.TickCount);
  66. ((EmergencyExit)(gameMap.GameObjDict[GameObjType.EmergencyExit][r.Next(0, gameMap.GameObjDict[GameObjType.EmergencyExit].Count)])).CanOpen = true;
  67. }
  68. finally
  69. {
  70. gameMap.GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  71. }
  72. }
  73. else
  74. if (gameMap.NumOfRepairedGenerators == GameData.numOfGeneratorRequiredForRepair)
  75. {
  76. gameMap.GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  77. try
  78. {
  79. foreach (Doorway doorway in gameMap.GameObjDict[GameObjType.Doorway])
  80. doorway.PowerSupply = true;
  81. }
  82. finally
  83. {
  84. gameMap.GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  85. }
  86. }
  87. }
  88. }
  89. )
  90. { IsBackground = true }.Start();
  91. return true;
  92. }
  93. public bool OpenDoorway(Student player)
  94. {
  95. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.OpeningTheDoorway)
  96. return false;
  97. Doorway? doorwayToOpen = (Doorway?)gameMap.OneForInteract(player.Position, GameObjType.Doorway);
  98. if (doorwayToOpen == null || doorwayToOpen.IsOpening || !doorwayToOpen.PowerSupply)
  99. return false;
  100. player.PlayerState = PlayerStateType.OpeningTheDoorway;
  101. doorwayToOpen.IsOpening = true;
  102. new Thread
  103. (
  104. () =>
  105. {
  106. new FrameRateTaskExecutor<int>(
  107. loopCondition: () => player.PlayerState == PlayerStateType.OpeningTheDoorway && gameMap.Timer.IsGaming && doorwayToOpen.OpenDegree < GameData.degreeOfOpenedDoorway,
  108. loopToDo: () =>
  109. {
  110. doorwayToOpen.OpenDegree += GameData.frameDuration;
  111. },
  112. timeInterval: GameData.frameDuration,
  113. finallyReturn: () => 0
  114. )
  115. .Start();
  116. doorwayToOpen.IsOpening = false;
  117. if (doorwayToOpen.OpenDegree >= GameData.degreeOfOpenedDoorway)
  118. {
  119. if (player.PlayerState == PlayerStateType.OpeningTheDoorway)
  120. player.PlayerState = PlayerStateType.Null;
  121. }
  122. }
  123. )
  124. { IsBackground = true }.Start();
  125. return true;
  126. }
  127. public bool Escape(Student player)
  128. {
  129. if (!(player.Commandable()))
  130. return false;
  131. Doorway? doorwayForEscape = (Doorway?)gameMap.OneInTheSameCell(player.Position, GameObjType.Doorway);
  132. if (doorwayForEscape != null)
  133. {
  134. if (doorwayForEscape.IsOpen())
  135. {
  136. player.AddScore(GameData.StudentScoreEscape);
  137. player.Die(PlayerStateType.Escaped);
  138. return true;
  139. }
  140. else
  141. return false;
  142. }
  143. else
  144. {
  145. EmergencyExit? emergencyExit = (EmergencyExit?)gameMap.OneInTheSameCell(player.Position, GameObjType.EmergencyExit);
  146. if (emergencyExit != null && emergencyExit.IsOpen)
  147. {
  148. player.Die(PlayerStateType.Escaped);
  149. return true;
  150. }
  151. else
  152. return false;
  153. }
  154. }
  155. public bool Treat(Student player, Student playerTreated)
  156. {
  157. if ((!player.Commandable()) || player.PlayerState == PlayerStateType.Treating ||
  158. (!playerTreated.Commandable()) ||
  159. playerTreated.HP == playerTreated.MaxHp || !GameData.ApproachToInteract(playerTreated.Position, player.Position))
  160. return false;
  161. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  162. {
  163. playerTreated.HP = playerTreated.MaxHp;
  164. playerTreated.DegreeOfTreatment = 0;
  165. return false;
  166. }
  167. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  168. {
  169. playerTreated.HP += GameData.basicTreatmentDegree;
  170. playerTreated.DegreeOfTreatment = 0;
  171. return false;
  172. }
  173. new Thread
  174. (
  175. () =>
  176. {
  177. new FrameRateTaskExecutor<int>(
  178. loopCondition: () => playerTreated.PlayerState == PlayerStateType.Treated && player.PlayerState == PlayerStateType.Treating && gameMap.Timer.IsGaming && playerTreated.HP + playerTreated.DegreeOfTreatment < playerTreated.MaxHp && playerTreated.DegreeOfTreatment < GameData.basicTreatmentDegree && GameData.ApproachToInteract(playerTreated.Position, player.Position),
  179. loopToDo: () =>
  180. {
  181. playerTreated.DegreeOfTreatment += GameData.frameDuration * player.TreatSpeed;
  182. },
  183. timeInterval: GameData.frameDuration,
  184. finallyReturn: () => 0
  185. )
  186. .Start();
  187. if (playerTreated.PlayerState == PlayerStateType.Treated) playerTreated.PlayerState = PlayerStateType.Null;
  188. if (player.PlayerState == PlayerStateType.Treating) player.PlayerState = PlayerStateType.Null;
  189. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  190. {
  191. player.AddScore(GameData.StudentScoreTreat(playerTreated.MaxHp - playerTreated.HP));
  192. playerTreated.HP = playerTreated.MaxHp;
  193. playerTreated.DegreeOfTreatment = 0;
  194. }
  195. else
  196. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  197. {
  198. player.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
  199. playerTreated.HP += GameData.basicTreatmentDegree;
  200. playerTreated.DegreeOfTreatment = 0;
  201. }
  202. }
  203. )
  204. { IsBackground = true }.Start();
  205. return true;
  206. }
  207. public bool Rescue(Student player, Student playerRescued)
  208. {
  209. if ((!player.Commandable()) || playerRescued.PlayerState != PlayerStateType.Addicted || !GameData.ApproachToInteract(playerRescued.Position, player.Position))
  210. return false;
  211. player.PlayerState = PlayerStateType.Rescuing;
  212. playerRescued.PlayerState = PlayerStateType.Rescued;
  213. player.TimeOfRescue = 0;
  214. new Thread
  215. (
  216. () =>
  217. {
  218. new FrameRateTaskExecutor<int>(
  219. loopCondition: () => playerRescued.PlayerState == PlayerStateType.Rescued && player.PlayerState == PlayerStateType.Rescuing && gameMap.Timer.IsGaming && GameData.ApproachToInteract(playerRescued.Position, player.Position),
  220. loopToDo: () =>
  221. {
  222. player.TimeOfRescue += GameData.frameDuration;
  223. },
  224. timeInterval: GameData.frameDuration,
  225. finallyReturn: () => 0,
  226. maxTotalDuration: GameData.basicTimeOfRescue
  227. )
  228. .Start();
  229. if (playerRescued.PlayerState == PlayerStateType.Rescued)
  230. {
  231. if (player.TimeOfRescue >= GameData.basicTimeOfRescue)
  232. {
  233. playerRescued.PlayerState = PlayerStateType.Null;
  234. player.AddScore(GameData.StudentScoreRescue);
  235. }
  236. else
  237. playerRescued.PlayerState = PlayerStateType.Addicted;
  238. }
  239. if (player.PlayerState == PlayerStateType.Rescuing) player.PlayerState = PlayerStateType.Null;
  240. player.TimeOfRescue = 0;
  241. }
  242. )
  243. { IsBackground = true }.Start();
  244. return true;
  245. }
  246. public bool OpenChest(Character player)
  247. {
  248. if ((!player.Commandable()) || player.PlayerState == PlayerStateType.OpeningTheChest)
  249. return false;
  250. Chest? chestToOpen = (Chest?)gameMap.OneForInteract(player.Position, GameObjType.Chest);
  251. if (chestToOpen == null || chestToOpen.IsOpen() || chestToOpen.OpenDegree > 0)
  252. return false;
  253. player.PlayerState = PlayerStateType.OpeningTheChest;
  254. new Thread
  255. (
  256. () =>
  257. {
  258. new FrameRateTaskExecutor<int>(
  259. loopCondition: () => player.PlayerState == PlayerStateType.OpeningTheChest && gameMap.Timer.IsGaming && (!chestToOpen.IsOpen()),
  260. loopToDo: () =>
  261. {
  262. chestToOpen.OpenDegree += GameData.frameDuration * player.SpeedOfOpenChest;
  263. },
  264. timeInterval: GameData.frameDuration,
  265. finallyReturn: () => 0
  266. )
  267. .Start();
  268. if (chestToOpen.IsOpen())
  269. {
  270. if (player.PlayerState == PlayerStateType.OpeningTheChest)
  271. player.PlayerState = PlayerStateType.Null;
  272. for (int i = 0; i < GameData.maxNumOfPropInChest; ++i)
  273. {
  274. Prop prop = chestToOpen.PropInChest[i];
  275. chestToOpen.PropInChest[i] = new NullProp();
  276. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  277. gameMap.Add(prop);
  278. }
  279. }
  280. else chestToOpen.OpenDegree = 0;
  281. }
  282. )
  283. { IsBackground = true }.Start();
  284. return true;
  285. }
  286. public bool ClimbingThroughWindow(Character player)
  287. {
  288. if (!player.Commandable())
  289. return false;
  290. Window? windowForClimb = (Window?)gameMap.OneForInteractInACross(player.Position, GameObjType.Window);
  291. if (windowForClimb == null || windowForClimb.WhoIsClimbing != null)
  292. return false;
  293. player.PlayerState = PlayerStateType.ClimbingThroughWindows;
  294. windowForClimb.WhoIsClimbing = player;
  295. XY windowToPlayer = new XY(
  296. (Math.Abs(player.Position.x - windowForClimb.Position.x) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.x > windowForClimb.Position.x ? 1 : -1)) : 0,
  297. (Math.Abs(player.Position.y - windowForClimb.Position.y) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.y > windowForClimb.Position.y ? 1 : -1)) : 0)
  298. ;
  299. new Thread
  300. (
  301. () =>
  302. {
  303. new FrameRateTaskExecutor<int>(
  304. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  305. loopToDo: () => { },
  306. timeInterval: GameData.frameDuration,
  307. finallyReturn: () => 0,
  308. maxTotalDuration: (int)((windowToPlayer + windowForClimb.Position - player.Position).Length() / player.MoveSpeed)
  309. )
  310. .Start();
  311. if (player.PlayerState != PlayerStateType.ClimbingThroughWindows)
  312. {
  313. windowForClimb.WhoIsClimbing = null;
  314. return;
  315. }
  316. player.ReSetPos(windowToPlayer + windowForClimb.Position, PlaceType.Window);
  317. player.MoveSpeed = player.SpeedOfClimbingThroughWindows;
  318. MovePlayer(player, (int)(windowToPlayer.Length() * 3.0 / player.MoveSpeed), (-1 * windowToPlayer).Angle());
  319. new FrameRateTaskExecutor<int>(
  320. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && player.IsMoving && gameMap.Timer.IsGaming,
  321. loopToDo: () => { },
  322. timeInterval: GameData.frameDuration,
  323. finallyReturn: () => 0,
  324. maxTotalDuration: (int)(windowToPlayer.Length() * 3.0 / player.MoveSpeed)
  325. )
  326. .Start();
  327. XY PosJumpOff = windowForClimb.Position - 2 * windowToPlayer;
  328. player.ReSetPos(PosJumpOff, gameMap.GetPlaceType(PosJumpOff));
  329. player.MoveSpeed = player.ReCalculateBuff(BuffType.AddSpeed, player.OrgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed);
  330. windowForClimb.WhoIsClimbing = null;
  331. if (player.PlayerState == PlayerStateType.ClimbingThroughWindows)
  332. {
  333. player.PlayerState = PlayerStateType.Null;
  334. }
  335. }
  336. )
  337. { IsBackground = true }.Start();
  338. return true;
  339. }
  340. public bool LockOrOpenDoor(Character player)
  341. {
  342. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  343. return false;
  344. Door? doorToLock = (Door?)gameMap.OneForInteract(player.Position, GameObjType.Door);
  345. if (doorToLock == null || doorToLock.OpenOrLockDegree > 0)
  346. return false;
  347. bool flag = false;
  348. foreach (Prop prop in player.PropInventory)
  349. {
  350. switch (prop.GetPropType())
  351. {
  352. case PropType.Key3:
  353. if (doorToLock.Place == PlaceType.Door3)
  354. flag = true;
  355. break;
  356. case PropType.Key5:
  357. if (doorToLock.Place == PlaceType.Door5)
  358. flag = true;
  359. break;
  360. case PropType.Key6:
  361. if (doorToLock.Place == PlaceType.Door6)
  362. flag = true;
  363. break;
  364. default:
  365. break;
  366. }
  367. if (flag) break;
  368. }
  369. if (!flag) return false;
  370. player.PlayerState = PlayerStateType.LockingOrOpeningTheDoor;
  371. new Thread
  372. (
  373. () =>
  374. {
  375. new FrameRateTaskExecutor<int>(
  376. loopCondition: () => flag && player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor && gameMap.Timer.IsGaming && doorToLock.OpenOrLockDegree < GameData.degreeOfLockingOrOpeningTheDoor,
  377. loopToDo: () =>
  378. {
  379. flag = ((gameMap.OneInTheSameCell(doorToLock.Position, GameObjType.Character)) != null);
  380. doorToLock.OpenOrLockDegree += GameData.frameDuration * player.SpeedOfOpeningOrLocking;
  381. },
  382. timeInterval: GameData.frameDuration,
  383. finallyReturn: () => 0
  384. )
  385. .Start();
  386. if (doorToLock.OpenOrLockDegree >= GameData.degreeOfLockingOrOpeningTheDoor)
  387. {
  388. doorToLock.IsOpen = (!doorToLock.IsOpen);
  389. }
  390. if (player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  391. player.PlayerState = PlayerStateType.Null;
  392. doorToLock.OpenOrLockDegree = 0;
  393. }
  394. )
  395. { IsBackground = true }.Start();
  396. return true;
  397. }
  398. /*
  399. private void ActivateMine(Character player, Mine mine)
  400. {
  401. gameMap.ObjListLock.EnterWriteLock();
  402. try { gameMap.ObjList.Remove(mine); }
  403. catch { }
  404. finally { gameMap.ObjListLock.ExitWriteLock(); }
  405. switch (mine.GetPropType())
  406. {
  407. case PropType.Dirt:
  408. player.AddMoveSpeed(Constant.dirtMoveSpeedDebuff, Constant.buffPropTime);
  409. break;
  410. case PropType.Attenuator:
  411. player.AddAP(Constant.attenuatorAtkDebuff, Constant.buffPropTime);
  412. break;
  413. case PropType.Divider:
  414. player.ChangeCD(Constant.dividerCdDiscount, Constant.buffPropTime);
  415. break;
  416. }
  417. }
  418. */
  419. private readonly Map gameMap;
  420. private readonly MoveEngine moveEngine;
  421. public ActionManager(Map gameMap)
  422. {
  423. this.gameMap = gameMap;
  424. this.moveEngine = new MoveEngine(
  425. gameMap: gameMap,
  426. OnCollision: (obj, collisionObj, moveVec) =>
  427. {
  428. //if (collisionObj is Mine)
  429. //{
  430. // ActivateMine((Character)obj, (Mine)collisionObj);
  431. // return MoveEngine.AfterCollision.ContinueCheck;
  432. //}
  433. return MoveEngine.AfterCollision.MoveMax;
  434. },
  435. EndMove: obj =>
  436. {
  437. // Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  438. }
  439. );
  440. }
  441. }
  442. }
  443. }