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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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(newPlayer.CD);
  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. 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((int)GameData.checkInterval);
  90. new FrameRateTaskExecutor<int>(
  91. loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsResetting,
  92. loopToDo: () =>
  93. {
  94. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  95. try
  96. {
  97. if (newPlayer.IsGhost())
  98. {
  99. double bgmVolume = 0;
  100. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  101. {
  102. if (!person.IsGhost() && XY.Distance(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  103. {
  104. if ((double)newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position) > bgmVolume)
  105. bgmVolume = newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position);
  106. }
  107. }
  108. if (bgmVolume > 0)
  109. newPlayer.BgmDictionary.Add(BgmType.StudentIsApproaching, bgmVolume);
  110. }
  111. else
  112. {
  113. foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
  114. {
  115. if (person.IsGhost())
  116. {
  117. if (XY.Distance(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
  118. newPlayer.BgmDictionary.Add(BgmType.GhostIsComing, (double)newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position));
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. finally
  125. {
  126. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  127. }
  128. gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
  129. try
  130. {
  131. double bgmVolume = 0;
  132. foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
  133. {
  134. if (XY.Distance(newPlayer.Position, generator.Position) <= newPlayer.AlertnessRadius)
  135. {
  136. if ((double)newPlayer.AlertnessRadius * generator.DegreeOfFRepair / GameData.degreeOfFixedGenerator / XY.Distance(newPlayer.Position, generator.Position) > bgmVolume)
  137. bgmVolume = (double)newPlayer.AlertnessRadius * generator.DegreeOfFRepair / GameData.degreeOfFixedGenerator / XY.Distance(newPlayer.Position, generator.Position);
  138. }
  139. }
  140. if (bgmVolume > 0)
  141. newPlayer.BgmDictionary.Add(BgmType.StudentIsApproaching, bgmVolume);
  142. }
  143. finally
  144. {
  145. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  146. }
  147. },
  148. timeInterval: GameData.checkInterval,
  149. finallyReturn: () => 0
  150. )
  151. {
  152. AllowTimeExceed = true/*,
  153. MaxTolerantTimeExceedCount = 5,
  154. TimeExceedAction = exceedTooMuch =>
  155. {
  156. if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
  157. }*/
  158. }
  159. .Start();
  160. }
  161. )
  162. { IsBackground = true }.Start();
  163. #endregion
  164. return newPlayer.ID;
  165. }
  166. public bool StartGame(int milliSeconds)
  167. {
  168. if (gameMap.Timer.IsGaming)
  169. return false;
  170. propManager.StartProducing();
  171. // 开始游戏
  172. if (!gameMap.Timer.StartGame(milliSeconds))
  173. return false;
  174. EndGame(); // 游戏结束时要做的事
  175. return true;
  176. }
  177. public void EndGame()
  178. {
  179. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  180. /*try
  181. {
  182. }
  183. finally
  184. {
  185. }*/
  186. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  187. }
  188. public bool MovePlayer(long playerID, int moveTimeInMilliseconds, double angle)
  189. {
  190. if (!gameMap.Timer.IsGaming)
  191. return false;
  192. Character? player = gameMap.FindPlayer(playerID);
  193. if (player != null)
  194. {
  195. bool res = actionManager.MovePlayer(player, moveTimeInMilliseconds, angle);
  196. #if DEBUG
  197. Console.WriteLine($"PlayerID:{playerID} move to ({player.Position.x},{player.Position.y})!");
  198. #endif
  199. return res;
  200. }
  201. else
  202. {
  203. #if DEBUG
  204. Console.WriteLine($"PlayerID:{playerID} player does not exists!");
  205. #endif
  206. return false;
  207. }
  208. }
  209. public bool Treat(long playerID, long playerTreatedID)
  210. {
  211. if (!gameMap.Timer.IsGaming)
  212. return false;
  213. ICharacter? player = gameMap.FindPlayer(playerID);
  214. ICharacter? playerTreated = gameMap.FindPlayer(playerTreatedID);
  215. if (player != null && playerTreated != null)
  216. {
  217. if (!playerTreated.IsGhost() && !player.IsGhost())
  218. return actionManager.Treat((Student)player, (Student)playerTreated);
  219. }
  220. return false;
  221. }
  222. public bool Rescue(long playerID, long playerRescuedID)
  223. {
  224. if (!gameMap.Timer.IsGaming)
  225. return false;
  226. ICharacter? player = gameMap.FindPlayer(playerID);
  227. ICharacter? playerRescued = gameMap.FindPlayer(playerRescuedID);
  228. if (player != null && playerRescued != null)
  229. {
  230. if (!playerRescued.IsGhost() && !player.IsGhost())
  231. return actionManager.Treat((Student)player, (Student)playerRescued);
  232. }
  233. return false;
  234. }
  235. public bool Fix(long playerID)
  236. {
  237. if (!gameMap.Timer.IsGaming)
  238. return false;
  239. ICharacter? player = gameMap.FindPlayer(playerID);
  240. if (player != null)
  241. {
  242. if (!player.IsGhost())
  243. return actionManager.Fix((Student)player);
  244. }
  245. return false;
  246. }
  247. public bool Escape(long playerID)
  248. {
  249. if (!gameMap.Timer.IsGaming)
  250. return false;
  251. ICharacter? player = gameMap.FindPlayer(playerID);
  252. if (player != null)
  253. {
  254. if (!player.IsGhost())
  255. return actionManager.Escape((Student)player);
  256. }
  257. return false;
  258. }
  259. public bool Stop(long playerID)
  260. {
  261. if (!gameMap.Timer.IsGaming)
  262. return false;
  263. Character player = gameMap.FindPlayer(playerID);
  264. if (player != null)
  265. {
  266. return actionManager.Stop(player);
  267. }
  268. return false;
  269. }
  270. public void Attack(long playerID, double angle)
  271. {
  272. if (!gameMap.Timer.IsGaming)
  273. return;
  274. Character? player = gameMap.FindPlayer(playerID);
  275. if (player != null)
  276. {
  277. _ = attackManager.Attack(player, angle);
  278. }
  279. }
  280. public void UseProp(long playerID)
  281. {
  282. if (!gameMap.Timer.IsGaming)
  283. return;
  284. Character? player = gameMap.FindPlayer(playerID);
  285. if (player != null)
  286. {
  287. propManager.UseProp(player);
  288. }
  289. }
  290. public void ThrowProp(long playerID, int timeInmillionSeconds, double angle)
  291. {
  292. if (!gameMap.Timer.IsGaming)
  293. return;
  294. Character? player = gameMap.FindPlayer(playerID);
  295. if (player != null)
  296. {
  297. propManager.ThrowProp(player, timeInmillionSeconds, angle);
  298. }
  299. }
  300. public bool PickProp(long playerID, PropType propType = PropType.Null)
  301. {
  302. if (!gameMap.Timer.IsGaming)
  303. return false;
  304. Character? player = gameMap.FindPlayer(playerID);
  305. if (player != null)
  306. {
  307. return propManager.PickProp(player, propType);
  308. }
  309. return false;
  310. }
  311. public bool UseActiveSkill(long playerID, ActiveSkillType activeSkillType)
  312. {
  313. if (!gameMap.Timer.IsGaming)
  314. return false;
  315. Character? player = gameMap.FindPlayer(playerID);
  316. if (player != null)
  317. {
  318. return skillManager.UseActiveSkill(this.GameMap, player, activeSkillType);
  319. }
  320. else
  321. return false;
  322. }
  323. public void AllPlayerUsePassiveSkill()
  324. {
  325. if (!gameMap.Timer.IsGaming)
  326. return;
  327. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  328. try
  329. {
  330. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  331. {
  332. skillManager.UseAllPassiveSkill(this.GameMap, player);
  333. }
  334. }
  335. finally
  336. {
  337. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  338. }
  339. }
  340. public void ClearLists(GameObjType[] objIdxes)
  341. {
  342. foreach (var idx in objIdxes)
  343. {
  344. if (idx != GameObjType.Null)
  345. {
  346. gameMap.GameObjLockDict[idx].EnterWriteLock();
  347. try
  348. {
  349. gameMap.GameObjDict[idx].Clear();
  350. }
  351. finally
  352. {
  353. gameMap.GameObjLockDict[idx].ExitWriteLock();
  354. }
  355. }
  356. }
  357. }
  358. public void ClearAllLists()
  359. {
  360. foreach (var keyValuePair in gameMap.GameObjDict)
  361. {
  362. if (!GameData.IsMap(keyValuePair.Key))
  363. {
  364. gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
  365. try
  366. {
  367. if (keyValuePair.Key == GameObjType.Character)
  368. {
  369. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  370. {
  371. player.CanMove = false;
  372. }
  373. }
  374. gameMap.GameObjDict[keyValuePair.Key].Clear();
  375. }
  376. finally
  377. {
  378. gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
  379. }
  380. }
  381. }
  382. }
  383. public int GetTeamScore(long teamID)
  384. {
  385. return teamList[(int)teamID].Score;
  386. }
  387. public List<IGameObj> GetGameObj()
  388. {
  389. var gameObjList = new List<IGameObj>();
  390. foreach (var keyValuePair in gameMap.GameObjDict)
  391. {
  392. if (!GameData.IsMap(keyValuePair.Key))
  393. {
  394. gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
  395. try
  396. {
  397. gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
  398. }
  399. finally
  400. {
  401. gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
  402. }
  403. }
  404. }
  405. return gameObjList;
  406. }
  407. public Game(uint[,] mapResource, int numOfTeam)
  408. {
  409. // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
  410. gameMap = new Map(mapResource);
  411. // 加入队伍
  412. // this.numOfTeam = numOfTeam;
  413. teamList = new List<Team>();
  414. for (int i = 0; i < numOfTeam; ++i)
  415. {
  416. teamList.Add(new Team());
  417. }
  418. skillManager = new SkillManager();
  419. attackManager = new AttackManager(gameMap);
  420. actionManager = new ActionManager(gameMap);
  421. propManager = new PropManager(gameMap);
  422. }
  423. }
  424. }