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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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.Generator].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. new Thread
  172. (
  173. () =>
  174. {
  175. if (!gameMap.Timer.StartGame(milliSeconds))
  176. return;
  177. EndGame(); // 游戏结束时要做的事
  178. }
  179. )
  180. { IsBackground = true }.Start();
  181. return true;
  182. }
  183. public void EndGame()
  184. {
  185. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  186. /*try
  187. {
  188. }
  189. finally
  190. {
  191. }*/
  192. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  193. }
  194. public bool MovePlayer(long playerID, int moveTimeInMilliseconds, double angle)
  195. {
  196. if (!gameMap.Timer.IsGaming)
  197. return false;
  198. Character? player = gameMap.FindPlayer(playerID);
  199. if (player != null)
  200. {
  201. bool res = actionManager.MovePlayer(player, moveTimeInMilliseconds, angle);
  202. #if DEBUG
  203. Console.WriteLine($"PlayerID:{playerID} move to ({player.Position.x},{player.Position.y})!");
  204. #endif
  205. return res;
  206. }
  207. else
  208. {
  209. #if DEBUG
  210. Console.WriteLine($"PlayerID:{playerID} player does not exists!");
  211. #endif
  212. return false;
  213. }
  214. }
  215. public bool Treat(long playerID, long playerTreatedID)
  216. {
  217. if (!gameMap.Timer.IsGaming)
  218. return false;
  219. ICharacter? player = gameMap.FindPlayer(playerID);
  220. ICharacter? playerTreated = gameMap.FindPlayer(playerTreatedID);
  221. if (player != null && playerTreated != null)
  222. {
  223. if (!playerTreated.IsGhost() && !player.IsGhost())
  224. return actionManager.Treat((Student)player, (Student)playerTreated);
  225. }
  226. return false;
  227. }
  228. public bool Rescue(long playerID, long playerRescuedID)
  229. {
  230. if (!gameMap.Timer.IsGaming)
  231. return false;
  232. ICharacter? player = gameMap.FindPlayer(playerID);
  233. ICharacter? playerRescued = gameMap.FindPlayer(playerRescuedID);
  234. if (player != null && playerRescued != null)
  235. {
  236. if (!playerRescued.IsGhost() && !player.IsGhost())
  237. return actionManager.Treat((Student)player, (Student)playerRescued);
  238. }
  239. return false;
  240. }
  241. public bool Fix(long playerID)
  242. {
  243. if (!gameMap.Timer.IsGaming)
  244. return false;
  245. ICharacter? player = gameMap.FindPlayer(playerID);
  246. if (player != null)
  247. {
  248. if (!player.IsGhost())
  249. return actionManager.Fix((Student)player);
  250. }
  251. return false;
  252. }
  253. public bool Escape(long playerID)
  254. {
  255. if (!gameMap.Timer.IsGaming)
  256. return false;
  257. ICharacter? player = gameMap.FindPlayer(playerID);
  258. if (player != null)
  259. {
  260. if (!player.IsGhost())
  261. return actionManager.Escape((Student)player);
  262. }
  263. return false;
  264. }
  265. public bool Stop(long playerID)
  266. {
  267. if (!gameMap.Timer.IsGaming)
  268. return false;
  269. Character player = gameMap.FindPlayer(playerID);
  270. if (player != null)
  271. {
  272. return actionManager.Stop(player);
  273. }
  274. return false;
  275. }
  276. public void Attack(long playerID, double angle)
  277. {
  278. if (!gameMap.Timer.IsGaming)
  279. return;
  280. Character? player = gameMap.FindPlayer(playerID);
  281. if (player != null)
  282. {
  283. _ = attackManager.Attack(player, angle);
  284. }
  285. }
  286. public void UseProp(long playerID)
  287. {
  288. if (!gameMap.Timer.IsGaming)
  289. return;
  290. Character? player = gameMap.FindPlayer(playerID);
  291. if (player != null)
  292. {
  293. propManager.UseProp(player);
  294. }
  295. }
  296. public void ThrowProp(long playerID, int timeInmillionSeconds, double angle)
  297. {
  298. if (!gameMap.Timer.IsGaming)
  299. return;
  300. Character? player = gameMap.FindPlayer(playerID);
  301. if (player != null)
  302. {
  303. propManager.ThrowProp(player, timeInmillionSeconds, angle);
  304. }
  305. }
  306. public bool PickProp(long playerID, PropType propType = PropType.Null)
  307. {
  308. if (!gameMap.Timer.IsGaming)
  309. return false;
  310. Character? player = gameMap.FindPlayer(playerID);
  311. if (player != null)
  312. {
  313. return propManager.PickProp(player, propType);
  314. }
  315. return false;
  316. }
  317. public bool UseActiveSkill(long playerID, ActiveSkillType activeSkillType)
  318. {
  319. if (!gameMap.Timer.IsGaming)
  320. return false;
  321. Character? player = gameMap.FindPlayer(playerID);
  322. if (player != null)
  323. {
  324. return skillManager.UseActiveSkill(player, activeSkillType);
  325. }
  326. else
  327. return false;
  328. }
  329. public void AllPlayerUsePassiveSkill()
  330. {
  331. if (!gameMap.Timer.IsGaming)
  332. return;
  333. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  334. try
  335. {
  336. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  337. {
  338. skillManager.UseAllPassiveSkill(player);
  339. }
  340. }
  341. finally
  342. {
  343. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  344. }
  345. }
  346. public void ClearLists(GameObjType[] objIdxes)
  347. {
  348. foreach (var idx in objIdxes)
  349. {
  350. if (idx != GameObjType.Null)
  351. {
  352. gameMap.GameObjLockDict[idx].EnterWriteLock();
  353. try
  354. {
  355. gameMap.GameObjDict[idx].Clear();
  356. }
  357. finally
  358. {
  359. gameMap.GameObjLockDict[idx].ExitWriteLock();
  360. }
  361. }
  362. }
  363. }
  364. public void ClearAllLists()
  365. {
  366. foreach (var keyValuePair in gameMap.GameObjDict)
  367. {
  368. if (!GameData.IsMap(keyValuePair.Key))
  369. {
  370. gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
  371. try
  372. {
  373. if (keyValuePair.Key == GameObjType.Character)
  374. {
  375. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  376. {
  377. player.CanMove = false;
  378. }
  379. }
  380. gameMap.GameObjDict[keyValuePair.Key].Clear();
  381. }
  382. finally
  383. {
  384. gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
  385. }
  386. }
  387. }
  388. }
  389. public int GetTeamScore(long teamID)
  390. {
  391. return teamList[(int)teamID].Score;
  392. }
  393. public List<IGameObj> GetGameObj()
  394. {
  395. var gameObjList = new List<IGameObj>();
  396. foreach (var keyValuePair in gameMap.GameObjDict)
  397. {
  398. if (!GameData.IsMap(keyValuePair.Key))
  399. {
  400. gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
  401. try
  402. {
  403. gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
  404. }
  405. finally
  406. {
  407. gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
  408. }
  409. }
  410. }
  411. return gameObjList;
  412. }
  413. public Game(uint[,] mapResource, int numOfTeam)
  414. {
  415. // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
  416. gameMap = new Map(mapResource);
  417. // 加入队伍
  418. // this.numOfTeam = numOfTeam;
  419. teamList = new List<Team>();
  420. for (int i = 0; i < numOfTeam; ++i)
  421. {
  422. teamList.Add(new Team());
  423. }
  424. attackManager = new AttackManager(gameMap);
  425. actionManager = new ActionManager(gameMap);
  426. propManager = new PropManager(gameMap);
  427. skillManager = new SkillManager(gameMap, actionManager, attackManager, propManager);
  428. }
  429. }
  430. }