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.

Game.cs 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using Preparation.Utility;
  5. using Timothy.FrameRateTask;
  6. using Preparation.Interface;
  7. using GameClass.GameObj;
  8. using System.Numerics;
  9. namespace Gaming
  10. {
  11. public partial class Game
  12. {
  13. public struct PlayerInitInfo
  14. {
  15. public uint birthPointIndex;
  16. public long teamID;
  17. public long playerID;
  18. public CharacterType characterType;
  19. public PlayerInitInfo(uint birthPointIndex, long teamID, long playerID, CharacterType characterType)
  20. {
  21. this.birthPointIndex = birthPointIndex;
  22. this.teamID = teamID;
  23. this.characterType = characterType;
  24. this.playerID = playerID;
  25. }
  26. }
  27. private readonly List<Team> teamList;
  28. public List<Team> TeamList => teamList;
  29. private readonly Map gameMap;
  30. public Map GameMap => gameMap;
  31. // private readonly int numOfTeam;
  32. public long AddPlayer(PlayerInitInfo playerInitInfo)
  33. {
  34. if (!Team.teamExists(playerInitInfo.teamID))
  35. /* || !MapInfo.ValidBirthPointIdx(playerInitInfo.birthPointIdx)
  36. || gameMap.BirthPointList[playerInitInfo.birthPointIdx].Parent != null)*/
  37. return GameObj.invalidID;
  38. XY pos = gameMap.BirthPointList[playerInitInfo.birthPointIndex];
  39. // Console.WriteLine($"x,y: {pos.x},{pos.y}");
  40. Character newPlayer = (GameData.IsGhost(playerInitInfo.characterType)) ? new Ghost(pos, GameData.characterRadius, playerInitInfo.characterType) : new Student(pos, GameData.characterRadius, playerInitInfo.characterType);
  41. gameMap.Add(newPlayer);
  42. // Console.WriteLine($"GameObjDict[GameObjType.Character] length:{gameMap.GameObjDict[GameObjType.Character].Count}");
  43. teamList[(int)playerInitInfo.teamID].AddPlayer(newPlayer);
  44. newPlayer.TeamID = playerInitInfo.teamID;
  45. newPlayer.PlayerID = playerInitInfo.playerID;
  46. #region 人物装弹
  47. new Thread
  48. (
  49. () =>
  50. {
  51. while (!gameMap.Timer.IsGaming)
  52. Thread.Sleep(Math.Max(newPlayer.CD, GameData.checkInterval));
  53. long lastTime = Environment.TickCount64;
  54. new FrameRateTaskExecutor<int>(
  55. loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsResetting,
  56. loopToDo: () =>
  57. {
  58. long nowTime = Environment.TickCount64;
  59. if (newPlayer.BulletNum == newPlayer.MaxBulletNum)
  60. lastTime = nowTime;
  61. else if (nowTime - lastTime >= newPlayer.CD)
  62. {
  63. _ = newPlayer.TryAddBulletNum();
  64. lastTime = nowTime;
  65. }
  66. },
  67. timeInterval: GameData.checkInterval,
  68. finallyReturn: () => 0
  69. )
  70. {
  71. AllowTimeExceed = true/*,
  72. MaxTolerantTimeExceedCount = 5,
  73. TimeExceedAction = exceedTooMuch =>
  74. {
  75. if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
  76. }*/
  77. }
  78. .Start();
  79. }
  80. )
  81. { IsBackground = true }.Start();
  82. #endregion
  83. #region BGM,牵制得分更新
  84. new Thread
  85. (
  86. () =>
  87. {
  88. while (!gameMap.Timer.IsGaming)
  89. Thread.Sleep(GameData.checkInterval);
  90. int TimePinningDown = 0, ScoreAdded = 0;
  91. new FrameRateTaskExecutor<int>(
  92. loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsResetting,
  93. loopToDo: () =>
  94. {
  95. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  96. try
  97. {
  98. if (newPlayer.IsGhost())
  99. {
  100. double bgmVolume = 0;
  101. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  102. {
  103. if (!person.IsGhost() && XY.Distance(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  104. {
  105. if ((double)newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position) > bgmVolume)
  106. bgmVolume = newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position);
  107. }
  108. }
  109. if (bgmVolume > 0)
  110. newPlayer.AddBgm(BgmType.StudentIsApproaching, bgmVolume);
  111. }
  112. else
  113. {
  114. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  115. {
  116. if (person.IsGhost())
  117. {
  118. if (XY.Distance(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  119. newPlayer.AddBgm(BgmType.GhostIsComing, (double)newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position));
  120. if (XY.Distance(newPlayer.Position, person.Position) <= GameData.basicViewRange)
  121. {
  122. TimePinningDown += GameData.checkInterval;
  123. newPlayer.AddScore(GameData.StudentScorePinDown(TimePinningDown) - ScoreAdded);
  124. ScoreAdded = GameData.StudentScorePinDown(TimePinningDown);
  125. }
  126. else TimePinningDown = ScoreAdded = 0;
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. finally
  133. {
  134. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  135. }
  136. gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
  137. try
  138. {
  139. double bgmVolume = 0;
  140. foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
  141. {
  142. if (XY.Distance(newPlayer.Position, generator.Position) <= newPlayer.AlertnessRadius)
  143. {
  144. if ((double)newPlayer.AlertnessRadius * generator.DegreeOfRepair / GameData.degreeOfFixedGenerator / XY.Distance(newPlayer.Position, generator.Position) > bgmVolume)
  145. bgmVolume = (double)newPlayer.AlertnessRadius * generator.DegreeOfRepair / GameData.degreeOfFixedGenerator / XY.Distance(newPlayer.Position, generator.Position);
  146. }
  147. }
  148. if (bgmVolume > 0)
  149. newPlayer.AddBgm(BgmType.StudentIsApproaching, bgmVolume);
  150. }
  151. finally
  152. {
  153. gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
  154. }
  155. },
  156. timeInterval: GameData.checkInterval,
  157. finallyReturn: () => 0
  158. )
  159. {
  160. AllowTimeExceed = true/*,
  161. MaxTolerantTimeExceedCount = 5,
  162. TimeExceedAction = exceedTooMuch =>
  163. {
  164. if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
  165. }*/
  166. }
  167. .Start();
  168. }
  169. )
  170. { IsBackground = true }.Start();
  171. #endregion
  172. return newPlayer.ID;
  173. }
  174. public bool StartGame(int milliSeconds)
  175. {
  176. if (gameMap.Timer.IsGaming)
  177. return false;
  178. propManager.StartProducing();
  179. // 开始游戏
  180. new Thread
  181. (
  182. () =>
  183. {
  184. if (!gameMap.Timer.StartGame(milliSeconds))
  185. return;
  186. EndGame(); // 游戏结束时要做的事
  187. }
  188. )
  189. { IsBackground = true }.Start();
  190. return true;
  191. }
  192. public void EndGame()
  193. {
  194. }
  195. public bool MovePlayer(long playerID, int moveTimeInMilliseconds, double angle)
  196. {
  197. if (!gameMap.Timer.IsGaming)
  198. return false;
  199. Character? player = gameMap.FindPlayer(playerID);
  200. if (player != null)
  201. {
  202. bool res = actionManager.MovePlayer(player, moveTimeInMilliseconds, angle);
  203. #if DEBUG
  204. Console.WriteLine($"PlayerID:{playerID} move to ({player.Position.x},{player.Position.y})!");
  205. #endif
  206. return res;
  207. }
  208. else
  209. {
  210. #if DEBUG
  211. Console.WriteLine($"PlayerID:{playerID} player does not exists!");
  212. #endif
  213. return false;
  214. }
  215. }
  216. public bool Treat(long playerID, long playerTreatedID)
  217. {
  218. if (!gameMap.Timer.IsGaming)
  219. return false;
  220. ICharacter? player = gameMap.FindPlayer(playerID);
  221. ICharacter? playerTreated = gameMap.FindPlayer(playerTreatedID);
  222. if (player != null && playerTreated != null)
  223. {
  224. if (!playerTreated.IsGhost() && !player.IsGhost())
  225. return actionManager.Treat((Student)player, (Student)playerTreated);
  226. }
  227. return false;
  228. }
  229. public bool Rescue(long playerID, long playerRescuedID)
  230. {
  231. if (!gameMap.Timer.IsGaming)
  232. return false;
  233. ICharacter? player = gameMap.FindPlayer(playerID);
  234. ICharacter? playerRescued = gameMap.FindPlayer(playerRescuedID);
  235. if (player != null && playerRescued != null)
  236. {
  237. if (!playerRescued.IsGhost() && !player.IsGhost())
  238. return actionManager.Treat((Student)player, (Student)playerRescued);
  239. }
  240. return false;
  241. }
  242. public bool Fix(long playerID)
  243. {
  244. if (!gameMap.Timer.IsGaming)
  245. return false;
  246. ICharacter? player = gameMap.FindPlayer(playerID);
  247. if (player != null && !player.IsGhost())
  248. return actionManager.Fix((Student)player);
  249. return false;
  250. }
  251. public bool Escape(long playerID)
  252. {
  253. if (!gameMap.Timer.IsGaming)
  254. return false;
  255. ICharacter? player = gameMap.FindPlayer(playerID);
  256. if (player != null)
  257. {
  258. if (!player.IsGhost())
  259. return actionManager.Escape((Student)player);
  260. }
  261. return false;
  262. }
  263. public bool Stop(long playerID)
  264. {
  265. if (!gameMap.Timer.IsGaming)
  266. return false;
  267. Character? player = gameMap.FindPlayer(playerID);
  268. if (player != null)
  269. {
  270. return ActionManager.Stop(player);
  271. }
  272. return false;
  273. }
  274. public bool OpenDoorway(long playerID)
  275. {
  276. if (!gameMap.Timer.IsGaming)
  277. return false;
  278. Character? player = gameMap.FindPlayer(playerID);
  279. if (player != null && !player.IsGhost())
  280. {
  281. return actionManager.OpenDoorway((Student)player);
  282. }
  283. return false;
  284. }
  285. public bool OpenChest(long playerID)
  286. {
  287. if (!gameMap.Timer.IsGaming)
  288. return false;
  289. Character? player = gameMap.FindPlayer(playerID);
  290. if (player != null)
  291. {
  292. return actionManager.OpenChest(player);
  293. }
  294. return false;
  295. }
  296. public bool ClimbingThroughWindow(long playerID)
  297. {
  298. if (!gameMap.Timer.IsGaming)
  299. return false;
  300. Character? player = gameMap.FindPlayer(playerID);
  301. if (player != null)
  302. {
  303. return actionManager.ClimbingThroughWindow(player);
  304. }
  305. return false;
  306. }
  307. public bool LockOrOpenDoor(long playerID)
  308. {
  309. if (!gameMap.Timer.IsGaming)
  310. return false;
  311. Character? player = gameMap.FindPlayer(playerID);
  312. if (player != null)
  313. {
  314. return actionManager.LockOrOpenDoor(player);
  315. }
  316. return false;
  317. }
  318. public void Attack(long playerID, double angle)
  319. {
  320. if (!gameMap.Timer.IsGaming)
  321. return;
  322. Character? player = gameMap.FindPlayer(playerID);
  323. if (player != null)
  324. {
  325. _ = attackManager.Attack(player, angle);
  326. }
  327. }
  328. public void UseProp(long playerID, int indexing)
  329. {
  330. if (!gameMap.Timer.IsGaming)
  331. return;
  332. Character? player = gameMap.FindPlayer(playerID);
  333. if (player != null)
  334. {
  335. propManager.UseProp(player, indexing);
  336. }
  337. }
  338. public void ThrowProp(long playerID, int indexing)
  339. {
  340. if (!gameMap.Timer.IsGaming)
  341. return;
  342. Character? player = gameMap.FindPlayer(playerID);
  343. if (player != null)
  344. {
  345. propManager.ThrowProp(player, indexing);
  346. }
  347. }
  348. public bool PickProp(long playerID, PropType propType = PropType.Null)
  349. {
  350. if (!gameMap.Timer.IsGaming)
  351. return false;
  352. Character? player = gameMap.FindPlayer(playerID);
  353. if (player != null)
  354. {
  355. return propManager.PickProp(player, propType);
  356. }
  357. return false;
  358. }
  359. public bool UseActiveSkill(long playerID, ActiveSkillType activeSkillType)
  360. {
  361. if (!gameMap.Timer.IsGaming)
  362. return false;
  363. Character? player = gameMap.FindPlayer(playerID);
  364. if (player != null)
  365. {
  366. return skillManager.UseActiveSkill(player, activeSkillType);
  367. }
  368. else
  369. return false;
  370. }
  371. public void AllPlayerUsePassiveSkill()
  372. {
  373. if (!gameMap.Timer.IsGaming)
  374. return;
  375. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  376. try
  377. {
  378. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  379. {
  380. skillManager.UseAllPassiveSkill(player);
  381. }
  382. }
  383. finally
  384. {
  385. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  386. }
  387. }
  388. public void ClearLists(GameObjType[] objIdxes)
  389. {
  390. foreach (var idx in objIdxes)
  391. {
  392. if (idx != GameObjType.Null)
  393. {
  394. gameMap.GameObjLockDict[idx].EnterWriteLock();
  395. try
  396. {
  397. gameMap.GameObjDict[idx].Clear();
  398. }
  399. finally
  400. {
  401. gameMap.GameObjLockDict[idx].ExitWriteLock();
  402. }
  403. }
  404. }
  405. }
  406. public void ClearAllLists()
  407. {
  408. foreach (var keyValuePair in gameMap.GameObjDict)
  409. {
  410. if (!GameData.IsMap(keyValuePair.Key))
  411. {
  412. gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
  413. try
  414. {
  415. if (keyValuePair.Key == GameObjType.Character)
  416. {
  417. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  418. {
  419. player.CanMove = false;
  420. }
  421. }
  422. gameMap.GameObjDict[keyValuePair.Key].Clear();
  423. }
  424. finally
  425. {
  426. gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
  427. }
  428. }
  429. }
  430. }
  431. public int GetTeamScore(long teamID)
  432. {
  433. return teamList[(int)teamID].Score;
  434. }
  435. public List<IGameObj> GetGameObj()
  436. {
  437. var gameObjList = new List<IGameObj>();
  438. foreach (var keyValuePair in gameMap.GameObjDict)
  439. {
  440. if (!GameData.IsMap(keyValuePair.Key))
  441. {
  442. gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
  443. try
  444. {
  445. gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
  446. }
  447. finally
  448. {
  449. gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
  450. }
  451. }
  452. }
  453. return gameObjList;
  454. }
  455. public Game(uint[,] mapResource, int numOfTeam)
  456. {
  457. // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
  458. gameMap = new Map(mapResource);
  459. // 加入队伍
  460. // this.numOfTeam = numOfTeam;
  461. teamList = new List<Team>();
  462. for (int i = 0; i < numOfTeam; ++i)
  463. {
  464. teamList.Add(new Team());
  465. }
  466. attackManager = new AttackManager(gameMap);
  467. actionManager = new ActionManager(gameMap);
  468. propManager = new PropManager(gameMap);
  469. skillManager = new SkillManager(gameMap, actionManager, attackManager, propManager);
  470. }
  471. }
  472. }