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

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