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

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