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

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