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

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