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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  144. try
  145. {
  146. foreach (ObjOfCharacter _bullet in gameMap.GameObjDict[GameObjType.Bullet])
  147. {
  148. if (_bullet.ID == bullet.ID)
  149. {
  150. gameMap.GameObjLockDict[GameObjType.BombedBullet].EnterWriteLock();
  151. try
  152. {
  153. gameMap.GameObjDict[GameObjType.BombedBullet].Add(new BombedBullet(bullet));
  154. }
  155. finally
  156. {
  157. gameMap.GameObjLockDict[GameObjType.BombedBullet].ExitWriteLock();
  158. }
  159. gameMap.GameObjDict[GameObjType.Bullet].Remove(_bullet);
  160. break;
  161. }
  162. }
  163. }
  164. finally
  165. {
  166. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  167. }
  168. if (!bullet.IsToBomb)
  169. {
  170. if (objBeingShot == null)
  171. {
  172. if (bullet.Backswing > 0)
  173. {
  174. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  175. new Thread
  176. (() =>
  177. {
  178. Thread.Sleep(bullet.Backswing);
  179. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  180. {
  181. bullet.Parent.PlayerState = PlayerStateType.Null;
  182. }
  183. }
  184. )
  185. { IsBackground = true }.Start();
  186. }
  187. return;
  188. }
  189. BombObj(bullet, objBeingShot);
  190. if (bullet.RecoveryFromHit > 0)
  191. {
  192. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  193. new Thread
  194. (() =>
  195. {
  196. Thread.Sleep(bullet.RecoveryFromHit);
  197. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  198. {
  199. bullet.Parent.PlayerState = PlayerStateType.Null;
  200. }
  201. }
  202. )
  203. { IsBackground = true }.Start();
  204. }
  205. return;
  206. }
  207. /*if (objBeingShot != null)
  208. {
  209. else if (objBeingShot is Bullet) //子弹不能相互引爆,若要更改这一设定,取消注释即可。
  210. {
  211. new Thread(() => { BulletBomb((Bullet)objBeingShot, null); }) { IsBackground = true }.Start();
  212. }
  213. }*/
  214. // 子弹爆炸会发生的事↓↓↓
  215. var beAttackedList = new List<IGameObj>();
  216. foreach (var kvp in gameMap.GameObjDict)
  217. {
  218. if (CanBeBombed(bullet, kvp.Key))
  219. {
  220. gameMap.GameObjLockDict[kvp.Key].EnterWriteLock();
  221. try
  222. {
  223. foreach (var item in gameMap.GameObjDict[kvp.Key])
  224. if (bullet.CanAttack((GameObj)item))
  225. {
  226. beAttackedList.Add(item);
  227. }
  228. }
  229. finally
  230. {
  231. gameMap.GameObjLockDict[kvp.Key].ExitWriteLock();
  232. }
  233. }
  234. }
  235. foreach (GameObj beAttackedObj in beAttackedList)
  236. {
  237. BombObj(bullet, beAttackedObj);
  238. }
  239. if (objBeingShot == null)
  240. {
  241. if (bullet.Backswing > 0)
  242. {
  243. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  244. new Thread
  245. (() =>
  246. {
  247. Thread.Sleep(bullet.Backswing);
  248. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  249. {
  250. bullet.Parent.PlayerState = PlayerStateType.Null;
  251. }
  252. }
  253. )
  254. { IsBackground = true }.Start();
  255. }
  256. }
  257. else
  258. {
  259. if (bullet.RecoveryFromHit > 0)
  260. {
  261. bullet.Parent.PlayerState = PlayerStateType.IsSwinging;
  262. new Thread
  263. (() =>
  264. {
  265. Thread.Sleep(bullet.RecoveryFromHit);
  266. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.IsSwinging)
  267. {
  268. bullet.Parent.PlayerState = PlayerStateType.Null;
  269. }
  270. }
  271. )
  272. { IsBackground = true }.Start();
  273. }
  274. }
  275. beAttackedList.Clear();
  276. }
  277. public bool Attack(Character? player, double angle) // 射出去的子弹泼出去的水(狗头)
  278. { // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
  279. if (player == null)
  280. {
  281. #if DEBUG
  282. Console.WriteLine("the player who will attack is NULL!");
  283. #endif
  284. return false;
  285. }
  286. if (player.PlayerState != PlayerStateType.Null || player.PlayerState != PlayerStateType.IsMoving)
  287. return false;
  288. Bullet? bullet = player.RemoteAttack(
  289. new XY // 子弹紧贴人物生成。
  290. (
  291. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Cos(angle)),
  292. (int)((player.Radius + BulletFactory.BulletRadius(player.BulletOfPlayer)) * Math.Sin(angle))
  293. )
  294. );
  295. if (bullet.CastTime > 0)
  296. {
  297. player.PlayerState = PlayerStateType.IsTryingToAttack;
  298. new Thread
  299. (() =>
  300. {
  301. Thread.Sleep(bullet.CastTime);
  302. if (gameMap.Timer.IsGaming && player.PlayerState == PlayerStateType.IsTryingToAttack)
  303. {
  304. player.PlayerState = PlayerStateType.Null;
  305. }
  306. }
  307. )
  308. { IsBackground = true }.Start();
  309. }
  310. if (bullet != null)
  311. {
  312. bullet.CanMove = true;
  313. gameMap.GameObjLockDict[GameObjType.Bullet].EnterWriteLock();
  314. try
  315. {
  316. gameMap.GameObjDict[GameObjType.Bullet].Add(bullet);
  317. }
  318. finally
  319. {
  320. gameMap.GameObjLockDict[GameObjType.Bullet].ExitWriteLock();
  321. }
  322. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  323. #if DEBUG
  324. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  325. #endif
  326. return true;
  327. }
  328. else
  329. {
  330. #if DEBUG
  331. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  332. #endif
  333. return false;
  334. }
  335. }
  336. }
  337. }
  338. }