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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. 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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(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.FindPlayerToAction(playerID);
  200. if (player != null)
  201. {
  202. return actionManager.LockOrOpenDoor(player);
  203. }
  204. return false;
  205. }
  206. public bool Attack(long playerID, double angle)
  207. {
  208. if (!gameMap.Timer.IsGaming)
  209. return false;
  210. Character? player = gameMap.FindPlayerToAction(playerID);
  211. if (player != null && player.Commandable())
  212. {
  213. return attackManager.Attack(player, angle);
  214. }
  215. return false;
  216. }
  217. public void UseProp(long playerID, PropType propType = PropType.Null)
  218. {
  219. if (!gameMap.Timer.IsGaming)
  220. return;
  221. Character? player = gameMap.FindPlayerToAction(playerID);
  222. if (player != null)
  223. {
  224. propManager.UseProp(player, propType);
  225. }
  226. }
  227. public void ThrowProp(long playerID, PropType propType = PropType.Null)
  228. {
  229. if (!gameMap.Timer.IsGaming)
  230. return;
  231. Character? player = gameMap.FindPlayerToAction(playerID);
  232. if (player != null)
  233. {
  234. propManager.ThrowProp(player, propType);
  235. }
  236. }
  237. public bool PickProp(long playerID, PropType propType = PropType.Null)
  238. {
  239. if (!gameMap.Timer.IsGaming)
  240. return false;
  241. Character? player = gameMap.FindPlayerToAction(playerID);
  242. if (player != null)
  243. {
  244. return propManager.PickProp(player, propType);
  245. }
  246. return false;
  247. }
  248. public bool UseActiveSkill(long playerID, int skillNum)
  249. {
  250. if (!gameMap.Timer.IsGaming)
  251. return false;
  252. Character? player = gameMap.FindPlayer(playerID);
  253. if (player != null)
  254. {
  255. if (player.Occupation.ListOfIActiveSkill.Count <= skillNum) return false;
  256. return skillManager.UseActiveSkill(player, player.Occupation.ListOfIActiveSkill[skillNum]);
  257. }
  258. else
  259. return false;
  260. }
  261. public void AllPlayerUsePassiveSkill()
  262. {
  263. if (!gameMap.Timer.IsGaming)
  264. return;
  265. gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  266. try
  267. {
  268. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  269. {
  270. skillManager.UseAllPassiveSkill(player);
  271. }
  272. }
  273. finally
  274. {
  275. gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  276. }
  277. }
  278. /*public void ClearLists(GameObjType[] objIdxes)
  279. {
  280. foreach (var idx in objIdxes)
  281. {
  282. if (idx != GameObjType.Null)
  283. {
  284. gameMap.GameObjLockDict[idx].EnterWriteLock();
  285. try
  286. {
  287. gameMap.GameObjDict[idx].Clear();
  288. }
  289. finally
  290. {
  291. gameMap.GameObjLockDict[idx].ExitWriteLock();
  292. }
  293. }
  294. }
  295. }*/
  296. public void ClearAllLists()
  297. {
  298. foreach (var keyValuePair in gameMap.GameObjDict)
  299. {
  300. if (!GameData.NeedCopy(keyValuePair.Key))
  301. {
  302. gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
  303. try
  304. {
  305. if (keyValuePair.Key == GameObjType.Character)
  306. {
  307. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  308. {
  309. player.CanMove = false;
  310. }
  311. }
  312. gameMap.GameObjDict[keyValuePair.Key].Clear();
  313. }
  314. finally
  315. {
  316. gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
  317. }
  318. }
  319. }
  320. }
  321. public int GetTeamScore(long teamID)
  322. {
  323. return teamList[(int)teamID].Score;
  324. }
  325. public List<IGameObj> GetGameObj()
  326. {
  327. var gameObjList = new List<IGameObj>();
  328. foreach (var keyValuePair in gameMap.GameObjDict)
  329. {
  330. if (GameData.NeedCopy(keyValuePair.Key))
  331. {
  332. gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
  333. try
  334. {
  335. gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
  336. }
  337. finally
  338. {
  339. gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
  340. }
  341. }
  342. }
  343. return gameObjList;
  344. }
  345. public Game(uint[,] mapResource, int numOfTeam)
  346. {
  347. // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
  348. gameMap = new Map(mapResource);
  349. // 加入队伍
  350. // this.numOfTeam = numOfTeam;
  351. teamList = new List<Team>();
  352. for (int i = 0; i < numOfTeam; ++i)
  353. {
  354. teamList.Add(new Team());
  355. }
  356. characterManager = new CharacterManager(gameMap);
  357. attackManager = new AttackManager(gameMap, characterManager);
  358. actionManager = new ActionManager(gameMap, characterManager);
  359. propManager = new PropManager(gameMap, characterManager);
  360. skillManager = new SkillManager(gameMap, actionManager, attackManager, propManager, characterManager);
  361. }
  362. }
  363. }