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

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