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

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