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

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