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

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