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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 bool BeStunned(Character player, int time)
  71. {
  72. if (player.PlayerState == PlayerStateType.Stunned || player.NoHp()) return false;
  73. new Thread
  74. (() =>
  75. {
  76. player.PlayerState = PlayerStateType.Stunned;
  77. Thread.Sleep(time);
  78. if (player.PlayerState == PlayerStateType.Stunned)
  79. player.PlayerState = PlayerStateType.Null;
  80. }
  81. )
  82. { IsBackground = true }.Start();
  83. return true;
  84. }
  85. private void Die(Character player)
  86. {
  87. player.Die(PlayerStateType.Deceased);
  88. for (int i = 0; i < GameData.maxNumOfPropInPropInventory; i++)
  89. {
  90. Prop? prop = player.UseProp(i);
  91. if (prop != null)
  92. {
  93. prop.ReSetPos(player.Position, gameMap.GetPlaceType(player.Position));
  94. gameMap.Add(prop);
  95. }
  96. }
  97. ++gameMap.NumOfDeceasedStudent;
  98. if (GameData.numOfStudent - gameMap.NumOfDeceasedStudent - gameMap.NumOfEscapedStudent == 1)
  99. {
  100. gameMap.GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
  101. try
  102. {
  103. foreach (EmergencyExit emergencyExit in gameMap.GameObjDict[GameObjType.EmergencyExit])
  104. if (emergencyExit.CanOpen)
  105. {
  106. emergencyExit.IsOpen = true;
  107. break;
  108. }
  109. }
  110. finally
  111. {
  112. gameMap.GameObjLockDict[GameObjType.EmergencyExit].ExitReadLock();
  113. }
  114. }
  115. // player.Reset();
  116. // ((Character?)bullet.Parent)?.AddScore(GameData.addScoreWhenKillOneLevelPlayer); // 给击杀者加分
  117. }
  118. private void BombObj(Bullet bullet, GameObj objBeingShot)
  119. {
  120. #if DEBUG
  121. Debugger.Output(bullet, "bombed " + objBeingShot.ToString());
  122. #endif
  123. switch (objBeingShot.Type)
  124. {
  125. case GameObjType.Character:
  126. if ((!(((Character)objBeingShot).IsGhost())) && bullet.Parent.IsGhost())
  127. {
  128. Student whoBeAttacked = (Student)objBeingShot;
  129. if (whoBeAttacked.BeAttacked(bullet))
  130. {
  131. BeAddictedToGame(whoBeAttacked, (Ghost)bullet.Parent);
  132. }
  133. if (whoBeAttacked.CanBeAwed())
  134. {
  135. if (BeStunned(whoBeAttacked, GameData.basicStunnedTimeOfStudent))
  136. bullet.Parent.AddScore(GameData.TrickerScoreStudentBeStunned);
  137. }
  138. }
  139. // if (((Character)objBeingShot).IsGhost() && !bullet.Parent.IsGhost() && bullet.TypeOfBullet == BulletType.Ram)
  140. // BeStunned((Character)objBeingShot, bullet.AP);
  141. break;
  142. case GameObjType.Generator:
  143. if (bullet.CanBeBombed(GameObjType.Generator))
  144. ((Generator)objBeingShot).DegreeOfRepair -= bullet.AP * GameData.factorDamageGenerator;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
  151. {
  152. #if DEBUG
  153. if (objBeingShot != null)
  154. Debugger.Output(bullet, "bombed with" + objBeingShot.ToString());
  155. else
  156. Debugger.Output(bullet, "bombed without objBeingShot");
  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.Swinging;
  168. new Thread
  169. (() =>
  170. {
  171. Thread.Sleep(bullet.Backswing);
  172. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  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.Swinging;
  186. new Thread
  187. (() =>
  188. {
  189. Thread.Sleep(bullet.RecoveryFromHit);
  190. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  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.Swinging;
  237. new Thread
  238. (() =>
  239. {
  240. Thread.Sleep(bullet.Backswing);
  241. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  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.Swinging;
  255. new Thread
  256. (() =>
  257. {
  258. Thread.Sleep(bullet.RecoveryFromHit);
  259. if (gameMap.Timer.IsGaming && bullet.Parent.PlayerState == PlayerStateType.Swinging)
  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 = player.Position + 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.AP += player.TryAddAp() ? GameData.ApPropAdd : 0;
  290. bullet.CanMove = true;
  291. gameMap.Add(bullet);
  292. moveEngine.MoveObj(bullet, (int)((bullet.BulletAttackRange - player.Radius - BulletFactory.BulletRadius(player.BulletOfPlayer)) * 1000 / bullet.MoveSpeed), angle); // 这里时间参数除出来的单位要是ms
  293. if (bullet.CastTime > 0)
  294. {
  295. player.PlayerState = PlayerStateType.TryingToAttack;
  296. new Thread
  297. (() =>
  298. {
  299. new FrameRateTaskExecutor<int>(
  300. loopCondition: () => player.PlayerState == PlayerStateType.TryingToAttack && gameMap.Timer.IsGaming,
  301. loopToDo: () =>
  302. {
  303. },
  304. timeInterval: GameData.frameDuration,
  305. finallyReturn: () => 0,
  306. maxTotalDuration: bullet.CastTime
  307. )
  308. .Start();
  309. if (gameMap.Timer.IsGaming)
  310. {
  311. if (player.PlayerState == PlayerStateType.TryingToAttack)
  312. {
  313. player.PlayerState = PlayerStateType.Null;
  314. }
  315. else
  316. bullet.IsMoving = false;
  317. gameMap.Remove(bullet);
  318. }
  319. }
  320. )
  321. { IsBackground = true }.Start();
  322. }
  323. }
  324. if (bullet != null)
  325. {
  326. #if DEBUG
  327. Console.WriteLine($"playerID:{player.ID} successfully attacked!");
  328. #endif
  329. return true;
  330. }
  331. else
  332. {
  333. #if DEBUG
  334. Console.WriteLine($"playerID:{player.ID} has no bullets so that he can't attack!");
  335. #endif
  336. return false;
  337. }
  338. }
  339. }
  340. }
  341. }