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

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