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

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