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

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 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 && doorwayForEscape.IsOpen())
  154. {
  155. player.AddScore(GameData.StudentScoreEscape);
  156. ++gameMap.NumOfEscapedStudent;
  157. player.Die(PlayerStateType.Escaped);
  158. return true;
  159. }
  160. else
  161. {
  162. EmergencyExit? emergencyExit = (EmergencyExit?)gameMap.OneForInteractInACross(player.Position, GameObjType.EmergencyExit);
  163. if (emergencyExit != null && emergencyExit.IsOpen)
  164. {
  165. ++gameMap.NumOfEscapedStudent;
  166. player.Die(PlayerStateType.Escaped);
  167. return true;
  168. }
  169. return false;
  170. }
  171. }
  172. public bool Treat(Student player, Student? playerTreated = null)
  173. {
  174. if (playerTreated == null)
  175. {
  176. playerTreated = gameMap.StudentForInteract(player.Position);
  177. if (playerTreated == null) return false;
  178. }
  179. if (player == playerTreated || (!player.Commandable()) || player.PlayerState == PlayerStateType.Treating ||
  180. (!playerTreated.Commandable()) ||
  181. playerTreated.HP == playerTreated.MaxHp || !GameData.ApproachToInteract(playerTreated.Position, player.Position))
  182. return false;
  183. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  184. {
  185. playerTreated.HP = playerTreated.MaxHp;
  186. playerTreated.DegreeOfTreatment = 0;
  187. return false;
  188. }
  189. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  190. {
  191. playerTreated.HP += GameData.basicTreatmentDegree;
  192. playerTreated.DegreeOfTreatment = 0;
  193. return false;
  194. }
  195. new Thread
  196. (
  197. () =>
  198. {
  199. playerTreated.PlayerState = PlayerStateType.Treated;
  200. player.PlayerState = PlayerStateType.Treating;
  201. new FrameRateTaskExecutor<int>(
  202. 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),
  203. loopToDo: () =>
  204. {
  205. playerTreated.DegreeOfTreatment += GameData.frameDuration * player.TreatSpeed;
  206. },
  207. timeInterval: GameData.frameDuration,
  208. finallyReturn: () => 0
  209. )
  210. .Start();
  211. if (playerTreated.PlayerState == PlayerStateType.Treated) playerTreated.PlayerState = PlayerStateType.Null;
  212. if (player.PlayerState == PlayerStateType.Treating) player.PlayerState = PlayerStateType.Null;
  213. if (playerTreated.HP + playerTreated.DegreeOfTreatment >= playerTreated.MaxHp)
  214. {
  215. player.AddScore(GameData.StudentScoreTreat(playerTreated.MaxHp - playerTreated.HP));
  216. playerTreated.HP = playerTreated.MaxHp;
  217. playerTreated.DegreeOfTreatment = 0;
  218. }
  219. else
  220. if (playerTreated.DegreeOfTreatment >= GameData.basicTreatmentDegree)
  221. {
  222. player.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
  223. playerTreated.HP += GameData.basicTreatmentDegree;
  224. playerTreated.DegreeOfTreatment = 0;
  225. }
  226. }
  227. )
  228. { IsBackground = true }.Start();
  229. return true;
  230. }
  231. public bool Rescue(Student player, Student? playerRescued = null)
  232. {
  233. if (playerRescued == null)
  234. {
  235. playerRescued = gameMap.StudentForInteract(player.Position);
  236. if (playerRescued == null) return false;
  237. }
  238. if ((!player.Commandable()) || playerRescued.PlayerState != PlayerStateType.Addicted || player == playerRescued
  239. || !GameData.ApproachToInteract(playerRescued.Position, player.Position) || playerRescued.TimeOfRescue > 0)
  240. return false;
  241. player.PlayerState = PlayerStateType.Rescuing;
  242. playerRescued.PlayerState = PlayerStateType.Rescued;
  243. new Thread
  244. (
  245. () =>
  246. {
  247. new FrameRateTaskExecutor<int>(
  248. loopCondition: () => playerRescued.PlayerState == PlayerStateType.Rescued && player.PlayerState == PlayerStateType.Rescuing && gameMap.Timer.IsGaming && GameData.ApproachToInteract(playerRescued.Position, player.Position),
  249. loopToDo: () =>
  250. {
  251. playerRescued.TimeOfRescue += GameData.frameDuration;
  252. },
  253. timeInterval: GameData.frameDuration,
  254. finallyReturn: () => 0,
  255. maxTotalDuration: GameData.basicTimeOfRescue
  256. )
  257. .Start();
  258. if (playerRescued.PlayerState == PlayerStateType.Rescued)
  259. {
  260. if (playerRescued.TimeOfRescue >= GameData.basicTimeOfRescue)
  261. {
  262. playerRescued.PlayerState = PlayerStateType.Null;
  263. playerRescued.HP = GameData.RemainHpWhenAddLife;
  264. player.AddScore(GameData.StudentScoreRescue);
  265. }
  266. else
  267. playerRescued.PlayerState = PlayerStateType.Addicted;
  268. }
  269. if (player.PlayerState == PlayerStateType.Rescuing) player.PlayerState = PlayerStateType.Null;
  270. playerRescued.TimeOfRescue = 0;
  271. }
  272. )
  273. { IsBackground = true }.Start();
  274. return true;
  275. }
  276. public bool OpenChest(Character player)
  277. {
  278. if ((!player.Commandable()) || player.PlayerState == PlayerStateType.OpeningTheChest)
  279. return false;
  280. Chest? chestToOpen = (Chest?)gameMap.OneForInteract(player.Position, GameObjType.Chest);
  281. if (chestToOpen == null || chestToOpen.IsOpen() || chestToOpen.OpenDegree > 0)
  282. return false;
  283. player.PlayerState = PlayerStateType.OpeningTheChest;
  284. new Thread
  285. (
  286. () =>
  287. {
  288. new FrameRateTaskExecutor<int>(
  289. loopCondition: () => player.PlayerState == PlayerStateType.OpeningTheChest && gameMap.Timer.IsGaming && (!chestToOpen.IsOpen()),
  290. loopToDo: () =>
  291. {
  292. chestToOpen.OpenDegree += GameData.frameDuration * player.SpeedOfOpenChest;
  293. },
  294. timeInterval: GameData.frameDuration,
  295. finallyReturn: () => 0
  296. )
  297. .Start();
  298. if (chestToOpen.IsOpen())
  299. {
  300. if (player.PlayerState == PlayerStateType.OpeningTheChest)
  301. player.PlayerState = PlayerStateType.Null;
  302. for (int i = 0; i < GameData.maxNumOfPropInChest; ++i)
  303. {
  304. Prop prop = chestToOpen.PropInChest[i];
  305. chestToOpen.PropInChest[i] = new NullProp();
  306. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  307. gameMap.Add(prop);
  308. }
  309. }
  310. else chestToOpen.OpenDegree = 0;
  311. }
  312. )
  313. { IsBackground = true }.Start();
  314. return true;
  315. }
  316. public bool ClimbingThroughWindow(Character player)
  317. {
  318. if (!player.Commandable())
  319. return false;
  320. Window? windowForClimb = (Window?)gameMap.OneForInteractInACross(player.Position, GameObjType.Window);
  321. if (windowForClimb == null || windowForClimb.WhoIsClimbing != null)
  322. return false;
  323. player.PlayerState = PlayerStateType.ClimbingThroughWindows;
  324. windowForClimb.WhoIsClimbing = player;
  325. XY windowToPlayer = new XY(
  326. (Math.Abs(player.Position.x - windowForClimb.Position.x) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.x > windowForClimb.Position.x ? 1 : -1)) : 0,
  327. (Math.Abs(player.Position.y - windowForClimb.Position.y) > GameData.numOfPosGridPerCell / 2) ? (GameData.numOfPosGridPerCell / 2 * (player.Position.y > windowForClimb.Position.y ? 1 : -1)) : 0)
  328. ;
  329. new Thread
  330. (
  331. () =>
  332. {
  333. new FrameRateTaskExecutor<int>(
  334. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  335. loopToDo: () => { },
  336. timeInterval: GameData.frameDuration,
  337. finallyReturn: () => 0,
  338. maxTotalDuration: (int)((windowToPlayer + windowForClimb.Position - player.Position).Length() * 1000 / player.MoveSpeed)
  339. )
  340. .Start();
  341. if (player.PlayerState != PlayerStateType.ClimbingThroughWindows)
  342. {
  343. windowForClimb.WhoIsClimbing = null;
  344. return;
  345. }
  346. player.ReSetPos(windowToPlayer + windowForClimb.Position, PlaceType.Window);
  347. player.MoveSpeed = player.SpeedOfClimbingThroughWindows;
  348. moveEngine.MoveObj(player, (int)(windowToPlayer.Length() * 3.0 * 1000 / player.MoveSpeed), (-1 * windowToPlayer).Angle());
  349. new FrameRateTaskExecutor<int>(
  350. loopCondition: () => player.PlayerState == PlayerStateType.ClimbingThroughWindows && gameMap.Timer.IsGaming,
  351. loopToDo: () =>
  352. {
  353. },
  354. timeInterval: GameData.frameDuration,
  355. finallyReturn: () => 0,
  356. maxTotalDuration: (int)(windowToPlayer.Length() * 3.0 * 1000 / player.MoveSpeed)
  357. )
  358. .Start();
  359. XY PosJumpOff = windowForClimb.Position - 2 * windowToPlayer;
  360. player.ReSetPos(PosJumpOff, gameMap.GetPlaceType(PosJumpOff));
  361. player.MoveSpeed = player.ReCalculateBuff(BuffType.AddSpeed, player.OrgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed);
  362. windowForClimb.WhoIsClimbing = null;
  363. if (player.PlayerState == PlayerStateType.ClimbingThroughWindows)
  364. {
  365. player.PlayerState = PlayerStateType.Null;
  366. }
  367. }
  368. )
  369. { IsBackground = true }.Start();
  370. return true;
  371. }
  372. public bool LockOrOpenDoor(Character player)
  373. {
  374. if (!(player.Commandable()) || player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  375. return false;
  376. Door? doorToLock = (Door?)gameMap.OneForInteract(player.Position, GameObjType.Door);
  377. if (doorToLock == null || doorToLock.OpenOrLockDegree > 0)
  378. return false;
  379. bool flag = false;
  380. foreach (Prop prop in player.PropInventory)
  381. {
  382. switch (prop.GetPropType())
  383. {
  384. case PropType.Key3:
  385. if (doorToLock.Place == PlaceType.Door3)
  386. flag = true;
  387. break;
  388. case PropType.Key5:
  389. if (doorToLock.Place == PlaceType.Door5)
  390. flag = true;
  391. break;
  392. case PropType.Key6:
  393. if (doorToLock.Place == PlaceType.Door6)
  394. flag = true;
  395. break;
  396. default:
  397. break;
  398. }
  399. if (flag) break;
  400. }
  401. if (!flag) return false;
  402. player.PlayerState = PlayerStateType.LockingOrOpeningTheDoor;
  403. new Thread
  404. (
  405. () =>
  406. {
  407. new FrameRateTaskExecutor<int>(
  408. loopCondition: () => flag && player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor && gameMap.Timer.IsGaming && doorToLock.OpenOrLockDegree < GameData.degreeOfLockingOrOpeningTheDoor,
  409. loopToDo: () =>
  410. {
  411. flag = ((gameMap.OneInTheSameCell(doorToLock.Position, GameObjType.Character)) == null);
  412. doorToLock.OpenOrLockDegree += GameData.frameDuration * player.SpeedOfOpeningOrLocking;
  413. },
  414. timeInterval: GameData.frameDuration,
  415. finallyReturn: () => 0
  416. )
  417. .Start();
  418. if (doorToLock.OpenOrLockDegree >= GameData.degreeOfLockingOrOpeningTheDoor)
  419. {
  420. doorToLock.IsOpen = (!doorToLock.IsOpen);
  421. }
  422. if (player.PlayerState == PlayerStateType.LockingOrOpeningTheDoor)
  423. player.PlayerState = PlayerStateType.Null;
  424. doorToLock.OpenOrLockDegree = 0;
  425. }
  426. )
  427. { IsBackground = true }.Start();
  428. return true;
  429. }
  430. /*
  431. private void ActivateMine(Character player, Mine mine)
  432. {
  433. gameMap.ObjListLock.EnterWriteLock();
  434. try { gameMap.ObjList.Remove(mine); }
  435. catch { }
  436. finally { gameMap.ObjListLock.ExitWriteLock(); }
  437. switch (mine.GetPropType())
  438. {
  439. case PropType.Dirt:
  440. player.AddMoveSpeed(Constant.dirtMoveSpeedDebuff, Constant.buffPropTime);
  441. break;
  442. case PropType.Attenuator:
  443. player.AddAP(Constant.attenuatorAtkDebuff, Constant.buffPropTime);
  444. break;
  445. case PropType.Divider:
  446. player.ChangeCD(Constant.dividerCdDiscount, Constant.buffPropTime);
  447. break;
  448. }
  449. }
  450. */
  451. private readonly Map gameMap;
  452. public readonly MoveEngine moveEngine;
  453. public ActionManager(Map gameMap)
  454. {
  455. this.gameMap = gameMap;
  456. this.moveEngine = new MoveEngine(
  457. gameMap: gameMap,
  458. OnCollision: (obj, collisionObj, moveVec) =>
  459. {
  460. SkillWhenColliding((Character)obj, collisionObj);
  461. Preparation.Utility.Debugger.Output(obj, " end move with " + collisionObj.ToString());
  462. //if (collisionObj is Mine)
  463. //{
  464. // ActivateMine((Character)obj, (Mine)collisionObj);
  465. // return MoveEngine.AfterCollision.ContinueCheck;
  466. //}
  467. return MoveEngine.AfterCollision.MoveMax;
  468. },
  469. EndMove: obj =>
  470. {
  471. // Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  472. }
  473. );
  474. }
  475. }
  476. }
  477. }