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

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