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

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