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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net.NetworkInformation;
  4. using System.Threading;
  5. using GameClass.GameObj;
  6. using GameEngine;
  7. using Preparation.Interface;
  8. using Preparation.Utility;
  9. using Timothy.FrameRateTask;
  10. namespace Gaming
  11. {
  12. public partial class Game
  13. {
  14. private readonly ActionManager actionManager;
  15. private class ActionManager
  16. {
  17. // 人物移动
  18. private void SkillWhenColliding(Character player, IGameObj collisionObj)
  19. {
  20. if (collisionObj.Type == GameObjType.Bullet)
  21. {
  22. if (((Bullet)collisionObj).Parent != player && ((Bullet)collisionObj).TypeOfBullet == BulletType.JumpyDumpty)
  23. {
  24. if (CharacterManager.BeStunned((Character)player, ((Bullet)collisionObj).AP / GameData.timeFactorOfGhostFainting))
  25. player.AddScore(GameData.TrickerScoreStudentBeStunned(((Bullet)collisionObj).AP / GameData.timeFactorOfGhostFainting));
  26. gameMap.Remove((GameObj)collisionObj);
  27. }
  28. }
  29. if (player.FindIActiveSkill(ActiveSkillType.CanBeginToCharge).IsBeingUsed && collisionObj.Type == GameObjType.Character && ((Character)collisionObj).IsGhost())
  30. {
  31. if (CharacterManager.BeStunned((Character)collisionObj, GameData.TimeOfGhostFaintingWhenCharge))
  32. player.AddScore(GameData.StudentScoreTrickerBeStunned(GameData.TimeOfGhostFaintingWhenCharge));
  33. CharacterManager.BeStunned(player, GameData.TimeOfStudentFaintingWhenCharge);
  34. }
  35. }
  36. public bool MovePlayer(Character playerToMove, int moveTimeInMilliseconds, double moveDirection)
  37. {
  38. if (!playerToMove.Commandable()) return false;
  39. playerToMove.PlayerState = PlayerStateType.Moving;
  40. moveEngine.MoveObj(playerToMove, moveTimeInMilliseconds, moveDirection);
  41. return true;
  42. }
  43. public static bool Stop(Character player)
  44. {
  45. if (player.Commandable())
  46. {
  47. player.PlayerState = PlayerStateType.Null;
  48. return true;
  49. }
  50. return false;
  51. }
  52. public bool Fix(Student player)// 自动检查有无发电机可修
  53. {
  54. if ((!player.Commandable()) || player.PlayerState == PlayerStateType.Fixing)
  55. return false;
  56. Generator? generatorForFix = (Generator?)gameMap.OneForInteract(player.Position, GameObjType.Generator);
  57. if (generatorForFix == null || generatorForFix.DegreeOfRepair == GameData.degreeOfFixedGenerator)
  58. return false;
  59. player.PlayerState = PlayerStateType.Fixing;
  60. new Thread
  61. (
  62. () =>
  63. {
  64. new FrameRateTaskExecutor<int>(
  65. loopCondition: () => player.PlayerState == PlayerStateType.Fixing && gameMap.Timer.IsGaming && generatorForFix.DegreeOfRepair < GameData.degreeOfFixedGenerator,
  66. loopToDo: () =>
  67. {
  68. generatorForFix.Repair(player.FixSpeed * GameData.frameDuration, player);
  69. },
  70. timeInterval: GameData.frameDuration,
  71. finallyReturn: () => 0
  72. )
  73. .Start();
  74. if (generatorForFix.DegreeOfRepair >= GameData.degreeOfFixedGenerator)
  75. {
  76. gameMap.NumOfRepairedGenerators++;
  77. if (player.PlayerState == PlayerStateType.Fixing) player.PlayerState = PlayerStateType.Null;
  78. }
  79. }
  80. )
  81. { IsBackground = true }.Start();
  82. return true;
  83. }
  84. public bool OpenDoorway(Student player)
  85. {
  86. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.OpeningTheDoorway)
  87. return false;
  88. Doorway? doorwayToOpen = (Doorway?)gameMap.OneForInteract(player.Position, GameObjType.Doorway);
  89. if (doorwayToOpen == null || doorwayToOpen.IsOpening || !doorwayToOpen.PowerSupply)
  90. return false;
  91. player.PlayerState = PlayerStateType.OpeningTheDoorway;
  92. doorwayToOpen.IsOpening = true;
  93. new Thread
  94. (
  95. () =>
  96. {
  97. new FrameRateTaskExecutor<int>(
  98. loopCondition: () => player.PlayerState == PlayerStateType.OpeningTheDoorway && gameMap.Timer.IsGaming && doorwayToOpen.OpenDegree < GameData.degreeOfOpenedDoorway,
  99. loopToDo: () =>
  100. {
  101. doorwayToOpen.OpenDegree += GameData.frameDuration;
  102. },
  103. timeInterval: GameData.frameDuration,
  104. finallyReturn: () => 0
  105. )
  106. .Start();
  107. doorwayToOpen.IsOpening = false;
  108. if (doorwayToOpen.OpenDegree >= GameData.degreeOfOpenedDoorway)
  109. {
  110. if (player.PlayerState == PlayerStateType.OpeningTheDoorway)
  111. player.PlayerState = PlayerStateType.Null;
  112. }
  113. }
  114. )
  115. { IsBackground = true }.Start();
  116. return true;
  117. }
  118. public bool Escape(Student player)
  119. {
  120. if (!(player.Commandable()) || player.CharacterType == CharacterType.Robot)
  121. return false;
  122. Doorway? doorwayForEscape = (Doorway?)gameMap.OneForInteract(player.Position, GameObjType.Doorway);
  123. if (doorwayForEscape != null && doorwayForEscape.IsOpen())
  124. {
  125. player.AddScore(GameData.StudentScoreEscape);
  126. ++gameMap.NumOfEscapedStudent;
  127. player.Die(PlayerStateType.Escaped);
  128. return true;
  129. }
  130. else
  131. {
  132. EmergencyExit? emergencyExit = (EmergencyExit?)gameMap.OneForInteract(player.Position, GameObjType.EmergencyExit);
  133. if (emergencyExit != null && emergencyExit.IsOpen)
  134. {
  135. ++gameMap.NumOfEscapedStudent;
  136. player.Die(PlayerStateType.Escaped);
  137. return true;
  138. }
  139. return false;
  140. }
  141. }
  142. public bool Treat(Student player, Student? playerTreated = null)
  143. {
  144. if (playerTreated == null)
  145. {
  146. playerTreated = gameMap.StudentForInteract(player.Position);
  147. if (playerTreated == null) return false;
  148. }
  149. if (player == playerTreated || (!player.Commandable()) || player.PlayerState == PlayerStateType.Treating ||
  150. (!playerTreated.Commandable()) ||
  151. playerTreated.HP == playerTreated.MaxHp || !GameData.ApproachToInteract(playerTreated.Position, player.Position))
  152. return false;
  153. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  154. {
  155. playerTreated.HP = playerTreated.MaxHp;
  156. playerTreated.DegreeOfTreatment = 0;
  157. return false;
  158. }
  159. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  160. {
  161. playerTreated.HP += GameData.basicTreatmentDegree;
  162. playerTreated.DegreeOfTreatment = 0;
  163. return false;
  164. }
  165. new Thread
  166. (
  167. () =>
  168. {
  169. playerTreated.PlayerState = PlayerStateType.Treated;
  170. player.PlayerState = PlayerStateType.Treating;
  171. new FrameRateTaskExecutor<int>(
  172. 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),
  173. loopToDo: () =>
  174. {
  175. playerTreated.DegreeOfTreatment += GameData.frameDuration * player.TreatSpeed;
  176. },
  177. timeInterval: GameData.frameDuration,
  178. finallyReturn: () => 0
  179. )
  180. .Start();
  181. if (playerTreated.PlayerState == PlayerStateType.Treated) playerTreated.PlayerState = PlayerStateType.Null;
  182. if (player.PlayerState == PlayerStateType.Treating) player.PlayerState = PlayerStateType.Null;
  183. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  184. {
  185. player.AddScore(GameData.StudentScoreTreat(playerTreated.MaxHp - playerTreated.HP));
  186. playerTreated.HP = playerTreated.MaxHp;
  187. playerTreated.DegreeOfTreatment = 0;
  188. }
  189. else
  190. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  191. {
  192. player.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
  193. playerTreated.HP += GameData.basicTreatmentDegree;
  194. playerTreated.DegreeOfTreatment = 0;
  195. }
  196. }
  197. )
  198. { IsBackground = true }.Start();
  199. return true;
  200. }
  201. public bool Rescue(Student player, Student? playerRescued = null)
  202. {
  203. if (playerRescued == null)
  204. {
  205. playerRescued = gameMap.StudentForInteract(player.Position);
  206. if (playerRescued == null) return false;
  207. }
  208. if ((!player.Commandable()) || playerRescued.PlayerState != PlayerStateType.Addicted || player == playerRescued
  209. || !GameData.ApproachToInteract(playerRescued.Position, player.Position) || playerRescued.TimeOfRescue > 0)
  210. return false;
  211. player.PlayerState = PlayerStateType.Rescuing;
  212. playerRescued.PlayerState = PlayerStateType.Rescued;
  213. new Thread
  214. (
  215. () =>
  216. {
  217. new FrameRateTaskExecutor<int>(
  218. loopCondition: () => playerRescued.PlayerState == PlayerStateType.Rescued && player.PlayerState == PlayerStateType.Rescuing && gameMap.Timer.IsGaming && GameData.ApproachToInteract(playerRescued.Position, player.Position),
  219. loopToDo: () =>
  220. {
  221. playerRescued.TimeOfRescue += GameData.frameDuration;
  222. },
  223. timeInterval: GameData.frameDuration,
  224. finallyReturn: () => 0,
  225. maxTotalDuration: GameData.basicTimeOfRescue
  226. )
  227. .Start();
  228. if (playerRescued.PlayerState == PlayerStateType.Rescued)
  229. {
  230. if (playerRescued.TimeOfRescue >= GameData.basicTimeOfRescue)
  231. {
  232. playerRescued.PlayerState = PlayerStateType.Null;
  233. playerRescued.HP = playerRescued.MaxHp / 2;
  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. playerRescued.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. XY windowToPlayer = new(
  294. (Math.Abs(player.Position.x - windowForClimb.Position.x) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.x > windowForClimb.Position.x ? 1 : -1)) : 0,
  295. (Math.Abs(player.Position.y - windowForClimb.Position.y) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.y > windowForClimb.Position.y ? 1 : -1)) : 0);
  296. /* Character? characterInWindow = (Character?)gameMap.OneInTheSameCell(windowForClimb.Position - 2 * windowToPlayer, GameObjType.Character);
  297. if (characterInWindow != null)
  298. {
  299. if (player.IsGhost() && !characterInWindow.IsGhost())
  300. characterManager.BeAttacked((Student)(characterInWindow), player.Attack(characterInWindow.Position, PlaceType.Null));
  301. return false;
  302. }*/
  303. //Wall addWall = new Wall(windowForClimb.Position - 2 * windowToPlayer);
  304. // gameMap.Add(addWall);
  305. player.PlayerState = PlayerStateType.ClimbingThroughWindows;
  306. windowForClimb.WhoIsClimbing = player;
  307. new Thread
  308. (
  309. () =>
  310. {
  311. new FrameRateTaskExecutor<int>(
  312. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  313. loopToDo: () => { },
  314. timeInterval: GameData.frameDuration,
  315. finallyReturn: () => 0,
  316. maxTotalDuration: (int)((windowToPlayer + windowForClimb.Position - player.Position).Length() * 1000 / player.MoveSpeed)
  317. )
  318. .Start();
  319. if (player.PlayerState != PlayerStateType.ClimbingThroughWindows)
  320. {
  321. windowForClimb.WhoIsClimbing = null;
  322. return;
  323. }
  324. player.ReSetPos(windowToPlayer + windowForClimb.Position, PlaceType.Window);
  325. player.MoveSpeed = player.SpeedOfClimbingThroughWindows;
  326. moveEngine.MoveObj(player, (int)(windowToPlayer.Length() * 3.0 * 1000 / player.MoveSpeed), (-1 * windowToPlayer).Angle());
  327. new FrameRateTaskExecutor<int>(
  328. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  329. loopToDo: () =>
  330. {
  331. },
  332. timeInterval: GameData.frameDuration,
  333. finallyReturn: () => 0,
  334. maxTotalDuration: (int)(windowToPlayer.Length() * 3.0 * 1000 / player.MoveSpeed)
  335. )
  336. .Start();
  337. XY PosJumpOff = windowForClimb.Position - 2 * windowToPlayer;
  338. player.ReSetPos(PosJumpOff, gameMap.GetPlaceType(PosJumpOff));
  339. player.MoveSpeed = player.ReCalculateBuff(BuffType.AddSpeed, player.OrgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed);
  340. windowForClimb.WhoIsClimbing = null;
  341. // gameMap.Remove(addWall);
  342. if (player.PlayerState == PlayerStateType.ClimbingThroughWindows)
  343. {
  344. player.PlayerState = PlayerStateType.Null;
  345. }
  346. }
  347. )
  348. { IsBackground = true }.Start();
  349. return true;
  350. }
  351. public bool LockOrOpenDoor(Character player)
  352. {
  353. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  354. return false;
  355. Door? doorToLock = (Door?)gameMap.OneForInteract(player.Position, GameObjType.Door);
  356. if (doorToLock == null || doorToLock.OpenOrLockDegree > 0)
  357. return false;
  358. bool flag = false;
  359. foreach (Prop prop in player.PropInventory)
  360. {
  361. switch (prop.GetPropType())
  362. {
  363. case PropType.Key3:
  364. if (doorToLock.Place == PlaceType.Door3)
  365. flag = true;
  366. break;
  367. case PropType.Key5:
  368. if (doorToLock.Place == PlaceType.Door5)
  369. flag = true;
  370. break;
  371. case PropType.Key6:
  372. if (doorToLock.Place == PlaceType.Door6)
  373. flag = true;
  374. break;
  375. default:
  376. break;
  377. }
  378. if (flag) break;
  379. }
  380. if (!flag) return false;
  381. player.PlayerState = PlayerStateType.LockingOrOpeningTheDoor;
  382. new Thread
  383. (
  384. () =>
  385. {
  386. new FrameRateTaskExecutor<int>(
  387. loopCondition: () => flag && player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor && gameMap.Timer.IsGaming && doorToLock.OpenOrLockDegree < GameData.degreeOfLockingOrOpeningTheDoor,
  388. loopToDo: () =>
  389. {
  390. flag = ((gameMap.OneInTheSameCell(doorToLock.Position, GameObjType.Character)) == null);
  391. doorToLock.OpenOrLockDegree += GameData.frameDuration * player.SpeedOfOpeningOrLocking;
  392. },
  393. timeInterval: GameData.frameDuration,
  394. finallyReturn: () => 0
  395. )
  396. .Start();
  397. if (doorToLock.OpenOrLockDegree >= GameData.degreeOfLockingOrOpeningTheDoor)
  398. {
  399. doorToLock.IsOpen = (!doorToLock.IsOpen);
  400. }
  401. if (player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  402. player.PlayerState = PlayerStateType.Null;
  403. doorToLock.OpenOrLockDegree = 0;
  404. }
  405. )
  406. { IsBackground = true }.Start();
  407. return true;
  408. }
  409. /*
  410. private void ActivateMine(Character player, Mine mine)
  411. {
  412. gameMap.ObjListLock.EnterWriteLock();
  413. try { gameMap.ObjList.Remove(mine); }
  414. catch { }
  415. finally { gameMap.ObjListLock.ExitWriteLock(); }
  416. switch (mine.GetPropType())
  417. {
  418. case PropType.Dirt:
  419. player.AddMoveSpeed(Constant.dirtMoveSpeedDebuff, Constant.buffPropTime);
  420. break;
  421. case PropType.Attenuator:
  422. player.AddAP(Constant.attenuatorAtkDebuff, Constant.buffPropTime);
  423. break;
  424. case PropType.Divider:
  425. player.ChangeCD(Constant.dividerCdDiscount, Constant.buffPropTime);
  426. break;
  427. }
  428. }
  429. */
  430. private readonly Map gameMap;
  431. private readonly CharacterManager characterManager;
  432. public readonly MoveEngine moveEngine;
  433. public ActionManager(Map gameMap, CharacterManager characterManager)
  434. {
  435. this.gameMap = gameMap;
  436. this.moveEngine = new MoveEngine(
  437. gameMap: gameMap,
  438. OnCollision: (obj, collisionObj, moveVec) =>
  439. {
  440. SkillWhenColliding((Character)obj, collisionObj);
  441. Preparation.Utility.Debugger.Output(obj, " end move with " + collisionObj.ToString());
  442. //if (collisionObj is Mine)
  443. //{
  444. // ActivateMine((Character)obj, (Mine)collisionObj);
  445. // return MoveEngine.AfterCollision.ContinueCheck;
  446. //}
  447. return MoveEngine.AfterCollision.MoveMax;
  448. },
  449. EndMove: obj =>
  450. {
  451. // Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  452. }
  453. );
  454. this.characterManager = characterManager;
  455. }
  456. }
  457. }
  458. }