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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. readonly CharacterManager characterManager;
  20. public AttackManager(Map gameMap, CharacterManager characterManager)
  21. {
  22. this.gameMap = gameMap;
  23. this.moveEngine = new MoveEngine(
  24. gameMap: gameMap,
  25. OnCollision: (obj, collisionObj, moveVec) =>
  26. {
  27. BulletBomb((Bullet)obj, (GameObj)collisionObj);
  28. return MoveEngine.AfterCollision.Destroyed;
  29. },
  30. EndMove: obj =>
  31. {
  32. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  33. if (obj.CanMove && ((Bullet)obj).TypeOfBullet != BulletType.JumpyDumpty)
  34. BulletBomb((Bullet)obj, null);
  35. }
  36. );
  37. this.characterManager = characterManager;
  38. }
  39. private void BombObj(Bullet bullet, GameObj objBeingShot)
  40. {
  41. #if DEBUG
  42. Debugger.Output(bullet, "bombed " + objBeingShot.ToString());
  43. #endif
  44. switch (objBeingShot.Type)
  45. {
  46. case GameObjType.Character:
  47. if ((!(((Character)objBeingShot).IsGhost())) && bullet.Parent.IsGhost())
  48. {
  49. characterManager.BeAttacked((Student)objBeingShot, bullet);
  50. }
  51. // if (((Character)objBeingShot).IsGhost() && !bullet.Parent.IsGhost() && bullet.TypeOfBullet == BulletType.Ram)
  52. // BeStunned((Character)objBeingShot, bullet.AP);
  53. break;
  54. case GameObjType.Generator:
  55. if (bullet.CanBeBombed(GameObjType.Generator))
  56. ((Generator)objBeingShot).Repair(-bullet.AP * GameData.factorDamageGenerator, (Character)bullet.Parent);
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. public bool TryRemoveBullet(Bullet bullet)
  63. {
  64. bullet.CanMove = false;
  65. if (gameMap.Remove(bullet))
  66. {
  67. if (bullet.BulletBombRange > 0)
  68. {
  69. BombedBullet bombedBullet = new(bullet);
  70. gameMap.Add(bombedBullet);
  71. new Thread
  72. (() =>
  73. {
  74. Thread.Sleep(GameData.frameDuration * 5);
  75. gameMap.RemoveJustFromMap(bombedBullet);
  76. }
  77. )
  78. { IsBackground = true }.Start();
  79. }
  80. return true;
  81. }
  82. else return false;
  83. }
  84. private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
  85. {
  86. #if DEBUG
  87. if (objBeingShot != null)
  88. Debugger.Output(bullet, "bombed with" + objBeingShot.ToString());
  89. else
  90. Debugger.Output(bullet, "bombed without objBeingShot");
  91. #endif
  92. if (!TryRemoveBullet(bullet)) return;
  93. if (bullet.BulletBombRange == 0)
  94. {
  95. if (objBeingShot == null)
  96. {
  97. characterManager.BackSwing((Character)bullet.Parent, bullet.Backswing);
  98. return;
  99. }
  100. BombObj(bullet, objBeingShot);
  101. characterManager.BackSwing((Character)bullet.Parent, bullet.RecoveryFromHit);
  102. return;
  103. }
  104. /*if (objBeingShot != null)
  105. {
  106. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  107. {
  108. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  109. }
  110. }*/
  111. // 子弹爆炸会发生的事↓↓↓
  112. if (bullet.TypeOfBullet == BulletType.BombBomb && objBeingShot != null)
  113. {
  114. bullet.Parent.BulletOfPlayer = BulletType.JumpyDumpty;
  115. Debugger.Output(bullet.Parent, bullet.Parent.CharacterType.ToString() + " " + bullet.Parent.BulletNum.ToString());
  116. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI / 2.0);
  117. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI * 3.0 / 2.0);
  118. }
  119. var beAttackedList = new List<IGameObj>();
  120. foreach (var kvp in gameMap.GameObjDict)
  121. {
  122. if (bullet.CanBeBombed(kvp.Key))
  123. {
  124. gameMap.GameObjLockDict[kvp.Key].EnterReadLock();
  125. try
  126. {
  127. foreach (var item in gameMap.GameObjDict[kvp.Key])
  128. if (bullet.CanAttack((GameObj)item))
  129. {
  130. beAttackedList.Add(item);
  131. }
  132. }
  133. finally
  134. {
  135. gameMap.GameObjLockDict[kvp.Key].ExitReadLock();
  136. }
  137. }
  138. }
  139. foreach (GameObj beAttackedObj in beAttackedList)
  140. {
  141. BombObj(bullet, beAttackedObj);
  142. }
  143. beAttackedList.Clear();
  144. if (objBeingShot == null)
  145. {
  146. characterManager.BackSwing((Character)bullet.Parent, bullet.Backswing);
  147. }
  148. else
  149. characterManager.BackSwing((Character)bullet.Parent, bullet.RecoveryFromHit);
  150. }
  151. public bool Attack(Character player, double angle)
  152. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  153. if (player.BulletOfPlayer == BulletType.Null)
  154. return false;
  155. Debugger.Output(player, player.CharacterType.ToString() + "Attack in " + player.BulletOfPlayer.ToString());
  156. XY res = player.Position + new XY // 子弹紧贴人物生成。
  157. (
  158. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  159. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  160. );
  161. Bullet? bullet = player.Attack(res, gameMap.GetPlaceType(res));
  162. if (bullet != null)
  163. {
  164. Debugger.Output(player, "Attack in " + bullet.ToString());
  165. bullet.AP += player.TryAddAp() ? GameData.ApPropAdd : 0;
  166. bullet.CanMove = true;
  167. gameMap.Add(bullet);
  168. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  169. if (bullet.CastTime > 0)
  170. {
  171. characterManager.SetPlayerState(player, PlayerStateType.TryingToAttack);
  172. new Thread
  173. (() =>
  174. {
  175. new FrameRateTaskExecutor<int>(
  176. loopCondition: () => player.PlayerState == PlayerStateType.TryingToAttack && gameMap.Timer.IsGaming,
  177. loopToDo: () =>
  178. {
  179. },
  180. timeInterval: GameData.checkInterval,
  181. finallyReturn: () => 0,
  182. maxTotalDuration: bullet.CastTime
  183. )
  184. .Start();
  185. if (gameMap.Timer.IsGaming)
  186. {
  187. if (player.PlayerState == PlayerStateType.TryingToAttack)
  188. {
  189. characterManager.SetPlayerState(player);
  190. }
  191. else TryRemoveBullet(bullet);
  192. }
  193. }
  194. )
  195. { IsBackground = true }.Start();
  196. }
  197. }
  198. if (bullet != null)
  199. {
  200. #if DEBUG
  201. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  202. #endif
  203. return true;
  204. }
  205. else
  206. {
  207. #if DEBUG
  208. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  209. #endif
  210. return false;
  211. }
  212. }
  213. }
  214. }
  215. }