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

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