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

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