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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using GameClass.GameObj;
  5. using Preparation.Utility;
  6. using GameEngine;
  7. namespace Gaming
  8. {
  9. public partial class Game
  10. {
  11. private readonly AttackManager attackManager;
  12. private class AttackManager
  13. {
  14. readonly Map gameMap;
  15. readonly MoveEngine moveEngine;
  16. public AttackManager(Map gameMap)
  17. {
  18. this.gameMap = gameMap;
  19. this.moveEngine = new MoveEngine(
  20. gameMap: gameMap,
  21. OnCollision: (obj, collisionObj, moveVec) =>
  22. {
  23. //BulletBomb((Bullet)obj, (GameObj)collisionObj);
  24. return MoveEngine.AfterCollision.Destroyed;
  25. },
  26. EndMove: obj =>
  27. {
  28. #if DEBUG
  29. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  30. #endif
  31. BulletBomb((Bullet)obj, null);
  32. }
  33. );
  34. }
  35. private void BombOnePlayer(Bullet bullet, Character playerBeingShot)
  36. {
  37. if (playerBeingShot.BeAttack(bullet))
  38. {
  39. playerBeingShot.CanMove = false;
  40. playerBeingShot.IsResetting = true;
  41. // gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  42. // try
  43. //{
  44. // gameMap.GameObjDict[GameObjType.Character].Remove(playerBeingShot);
  45. // }
  46. // finally
  47. //{
  48. // gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  49. // }
  50. Prop? dropProp = null;
  51. if (playerBeingShot.PropInventory != null) // 若角色原来有道具,则原始道具掉落在原地
  52. {
  53. dropProp = playerBeingShot.PropInventory;
  54. dropProp.SetNewPos(GameData.GetCellCenterPos(playerBeingShot.Position.x / GameData.numOfPosGridPerCell, playerBeingShot.Position.y / GameData.numOfPosGridPerCell));
  55. }
  56. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  57. try
  58. {
  59. if (dropProp != null)
  60. gameMap.GameObjDict[GameObjType.Prop].Add(dropProp);
  61. }
  62. finally
  63. {
  64. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  65. }
  66. playerBeingShot.Reset();
  67. ((Character?)bullet.Parent)?.AddScore(GameData.addScoreWhenKillOneLevelPlayer); // 给击杀者加分
  68. new Thread
  69. (() =>
  70. {
  71. Thread.Sleep(GameData.reviveTime);
  72. playerBeingShot.AddShield(GameData.shieldTimeAtBirth); // 复活加个盾
  73. // gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  74. // try
  75. //{
  76. // gameMap.GameObjDict[GameObjType.Character].Add(playerBeingShot);
  77. // }
  78. // finally { gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock(); }
  79. if (gameMap.Timer.IsGaming)
  80. {
  81. playerBeingShot.CanMove = true;
  82. }
  83. playerBeingShot.IsResetting = false;
  84. }
  85. )
  86. { IsBackground = true }.Start();
  87. }
  88. }
  89. private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
  90. {
  91. #if DEBUG
  92. Debugger.Output(bullet, "bombed!");
  93. #endif
  94. bullet.CanMove = false;
  95. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  96. try
  97. {
  98. foreach (ObjOfCharacter _bullet in gameMap.GameObjDict[GameObjType.Bullet])
  99. {
  100. if (_bullet.ID == bullet.ID)
  101. {
  102. gameMap.GameObjLockDict[GameObjType.BombedBullet].EnterWriteLock();
  103. try
  104. {
  105. gameMap.GameObjDict[GameObjType.BombedBullet].Add(new BombedBullet(bullet));
  106. }
  107. finally
  108. {
  109. gameMap.GameObjLockDict[GameObjType.BombedBullet].ExitWriteLock();
  110. }
  111. gameMap.GameObjDict[GameObjType.Bullet].Remove(_bullet);
  112. break;
  113. }
  114. }
  115. }
  116. finally
  117. {
  118. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  119. }
  120. /*if (objBeingShot != null)
  121. {
  122. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  123. {
  124. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  125. }
  126. }*/
  127. // 子弹爆炸会发生的事↓↓↓
  128. var beAttackedList = new List<Character>();
  129. gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
  130. try
  131. {
  132. foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
  133. {
  134. if (bullet.CanAttack(player))
  135. {
  136. beAttackedList.Add(player);
  137. if (player.ID != bullet.Parent.ID)
  138. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * bullet.AP));
  139. }
  140. }
  141. }
  142. finally
  143. {
  144. gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
  145. }
  146. foreach (Character beAttackedPlayer in beAttackedList)
  147. {
  148. BombOnePlayer(bullet, beAttackedPlayer);
  149. }
  150. beAttackedList.Clear();
  151. }
  152. public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
  153. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  154. if (player == null)
  155. {
  156. #if DEBUG
  157. Console.WriteLine("the player who will attack is NULL!");
  158. #endif
  159. return false;
  160. }
  161. if (player.IsResetting)
  162. return false;
  163. Bullet? bullet = player.RemoteAttack(
  164. new XY // 子弹紧贴人物生成。
  165. (
  166. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  167. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  168. )
  169. );
  170. if (bullet != null)
  171. {
  172. bullet.CanMove = true;
  173. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  174. try
  175. {
  176. gameMap.GameObjDict[GameObjType.Bullet].Add(bullet);
  177. }
  178. finally
  179. {
  180. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  181. }
  182. moveEngine.MoveObj(bullet, (int)((player.AttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  183. #if DEBUG
  184. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  185. #endif
  186. return true;
  187. }
  188. else
  189. {
  190. #if DEBUG
  191. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  192. #endif
  193. return false;
  194. }
  195. }
  196. }
  197. }
  198. }