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. namespace Gaming
  10. {
  11. public partial class Game
  12. {
  13. private readonly AttackManager attackManager;
  14. private class AttackManager
  15. {
  16. readonly Map gameMap;
  17. readonly MoveEngine moveEngine;
  18. readonly CharacterManager characterManager;
  19. public AttackManager(Map gameMap, CharacterManager characterManager)
  20. {
  21. this.gameMap = gameMap;
  22. this.moveEngine = new MoveEngine(
  23. gameMap: gameMap,
  24. OnCollision: (obj, collisionObj, moveVec) =>
  25. {
  26. BulletBomb((Bullet)obj, (GameObj)collisionObj);
  27. return MoveEngine.AfterCollision.Destroyed;
  28. },
  29. EndMove: obj =>
  30. {
  31. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  32. if (obj.CanMove && ((Bullet)obj).TypeOfBullet != BulletType.JumpyDumpty)
  33. BulletBomb((Bullet)obj, null);
  34. obj.ReSetCanMove(false);
  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. if (gameMap.Remove(bullet))
  65. {
  66. bullet.ReSetCanMove(false);
  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. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI / 2.0);
  116. Attack((Character)bullet.Parent, bullet.FacingDirection.Angle() + Math.PI * 3.0 / 2.0);
  117. }
  118. var beAttackedList = new List<IGameObj>();
  119. foreach (var kvp in gameMap.GameObjDict)
  120. {
  121. if (bullet.CanBeBombed(kvp.Key))
  122. {
  123. gameMap.GameObjLockDict[kvp.Key].EnterReadLock();
  124. try
  125. {
  126. foreach (var item in gameMap.GameObjDict[kvp.Key])
  127. if (bullet.CanAttack((GameObj)item))
  128. {
  129. beAttackedList.Add(item);
  130. }
  131. }
  132. finally
  133. {
  134. gameMap.GameObjLockDict[kvp.Key].ExitReadLock();
  135. }
  136. }
  137. }
  138. foreach (GameObj beAttackedObj in beAttackedList)
  139. {
  140. BombObj(bullet, beAttackedObj);
  141. }
  142. beAttackedList.Clear();
  143. if (objBeingShot == null)
  144. {
  145. characterManager.BackSwing((Character)bullet.Parent!, bullet.Backswing);
  146. }
  147. else
  148. characterManager.BackSwing((Character)bullet.Parent!, bullet.RecoveryFromHit);
  149. }
  150. public bool Attack(Character player, double angle)
  151. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  152. Bullet? bullet = player.Attack(angle, gameMap.Timer.nowTime());
  153. if (bullet != null)
  154. {
  155. Debugger.Output(bullet, "Attack in " + bullet.Position.ToString());
  156. bullet.AP += player.TryAddAp() ? GameData.ApPropAdd : 0;
  157. gameMap.Add(bullet);
  158. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  159. if (bullet.CastTime > 0)
  160. {
  161. characterManager.SetPlayerState(player, PlayerStateType.TryingToAttack);
  162. long threadNum = player.ThreadNum;
  163. new Thread
  164. (() =>
  165. {
  166. new FrameRateTaskExecutor<int>(
  167. loopCondition: () => threadNum == player.ThreadNum && gameMap.Timer.IsGaming,
  168. loopToDo: () =>
  169. {
  170. },
  171. timeInterval: GameData.checkInterval,
  172. finallyReturn: () => 0,
  173. maxTotalDuration: bullet.CastTime
  174. )
  175. .Start();
  176. if (gameMap.Timer.IsGaming)
  177. {
  178. if (threadNum == player.ThreadNum)
  179. {
  180. characterManager.SetPlayerState(player);
  181. }
  182. else TryRemoveBullet(bullet);
  183. }
  184. }
  185. )
  186. { IsBackground = true }.Start();
  187. }
  188. }
  189. if (bullet != null)
  190. {
  191. #if DEBUG
  192. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  193. #endif
  194. return true;
  195. }
  196. else
  197. {
  198. #if DEBUG
  199. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  200. #endif
  201. return false;
  202. }
  203. }
  204. }
  205. }
  206. }