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

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