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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. 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 bool CanBeBombed(Bullet bullet, GameObjType gameObjType)
  37. {
  38. if (gameObjType == GameObjType.Character) return true;
  39. return false;
  40. }
  41. private void BombObj(Bullet bullet, GameObj objBeingShot)
  42. {
  43. switch (objBeingShot.Type)
  44. {
  45. case GameObjType.Character:
  46. Character playerBeingShot = (Character)objBeingShot;
  47. if (playerBeingShot.BeAttacked(bullet))
  48. {
  49. playerBeingShot.CanMove = false;
  50. playerBeingShot.IsResetting = true;
  51. // gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  52. // try
  53. //{
  54. // gameMap.GameObjDict[GameObjType.Character].Remove(playerBeingShot);
  55. // }
  56. // finally
  57. //{
  58. // gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
  59. // }
  60. Prop? dropProp = null;
  61. if (playerBeingShot.PropInventory != null) // 若角色原来有道具,则原始道具掉落在原地
  62. {
  63. dropProp = playerBeingShot.PropInventory;
  64. dropProp.SetNewPos(GameData.GetCellCenterPos(playerBeingShot.Position.x / GameData.numOfPosGridPerCell, playerBeingShot.Position.y / GameData.numOfPosGridPerCell));
  65. }
  66. gameMap.GameObjLockDict[GameObjType.Prop].EnterWriteLock();
  67. try
  68. {
  69. if (dropProp != null)
  70. gameMap.GameObjDict[GameObjType.Prop].Add(dropProp);
  71. }
  72. finally
  73. {
  74. gameMap.GameObjLockDict[GameObjType.Prop].ExitWriteLock();
  75. }
  76. playerBeingShot.Reset();
  77. ((Character?)bullet.Parent)?.AddScore(GameData.addScoreWhenKillOneLevelPlayer); // 给击杀者加分
  78. /* new Thread
  79. (() =>
  80. {
  81. Thread.Sleep(GameData.reviveTime);
  82. playerBeingShot.AddShield(GameData.shieldTimeAtBirth); // 复活加个盾
  83. // gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
  84. // try
  85. //{
  86. // gameMap.GameObjDict[GameObjType.Character].Add(playerBeingShot);
  87. // }
  88. // finally { gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock(); }
  89. if (gameMap.Timer.IsGaming)
  90. {
  91. playerBeingShot.CanMove = true;
  92. }
  93. playerBeingShot.IsResetting = false;
  94. }
  95. )
  96. { IsBackground = true }.Start();
  97. */
  98. }
  99. break;
  100. }
  101. }
  102. private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
  103. {
  104. #if DEBUG
  105. Debugger.Output(bullet, "bombed!");
  106. #endif
  107. bullet.CanMove = false;
  108. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  109. try
  110. {
  111. foreach (ObjOfCharacter _bullet in gameMap.GameObjDict[GameObjType.Bullet])
  112. {
  113. if (_bullet.ID == bullet.ID)
  114. {
  115. gameMap.GameObjLockDict[GameObjType.BombedBullet].EnterWriteLock();
  116. try
  117. {
  118. gameMap.GameObjDict[GameObjType.BombedBullet].Add(new BombedBullet(bullet));
  119. }
  120. finally
  121. {
  122. gameMap.GameObjLockDict[GameObjType.BombedBullet].ExitWriteLock();
  123. }
  124. gameMap.GameObjDict[GameObjType.Bullet].Remove(_bullet);
  125. break;
  126. }
  127. }
  128. }
  129. finally
  130. {
  131. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  132. }
  133. if (!bullet.IsToBomb)
  134. {
  135. if (objBeingShot == null)
  136. {
  137. if (bullet.Backswing > 0)
  138. {
  139. bullet.Parent.CanMove = false;
  140. bullet.Parent.IsMoving = false;
  141. new Thread
  142. (() =>
  143. {
  144. Thread.Sleep(bullet.Backswing);
  145. if (gameMap.Timer.IsGaming)
  146. {
  147. bullet.Parent.CanMove = true;
  148. }
  149. }
  150. )
  151. { IsBackground = true }.Start();
  152. }
  153. return;
  154. }
  155. BombObj(bullet, objBeingShot);
  156. if (bullet.RecoveryFromHit > 0)
  157. {
  158. bullet.Parent.CanMove = false;
  159. bullet.Parent.IsMoving = false;
  160. new Thread
  161. (() =>
  162. {
  163. Thread.Sleep(bullet.RecoveryFromHit);
  164. if (gameMap.Timer.IsGaming)
  165. {
  166. bullet.Parent.CanMove = true;
  167. }
  168. }
  169. )
  170. { IsBackground = true }.Start();
  171. }
  172. return;
  173. }
  174. /*if (objBeingShot != null)
  175. {
  176. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  177. {
  178. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  179. }
  180. }*/
  181. // 子弹爆炸会发生的事↓↓↓
  182. var beAttackedList = new List<IGameObj>();
  183. foreach (var kvp in gameMap.GameObjDict)
  184. {
  185. if (CanBeBombed(bullet, kvp.Key))
  186. {
  187. gameMap.GameObjLockDict[kvp.Key].EnterWriteLock();
  188. try
  189. {
  190. foreach (var item in gameMap.GameObjDict[kvp.Key])
  191. if (bullet.CanAttack((GameObj)item))
  192. {
  193. beAttackedList.Add(item);
  194. }
  195. }
  196. finally
  197. {
  198. gameMap.GameObjLockDict[kvp.Key].ExitWriteLock();
  199. }
  200. }
  201. }
  202. foreach (GameObj beAttackedObj in beAttackedList)
  203. {
  204. BombObj(bullet, beAttackedObj);
  205. }
  206. if (objBeingShot == null)
  207. {
  208. if (bullet.Backswing > 0)
  209. {
  210. bullet.Parent.CanMove = false;
  211. bullet.Parent.IsMoving = false;
  212. new Thread
  213. (() =>
  214. {
  215. Thread.Sleep(bullet.Backswing);
  216. if (gameMap.Timer.IsGaming)
  217. {
  218. bullet.Parent.CanMove = true;
  219. }
  220. }
  221. )
  222. { IsBackground = true }.Start();
  223. }
  224. }
  225. else
  226. {
  227. if (bullet.RecoveryFromHit > 0)
  228. {
  229. bullet.Parent.CanMove = false;
  230. bullet.Parent.IsMoving = false;
  231. new Thread
  232. (() =>
  233. {
  234. Thread.Sleep(bullet.RecoveryFromHit);
  235. if (gameMap.Timer.IsGaming)
  236. {
  237. bullet.Parent.CanMove = true;
  238. }
  239. }
  240. )
  241. { IsBackground = true }.Start();
  242. }
  243. }
  244. beAttackedList.Clear();
  245. }
  246. public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
  247. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  248. if (player == null)
  249. {
  250. #if DEBUG
  251. Console.WriteLine("the player who will attack is NULL!");
  252. #endif
  253. return false;
  254. }
  255. if (player.IsResetting)
  256. return false;
  257. Bullet? bullet = player.RemoteAttack(
  258. new XY // 子弹紧贴人物生成。
  259. (
  260. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  261. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  262. )
  263. );
  264. if (bullet != null)
  265. {
  266. bullet.CanMove = true;
  267. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  268. try
  269. {
  270. gameMap.GameObjDict[GameObjType.Bullet].Add(bullet);
  271. }
  272. finally
  273. {
  274. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  275. }
  276. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  277. #if DEBUG
  278. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  279. #endif
  280. return true;
  281. }
  282. else
  283. {
  284. #if DEBUG
  285. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  286. #endif
  287. return false;
  288. }
  289. }
  290. }
  291. }
  292. }