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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. 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. private void BombOnePlayer(Bullet bullet, Character playerBeingShot)
  35. {
  36. if (playerBeingShot.BeAttack(bullet))
  37. {
  38. playerBeingShot.CanMove = false;
  39. playerBeingShot.IsResetting = true;
  40. // gameMap.GameObjLockDict[GameObjIdx.Player].EnterWriteLock();
  41. // try
  42. //{
  43. // gameMap.GameObjDict[GameObjIdx.Player].Remove(playerBeingShot);
  44. // }
  45. // finally
  46. //{
  47. // gameMap.GameObjLockDict[GameObjIdx.Player].ExitWriteLock();
  48. // }
  49. Prop? dropProp = null;
  50. if (playerBeingShot.PropInventory != null) // 若角色原来有道具,则原始道具掉落在原地
  51. {
  52. dropProp = playerBeingShot.PropInventory;
  53. dropProp.SetNewPos(GameData.GetCellCenterPos(playerBeingShot.Position.x / GameData.numOfPosGridPerCell, playerBeingShot.Position.y / GameData.numOfPosGridPerCell));
  54. }
  55. gameMap.GameObjLockDict[GameObjIdx.Prop].EnterWriteLock();
  56. try
  57. {
  58. if (dropProp != null)
  59. gameMap.GameObjDict[GameObjIdx.Prop].Add(dropProp);
  60. }
  61. finally
  62. {
  63. gameMap.GameObjLockDict[GameObjIdx.Prop].ExitWriteLock();
  64. }
  65. playerBeingShot.Reset();
  66. ((Character?)bullet.Parent)?.AddScore(GameData.addScoreWhenKillOneLevelPlayer); // 给击杀者加分
  67. new Thread
  68. (() =>
  69. {
  70. Thread.Sleep(GameData.reviveTime);
  71. playerBeingShot.AddShield(GameData.shieldTimeAtBirth); // 复活加个盾
  72. // gameMap.GameObjLockDict[GameObjIdx.Player].EnterWriteLock();
  73. // try
  74. //{
  75. // gameMap.GameObjDict[GameObjIdx.Player].Add(playerBeingShot);
  76. // }
  77. // finally { gameMap.GameObjLockDict[GameObjIdx.Player].ExitWriteLock(); }
  78. if (gameMap.Timer.IsGaming)
  79. {
  80. playerBeingShot.CanMove = true;
  81. }
  82. playerBeingShot.IsResetting = false;
  83. }
  84. )
  85. { IsBackground = true }.Start();
  86. }
  87. }
  88. private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
  89. {
  90. #if DEBUG
  91. Debugger.Output(bullet, "bombed!");
  92. #endif
  93. bullet.CanMove = false;
  94. gameMap.GameObjLockDict[GameObjIdx.Bullet].EnterWriteLock();
  95. try
  96. {
  97. foreach (ObjOfCharacter _bullet in gameMap.GameObjDict[GameObjIdx.Bullet])
  98. {
  99. if (_bullet.ID == bullet.ID)
  100. {
  101. gameMap.GameObjLockDict[GameObjIdx.BombedBullet].EnterWriteLock();
  102. try
  103. {
  104. gameMap.GameObjDict[GameObjIdx.BombedBullet].Add(new BombedBullet(bullet));
  105. }
  106. finally
  107. {
  108. gameMap.GameObjLockDict[GameObjIdx.BombedBullet].ExitWriteLock();
  109. }
  110. gameMap.GameObjDict[GameObjIdx.Bullet].Remove(_bullet);
  111. break;
  112. }
  113. }
  114. }
  115. finally
  116. {
  117. gameMap.GameObjLockDict[GameObjIdx.Bullet].ExitWriteLock();
  118. }
  119. /*if (objBeingShot != null)
  120. {
  121. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  122. {
  123. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  124. }
  125. }*/
  126. // 子弹爆炸会发生的事↓↓↓
  127. var beAttackedList = new List<Character>();
  128. gameMap.GameObjLockDict[GameObjIdx.Player].EnterReadLock();
  129. try
  130. {
  131. foreach (Character player in gameMap.GameObjDict[GameObjIdx.Player])
  132. {
  133. if (bullet.CanAttack(player))
  134. {
  135. beAttackedList.Add(player);
  136. if (player.ID != bullet.Parent.ID)
  137. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * bullet.AP));
  138. }
  139. }
  140. }
  141. finally
  142. {
  143. gameMap.GameObjLockDict[GameObjIdx.Player].ExitReadLock();
  144. }
  145. foreach (Character beAttackedPlayer in beAttackedList)
  146. {
  147. BombOnePlayer(bullet, beAttackedPlayer);
  148. }
  149. beAttackedList.Clear();
  150. }
  151. public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
  152. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  153. if (player == null)
  154. {
  155. #if DEBUG
  156. Console.WriteLine("the player who will attack is NULL!");
  157. #endif
  158. return false;
  159. }
  160. if (player.IsResetting)
  161. return false;
  162. Bullet? bullet = player.RemoteAttack(
  163. new XY // 子弹紧贴人物生成。
  164. (
  165. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  166. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  167. )
  168. );
  169. if (bullet != null)
  170. {
  171. bullet.CanMove = true;
  172. gameMap.GameObjLockDict[GameObjIdx.Bullet].EnterWriteLock();
  173. try
  174. {
  175. gameMap.GameObjDict[GameObjIdx.Bullet].Add(bullet);
  176. }
  177. finally
  178. {
  179. gameMap.GameObjLockDict[GameObjIdx.Bullet].ExitWriteLock();
  180. }
  181. moveEngine.MoveObj(bullet, (int)((player.AttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  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. }