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

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