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