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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 int teamID;
  17. public int playerID;
  18. public CharacterType characterType;
  19. public PlayerInitInfo(uint birthPointIndex, int teamID, int 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. Character? newPlayer = characterManager.AddPlayer(pos, playerInitInfo.teamID, playerInitInfo.playerID, playerInitInfo.characterType);
  40. if (newPlayer == null) return GameObj.invalidID;
  41. // Console.WriteLine($"x,y: {pos.x},{pos.y}");
  42. // Console.WriteLine($"GameObjDict[GameObjType.Character] length:{gameMap.GameObjDict[GameObjType.Character].Count}");
  43. teamList[(int)playerInitInfo.teamID].AddPlayer(newPlayer);
  44. return newPlayer.ID;
  45. }
  46. public bool StartGame(int milliSeconds)
  47. {
  48. if (gameMap.Timer.IsGaming)
  49. return false;
  50. propManager.StartProducing();
  51. // 开始游戏
  52. new Thread
  53. (
  54. () =>
  55. {
  56. if (!gameMap.Timer.StartGame(milliSeconds))
  57. return;
  58. EndGame(); // 游戏结束时要做的事
  59. }
  60. )
  61. { IsBackground = true }.Start();
  62. return true;
  63. }
  64. public void EndGame()
  65. {
  66. }
  67. public bool MovePlayer(long playerID, int moveTimeInMilliseconds, double angle)
  68. {
  69. if (!gameMap.Timer.IsGaming)
  70. return false;
  71. Character? player = gameMap.FindPlayerToAction(playerID);
  72. if (player != null)
  73. {
  74. return actionManager.MovePlayer(player, moveTimeInMilliseconds, angle);
  75. }
  76. else
  77. {
  78. #if DEBUG
  79. Console.WriteLine($"PlayerID:{playerID} player does not exists!");
  80. #endif
  81. return false;
  82. }
  83. }
  84. public bool Treat(long playerID, long playerTreatedID = -1)
  85. {
  86. if (!gameMap.Timer.IsGaming)
  87. return false;
  88. ICharacter? player = gameMap.FindPlayerToAction(playerID);
  89. if (playerTreatedID == -1)
  90. {
  91. if (player != null && !player.IsGhost())
  92. return actionManager.Treat((Student)player);
  93. }
  94. else
  95. {
  96. ICharacter? playerTreated = gameMap.FindPlayer(playerTreatedID);
  97. if (player != null && playerTreated != null)
  98. {
  99. if (!playerTreated.IsGhost() && !player.IsGhost())
  100. return actionManager.Treat((Student)player, (Student)playerTreated);
  101. }
  102. }
  103. return false;
  104. }
  105. public bool Rescue(long playerID, long playerRescuedID = -1)
  106. {
  107. if (!gameMap.Timer.IsGaming)
  108. return false;
  109. ICharacter? player = gameMap.FindPlayerToAction(playerID);
  110. if (playerRescuedID == -1)
  111. {
  112. if (player != null && !player.IsGhost())
  113. return actionManager.Rescue((Student)player);
  114. }
  115. else
  116. {
  117. ICharacter? playerRescued = gameMap.FindPlayer(playerRescuedID);
  118. if (player != null && playerRescued != null)
  119. {
  120. if (!playerRescued.IsGhost() && !player.IsGhost())
  121. return actionManager.Rescue((Student)player, (Student)playerRescued);
  122. }
  123. }
  124. return false;
  125. }
  126. public bool Fix(long playerID)
  127. {
  128. if (!gameMap.Timer.IsGaming)
  129. return false;
  130. ICharacter? player = gameMap.FindPlayerToAction(playerID);
  131. if (player != null && !player.IsGhost())
  132. return actionManager.Fix((Student)player);
  133. return false;
  134. }
  135. public bool Escape(long playerID)
  136. {
  137. if (!gameMap.Timer.IsGaming)
  138. return false;
  139. ICharacter? player = gameMap.FindPlayerToAction(playerID);
  140. if (player != null)
  141. {
  142. if (!player.IsGhost())
  143. return actionManager.Escape((Student)player);
  144. }
  145. return false;
  146. }
  147. public bool Stop(long playerID)
  148. {
  149. if (!gameMap.Timer.IsGaming)
  150. return false;
  151. Character? player = gameMap.FindPlayerToAction(playerID);
  152. if (player != null)
  153. {
  154. return actionManager.Stop(player);
  155. }
  156. return false;
  157. }
  158. public bool OpenDoorway(long playerID)
  159. {
  160. if (!gameMap.Timer.IsGaming)
  161. return false;
  162. Character? player = gameMap.FindPlayerToAction(playerID);
  163. if (player != null && !player.IsGhost())
  164. {
  165. return actionManager.OpenDoorway((Student)player);
  166. }
  167. return false;
  168. }
  169. public bool OpenChest(long playerID)
  170. {
  171. if (!gameMap.Timer.IsGaming)
  172. return false;
  173. Character? player = gameMap.FindPlayerToAction(playerID);
  174. if (player != null)
  175. {
  176. return actionManager.OpenChest(player);
  177. }
  178. return false;
  179. }
  180. public bool ClimbingThroughWindow(long playerID)
  181. {
  182. if (!gameMap.Timer.IsGaming)
  183. return false;
  184. Character? player = gameMap.FindPlayerToAction(playerID);
  185. if (player != null)
  186. {
  187. return actionManager.ClimbingThroughWindow(player);
  188. }
  189. return false;
  190. }
  191. public bool LockOrOpenDoor(long playerID)
  192. {
  193. if (!gameMap.Timer.IsGaming)
  194. return false;
  195. Character? player = gameMap.FindPlayerToAction(playerID);
  196. if (player != null)
  197. {
  198. return actionManager.LockOrOpenDoor(player);
  199. }
  200. return false;
  201. }
  202. public bool Attack(long playerID, double angle)
  203. {
  204. if (!gameMap.Timer.IsGaming)
  205. return false;
  206. Character? player = gameMap.FindPlayerToAction(playerID);
  207. if (player != null && player.Commandable())
  208. {
  209. return attackManager.Attack(player, angle);
  210. }
  211. return false;
  212. }
  213. public void UseProp(long playerID, PropType propType = PropType.Null)
  214. {
  215. if (!gameMap.Timer.IsGaming)
  216. return;
  217. Character? player = gameMap.FindPlayerToAction(playerID);
  218. if (player != null)
  219. {
  220. propManager.UseProp(player, propType);
  221. }
  222. }
  223. public void ThrowProp(long playerID, PropType propType = PropType.Null)
  224. {
  225. if (!gameMap.Timer.IsGaming)
  226. return;
  227. Character? player = gameMap.FindPlayerToAction(playerID);
  228. if (player != null)
  229. {
  230. propManager.ThrowProp(player, propType);
  231. }
  232. }
  233. public bool PickProp(long playerID, PropType propType = PropType.Null)
  234. {
  235. if (!gameMap.Timer.IsGaming)
  236. return false;
  237. Character? player = gameMap.FindPlayerToAction(playerID);
  238. if (player != null)
  239. {
  240. return propManager.PickProp(player, propType);
  241. }
  242. return false;
  243. }
  244. public bool UseActiveSkill(long playerID, int skillNum)
  245. {
  246. if (!gameMap.Timer.IsGaming)
  247. return false;
  248. Character? player = gameMap.FindPlayer(playerID);
  249. if (player != null)
  250. {
  251. if (player.Occupation.ListOfIActiveSkill.Count <= skillNum) return false;
  252. return skillManager.UseActiveSkill(player, player.Occupation.ListOfIActiveSkill[skillNum]);
  253. }
  254. else
  255. return false;
  256. }
  257. public void AllPlayerUsePassiveSkill()
  258. {
  259. if (!gameMap.Timer.IsGaming)
  260. return;
  261. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  262. try
  263. {
  264. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  265. {
  266. skillManager.UseAllPassiveSkill(player);
  267. }
  268. }
  269. finally
  270. {
  271. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  272. }
  273. }
  274. /*public void ClearLists(GameObjType[] objIdxes)
  275. {
  276. foreach (var idx in objIdxes)
  277. {
  278. if (idx != GameObjType.Null)
  279. {
  280. gameMap.GameObjLockDict[idx].EnterWriteLock();
  281. try
  282. {
  283. gameMap.GameObjDict[idx].Clear();
  284. }
  285. finally
  286. {
  287. gameMap.GameObjLockDict[idx].ExitWriteLock();
  288. }
  289. }
  290. }
  291. }*/
  292. public void ClearAllLists()
  293. {
  294. foreach (var keyValuePair in gameMap.GameObjDict)
  295. {
  296. if (!GameData.NeedCopy(keyValuePair.Key))
  297. {
  298. gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
  299. try
  300. {
  301. if (keyValuePair.Key == GameObjType.Character)
  302. {
  303. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  304. {
  305. player.CanMove = false;
  306. }
  307. }
  308. gameMap.GameObjDict[keyValuePair.Key].Clear();
  309. }
  310. finally
  311. {
  312. gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
  313. }
  314. }
  315. }
  316. }
  317. public int GetTeamScore(long teamID)
  318. {
  319. return teamList[(int)teamID].Score;
  320. }
  321. public List<IGameObj> GetGameObj()
  322. {
  323. var gameObjList = new List<IGameObj>();
  324. foreach (var keyValuePair in gameMap.GameObjDict)
  325. {
  326. if (GameData.NeedCopy(keyValuePair.Key))
  327. {
  328. gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
  329. try
  330. {
  331. gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
  332. }
  333. finally
  334. {
  335. gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
  336. }
  337. }
  338. }
  339. return gameObjList;
  340. }
  341. public Game(uint[,] mapResource, int numOfTeam)
  342. {
  343. // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
  344. gameMap = new Map(mapResource);
  345. // 加入队伍
  346. // this.numOfTeam = numOfTeam;
  347. teamList = new List<Team>();
  348. for (int i = 0; i < numOfTeam; ++i)
  349. {
  350. teamList.Add(new Team());
  351. }
  352. characterManager = new CharacterManager(gameMap);
  353. attackManager = new AttackManager(gameMap, characterManager);
  354. actionManager = new ActionManager(gameMap, characterManager);
  355. propManager = new PropManager(gameMap, characterManager);
  356. skillManager = new SkillManager(gameMap, actionManager, attackManager, propManager, characterManager);
  357. }
  358. }
  359. }