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

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