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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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).DegreeOfRepair -= bullet.AP * GameData.factorDamageGenerator;
  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. Debugger.Output(bullet, "JumpyDumpty!");
  97. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle());
  98. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI);
  99. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI / 2.0);
  100. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI * 3.0 / 2.0);
  101. }
  102. var beAttackedList = new List<IGameObj>();
  103. foreach (var kvp in gameMap.GameObjDict)
  104. {
  105. if (bullet.CanBeBombed(kvp.Key))
  106. {
  107. gameMap.GameObjLockDict[kvp.Key].EnterReadLock();
  108. try
  109. {
  110. foreach (var item in gameMap.GameObjDict[kvp.Key])
  111. if (bullet.CanAttack((GameObj)item))
  112. {
  113. beAttackedList.Add(item);
  114. }
  115. }
  116. finally
  117. {
  118. gameMap.GameObjLockDict[kvp.Key].ExitReadLock();
  119. }
  120. }
  121. }
  122. foreach (GameObj beAttackedObj in beAttackedList)
  123. {
  124. BombObj(bullet, beAttackedObj);
  125. }
  126. beAttackedList.Clear();
  127. if (objBeingShot == null)
  128. {
  129. CharacterManager.BackSwing((Character?)bullet.Parent, bullet.Backswing);
  130. }
  131. else
  132. CharacterManager.BackSwing((Character?)bullet.Parent, bullet.RecoveryFromHit);
  133. }
  134. public bool Attack(Character? player, double angle)
  135. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  136. if (player == null)
  137. {
  138. return false;
  139. }
  140. if (player.BulletOfPlayer == BulletType.Null || !player.Commandable())
  141. return false;
  142. XY res = player.Position + new XY // 子弹紧贴人物生成。
  143. (
  144. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  145. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  146. );
  147. Bullet? bullet = player.Attack(res, gameMap.GetPlaceType(res));
  148. if (bullet != null)
  149. {
  150. Debugger.Output(player, "Attack in" + bullet.ToString());
  151. bullet.AP += player.TryAddAp() ? GameData.ApPropAdd : 0;
  152. bullet.CanMove = true;
  153. gameMap.Add(bullet);
  154. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  155. if (bullet.CastTime > 0)
  156. {
  157. player.PlayerState = PlayerStateType.TryingToAttack;
  158. new Thread
  159. (() =>
  160. {
  161. new FrameRateTaskExecutor<int>(
  162. loopCondition: () => player.PlayerState == PlayerStateType.TryingToAttack && gameMap.Timer.IsGaming,
  163. loopToDo: () =>
  164. {
  165. },
  166. timeInterval: GameData.frameDuration,
  167. finallyReturn: () => 0,
  168. maxTotalDuration: bullet.CastTime
  169. )
  170. .Start();
  171. if (gameMap.Timer.IsGaming)
  172. {
  173. if (player.PlayerState == PlayerStateType.TryingToAttack)
  174. {
  175. player.PlayerState = PlayerStateType.Null;
  176. }
  177. else
  178. bullet.IsMoving = false;
  179. gameMap.Remove(bullet);
  180. }
  181. }
  182. )
  183. { IsBackground = true }.Start();
  184. }
  185. }
  186. if (bullet != null)
  187. {
  188. #if DEBUG
  189. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  190. #endif
  191. return true;
  192. }
  193. else
  194. {
  195. #if DEBUG
  196. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  197. #endif
  198. return false;
  199. }
  200. }
  201. }
  202. }
  203. }