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

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