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.

AttackManager.cs 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using GameClass.GameObj;
  5. using Preparation.Utility;
  6. using GameEngine;
  7. using Preparation.Interface;
  8. using Timothy.FrameRateTask;
  9. using System.Numerics;
  10. namespace Gaming
  11. {
  12. public partial class Game
  13. {
  14. private readonly AttackManager attackManager;
  15. private class AttackManager
  16. {
  17. readonly Map gameMap;
  18. readonly MoveEngine moveEngine;
  19. public AttackManager(Map gameMap)
  20. {
  21. this.gameMap = gameMap;
  22. this.moveEngine = new MoveEngine(
  23. gameMap: gameMap,
  24. OnCollision: (obj, collisionObj, moveVec) =>
  25. {
  26. //BulletBomb((Bullet)obj, (GameObj)collisionObj);
  27. return MoveEngine.AfterCollision.Destroyed;
  28. },
  29. EndMove: obj =>
  30. {
  31. #if DEBUG
  32. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  33. #endif
  34. BulletBomb((Bullet)obj, null);
  35. }
  36. );
  37. }
  38. private void BeAddictedToGame(Student player)
  39. {
  40. new Thread
  41. (() =>
  42. {
  43. if (player.GamingAddiction > GameData.BeginGamingAddiction && player.GamingAddiction < GameData.MidGamingAddiction)
  44. player.GamingAddiction = GameData.MidGamingAddiction;
  45. player.PlayerState = PlayerStateType.Addicted;
  46. new FrameRateTaskExecutor<int>(
  47. () => player.PlayerState == PlayerStateType.Addicted && player.GamingAddiction < player.MaxGamingAddiction && gameMap.Timer.IsGaming,
  48. () =>
  49. {
  50. player.GamingAddiction += GameData.frameDuration;
  51. },
  52. timeInterval: GameData.frameDuration,
  53. () =>
  54. {
  55. if (player.GamingAddiction == player.MaxGamingAddiction && gameMap.Timer.IsGaming)
  56. {
  57. Die(player);
  58. }
  59. return 0;
  60. }
  61. )
  62. .Start();
  63. }
  64. )
  65. { IsBackground = true }.Start();
  66. }
  67. public void BeStunned(Character player, int time)
  68. {
  69. new Thread
  70. (() =>
  71. {
  72. player.PlayerState = PlayerStateType.Stunned;
  73. new FrameRateTaskExecutor<int>(
  74. () => player.PlayerState == PlayerStateType.Stunned && gameMap.Timer.IsGaming,
  75. () =>
  76. {
  77. },
  78. timeInterval: GameData.frameDuration,
  79. () =>
  80. {
  81. if (player.PlayerState == PlayerStateType.Stunned)
  82. player.PlayerState = PlayerStateType.Null;
  83. return 0;
  84. },
  85. maxTotalDuration: time
  86. )
  87. .Start();
  88. }
  89. )
  90. { IsBackground = true }.Start();
  91. }
  92. private void Die(Character player)
  93. {
  94. player.Die(PlayerStateType.Deceased);
  95. for (int i = 0; i < GameData.maxNumOfPropInPropInventory; i++)
  96. {
  97. Prop? prop = player.UseProp(i);
  98. if (prop != null)
  99. {
  100. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  101. gameMap.Add(prop);
  102. }
  103. }
  104. --gameMap.NumOfSurvivingStudent;
  105. if (gameMap.NumOfSurvivingStudent == 1)
  106. {
  107. gameMap.GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
  108. try
  109. {
  110. foreach (EmergencyExit emergencyExit in gameMap.GameObjDict[GameObjType.EmergencyExit])
  111. if (emergencyExit.CanOpen)
  112. {
  113. emergencyExit.IsOpen = true;
  114. break;
  115. }
  116. }
  117. finally
  118. {
  119. gameMap.GameObjLockDict[GameObjType.EmergencyExit].ExitReadLock();
  120. }
  121. }
  122. // player.Reset();
  123. // ((Character?)bullet.Parent)?.AddScore(GameData.addScoreWhenKillOneLevelPlayer); // 给击杀者加分
  124. }
  125. private void BombObj(Bullet bullet, GameObj objBeingShot)
  126. {
  127. switch (objBeingShot.Type)
  128. {
  129. case GameObjType.Character:
  130. if ((!((Character)objBeingShot).IsGhost()) && bullet.Parent.IsGhost())
  131. {
  132. Student oneBeAttacked = (Student)objBeingShot;
  133. if (oneBeAttacked.CanBeAwed())
  134. {
  135. oneBeAttacked.PlayerState = PlayerStateType.Stunned;
  136. new Thread
  137. (
  138. () =>
  139. {
  140. Thread.Sleep(GameData.basicStunnedTimeOfStudent);
  141. if (oneBeAttacked.PlayerState == PlayerStateType.Stunned)
  142. {
  143. oneBeAttacked.PlayerState = PlayerStateType.Null;
  144. }
  145. }
  146. )
  147. { IsBackground = true }.Start();
  148. }
  149. if (oneBeAttacked.BeAttacked(bullet))
  150. {
  151. BeAddictedToGame((Student)objBeingShot);
  152. }
  153. }
  154. // if (((Character)objBeingShot).IsGhost() && !bullet.Parent.IsGhost() && bullet.TypeOfBullet == BulletType.Ram)
  155. // BeStunned((Character)objBeingShot, bullet.AP);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
  162. {
  163. #if DEBUG
  164. Debugger.Output(bullet, "bombed!");
  165. #endif
  166. bullet.CanMove = false;
  167. if (gameMap.Remove(bullet) && bullet.IsToBomb)
  168. gameMap.Add(new BombedBullet(bullet));
  169. if (!bullet.IsToBomb)
  170. {
  171. if (objBeingShot == null)
  172. {
  173. if (bullet.Backswing > 0)
  174. {
  175. bullet.Parent.PlayerState = PlayerStateType.Swinging;
  176. new Thread
  177. (() =>
  178. {
  179. Thread.Sleep(bullet.Backswing);
  180. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  181. {
  182. bullet.Parent.PlayerState = PlayerStateType.Null;
  183. }
  184. }
  185. )
  186. { IsBackground = true }.Start();
  187. }
  188. return;
  189. }
  190. BombObj(bullet, objBeingShot);
  191. if (bullet.RecoveryFromHit > 0)
  192. {
  193. bullet.Parent.PlayerState = PlayerStateType.Swinging;
  194. new Thread
  195. (() =>
  196. {
  197. Thread.Sleep(bullet.RecoveryFromHit);
  198. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  199. {
  200. bullet.Parent.PlayerState = PlayerStateType.Null;
  201. }
  202. }
  203. )
  204. { IsBackground = true }.Start();
  205. }
  206. return;
  207. }
  208. /*if (objBeingShot != null)
  209. {
  210. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  211. {
  212. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  213. }
  214. }*/
  215. // 子弹爆炸会发生的事↓↓↓
  216. var beAttackedList = new List<IGameObj>();
  217. foreach (var kvp in gameMap.GameObjDict)
  218. {
  219. if (bullet.CanBeBombed(kvp.Key))
  220. {
  221. gameMap.GameObjLockDict[kvp.Key].EnterReadLock();
  222. try
  223. {
  224. foreach (var item in gameMap.GameObjDict[kvp.Key])
  225. if (bullet.CanAttack((GameObj)item))
  226. {
  227. beAttackedList.Add(item);
  228. }
  229. }
  230. finally
  231. {
  232. gameMap.GameObjLockDict[kvp.Key].ExitReadLock();
  233. }
  234. }
  235. }
  236. foreach (GameObj beAttackedObj in beAttackedList)
  237. {
  238. BombObj(bullet, beAttackedObj);
  239. }
  240. if (objBeingShot == null)
  241. {
  242. if (bullet.Backswing > 0)
  243. {
  244. bullet.Parent.PlayerState = PlayerStateType.Swinging;
  245. new Thread
  246. (() =>
  247. {
  248. Thread.Sleep(bullet.Backswing);
  249. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  250. {
  251. bullet.Parent.PlayerState = PlayerStateType.Null;
  252. }
  253. }
  254. )
  255. { IsBackground = true }.Start();
  256. }
  257. }
  258. else
  259. {
  260. if (bullet.RecoveryFromHit > 0)
  261. {
  262. bullet.Parent.PlayerState = PlayerStateType.Swinging;
  263. new Thread
  264. (() =>
  265. {
  266. Thread.Sleep(bullet.RecoveryFromHit);
  267. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  268. {
  269. bullet.Parent.PlayerState = PlayerStateType.Null;
  270. }
  271. }
  272. )
  273. { IsBackground = true }.Start();
  274. }
  275. }
  276. beAttackedList.Clear();
  277. }
  278. public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
  279. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  280. if (player == null)
  281. {
  282. #if DEBUG
  283. Console.WriteLine("the player who will attack is NULL!");
  284. #endif
  285. return false;
  286. }
  287. if (!player.Commandable())
  288. return false;
  289. XY res = new XY // 子弹紧贴人物生成。
  290. (
  291. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  292. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  293. );
  294. Bullet? bullet = player.Attack(res, gameMap.GetPlaceType(res));
  295. if (bullet != null)
  296. {
  297. bullet.CanMove = true;
  298. gameMap.Add(bullet);
  299. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  300. if (bullet.CastTime > 0)
  301. {
  302. player.PlayerState = PlayerStateType.TryingToAttack;
  303. new Thread
  304. (() =>
  305. {
  306. new FrameRateTaskExecutor<int>(
  307. loopCondition: () => player.PlayerState == PlayerStateType.TryingToAttack && gameMap.Timer.IsGaming,
  308. loopToDo: () =>
  309. {
  310. },
  311. timeInterval: GameData.frameDuration,
  312. finallyReturn: () => 0,
  313. maxTotalDuration: bullet.CastTime
  314. )
  315. .Start();
  316. if (gameMap.Timer.IsGaming)
  317. {
  318. if (player.PlayerState == PlayerStateType.TryingToAttack)
  319. {
  320. player.PlayerState = PlayerStateType.Null;
  321. }
  322. else
  323. bullet.IsMoving = false;
  324. gameMap.Remove(bullet);
  325. }
  326. }
  327. )
  328. { IsBackground = true }.Start();
  329. }
  330. }
  331. if (bullet != null)
  332. {
  333. #if DEBUG
  334. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  335. #endif
  336. return true;
  337. }
  338. else
  339. {
  340. #if DEBUG
  341. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  342. #endif
  343. return false;
  344. }
  345. }
  346. }
  347. }
  348. }