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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. using System.Numerics;
  10. namespace Gaming
  11. {
  12. public partial class Game
  13. {
  14. private readonly AttackManager attackManager;
  15. private class AttackManager
  16. {
  17. readonly Map gameMap;
  18. readonly MoveEngine moveEngine;
  19. public AttackManager(Map gameMap)
  20. {
  21. this.gameMap = gameMap;
  22. this.moveEngine = new MoveEngine(
  23. gameMap: gameMap,
  24. OnCollision: (obj, collisionObj, moveVec) =>
  25. {
  26. //BulletBomb((Bullet)obj, (GameObj)collisionObj);
  27. return MoveEngine.AfterCollision.Destroyed;
  28. },
  29. EndMove: obj =>
  30. {
  31. #if DEBUG
  32. Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
  33. #endif
  34. BulletBomb((Bullet)obj, null);
  35. }
  36. );
  37. }
  38. private void BeAddictedToGame(Student player)
  39. {
  40. new Thread
  41. (() =>
  42. {
  43. if (player.GamingAddiction > GameData.BeginGamingAddiction && player.GamingAddiction < GameData.MidGamingAddiction)
  44. player.GamingAddiction = GameData.MidGamingAddiction;
  45. player.PlayerState = PlayerStateType.IsAddicted;
  46. new FrameRateTaskExecutor<int>(
  47. () => player.PlayerState == PlayerStateType.IsAddicted && player.GamingAddiction < player.MaxGamingAddiction && gameMap.Timer.IsGaming,
  48. () =>
  49. {
  50. player.GamingAddiction += GameData.frameDuration;
  51. },
  52. timeInterval: GameData.frameDuration,
  53. () =>
  54. {
  55. if (player.GamingAddiction == player.MaxGamingAddiction && gameMap.Timer.IsGaming)
  56. {
  57. Die(player);
  58. }
  59. return 0;
  60. }
  61. )
  62. .Start();
  63. }
  64. )
  65. { IsBackground = true }.Start();
  66. }
  67. private void Die(Character player)
  68. {
  69. player.Die(PlayerStateType.IsDeceased);
  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.IsDeceased = 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. if (gameMap.Remove(bullet) && bullet.IsToBomb)
  143. gameMap.Add(new BombedBullet(bullet));
  144. if (!bullet.IsToBomb)
  145. {
  146. if (objBeingShot == null)
  147. {
  148. if (bullet.Backswing > 0)
  149. {
  150. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  151. new Thread
  152. (() =>
  153. {
  154. Thread.Sleep(bullet.Backswing);
  155. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  156. {
  157. bullet.Parent.PlayerState = PlayerStateType.Null;
  158. }
  159. }
  160. )
  161. { IsBackground = true }.Start();
  162. }
  163. return;
  164. }
  165. BombObj(bullet, objBeingShot);
  166. if (bullet.RecoveryFromHit > 0)
  167. {
  168. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  169. new Thread
  170. (() =>
  171. {
  172. Thread.Sleep(bullet.RecoveryFromHit);
  173. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  174. {
  175. bullet.Parent.PlayerState = PlayerStateType.Null;
  176. }
  177. }
  178. )
  179. { IsBackground = true }.Start();
  180. }
  181. return;
  182. }
  183. /*if (objBeingShot != null)
  184. {
  185. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  186. {
  187. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  188. }
  189. }*/
  190. // 子弹爆炸会发生的事↓↓↓
  191. var beAttackedList = new List<IGameObj>();
  192. foreach (var kvp in gameMap.GameObjDict)
  193. {
  194. if (CanBeBombed(bullet, kvp.Key))
  195. {
  196. gameMap.GameObjLockDict[kvp.Key].EnterWriteLock();
  197. try
  198. {
  199. foreach (var item in gameMap.GameObjDict[kvp.Key])
  200. if (bullet.CanAttack((GameObj)item))
  201. {
  202. beAttackedList.Add(item);
  203. }
  204. }
  205. finally
  206. {
  207. gameMap.GameObjLockDict[kvp.Key].ExitWriteLock();
  208. }
  209. }
  210. }
  211. foreach (GameObj beAttackedObj in beAttackedList)
  212. {
  213. BombObj(bullet, beAttackedObj);
  214. }
  215. if (objBeingShot == null)
  216. {
  217. if (bullet.Backswing > 0)
  218. {
  219. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  220. new Thread
  221. (() =>
  222. {
  223. Thread.Sleep(bullet.Backswing);
  224. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  225. {
  226. bullet.Parent.PlayerState = PlayerStateType.Null;
  227. }
  228. }
  229. )
  230. { IsBackground = true }.Start();
  231. }
  232. }
  233. else
  234. {
  235. if (bullet.RecoveryFromHit > 0)
  236. {
  237. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  238. new Thread
  239. (() =>
  240. {
  241. Thread.Sleep(bullet.RecoveryFromHit);
  242. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  243. {
  244. bullet.Parent.PlayerState = PlayerStateType.Null;
  245. }
  246. }
  247. )
  248. { IsBackground = true }.Start();
  249. }
  250. }
  251. beAttackedList.Clear();
  252. }
  253. public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
  254. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  255. if (player == null)
  256. {
  257. #if DEBUG
  258. Console.WriteLine("the player who will attack is NULL!");
  259. #endif
  260. return false;
  261. }
  262. if (player.PlayerState != PlayerStateType.Null || player.PlayerState != PlayerStateType.IsMoving)
  263. return false;
  264. Bullet? bullet = player.RemoteAttack(
  265. new XY // 子弹紧贴人物生成。
  266. (
  267. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  268. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  269. )
  270. );
  271. if (bullet.CastTime > 0)
  272. {
  273. player.PlayerState = PlayerStateType.IsTryingToAttack;
  274. new Thread
  275. (() =>
  276. {
  277. new FrameRateTaskExecutor<int>(
  278. loopCondition: () => player.PlayerState == PlayerStateType.IsTryingToAttack && gameMap.Timer.IsGaming,
  279. loopToDo: () =>
  280. {
  281. },
  282. timeInterval: GameData.frameDuration,
  283. finallyReturn: () => 0,
  284. maxTotalDuration: bullet.CastTime
  285. )
  286. .Start();
  287. if (gameMap.Timer.IsGaming)
  288. {
  289. if (player.PlayerState == PlayerStateType.IsTryingToAttack)
  290. {
  291. player.PlayerState = PlayerStateType.Null;
  292. }
  293. else
  294. bullet.IsMoving = false;
  295. gameMap.Remove(bullet);
  296. }
  297. }
  298. )
  299. { IsBackground = true }.Start();
  300. }
  301. if (bullet != null)
  302. {
  303. bullet.CanMove = true;
  304. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  305. try
  306. {
  307. gameMap.GameObjDict[GameObjType.Bullet].Add(bullet);
  308. }
  309. finally
  310. {
  311. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  312. }
  313. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  314. #if DEBUG
  315. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  316. #endif
  317. return true;
  318. }
  319. else
  320. {
  321. #if DEBUG
  322. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  323. #endif
  324. return false;
  325. }
  326. }
  327. }
  328. }
  329. }