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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 = 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. if (player.CharacterType == CharacterType.Robot)
  213. {
  214. ghost.AddScore(GameData.TrickerScoreDestroyRobot);
  215. Die(player);
  216. return;
  217. }
  218. ghost.AddScore(GameData.TrickerScoreStudentBeAddicted);
  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. ghost.AddScore(GameData.TrickerScoreStudentDie);
  228. Die(player);
  229. return;
  230. }
  231. }
  232. player.SetPlayerState(PlayerStateType.Addicted);
  233. long threadNum = player.StateNum;
  234. gameMap.MapAddictStudent();
  235. new Thread
  236. (() =>
  237. {
  238. #if DEBUG
  239. Debugger.Output(player, " is addicted ");
  240. #endif
  241. new FrameRateTaskExecutor<int>(
  242. () => threadNum == player.StateNum && player.GamingAddiction < player.MaxGamingAddiction && gameMap.Timer.IsGaming,
  243. () =>
  244. {
  245. player.GamingAddiction += (player.PlayerState == PlayerStateType.Addicted) ? GameData.frameDuration : 0;
  246. },
  247. timeInterval: GameData.frameDuration,
  248. () =>
  249. {
  250. gameMap.MapRescueStudent();
  251. if (player.GamingAddiction == player.MaxGamingAddiction && gameMap.Timer.IsGaming)
  252. {
  253. ghost.AddScore(GameData.TrickerScoreStudentDie);
  254. Die(player);
  255. }
  256. return 0;
  257. }
  258. )
  259. .Start();
  260. }
  261. )
  262. { IsBackground = true }.Start();
  263. }
  264. public static long BeStunned(Character player, int time)
  265. {
  266. if (player.CharacterType == CharacterType.Robot) return -1;
  267. long threadNum = player.SetPlayerState(PlayerStateType.Stunned);
  268. if (threadNum == -1) return -1;
  269. new Thread
  270. (() =>
  271. {
  272. Debugger.Output(player, " is stunned for " + time.ToString());
  273. Thread.Sleep(time);
  274. if (threadNum == player.StateNum)
  275. player.SetPlayerState();
  276. }
  277. )
  278. { IsBackground = true }.Start();
  279. return threadNum;
  280. }
  281. public bool TryBeAwed(Student character, Bullet bullet)
  282. {
  283. if (character.CanBeAwed())
  284. {
  285. if (BeStunned(character, GameData.basicStunnedTimeOfStudent) > 0)
  286. bullet.Parent!.AddScore(GameData.TrickerScoreStudentBeStunned(GameData.basicStunnedTimeOfStudent));
  287. return true;
  288. }
  289. return false;
  290. }
  291. /// <summary>
  292. /// 遭受攻击
  293. /// </summary>
  294. /// <param name="subHP"></param>
  295. /// <param name="hasSpear"></param>
  296. /// <param name="attacker">伤害来源</param>
  297. /// <returns>人物在受到攻击后死了吗</returns>
  298. public void BeAttacked(Student student, Bullet bullet)
  299. {
  300. #if DEBUG
  301. Debugger.Output(student, "is being shot!");
  302. #endif
  303. if (!bullet.Parent!.IsGhost()) return;
  304. if (student.CharacterType == CharacterType.StraightAStudent)
  305. {
  306. ((WriteAnswers)student.FindActiveSkill(ActiveSkillType.WriteAnswers)).DegreeOfMeditation = 0;
  307. }
  308. student.SetDegreeOfTreatment0();
  309. if (student.NoHp()) return; // 原来已经死了
  310. #if DEBUG
  311. Debugger.Output(bullet, " 's AP is " + bullet.AP.ToString());
  312. #endif
  313. if (student.TryUseShield())
  314. {
  315. if (bullet.HasSpear)
  316. {
  317. long subHp = student.TrySubHp(bullet.AP);
  318. #if DEBUG
  319. Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
  320. #endif
  321. bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp) + GameData.ScorePropUseSpear);
  322. bullet.Parent.HP = (long)(bullet.Parent.HP + (bullet.Parent.Vampire * subHp));
  323. }
  324. else return;
  325. }
  326. else
  327. {
  328. long subHp;
  329. if (bullet.HasSpear)
  330. {
  331. subHp = student.TrySubHp(bullet.AP + GameData.ApSpearAdd);
  332. #if DEBUG
  333. Debugger.Output(this, "is being shot with Spear! Now his hp is" + student.HP.ToString());
  334. #endif
  335. }
  336. else
  337. {
  338. subHp = student.TrySubHp(bullet.AP);
  339. #if DEBUG
  340. Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
  341. #endif
  342. }
  343. bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp));
  344. if (student.CharacterType == CharacterType.Teacher)
  345. {
  346. student.AddScore(subHp * GameData.factorOfScoreWhenTeacherAttacked / GameData.basicApOfGhost / FactorTeacher);
  347. }
  348. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * subHp));
  349. }
  350. if (student.HP <= 0)
  351. student.TryActivatingLIFE(); // 如果有复活甲
  352. if (student.HP <= 0)
  353. BeAddictedToGame(student, (Ghost)bullet.Parent);
  354. else TryBeAwed(student, bullet);
  355. }
  356. public bool BackSwing(Character player, int time)
  357. {
  358. if (time <= 0) return false;
  359. long stateNum = player.SetPlayerState(PlayerStateType.Swinging);
  360. if (stateNum == -1) return false;
  361. new Thread
  362. (() =>
  363. {
  364. player.ThreadNum.WaitOne();
  365. Thread.Sleep(time);
  366. lock (player.ActionLock)
  367. {
  368. if (stateNum == player.StateNum)
  369. {
  370. player.ThreadNum.Release();
  371. player.SetPlayerStateNaturally();
  372. }
  373. }
  374. }
  375. )
  376. { IsBackground = true }.Start();
  377. return true;
  378. }
  379. public void Die(Student player)
  380. {
  381. Debugger.Output(player, "die.");
  382. if (player.PlayerState == PlayerStateType.Deceased) return;
  383. player.RemoveFromGame(PlayerStateType.Deceased);
  384. for (int i = 0; i < GameData.maxNumOfPropInPropInventory; i++)
  385. {
  386. Gadget? prop = player.UseProp(i);
  387. if (prop != null)
  388. {
  389. prop.ReSetPos(player.Position);
  390. gameMap.Add(prop);
  391. }
  392. }
  393. if (player.CharacterType == CharacterType.Robot)
  394. {
  395. var parent = ((Golem)player).Parent;
  396. if (parent != null && parent.CharacterType == CharacterType.TechOtaku)
  397. {
  398. ((SummonGolem)(parent.FindActiveSkill(ActiveSkillType.SummonGolem))).DeleteGolem((int)(player.PlayerID - parent.PlayerID) / GameData.numOfPeople - 1);
  399. UseRobot useRobot = (UseRobot)parent.FindActiveSkill(ActiveSkillType.UseRobot);
  400. if (useRobot.TryResetNowPlayerID((int)player.PlayerID))
  401. {
  402. lock (parent.ActionLock)
  403. {
  404. if (parent.PlayerState == PlayerStateType.UsingSkill)
  405. parent.SetPlayerState();
  406. }
  407. }
  408. }
  409. return;
  410. }
  411. gameMap.MapDieStudent();
  412. }
  413. }
  414. }
  415. }