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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 = -1)
  217. {
  218. if (!gameMap.Timer.IsGaming)
  219. return false;
  220. ICharacter? player = gameMap.FindPlayer(playerID);
  221. if (playerTreatedID == -1)
  222. {
  223. if (player != null && !player.IsGhost())
  224. return actionManager.Treat((Student)player);
  225. }
  226. else
  227. {
  228. ICharacter? playerTreated = gameMap.FindPlayer(playerTreatedID);
  229. if (player != null && playerTreated != null)
  230. {
  231. if (!playerTreated.IsGhost() && !player.IsGhost())
  232. return actionManager.Treat((Student)player, (Student)playerTreated);
  233. }
  234. }
  235. return false;
  236. }
  237. public bool Rescue(long playerID, long playerRescuedID = -1)
  238. {
  239. if (!gameMap.Timer.IsGaming)
  240. return false;
  241. ICharacter? player = gameMap.FindPlayer(playerID);
  242. if (playerRescuedID == -1)
  243. {
  244. if (player != null && !player.IsGhost())
  245. return actionManager.Rescue((Student)player);
  246. }
  247. else
  248. {
  249. ICharacter? playerRescued = gameMap.FindPlayer(playerRescuedID);
  250. if (player != null && playerRescued != null)
  251. {
  252. if (!playerRescued.IsGhost() && !player.IsGhost())
  253. return actionManager.Rescue((Student)player, (Student)playerRescued);
  254. }
  255. }
  256. return false;
  257. }
  258. public bool Fix(long playerID)
  259. {
  260. if (!gameMap.Timer.IsGaming)
  261. return false;
  262. ICharacter? player = gameMap.FindPlayer(playerID);
  263. if (player != null && !player.IsGhost())
  264. return actionManager.Fix((Student)player);
  265. return false;
  266. }
  267. public bool Escape(long playerID)
  268. {
  269. if (!gameMap.Timer.IsGaming)
  270. return false;
  271. ICharacter? player = gameMap.FindPlayer(playerID);
  272. if (player != null)
  273. {
  274. if (!player.IsGhost())
  275. return actionManager.Escape((Student)player);
  276. }
  277. return false;
  278. }
  279. public bool Stop(long playerID)
  280. {
  281. if (!gameMap.Timer.IsGaming)
  282. return false;
  283. Character? player = gameMap.FindPlayer(playerID);
  284. if (player != null)
  285. {
  286. return ActionManager.Stop(player);
  287. }
  288. return false;
  289. }
  290. public bool OpenDoorway(long playerID)
  291. {
  292. if (!gameMap.Timer.IsGaming)
  293. return false;
  294. Character? player = gameMap.FindPlayer(playerID);
  295. if (player != null && !player.IsGhost())
  296. {
  297. return actionManager.OpenDoorway((Student)player);
  298. }
  299. return false;
  300. }
  301. public bool OpenChest(long playerID)
  302. {
  303. if (!gameMap.Timer.IsGaming)
  304. return false;
  305. Character? player = gameMap.FindPlayer(playerID);
  306. if (player != null)
  307. {
  308. return actionManager.OpenChest(player);
  309. }
  310. return false;
  311. }
  312. public bool ClimbingThroughWindow(long playerID)
  313. {
  314. if (!gameMap.Timer.IsGaming)
  315. return false;
  316. Character? player = gameMap.FindPlayer(playerID);
  317. if (player != null)
  318. {
  319. return actionManager.ClimbingThroughWindow(player);
  320. }
  321. return false;
  322. }
  323. public bool LockOrOpenDoor(long playerID)
  324. {
  325. if (!gameMap.Timer.IsGaming)
  326. return false;
  327. Character? player = gameMap.FindPlayer(playerID);
  328. if (player != null)
  329. {
  330. return actionManager.LockOrOpenDoor(player);
  331. }
  332. return false;
  333. }
  334. public void Attack(long playerID, double angle)
  335. {
  336. if (!gameMap.Timer.IsGaming)
  337. return;
  338. Character? player = gameMap.FindPlayer(playerID);
  339. if (player != null)
  340. {
  341. _ = attackManager.Attack(player, angle);
  342. }
  343. }
  344. public void UseProp(long playerID, PropType propType = PropType.Null)
  345. {
  346. if (!gameMap.Timer.IsGaming)
  347. return;
  348. Character? player = gameMap.FindPlayer(playerID);
  349. if (player != null)
  350. {
  351. PropManager.UseProp(player, propType);
  352. }
  353. }
  354. public void ThrowProp(long playerID, PropType propType = PropType.Null)
  355. {
  356. if (!gameMap.Timer.IsGaming)
  357. return;
  358. Character? player = gameMap.FindPlayer(playerID);
  359. if (player != null)
  360. {
  361. propManager.ThrowProp(player, propType);
  362. }
  363. }
  364. public bool PickProp(long playerID, PropType propType = PropType.Null)
  365. {
  366. if (!gameMap.Timer.IsGaming)
  367. return false;
  368. Character? player = gameMap.FindPlayer(playerID);
  369. if (player != null)
  370. {
  371. return propManager.PickProp(player, propType);
  372. }
  373. return false;
  374. }
  375. public bool UseActiveSkill(long playerID, int skillNum)
  376. {
  377. if (!gameMap.Timer.IsGaming)
  378. return false;
  379. Character? player = gameMap.FindPlayer(playerID);
  380. if (player != null)
  381. {
  382. return skillManager.UseActiveSkill(player, player.Occupation.ListOfIActiveSkill[skillNum]);
  383. }
  384. else
  385. return false;
  386. }
  387. public void AllPlayerUsePassiveSkill()
  388. {
  389. if (!gameMap.Timer.IsGaming)
  390. return;
  391. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  392. try
  393. {
  394. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  395. {
  396. skillManager.UseAllPassiveSkill(player);
  397. }
  398. }
  399. finally
  400. {
  401. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  402. }
  403. }
  404. public void ClearLists(GameObjType[] objIdxes)
  405. {
  406. foreach (var idx in objIdxes)
  407. {
  408. if (idx != GameObjType.Null)
  409. {
  410. gameMap.GameObjLockDict[idx].EnterWriteLock();
  411. try
  412. {
  413. gameMap.GameObjDict[idx].Clear();
  414. }
  415. finally
  416. {
  417. gameMap.GameObjLockDict[idx].ExitWriteLock();
  418. }
  419. }
  420. }
  421. }
  422. public void ClearAllLists()
  423. {
  424. foreach (var keyValuePair in gameMap.GameObjDict)
  425. {
  426. if (!GameData.IsMap(keyValuePair.Key))
  427. {
  428. gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
  429. try
  430. {
  431. if (keyValuePair.Key == GameObjType.Character)
  432. {
  433. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  434. {
  435. player.CanMove = false;
  436. }
  437. }
  438. gameMap.GameObjDict[keyValuePair.Key].Clear();
  439. }
  440. finally
  441. {
  442. gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
  443. }
  444. }
  445. }
  446. }
  447. public int GetTeamScore(long teamID)
  448. {
  449. return teamList[(int)teamID].Score;
  450. }
  451. public List<IGameObj> GetGameObj()
  452. {
  453. var gameObjList = new List<IGameObj>();
  454. foreach (var keyValuePair in gameMap.GameObjDict)
  455. {
  456. if (!GameData.IsMap(keyValuePair.Key))
  457. {
  458. gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
  459. try
  460. {
  461. gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
  462. }
  463. finally
  464. {
  465. gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
  466. }
  467. }
  468. }
  469. return gameObjList;
  470. }
  471. public Game(uint[,] mapResource, int numOfTeam)
  472. {
  473. // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
  474. gameMap = new Map(mapResource);
  475. // 加入队伍
  476. // this.numOfTeam = numOfTeam;
  477. teamList = new List<Team>();
  478. for (int i = 0; i < numOfTeam; ++i)
  479. {
  480. teamList.Add(new Team());
  481. }
  482. attackManager = new AttackManager(gameMap);
  483. actionManager = new ActionManager(gameMap);
  484. propManager = new PropManager(gameMap);
  485. skillManager = new SkillManager(gameMap, actionManager, attackManager, propManager);
  486. }
  487. }
  488. }