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

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