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

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