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