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

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