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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. int ScoreAdded = GameData.StudentScoreFix(generatorForFix.DegreeOfRepair);
  65. new FrameRateTaskExecutor<int>(
  66. loopCondition: () => player.PlayerState == PlayerStateType.Fixing && gameMap.Timer.IsGaming && generatorForFix.DegreeOfRepair < GameData.degreeOfFixedGenerator,
  67. loopToDo: () =>
  68. {
  69. generatorForFix.Repair(player.FixSpeed * GameData.frameDuration);
  70. player.AddScore(GameData.StudentScoreFix(generatorForFix.DegreeOfRepair - ScoreAdded));
  71. ScoreAdded = GameData.StudentScoreFix(generatorForFix.DegreeOfRepair);
  72. },
  73. timeInterval: GameData.frameDuration,
  74. finallyReturn: () => 0
  75. )
  76. .Start();
  77. if (generatorForFix.DegreeOfRepair >= GameData.degreeOfFixedGenerator)
  78. {
  79. gameMap.NumOfRepairedGenerators++;
  80. if (player.PlayerState == PlayerStateType.Fixing) player.PlayerState = PlayerStateType.Null;
  81. if (gameMap.NumOfRepairedGenerators == GameData.numOfGeneratorRequiredForEmergencyExit)
  82. {
  83. gameMap.GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  84. try
  85. {
  86. Random r = new Random(Environment.TickCount);
  87. ((EmergencyExit)(gameMap.GameObjDict[GameObjType.EmergencyExit][r.Next(0, gameMap.GameObjDict[GameObjType.EmergencyExit].Count)])).CanOpen = true;
  88. }
  89. finally
  90. {
  91. gameMap.GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  92. }
  93. }
  94. else
  95. if (gameMap.NumOfRepairedGenerators == GameData.numOfGeneratorRequiredForRepair)
  96. {
  97. gameMap.GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  98. try
  99. {
  100. foreach (Doorway doorway in gameMap.GameObjDict[GameObjType.Doorway])
  101. doorway.PowerSupply = true;
  102. }
  103. finally
  104. {
  105. gameMap.GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  106. }
  107. }
  108. }
  109. }
  110. )
  111. { IsBackground = true }.Start();
  112. return true;
  113. }
  114. public bool OpenDoorway(Student player)
  115. {
  116. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.OpeningTheDoorway)
  117. return false;
  118. Doorway? doorwayToOpen = (Doorway?)gameMap.OneForInteract(player.Position, GameObjType.Doorway);
  119. if (doorwayToOpen == null || doorwayToOpen.IsOpening || !doorwayToOpen.PowerSupply)
  120. return false;
  121. player.PlayerState = PlayerStateType.OpeningTheDoorway;
  122. doorwayToOpen.IsOpening = true;
  123. new Thread
  124. (
  125. () =>
  126. {
  127. new FrameRateTaskExecutor<int>(
  128. loopCondition: () => player.PlayerState == PlayerStateType.OpeningTheDoorway && gameMap.Timer.IsGaming && doorwayToOpen.OpenDegree < GameData.degreeOfOpenedDoorway,
  129. loopToDo: () =>
  130. {
  131. doorwayToOpen.OpenDegree += GameData.frameDuration;
  132. },
  133. timeInterval: GameData.frameDuration,
  134. finallyReturn: () => 0
  135. )
  136. .Start();
  137. doorwayToOpen.IsOpening = false;
  138. if (doorwayToOpen.OpenDegree >= GameData.degreeOfOpenedDoorway)
  139. {
  140. if (player.PlayerState == PlayerStateType.OpeningTheDoorway)
  141. player.PlayerState = PlayerStateType.Null;
  142. }
  143. }
  144. )
  145. { IsBackground = true }.Start();
  146. return true;
  147. }
  148. public bool Escape(Student player)
  149. {
  150. if (!(player.Commandable()) || player.CharacterType == CharacterType.Robot)
  151. return false;
  152. Doorway? doorwayForEscape = (Doorway?)gameMap.OneForInteractInACross(player.Position, GameObjType.Doorway);
  153. if (doorwayForEscape != null)
  154. {
  155. if (doorwayForEscape.IsOpen())
  156. {
  157. player.AddScore(GameData.StudentScoreEscape);
  158. ++gameMap.NumOfEscapedStudent;
  159. player.Die(PlayerStateType.Escaped);
  160. return true;
  161. }
  162. else
  163. return false;
  164. }
  165. else
  166. {
  167. EmergencyExit? emergencyExit = (EmergencyExit?)gameMap.OneForInteractInACross(player.Position, GameObjType.EmergencyExit);
  168. if (emergencyExit != null && emergencyExit.IsOpen)
  169. {
  170. player.Die(PlayerStateType.Escaped);
  171. return true;
  172. }
  173. else
  174. return false;
  175. }
  176. }
  177. public bool Treat(Student player, Student? playerTreated = null)
  178. {
  179. if (playerTreated == null)
  180. {
  181. playerTreated = gameMap.StudentForInteract(player.Position);
  182. if (playerTreated == null) return false;
  183. }
  184. if (player == playerTreated || (!player.Commandable()) || player.PlayerState == PlayerStateType.Treating ||
  185. (!playerTreated.Commandable()) ||
  186. playerTreated.HP == playerTreated.MaxHp || !GameData.ApproachToInteract(playerTreated.Position, player.Position))
  187. return false;
  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. playerRescued.HP = GameData.RemainHpWhenAddLife;
  269. player.AddScore(GameData.StudentScoreRescue);
  270. }
  271. else
  272. playerRescued.PlayerState = PlayerStateType.Addicted;
  273. }
  274. if (player.PlayerState == PlayerStateType.Rescuing) player.PlayerState = PlayerStateType.Null;
  275. playerRescued.TimeOfRescue = 0;
  276. }
  277. )
  278. { IsBackground = true }.Start();
  279. return true;
  280. }
  281. public bool OpenChest(Character player)
  282. {
  283. if ((!player.Commandable()) || player.PlayerState == PlayerStateType.OpeningTheChest)
  284. return false;
  285. Chest? chestToOpen = (Chest?)gameMap.OneForInteract(player.Position, GameObjType.Chest);
  286. if (chestToOpen == null || chestToOpen.IsOpen() || chestToOpen.OpenDegree > 0)
  287. return false;
  288. player.PlayerState = PlayerStateType.OpeningTheChest;
  289. new Thread
  290. (
  291. () =>
  292. {
  293. new FrameRateTaskExecutor<int>(
  294. loopCondition: () => player.PlayerState == PlayerStateType.OpeningTheChest && gameMap.Timer.IsGaming && (!chestToOpen.IsOpen()),
  295. loopToDo: () =>
  296. {
  297. chestToOpen.OpenDegree += GameData.frameDuration * player.SpeedOfOpenChest;
  298. },
  299. timeInterval: GameData.frameDuration,
  300. finallyReturn: () => 0
  301. )
  302. .Start();
  303. if (chestToOpen.IsOpen())
  304. {
  305. if (player.PlayerState == PlayerStateType.OpeningTheChest)
  306. player.PlayerState = PlayerStateType.Null;
  307. for (int i = 0; i < GameData.maxNumOfPropInChest; ++i)
  308. {
  309. Prop prop = chestToOpen.PropInChest[i];
  310. chestToOpen.PropInChest[i] = new NullProp();
  311. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  312. gameMap.Add(prop);
  313. }
  314. }
  315. else chestToOpen.OpenDegree = 0;
  316. }
  317. )
  318. { IsBackground = true }.Start();
  319. return true;
  320. }
  321. public bool ClimbingThroughWindow(Character player)
  322. {
  323. if (!player.Commandable())
  324. return false;
  325. Window? windowForClimb = (Window?)gameMap.OneForInteractInACross(player.Position, GameObjType.Window);
  326. if (windowForClimb == null || windowForClimb.WhoIsClimbing != null)
  327. return false;
  328. player.PlayerState = PlayerStateType.ClimbingThroughWindows;
  329. windowForClimb.WhoIsClimbing = player;
  330. XY windowToPlayer = new XY(
  331. (Math.Abs(player.Position.x - windowForClimb.Position.x) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.x > windowForClimb.Position.x ? 1 : -1)) : 0,
  332. (Math.Abs(player.Position.y - windowForClimb.Position.y) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.y > windowForClimb.Position.y ? 1 : -1)) : 0)
  333. ;
  334. new Thread
  335. (
  336. () =>
  337. {
  338. new FrameRateTaskExecutor<int>(
  339. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  340. loopToDo: () => { },
  341. timeInterval: GameData.frameDuration,
  342. finallyReturn: () => 0,
  343. maxTotalDuration: (int)((windowToPlayer + windowForClimb.Position - player.Position).Length() * 1000 / player.MoveSpeed)
  344. )
  345. .Start();
  346. if (player.PlayerState != PlayerStateType.ClimbingThroughWindows)
  347. {
  348. windowForClimb.WhoIsClimbing = null;
  349. return;
  350. }
  351. player.ReSetPos(windowToPlayer + windowForClimb.Position, PlaceType.Window);
  352. player.MoveSpeed = player.SpeedOfClimbingThroughWindows;
  353. moveEngine.MoveObj(player, (int)(windowToPlayer.Length() * 3.0 * 1000 / player.MoveSpeed), (-1 * windowToPlayer).Angle());
  354. new FrameRateTaskExecutor<int>(
  355. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  356. loopToDo: () =>
  357. {
  358. },
  359. timeInterval: GameData.frameDuration,
  360. finallyReturn: () => 0,
  361. maxTotalDuration: (int)(windowToPlayer.Length() * 3.0 * 1000 / player.MoveSpeed)
  362. )
  363. .Start();
  364. XY PosJumpOff = windowForClimb.Position - 2 * windowToPlayer;
  365. player.ReSetPos(PosJumpOff, gameMap.GetPlaceType(PosJumpOff));
  366. player.MoveSpeed = player.ReCalculateBuff(BuffType.AddSpeed, player.OrgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed);
  367. windowForClimb.WhoIsClimbing = null;
  368. if (player.PlayerState == PlayerStateType.ClimbingThroughWindows)
  369. {
  370. player.PlayerState = PlayerStateType.Null;
  371. }
  372. }
  373. )
  374. { IsBackground = true }.Start();
  375. return true;
  376. }
  377. public bool LockOrOpenDoor(Character player)
  378. {
  379. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  380. return false;
  381. Door? doorToLock = (Door?)gameMap.OneForInteract(player.Position, GameObjType.Door);
  382. if (doorToLock == null || doorToLock.OpenOrLockDegree > 0)
  383. return false;
  384. bool flag = false;
  385. foreach (Prop prop in player.PropInventory)
  386. {
  387. switch (prop.GetPropType())
  388. {
  389. case PropType.Key3:
  390. if (doorToLock.Place == PlaceType.Door3)
  391. flag = true;
  392. break;
  393. case PropType.Key5:
  394. if (doorToLock.Place == PlaceType.Door5)
  395. flag = true;
  396. break;
  397. case PropType.Key6:
  398. if (doorToLock.Place == PlaceType.Door6)
  399. flag = true;
  400. break;
  401. default:
  402. break;
  403. }
  404. if (flag) break;
  405. }
  406. if (!flag) return false;
  407. player.PlayerState = PlayerStateType.LockingOrOpeningTheDoor;
  408. new Thread
  409. (
  410. () =>
  411. {
  412. new FrameRateTaskExecutor<int>(
  413. loopCondition: () => flag && player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor && gameMap.Timer.IsGaming && doorToLock.OpenOrLockDegree < GameData.degreeOfLockingOrOpeningTheDoor,
  414. loopToDo: () =>
  415. {
  416. flag = ((gameMap.OneInTheSameCell(doorToLock.Position, GameObjType.Character)) == null);
  417. doorToLock.OpenOrLockDegree += GameData.frameDuration * player.SpeedOfOpeningOrLocking;
  418. },
  419. timeInterval: GameData.frameDuration,
  420. finallyReturn: () => 0
  421. )
  422. .Start();
  423. if (doorToLock.OpenOrLockDegree >= GameData.degreeOfLockingOrOpeningTheDoor)
  424. {
  425. doorToLock.IsOpen = (!doorToLock.IsOpen);
  426. }
  427. if (player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  428. player.PlayerState = PlayerStateType.Null;
  429. doorToLock.OpenOrLockDegree = 0;
  430. }
  431. )
  432. { IsBackground = true }.Start();
  433. return true;
  434. }
  435. /*
  436. private void ActivateMine(Character player, Mine mine)
  437. {
  438. gameMap.ObjListLock.EnterWriteLock();
  439. try { gameMap.ObjList.Remove(mine); }
  440. catch { }
  441. finally { gameMap.ObjListLock.ExitWriteLock(); }
  442. switch (mine.GetPropType())
  443. {
  444. case PropType.Dirt:
  445. player.AddMoveSpeed(Constant.dirtMoveSpeedDebuff, Constant.buffPropTime);
  446. break;
  447. case PropType.Attenuator:
  448. player.AddAP(Constant.attenuatorAtkDebuff, Constant.buffPropTime);
  449. break;
  450. case PropType.Divider:
  451. player.ChangeCD(Constant.dividerCdDiscount, Constant.buffPropTime);
  452. break;
  453. }
  454. }
  455. */
  456. private readonly Map gameMap;
  457. public readonly MoveEngine moveEngine;
  458. public ActionManager(Map gameMap)
  459. {
  460. this.gameMap = gameMap;
  461. this.moveEngine = new MoveEngine(
  462. gameMap: gameMap,
  463. OnCollision: (obj, collisionObj, moveVec) =>
  464. {
  465. SkillWhenColliding((Character)obj, collisionObj);
  466. Preparation.Utility.Debugger.Output(obj, " end move with " + collisionObj.ToString());
  467. //if (collisionObj is Mine)
  468. //{
  469. // ActivateMine((Character)obj, (Mine)collisionObj);
  470. // return MoveEngine.AfterCollision.ContinueCheck;
  471. //}
  472. return MoveEngine.AfterCollision.MoveMax;
  473. },
  474. EndMove: obj =>
  475. {
  476. // Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  477. }
  478. );
  479. }
  480. }
  481. }
  482. }