Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / room / DungeonManager.cs
@lijincheng lijincheng on 6 Jul 2023 19 KB 补上注释
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Config;
  7. using Godot;
  8.  
  9. /// <summary>
  10. /// 地牢管理器
  11. /// </summary>
  12. public partial class DungeonManager : Node2D
  13. {
  14. /// <summary>
  15. /// 起始房间
  16. /// </summary>
  17. public RoomInfo StartRoom => _dungeonGenerator?.StartRoom;
  18. /// <summary>
  19. /// 当前玩家所在的房间
  20. /// </summary>
  21. public RoomInfo ActiveRoom => Player.Current?.AffiliationArea?.RoomInfo;
  22. /// <summary>
  23. /// 当前玩家所在的区域
  24. /// </summary>
  25. public AffiliationArea ActiveAffiliationArea => Player.Current?.AffiliationArea;
  26.  
  27. /// <summary>
  28. /// 是否在地牢里
  29. /// </summary>
  30. public bool IsInDungeon { get; private set; }
  31.  
  32. private DungeonConfig _config;
  33. private DungeonTile _dungeonTile;
  34. private AutoTileConfig _autoTileConfig;
  35. private DungeonGenerator _dungeonGenerator;
  36. //房间内所有静态导航网格数据
  37. private List<NavigationPolygonData> _roomStaticNavigationList;
  38. private World _world;
  39. //用于检查房间敌人的计时器
  40. private float _checkEnemyTimer = 0;
  41.  
  42.  
  43. public DungeonManager()
  44. {
  45. //绑定事件
  46. EventManager.AddEventListener(EventEnum.OnPlayerFirstEnterRoom, OnPlayerFirstEnterRoom);
  47. EventManager.AddEventListener(EventEnum.OnPlayerEnterRoom, OnPlayerEnterRoom);
  48. }
  49. /// <summary>
  50. /// 加载地牢
  51. /// </summary>
  52. public void LoadDungeon(DungeonConfig config, Action finish = null)
  53. {
  54. _config = config;
  55. GameApplication.Instance.StartCoroutine(RunLoadDungeonCoroutine(finish));
  56. }
  57.  
  58. /// <summary>
  59. /// 退出地牢
  60. /// </summary>
  61. public void ExitDungeon(Action finish = null)
  62. {
  63. IsInDungeon = false;
  64. GameApplication.Instance.StartCoroutine(RunExitDungeonCoroutine(finish));
  65. }
  66.  
  67. public override void _PhysicsProcess(double delta)
  68. {
  69. if (IsInDungeon)
  70. {
  71. _checkEnemyTimer += (float)delta;
  72. if (_checkEnemyTimer >= 1)
  73. {
  74. _checkEnemyTimer %= 1;
  75. //检查房间内的敌人存活状况
  76. OnCheckEnemy();
  77. }
  78. }
  79. }
  80.  
  81. public override void _Process(double delta)
  82. {
  83. if (IsInDungeon)
  84. {
  85. UpdateEnemiesView();
  86. if (GameApplication.Instance.Debug)
  87. {
  88. QueueRedraw();
  89. }
  90. }
  91. }
  92.  
  93. //执行加载地牢协程
  94. private IEnumerator RunLoadDungeonCoroutine(Action finish)
  95. {
  96. //打开 loading UI
  97. UiManager.Open_Loading();
  98. yield return 0;
  99. //创建世界场景
  100. _world = GameApplication.Instance.CreateNewWorld();
  101. yield return new WaitForFixedProcess(10);
  102. //生成地牢房间
  103. _dungeonGenerator = new DungeonGenerator(_config);
  104. _dungeonGenerator.Generate();
  105. yield return 0;
  106. //填充地牢
  107. _autoTileConfig = new AutoTileConfig();
  108. _dungeonTile = new DungeonTile(_world.TileRoot);
  109. _dungeonTile.AutoFillRoomTile(_autoTileConfig, _dungeonGenerator.StartRoom);
  110. yield return 0;
  111. //生成寻路网格, 这一步操作只生成过道的导航
  112. _dungeonTile.GenerateNavigationPolygon(GameConfig.AisleFloorMapLayer);
  113. yield return 0;
  114. //挂载过道导航区域
  115. _dungeonTile.MountNavigationPolygon(_world.TileRoot);
  116. yield return 0;
  117. //过道导航区域数据
  118. _roomStaticNavigationList = new List<NavigationPolygonData>();
  119. _roomStaticNavigationList.AddRange(_dungeonTile.GetPolygonData());
  120. yield return 0;
  121. //门导航区域数据
  122. _roomStaticNavigationList.AddRange(_dungeonTile.GetConnectDoorPolygonData());
  123. yield return new WaitForFixedProcess(10);
  124. //初始化所有房间
  125. _dungeonGenerator.EachRoom(InitRoom);
  126. yield return new WaitForFixedProcess(10);
  127.  
  128. //播放bgm
  129. //SoundManager.PlayMusic(ResourcePath.resource_sound_bgm_Intro_ogg, -17f);
  130.  
  131. //初始房间创建玩家标记
  132. var playerBirthMark = StartRoom.ActivityMarks.FirstOrDefault(mark => mark.Type == ActivityIdPrefix.ActivityPrefixType.Player);
  133. //创建玩家
  134. var player = ActivityObject.Create<Player>(ActivityObject.Ids.Id_role0001);
  135. if (playerBirthMark != null)
  136. {
  137. //player.Position = new Vector2(50, 50);
  138. player.Position = playerBirthMark.Position;
  139. }
  140. player.Name = "Player";
  141. player.PutDown(RoomLayerEnum.YSortLayer);
  142. Player.SetCurrentPlayer(player);
  143.  
  144. //玩家手上添加武器
  145. //player.PickUpWeapon(ActivityObject.Create<Weapon>(ActivityObject.Ids.Id_weapon0001));
  146. // var weapon = ActivityObject.Create<Weapon>(ActivityObject.Ids.Id_weapon0001);
  147. // weapon.PutDown(player.Position, RoomLayerEnum.NormalLayer);
  148. // var weapon2 = ActivityObject.Create<Weapon>(ActivityObject.Ids.Id_weapon0002);
  149. // weapon2.PutDown(player.Position, RoomLayerEnum.NormalLayer);
  150. // var weapon3 = ActivityObject.Create<Weapon>(ActivityObject.Ids.Id_weapon0003);
  151. // weapon3.PutDown(player.Position, RoomLayerEnum.NormalLayer);
  152. // var weapon4 = ActivityObject.Create<Weapon>(ActivityObject.Ids.Id_weapon0004);
  153. // weapon4.PutDown(player.Position, RoomLayerEnum.NormalLayer);
  154.  
  155. GameApplication.Instance.Cursor.SetGuiMode(false);
  156. yield return 0;
  157. //打开游戏中的ui
  158. UiManager.Open_RoomUI();
  159. //派发进入地牢事件
  160. EventManager.EmitEvent(EventEnum.OnEnterDungeon);
  161. IsInDungeon = true;
  162. yield return 0;
  163. //关闭 loading UI
  164. UiManager.Dispose_Loading();
  165. if (finish != null)
  166. {
  167. finish();
  168. }
  169. }
  170.  
  171. private IEnumerator RunExitDungeonCoroutine(Action finish)
  172. {
  173. //打开 loading UI
  174. UiManager.Open_Loading();
  175. yield return 0;
  176. _world.Pause = true;
  177. yield return 0;
  178. _dungeonGenerator.EachRoom(DisposeRoomInfo);
  179. yield return 0;
  180. _dungeonTile = null;
  181. _autoTileConfig = null;
  182. _dungeonGenerator = null;
  183. _roomStaticNavigationList.Clear();
  184. _roomStaticNavigationList = null;
  185. UiManager.Hide_RoomUI();
  186. yield return new WaitForFixedProcess(10);
  187. Player.SetCurrentPlayer(null);
  188. _world = null;
  189. GameApplication.Instance.DestroyWorld();
  190. yield return new WaitForFixedProcess(10);
  191. //鼠标还原
  192. GameApplication.Instance.Cursor.SetGuiMode(false);
  193. //派发退出地牢事件
  194. EventManager.EmitEvent(EventEnum.OnExitDungeon);
  195. yield return 0;
  196. //关闭 loading UI
  197. UiManager.Dispose_Loading();
  198. if (finish != null)
  199. {
  200. finish();
  201. }
  202. }
  203. // 初始化房间
  204. private void InitRoom(RoomInfo roomInfo)
  205. {
  206. //挂载房间导航区域
  207. MountNavFromRoomInfo(roomInfo);
  208. //创建门
  209. CreateDoor(roomInfo);
  210. //创建房间归属区域
  211. CreateRoomAffiliation(roomInfo);
  212. //创建静态精灵画布
  213. CreateRoomStaticSpriteCanvas(roomInfo);
  214. }
  215. //挂载房间导航区域
  216. private void MountNavFromRoomInfo(RoomInfo roomInfo)
  217. {
  218. var polygonArray = roomInfo.RoomSplit.RoomInfo.NavigationList.ToArray();
  219. var polygon = new NavigationPolygon();
  220. var offset = roomInfo.GetOffsetPosition();
  221. for (var i = 0; i < polygonArray.Length; i++)
  222. {
  223. var navigationPolygonData = polygonArray[i];
  224. var polygonPointArray = navigationPolygonData.ConvertPointsToVector2Array();
  225. //这里的位置需要加上房间位置
  226. for (var j = 0; j < polygonPointArray.Length; j++)
  227. {
  228. polygonPointArray[j] = polygonPointArray[j] + roomInfo.GetWorldPosition() - offset;
  229. }
  230. polygon.AddOutline(polygonPointArray);
  231. var points = new List<SerializeVector2>();
  232. for (var j = 0; j < polygonPointArray.Length; j++)
  233. {
  234. points.Add(new SerializeVector2(polygonPointArray[j]));
  235. }
  236. //存入汇总列表
  237. _roomStaticNavigationList.Add(new NavigationPolygonData(navigationPolygonData.Type, points));
  238. }
  239. polygon.MakePolygonsFromOutlines();
  240. var navigationPolygon = new NavigationRegion2D();
  241. navigationPolygon.Name = "NavigationRegion" + (GetChildCount() + 1);
  242. navigationPolygon.NavigationPolygon = polygon;
  243. _world.TileRoot.AddChild(navigationPolygon);
  244. }
  245.  
  246. //创建门
  247. private void CreateDoor(RoomInfo roomInfo)
  248. {
  249. foreach (var doorInfo in roomInfo.Doors)
  250. {
  251. RoomDoor door;
  252. switch (doorInfo.Direction)
  253. {
  254. case DoorDirection.E:
  255. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_e);
  256. door.Position = (doorInfo.OriginPosition + new Vector2(0.5f, 2)) * GameConfig.TileCellSize;
  257. door.ZIndex = GameConfig.TopMapLayer;
  258. break;
  259. case DoorDirection.W:
  260. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_w);
  261. door.Position = (doorInfo.OriginPosition + new Vector2(-0.5f, 2)) * GameConfig.TileCellSize;
  262. door.ZIndex = GameConfig.TopMapLayer;
  263. break;
  264. case DoorDirection.S:
  265. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_s);
  266. door.Position = (doorInfo.OriginPosition + new Vector2(2f, 1.5f)) * GameConfig.TileCellSize;
  267. door.ZIndex = GameConfig.TopMapLayer;
  268. break;
  269. case DoorDirection.N:
  270. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_n);
  271. door.Position = (doorInfo.OriginPosition + new Vector2(2f, -0.5f)) * GameConfig.TileCellSize;
  272. door.ZIndex = GameConfig.MiddleMapLayer;
  273. break;
  274. default:
  275. return;
  276. }
  277. doorInfo.Door = door;
  278. door.Init(doorInfo);
  279. door.PutDown(RoomLayerEnum.NormalLayer, false);
  280. }
  281. }
  282.  
  283. //创建房间归属区域
  284. private void CreateRoomAffiliation(RoomInfo roomInfo)
  285. {
  286. var affiliation = new AffiliationArea();
  287. affiliation.Name = "AffiliationArea" + roomInfo.Id;
  288. affiliation.Init(roomInfo, new Rect2(
  289. roomInfo.GetWorldPosition() + new Vector2(GameConfig.TileCellSize, GameConfig.TileCellSize),
  290. (roomInfo.Size - new Vector2I(2, 2)) * GameConfig.TileCellSize));
  291. roomInfo.AffiliationArea = affiliation;
  292. _world.AffiliationAreaRoot.AddChild(affiliation);
  293. }
  294.  
  295. //创建静态精灵画布
  296. private void CreateRoomStaticSpriteCanvas(RoomInfo roomInfo)
  297. {
  298. var worldPos = roomInfo.GetWorldPosition();
  299. var pos = new Vector2I((int)worldPos.X, (int)worldPos.Y);
  300. int minX = pos.X;
  301. int minY = pos.Y;
  302. int maxX = minX + roomInfo.GetWidth();
  303. int maxY = minY + roomInfo.GetHeight();
  304.  
  305. //遍历每一个连接的门, 计算计算canvas覆盖范围
  306. foreach (var doorInfo in roomInfo.Doors)
  307. {
  308. var connectDoor = doorInfo.ConnectDoor;
  309. switch (connectDoor.Direction)
  310. {
  311. case DoorDirection.E:
  312. case DoorDirection.W:
  313. {
  314. var (px1, py1) = connectDoor.GetWorldOriginPosition();
  315. var py2 = py1 + 4 * GameConfig.TileCellSize;
  316. if (px1 < minX)
  317. {
  318. minX = px1;
  319. }
  320. else if (px1 > maxX)
  321. {
  322. maxX = px1;
  323. }
  324.  
  325. if (py1 < minY)
  326. {
  327. minY = py1;
  328. }
  329. else if (py1 > maxY)
  330. {
  331. maxY = py1;
  332. }
  333. if (py2 < minY)
  334. {
  335. minY = py2;
  336. }
  337. else if (py2 > maxY)
  338. {
  339. maxY = py2;
  340. }
  341. }
  342. break;
  343. case DoorDirection.S:
  344. case DoorDirection.N:
  345. {
  346. var (px1, py1) = connectDoor.GetWorldOriginPosition();
  347. var px2 = px1 + 4 * GameConfig.TileCellSize;
  348. if (px1 < minX)
  349. {
  350. minX = px1;
  351. }
  352. else if (px1 > maxX)
  353. {
  354. maxX = px1;
  355. }
  356.  
  357. if (py1 < minY)
  358. {
  359. minY = py1;
  360. }
  361. else if (py1 > maxY)
  362. {
  363. maxY = py1;
  364. }
  365. if (px2 < minX)
  366. {
  367. minX = px2;
  368. }
  369. else if (px2 > maxX)
  370. {
  371. maxX = px2;
  372. }
  373. }
  374. break;
  375. }
  376. }
  377.  
  378. minX -= GameConfig.TileCellSize;
  379. minY -= GameConfig.TileCellSize;
  380. maxX += GameConfig.TileCellSize;
  381. maxY += GameConfig.TileCellSize;
  382. var staticSpriteCanvas = new RoomStaticImageCanvas(
  383. _world.StaticSpriteRoot,
  384. new Vector2I(minX, minY),
  385. maxX - minX, maxY - minY
  386. );
  387. staticSpriteCanvas.RoomOffset = new Vector2I(worldPos.X - minX, worldPos.Y - minY);
  388. roomInfo.StaticImageCanvas = staticSpriteCanvas;
  389. }
  390.  
  391. /// <summary>
  392. /// 玩家第一次进入某个房间回调
  393. /// </summary>
  394. private void OnPlayerFirstEnterRoom(object o)
  395. {
  396. var room = (RoomInfo)o;
  397. room.BeReady();
  398. //如果关门了, 那么房间外的敌人就会丢失目标
  399. if (room.IsSeclusion)
  400. {
  401. var playerAffiliationArea = Player.Current.AffiliationArea;
  402. foreach (var enemy in _world.Enemy_InstanceList)
  403. {
  404. //不与玩家处于同一个房间
  405. if (enemy.AffiliationArea != playerAffiliationArea)
  406. {
  407. if (enemy.StateController.CurrState != AiStateEnum.AiNormal)
  408. {
  409. enemy.StateController.ChangeState(AiStateEnum.AiNormal);
  410. }
  411. }
  412. }
  413. }
  414. }
  415.  
  416. /// <summary>
  417. /// 玩家进入某个房间回调
  418. /// </summary>
  419. private void OnPlayerEnterRoom(object o)
  420. {
  421. }
  422. /// <summary>
  423. /// 检测当前房间敌人是否已经消灭干净, 应当每秒执行一次
  424. /// </summary>
  425. private void OnCheckEnemy()
  426. {
  427. var activeRoom = ActiveRoom;
  428. if (activeRoom != null)// && //activeRoom.IsSeclusion)
  429. {
  430. if (activeRoom.IsSeclusion) //房间处于关上状态
  431. {
  432. if (activeRoom.IsCurrWaveOver()) //所有标记执行完成
  433. {
  434. //是否有存活的敌人
  435. var flag = ActiveAffiliationArea.ExistIncludeItem(
  436. activityObject => activityObject.CollisionWithMask(PhysicsLayer.Enemy)
  437. );
  438. //GD.Print("当前房间存活数量: " + count);
  439. if (!flag)
  440. {
  441. activeRoom.OnClearRoom();
  442. }
  443. }
  444. }
  445. }
  446. }
  447.  
  448. /// <summary>
  449. /// 更新敌人视野
  450. /// </summary>
  451. private void UpdateEnemiesView()
  452. {
  453. _world.Enemy_IsFindTarget = false;
  454. _world.Enemy_FindTargetAffiliationSet.Clear();
  455. for (var i = 0; i < _world.Enemy_InstanceList.Count; i++)
  456. {
  457. var enemy = _world.Enemy_InstanceList[i];
  458. var state = enemy.StateController.CurrState;
  459. if (state == AiStateEnum.AiFollowUp || state == AiStateEnum.AiSurround) //目标在视野内
  460. {
  461. if (!_world.Enemy_IsFindTarget)
  462. {
  463. _world.Enemy_IsFindTarget = true;
  464. _world.Enemy_FindTargetPosition = Player.Current.GetCenterPosition();
  465. _world.Enemy_FindTargetAffiliationSet.Add(Player.Current.AffiliationArea);
  466. }
  467. _world.Enemy_FindTargetAffiliationSet.Add(enemy.AffiliationArea);
  468. }
  469. }
  470. }
  471.  
  472. private void DisposeRoomInfo(RoomInfo roomInfo)
  473. {
  474. roomInfo.Destroy();
  475. }
  476. public override void _Draw()
  477. {
  478. if (GameApplication.Instance.Debug)
  479. {
  480. if (_dungeonTile != null)
  481. {
  482. //绘制ai寻路区域
  483. Utils.DrawNavigationPolygon(this, _roomStaticNavigationList.ToArray());
  484. }
  485. //绘制房间区域
  486. // if (_dungeonGenerator != null)
  487. // {
  488. // DrawRoomInfo(StartRoom);
  489. // }
  490. //绘制边缘线
  491. }
  492. }
  493. //绘制房间区域, debug 用
  494. private void DrawRoomInfo(RoomInfo room)
  495. {
  496. var cellSize = _world.TileRoot.CellQuadrantSize;
  497. var pos1 = (room.Position + room.Size / 2) * cellSize;
  498. //绘制下一个房间
  499. foreach (var nextRoom in room.Next)
  500. {
  501. var pos2 = (nextRoom.Position + nextRoom.Size / 2) * cellSize;
  502. DrawLine(pos1, pos2, Colors.Red);
  503. DrawRoomInfo(nextRoom);
  504. }
  505.  
  506. DrawString(ResourceManager.DefaultFont16Px, pos1 - new Vector2I(0, 10), "Id: " + room.Id.ToString());
  507. DrawString(ResourceManager.DefaultFont16Px, pos1 + new Vector2I(0, 10), "Layer: " + room.Layer.ToString());
  508.  
  509. //绘制门
  510. foreach (var roomDoor in room.Doors)
  511. {
  512. var originPos = roomDoor.OriginPosition * cellSize;
  513. switch (roomDoor.Direction)
  514. {
  515. case DoorDirection.E:
  516. DrawLine(originPos, originPos + new Vector2(3, 0) * cellSize, Colors.Yellow);
  517. DrawLine(originPos + new Vector2(0, 4) * cellSize, originPos + new Vector2(3, 4) * cellSize,
  518. Colors.Yellow);
  519. break;
  520. case DoorDirection.W:
  521. DrawLine(originPos, originPos - new Vector2(3, 0) * cellSize, Colors.Yellow);
  522. DrawLine(originPos + new Vector2(0, 4) * cellSize, originPos - new Vector2(3, -4) * cellSize,
  523. Colors.Yellow);
  524. break;
  525. case DoorDirection.S:
  526. DrawLine(originPos, originPos + new Vector2(0, 3) * cellSize, Colors.Yellow);
  527. DrawLine(originPos + new Vector2(4, 0) * cellSize, originPos + new Vector2(4, 3) * cellSize,
  528. Colors.Yellow);
  529. break;
  530. case DoorDirection.N:
  531. DrawLine(originPos, originPos - new Vector2(0, 3) * cellSize, Colors.Yellow);
  532. DrawLine(originPos + new Vector2(4, 0) * cellSize, originPos - new Vector2(-4, 3) * cellSize,
  533. Colors.Yellow);
  534. break;
  535. }
  536. //绘制房间区域
  537. DrawRect(new Rect2(room.Position * cellSize, room.Size * cellSize), Colors.Blue, false);
  538.  
  539. if (roomDoor.HasCross && roomDoor.RoomInfo.Id < roomDoor.ConnectRoom.Id)
  540. {
  541. DrawRect(new Rect2(roomDoor.Cross * cellSize, new Vector2(cellSize * 4, cellSize * 4)), Colors.Yellow, false);
  542. }
  543. }
  544. }
  545. }