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.

CharacterManager.cs 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using System.Threading;
  2. using GameClass.GameObj;
  3. using Preparation.Utility;
  4. using Preparation.Interface;
  5. using Timothy.FrameRateTask;
  6. using static Gaming.Game;
  7. namespace Gaming
  8. {
  9. public partial class Game
  10. {
  11. private readonly CharacterManager characterManager;
  12. private class CharacterManager
  13. {
  14. readonly Map gameMap;
  15. public CharacterManager(Map gameMap)
  16. {
  17. this.gameMap = gameMap;
  18. }
  19. private readonly object numTeacherLock = new();
  20. private int factorTeacher = 1;
  21. public int FactorTeacher
  22. {
  23. get
  24. {
  25. lock (numTeacherLock)
  26. {
  27. return factorTeacher;
  28. }
  29. }
  30. }
  31. public void DoubleFactorTeacher()
  32. {
  33. lock (numTeacherLock)
  34. factorTeacher *= 2;
  35. }
  36. public Character? AddPlayer(XY pos, long teamID, long playerID, CharacterType characterType, Character? parent = null)
  37. {
  38. Character newPlayer;
  39. if (characterType == CharacterType.Robot)
  40. {
  41. newPlayer = new Golem(pos, GameData.characterRadius, parent);
  42. }
  43. else
  44. {
  45. if (GameData.IsGhost(characterType))
  46. newPlayer = gameMap.ghost = new Ghost(pos, GameData.characterRadius, characterType);
  47. else
  48. {
  49. newPlayer = new Student(pos, GameData.characterRadius, characterType);
  50. if (characterType == CharacterType.Teacher)
  51. DoubleFactorTeacher();
  52. }
  53. }
  54. gameMap.Add(newPlayer);
  55. newPlayer.TeamID = teamID;
  56. newPlayer.PlayerID = playerID;
  57. /* #region 人物装弹
  58. new Thread
  59. (
  60. () =>
  61. {
  62. while (!gameMap.Timer.IsGaming)
  63. Thread.Sleep(Math.Max(newPlayer.CD, GameData.checkInterval));
  64. long lastTime = Environment.TickCount64;
  65. new FrameRateTaskExecutor<int>(
  66. loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsRemoved,
  67. loopToDo: () =>
  68. {
  69. long nowTime = Environment.TickCount64;
  70. if (newPlayer.BulletNum == newPlayer.MaxBulletNum)
  71. lastTime = nowTime;
  72. else if (nowTime - lastTime >= newPlayer.CD)
  73. {
  74. _ = newPlayer.TryAddBulletNum();
  75. lastTime = nowTime;
  76. }
  77. },
  78. timeInterval: GameData.checkInterval,
  79. finallyReturn: () => 0
  80. )
  81. {
  82. AllowTimeExceed = true,
  83. }
  84. .Start();
  85. }
  86. )
  87. { IsBackground = true }.Start();
  88. #endregion
  89. */
  90. #region BGM,牵制得分更新
  91. new Thread
  92. (
  93. () =>
  94. {
  95. while (!gameMap.Timer.IsGaming)
  96. Thread.Sleep(GameData.checkInterval);
  97. int TimePinningDown = 0, ScoreAdded = 0;
  98. bool noise = false;
  99. if (!newPlayer.IsGhost())
  100. {
  101. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  102. try
  103. {
  104. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  105. {
  106. if (person.IsGhost())
  107. {
  108. if (person.CharacterType == CharacterType.ANoisyPerson)
  109. {
  110. noise = true;
  111. newPlayer.AddBgm(BgmType.GhostIsComing, 1411180);
  112. newPlayer.AddBgm(BgmType.GeneratorIsBeingFixed, 154991);
  113. }
  114. }
  115. }
  116. }
  117. finally
  118. {
  119. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  120. }
  121. }
  122. new FrameRateTaskExecutor<int>(
  123. loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsRemoved,
  124. loopToDo: () =>
  125. {
  126. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  127. try
  128. {
  129. if (newPlayer.IsGhost())
  130. {
  131. double bgmVolume = 0;
  132. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  133. {
  134. if (!person.IsGhost() && XY.DistanceFloor3(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  135. {
  136. if ((double)newPlayer.AlertnessRadius / XY.DistanceFloor3(newPlayer.Position, person.Position) > bgmVolume)
  137. bgmVolume = newPlayer.AlertnessRadius / XY.DistanceFloor3(newPlayer.Position, person.Position);
  138. }
  139. }
  140. newPlayer.AddBgm(BgmType.StudentIsApproaching, bgmVolume);
  141. }
  142. else
  143. {
  144. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  145. {
  146. if (person.IsGhost())
  147. {
  148. if (!noise)
  149. {
  150. if (XY.DistanceFloor3(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  151. newPlayer.AddBgm(BgmType.GhostIsComing, (double)newPlayer.AlertnessRadius / XY.DistanceFloor3(newPlayer.Position, person.Position));
  152. else newPlayer.AddBgm(BgmType.GhostIsComing, 0);
  153. }
  154. if (newPlayer.CharacterType != CharacterType.Teacher && newPlayer.CharacterType != CharacterType.Robot && newPlayer.CanPinDown() && XY.DistanceFloor3(newPlayer.Position, person.Position) <= GameData.PinningDownRange)
  155. {
  156. TimePinningDown += GameData.checkInterval;
  157. newPlayer.AddScore(GameData.StudentScorePinDown(TimePinningDown) - ScoreAdded);
  158. ScoreAdded = GameData.StudentScorePinDown(TimePinningDown);
  159. }
  160. else TimePinningDown = ScoreAdded = 0;
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. finally
  167. {
  168. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  169. }
  170. if (!noise)
  171. {
  172. gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
  173. try
  174. {
  175. double bgmVolume = 0;
  176. foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
  177. {
  178. if (XY.DistanceFloor3(newPlayer.Position, generator.Position) <= newPlayer.AlertnessRadius)
  179. {
  180. if (generator.NumOfFixing > 0 && (double)newPlayer.AlertnessRadius * generator.DegreeOfRepair / GameData.degreeOfFixedGenerator / XY.DistanceFloor3(newPlayer.Position, generator.Position) > bgmVolume)
  181. bgmVolume = (double)newPlayer.AlertnessRadius * generator.DegreeOfRepair / GameData.degreeOfFixedGenerator / XY.DistanceFloor3(newPlayer.Position, generator.Position);
  182. }
  183. }
  184. newPlayer.AddBgm(BgmType.GeneratorIsBeingFixed, bgmVolume);
  185. }
  186. finally
  187. {
  188. gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
  189. }
  190. }
  191. },
  192. timeInterval: GameData.checkInterval,
  193. finallyReturn: () => 0
  194. )
  195. {
  196. AllowTimeExceed = true/*,
  197. MaxTolerantTimeExceedCount = 5,
  198. TimeExceedAction = exceedTooMuch =>
  199. {
  200. if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
  201. }*/
  202. }
  203. .Start();
  204. }
  205. )
  206. { IsBackground = true }.Start();
  207. #endregion
  208. return newPlayer;
  209. }
  210. public void BeAddictedToGame(Student player, Ghost ghost)
  211. {
  212. long stateNum = player.SetPlayerState(RunningStateType.Waiting, PlayerStateType.Addicted);
  213. if (stateNum == -1) return;
  214. if (player.CharacterType == CharacterType.Robot)
  215. {
  216. Die(player);
  217. return;
  218. }
  219. if (player.GamingAddiction > 0)
  220. {
  221. if (player.GamingAddiction < GameData.BeginGamingAddiction)
  222. player.GamingAddiction = GameData.BeginGamingAddiction;
  223. else if (player.GamingAddiction < GameData.MidGamingAddiction)
  224. player.GamingAddiction = GameData.MidGamingAddiction;
  225. else
  226. {
  227. Die(player);
  228. return;
  229. }
  230. }
  231. ghost.AddScore(GameData.TrickerScoreStudentBeAddicted);
  232. gameMap.MapAddictStudent();
  233. new Thread
  234. (() =>
  235. {
  236. Debugger.Output(player, " is addicted ");
  237. new FrameRateTaskExecutor<int>(
  238. () => stateNum == player.StateNum && player.GamingAddiction < player.MaxGamingAddiction && gameMap.Timer.IsGaming,
  239. () =>
  240. {
  241. player.GamingAddiction += (player.PlayerState == PlayerStateType.Addicted) ? GameData.frameDuration : 0;
  242. },
  243. timeInterval: GameData.frameDuration,
  244. () =>
  245. {
  246. gameMap.MapRescueStudent();
  247. if (player.GamingAddiction == player.MaxGamingAddiction && gameMap.Timer.IsGaming)
  248. {
  249. Die(player);
  250. }
  251. return 0;
  252. }
  253. )
  254. .Start();
  255. }
  256. )
  257. { IsBackground = true }.Start();
  258. }
  259. public static long BeStunned(Character player, int time)
  260. {
  261. if (player.CharacterType == CharacterType.Robot) return -1;
  262. long stateNum = player.SetPlayerState(RunningStateType.RunningForcibly, PlayerStateType.Stunned);
  263. if (stateNum == -1) return -1;
  264. new Thread
  265. (() =>
  266. {
  267. Debugger.Output(player, " is stunned for " + time.ToString());
  268. Thread.Sleep(time);
  269. player.ResetPlayerState(stateNum);
  270. }
  271. )
  272. { IsBackground = true }.Start();
  273. return stateNum;
  274. }
  275. public bool TryBeAwed(Student character, Bullet bullet)
  276. {
  277. if (character.CanBeAwed())
  278. {
  279. if (BeStunned(character, GameData.basicStunnedTimeOfStudent) > 0)
  280. bullet.Parent!.AddScore(GameData.TrickerScoreStudentBeStunned(GameData.basicStunnedTimeOfStudent));
  281. return true;
  282. }
  283. return false;
  284. }
  285. /// <summary>
  286. /// 遭受攻击
  287. /// </summary>
  288. /// <param name="subHP"></param>
  289. /// <param name="hasSpear"></param>
  290. /// <param name="attacker">伤害来源</param>
  291. /// <returns>人物在受到攻击后死了吗</returns>
  292. public void BeAttacked(Student student, Bullet bullet)
  293. {
  294. #if DEBUG
  295. Debugger.Output(student, "is being shot!");
  296. #endif
  297. if (!bullet.Parent!.IsGhost()) return;
  298. if (student.CharacterType == CharacterType.StraightAStudent)
  299. {
  300. ((WriteAnswers)student.FindActiveSkill(ActiveSkillType.WriteAnswers)).DegreeOfMeditation = 0;
  301. }
  302. student.SetDegreeOfTreatment0();
  303. if (student.NoHp()) return; // 原来已经死了
  304. #if DEBUG
  305. Debugger.Output(bullet, " 's AP is " + bullet.AP.ToString());
  306. #endif
  307. if (student.TryUseShield())
  308. {
  309. if (bullet.HasSpear)
  310. {
  311. long subHp = student.TrySubHp(bullet.AP);
  312. #if DEBUG
  313. Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
  314. #endif
  315. bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp) + GameData.ScorePropUseSpear);
  316. bullet.Parent.HP = (long)(bullet.Parent.HP + (bullet.Parent.Vampire * subHp));
  317. }
  318. else return;
  319. }
  320. else
  321. {
  322. long subHp;
  323. if (bullet.HasSpear)
  324. {
  325. subHp = student.TrySubHp(bullet.AP + GameData.ApSpearAdd);
  326. #if DEBUG
  327. Debugger.Output(this, "is being shot with Spear! Now his hp is" + student.HP.ToString());
  328. #endif
  329. }
  330. else
  331. {
  332. subHp = student.TrySubHp(bullet.AP);
  333. #if DEBUG
  334. Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
  335. #endif
  336. }
  337. bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp));
  338. if (student.CharacterType == CharacterType.Teacher)
  339. {
  340. student.AddScore(subHp * GameData.factorOfScoreWhenTeacherAttacked / GameData.basicApOfGhost / FactorTeacher);
  341. }
  342. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * subHp));
  343. }
  344. if (student.HP <= 0)
  345. student.TryActivatingLIFE(); // 如果有复活甲
  346. if (student.HP <= 0)
  347. BeAddictedToGame(student, (Ghost)bullet.Parent);
  348. else TryBeAwed(student, bullet);
  349. }
  350. public bool BackSwing(Character player, int time)
  351. {
  352. if (time <= 0) return false;
  353. long stateNum = player.SetPlayerState(RunningStateType.RunningForcibly, PlayerStateType.Swinging);
  354. if (stateNum == -1) return false;
  355. new Thread
  356. (() =>
  357. {
  358. Thread.Sleep(time);
  359. player.ResetPlayerState(stateNum);
  360. }
  361. )
  362. { IsBackground = true }.Start();
  363. return true;
  364. }
  365. public void Die(Student player)
  366. {
  367. Debugger.Output(player, "die.");
  368. if (!player.TryToRemoveFromGame(PlayerStateType.Deceased)) return;
  369. for (int i = 0; i < GameData.maxNumOfPropInPropInventory; i++)
  370. {
  371. Gadget? prop = player.UseProp(i);
  372. if (prop != null)
  373. {
  374. prop.ReSetPos(player.Position);
  375. gameMap.Add(prop);
  376. }
  377. }
  378. if (player.CharacterType == CharacterType.Robot)
  379. {
  380. var parent = ((Golem)player).Parent;
  381. if (parent != null && parent.CharacterType == CharacterType.TechOtaku)
  382. {
  383. ((SummonGolem)(parent.FindActiveSkill(ActiveSkillType.SummonGolem))).DeleteGolem((int)(player.PlayerID - parent.PlayerID) / GameData.numOfPeople - 1);
  384. UseRobot useRobot = (UseRobot)parent.FindActiveSkill(ActiveSkillType.UseRobot);
  385. if (useRobot.TryResetNowPlayerID((int)player.PlayerID))
  386. {
  387. lock (parent.ActionLock)
  388. {
  389. if (parent.PlayerState == PlayerStateType.UsingSkill)
  390. parent.SetPlayerStateNaturally();
  391. }
  392. }
  393. gameMap.ghost.AddScore(GameData.TrickerScoreDestroyRobot);
  394. return;
  395. }
  396. }
  397. gameMap.MapDieStudent();
  398. }
  399. }
  400. }
  401. }