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

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