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