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