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

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