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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.SetReturnOri(teamID);
  56. newPlayer.PlayerID.SetReturnOri(playerID);
  57. #region BGM,牵制得分更新
  58. new Thread
  59. (
  60. () =>
  61. {
  62. while (!gameMap.Timer.IsGaming)
  63. Thread.Sleep(GameData.checkInterval);
  64. int TimePinningDown = 0, ScoreAdded = 0;
  65. bool noise = false;
  66. if (!newPlayer.IsGhost())
  67. {
  68. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  69. try
  70. {
  71. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  72. {
  73. if (person.IsGhost())
  74. {
  75. if (person.CharacterType == CharacterType.ANoisyPerson)
  76. {
  77. noise = true;
  78. newPlayer.AddBgm(BgmType.GhostIsComing, 1411180);
  79. newPlayer.AddBgm(BgmType.GeneratorIsBeingFixed, 154991);
  80. }
  81. }
  82. }
  83. }
  84. finally
  85. {
  86. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  87. }
  88. }
  89. new FrameRateTaskExecutor<int>(
  90. loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsRemoved,
  91. loopToDo: () =>
  92. {
  93. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  94. try
  95. {
  96. if (newPlayer.IsGhost())
  97. {
  98. double bgmVolume = 0;
  99. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  100. {
  101. if (!person.IsGhost() && XY.DistanceFloor3(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  102. {
  103. if ((double)newPlayer.AlertnessRadius / XY.DistanceFloor3(newPlayer.Position, person.Position) > bgmVolume)
  104. bgmVolume = newPlayer.AlertnessRadius / XY.DistanceFloor3(newPlayer.Position, person.Position);
  105. }
  106. }
  107. newPlayer.AddBgm(BgmType.StudentIsApproaching, bgmVolume);
  108. }
  109. else
  110. {
  111. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  112. {
  113. if (person.IsGhost())
  114. {
  115. if (!noise)
  116. {
  117. if (XY.DistanceFloor3(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  118. newPlayer.AddBgm(BgmType.GhostIsComing, (double)newPlayer.AlertnessRadius / XY.DistanceFloor3(newPlayer.Position, person.Position));
  119. else newPlayer.AddBgm(BgmType.GhostIsComing, 0);
  120. }
  121. if (newPlayer.CharacterType != CharacterType.Teacher && newPlayer.CharacterType != CharacterType.Robot && newPlayer.CanPinDown() && XY.DistanceFloor3(newPlayer.Position, person.Position) <= GameData.PinningDownRange)
  122. {
  123. TimePinningDown += GameData.checkInterval;
  124. newPlayer.AddScore(GameData.StudentScorePinDown(TimePinningDown) - ScoreAdded);
  125. ScoreAdded = GameData.StudentScorePinDown(TimePinningDown);
  126. }
  127. else TimePinningDown = ScoreAdded = 0;
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. finally
  134. {
  135. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  136. }
  137. if (!noise)
  138. {
  139. gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
  140. try
  141. {
  142. double bgmVolume = 0;
  143. foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
  144. {
  145. if (XY.DistanceFloor3(newPlayer.Position, generator.Position) <= newPlayer.AlertnessRadius)
  146. {
  147. if (generator.NumOfFixing > 0 && (double)newPlayer.AlertnessRadius * generator.DegreeOfRepair / GameData.degreeOfFixedGenerator / XY.DistanceFloor3(newPlayer.Position, generator.Position) > bgmVolume)
  148. bgmVolume = (double)newPlayer.AlertnessRadius * generator.DegreeOfRepair / GameData.degreeOfFixedGenerator / XY.DistanceFloor3(newPlayer.Position, generator.Position);
  149. }
  150. }
  151. newPlayer.AddBgm(BgmType.GeneratorIsBeingFixed, bgmVolume);
  152. }
  153. finally
  154. {
  155. gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
  156. }
  157. }
  158. },
  159. timeInterval: GameData.checkInterval,
  160. finallyReturn: () => 0
  161. )
  162. {
  163. AllowTimeExceed = true/*,
  164. MaxTolerantTimeExceedCount = 5,
  165. TimeExceedAction = exceedTooMuch =>
  166. {
  167. if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
  168. }*/
  169. }
  170. .Start();
  171. }
  172. )
  173. { IsBackground = true }.Start();
  174. #endregion
  175. return newPlayer;
  176. }
  177. public void BeAddictedToGame(Student player, Ghost ghost)
  178. {
  179. long stateNum = player.SetPlayerState(RunningStateType.RunningForcibly, PlayerStateType.Addicted);
  180. if (stateNum == -1) return;
  181. if (player.CharacterType == CharacterType.Robot)
  182. {
  183. Die(player);
  184. return;
  185. }
  186. if (player.GamingAddiction > 0)
  187. {
  188. if (player.GamingAddiction < GameData.BeginGamingAddiction)
  189. player.GamingAddiction.SetV(GameData.BeginGamingAddiction);
  190. else if (player.GamingAddiction < GameData.MidGamingAddiction)
  191. player.GamingAddiction.SetV(GameData.MidGamingAddiction);
  192. else
  193. {
  194. Die(player);
  195. return;
  196. }
  197. }
  198. ghost.AddScore(GameData.TrickerScoreStudentBeAddicted);
  199. gameMap.MapAddictStudent();
  200. new Thread
  201. (() =>
  202. {
  203. Debugger.Output(player, " is addicted ");
  204. new FrameRateTaskExecutor<int>(
  205. () => stateNum == player.StateNum && !player.GamingAddiction.IsMaxV() && gameMap.Timer.IsGaming,
  206. () =>
  207. {
  208. if (player.PlayerState == PlayerStateType.Addicted) player.GamingAddiction.AddPositiveV(GameData.frameDuration);
  209. },
  210. timeInterval: GameData.frameDuration,
  211. () =>
  212. {
  213. gameMap.MapRescueStudent();
  214. if (player.GamingAddiction.IsMaxV() && gameMap.Timer.IsGaming)
  215. {
  216. Die(player);
  217. }
  218. return 0;
  219. }
  220. )
  221. .Start();
  222. }
  223. )
  224. { IsBackground = true }.Start();
  225. }
  226. public static long BeStunned(Character player, int time)
  227. {
  228. if (player.CharacterType == CharacterType.Robot) return -1;
  229. long stateNum = player.SetPlayerState(RunningStateType.RunningForcibly, PlayerStateType.Stunned);
  230. if (stateNum == -1) return -1;
  231. new Thread
  232. (() =>
  233. {
  234. Debugger.Output(player, " is stunned for " + time.ToString());
  235. Thread.Sleep(time);
  236. player.ResetPlayerState(stateNum);
  237. }
  238. )
  239. { IsBackground = true }.Start();
  240. return stateNum;
  241. }
  242. public bool TryBeAwed(Student character, Bullet bullet)
  243. {
  244. if (character.CanBeAwed())
  245. {
  246. if (BeStunned(character, GameData.basicStunnedTimeOfStudent) > 0)
  247. bullet.Parent!.AddScore(GameData.TrickerScoreStudentBeStunned(GameData.basicStunnedTimeOfStudent));
  248. return true;
  249. }
  250. return false;
  251. }
  252. /// <summary>
  253. /// 遭受攻击
  254. /// </summary>
  255. /// <param name="subHP"></param>
  256. /// <param name="hasSpear"></param>
  257. /// <param name="attacker">伤害来源</param>
  258. /// <returns>人物在受到攻击后死了吗</returns>
  259. public void BeAttacked(Student student, Bullet bullet)
  260. {
  261. Debugger.Output(student, "is being shot!");
  262. if (!bullet.Parent!.IsGhost()) return;
  263. if (student.CharacterType == CharacterType.StraightAStudent)
  264. {
  265. ((WriteAnswers)student.FindActiveSkill(ActiveSkillType.WriteAnswers)).DegreeOfMeditation.SetReturnOri(0);
  266. }
  267. student.SetDegreeOfTreatment0();
  268. if (student.NoHp()) return; // 原来已经死了
  269. Debugger.Output(bullet, " 's AP is " + bullet.AP.ToString());
  270. if (student.TryUseShield())
  271. {
  272. if (bullet.HasSpear)
  273. {
  274. long subHp = student.HP.SubPositiveV(bullet.AP);
  275. Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
  276. bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp) + GameData.ScorePropUseSpear);
  277. bullet.Parent.HP.AddPositiveV((long)bullet.Parent.Vampire * subHp);
  278. }
  279. else return;
  280. }
  281. else
  282. {
  283. long subHp;
  284. if (bullet.HasSpear)
  285. {
  286. subHp = student.HP.SubPositiveV(bullet.AP + GameData.ApSpearAdd);
  287. Debugger.Output(this, "is being shot with Spear! Now his hp is" + student.HP.ToString());
  288. }
  289. else
  290. {
  291. subHp = student.HP.SubPositiveV(bullet.AP);
  292. Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
  293. }
  294. bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp));
  295. if (student.CharacterType == CharacterType.Teacher)
  296. {
  297. student.AddScore(subHp * GameData.factorOfScoreWhenTeacherAttacked / GameData.basicApOfGhost / FactorTeacher);
  298. }
  299. bullet.Parent.HP.AddPositiveV((long)(bullet.Parent.Vampire * subHp));
  300. }
  301. if (student.HP <= 0)
  302. student.TryActivatingLIFE(); // 如果有复活甲
  303. if (student.HP <= 0)
  304. BeAddictedToGame(student, (Ghost)bullet.Parent);
  305. else TryBeAwed(student, bullet);
  306. }
  307. public bool BackSwing(Character player, int time)
  308. {
  309. if (time <= 0) return false;
  310. long stateNum = player.SetPlayerState(RunningStateType.RunningForcibly, PlayerStateType.Swinging);
  311. if (stateNum == -1) return false;
  312. new Thread
  313. (() =>
  314. {
  315. Thread.Sleep(time);
  316. player.ResetPlayerState(stateNum);
  317. }
  318. )
  319. { IsBackground = true }.Start();
  320. return true;
  321. }
  322. public void Die(Student player)
  323. {
  324. Debugger.Output(player, "die.");
  325. if (!player.TryToRemoveFromGame(PlayerStateType.Deceased)) return;
  326. for (int i = 0; i < GameData.maxNumOfPropInPropInventory; i++)
  327. {
  328. Gadget? prop = player.ConsumeProp(i);
  329. if (prop != null)
  330. {
  331. prop.ReSetPos(player.Position);
  332. gameMap.Add(prop);
  333. }
  334. }
  335. if (player.CharacterType == CharacterType.Robot)
  336. {
  337. var parent = ((Golem)player).Parent;
  338. if (parent != null && parent.CharacterType == CharacterType.TechOtaku)
  339. {
  340. ((SummonGolem)(parent.FindActiveSkill(ActiveSkillType.SummonGolem))).DeleteGolem((int)(player.PlayerID - parent.PlayerID) / GameData.numOfPeople - 1);
  341. UseRobot useRobot = (UseRobot)parent.FindActiveSkill(ActiveSkillType.UseRobot);
  342. if (useRobot.TryResetNowPlayerID((int)player.PlayerID))
  343. {
  344. lock (parent.ActionLock)
  345. {
  346. if (parent.PlayerState == PlayerStateType.UsingSkill)
  347. parent.SetPlayerStateNaturally();
  348. }
  349. }
  350. gameMap.ghost.AddScore(GameData.TrickerScoreDestroyRobot);
  351. return;
  352. }
  353. }
  354. gameMap.MapDieStudent();
  355. }
  356. }
  357. }
  358. }