Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / room / DungeonManager.cs
  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. var roomUi = UiManager.Open_RoomUI();
  159. roomUi.InitData(player);
  160. //派发进入地牢事件
  161. EventManager.EmitEvent(EventEnum.OnEnterDungeon);
  162. IsInDungeon = true;
  163. yield return 0;
  164. //关闭 loading UI
  165. UiManager.Dispose_Loading();
  166. if (finish != null)
  167. {
  168. finish();
  169. }
  170. }
  171.  
  172. private IEnumerator RunExitDungeonCoroutine(Action finish)
  173. {
  174. //打开 loading UI
  175. UiManager.Open_Loading();
  176. yield return 0;
  177. _world.Pause = true;
  178. yield return 0;
  179. _dungeonGenerator.EachRoom(DisposeRoomInfo);
  180. yield return 0;
  181. _dungeonTile = null;
  182. _autoTileConfig = null;
  183. _dungeonGenerator = null;
  184. _roomStaticNavigationList.Clear();
  185. _roomStaticNavigationList = null;
  186. UiManager.Hide_RoomUI();
  187. yield return new WaitForFixedProcess(10);
  188. Player.SetCurrentPlayer(null);
  189. _world = null;
  190. GameApplication.Instance.DestroyWorld();
  191. yield return new WaitForFixedProcess(10);
  192. //鼠标还原
  193. GameApplication.Instance.Cursor.SetGuiMode(false);
  194. //派发退出地牢事件
  195. EventManager.EmitEvent(EventEnum.OnExitDungeon);
  196. yield return 0;
  197. //关闭 loading UI
  198. UiManager.Dispose_Loading();
  199. if (finish != null)
  200. {
  201. finish();
  202. }
  203. }
  204. // 初始化房间
  205. private void InitRoom(RoomInfo roomInfo)
  206. {
  207. //挂载房间导航区域
  208. MountNavFromRoomInfo(roomInfo);
  209. //创建门
  210. CreateDoor(roomInfo);
  211. //创建房间归属区域
  212. CreateRoomAffiliation(roomInfo);
  213. //创建静态精灵画布
  214. CreateRoomStaticSpriteCanvas(roomInfo);
  215. }
  216. //挂载房间导航区域
  217. private void MountNavFromRoomInfo(RoomInfo roomInfo)
  218. {
  219. var polygonArray = roomInfo.RoomSplit.RoomInfo.NavigationList.ToArray();
  220. var polygon = new NavigationPolygon();
  221. var offset = roomInfo.GetOffsetPosition();
  222. for (var i = 0; i < polygonArray.Length; i++)
  223. {
  224. var navigationPolygonData = polygonArray[i];
  225. var polygonPointArray = navigationPolygonData.ConvertPointsToVector2Array();
  226. //这里的位置需要加上房间位置
  227. for (var j = 0; j < polygonPointArray.Length; j++)
  228. {
  229. polygonPointArray[j] = polygonPointArray[j] + roomInfo.GetWorldPosition() - offset;
  230. }
  231. polygon.AddOutline(polygonPointArray);
  232. var points = new List<SerializeVector2>();
  233. for (var j = 0; j < polygonPointArray.Length; j++)
  234. {
  235. points.Add(new SerializeVector2(polygonPointArray[j]));
  236. }
  237. //存入汇总列表
  238. _roomStaticNavigationList.Add(new NavigationPolygonData(navigationPolygonData.Type, points));
  239. }
  240. polygon.MakePolygonsFromOutlines();
  241. var navigationPolygon = new NavigationRegion2D();
  242. navigationPolygon.Name = "NavigationRegion" + (GetChildCount() + 1);
  243. navigationPolygon.NavigationPolygon = polygon;
  244. _world.TileRoot.AddChild(navigationPolygon);
  245. }
  246.  
  247. //创建门
  248. private void CreateDoor(RoomInfo roomInfo)
  249. {
  250. foreach (var doorInfo in roomInfo.Doors)
  251. {
  252. RoomDoor door;
  253. switch (doorInfo.Direction)
  254. {
  255. case DoorDirection.E:
  256. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_e);
  257. door.Position = (doorInfo.OriginPosition + new Vector2(0.5f, 2)) * GameConfig.TileCellSize;
  258. door.ZIndex = GameConfig.TopMapLayer;
  259. break;
  260. case DoorDirection.W:
  261. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_w);
  262. door.Position = (doorInfo.OriginPosition + new Vector2(-0.5f, 2)) * GameConfig.TileCellSize;
  263. door.ZIndex = GameConfig.TopMapLayer;
  264. break;
  265. case DoorDirection.S:
  266. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_s);
  267. door.Position = (doorInfo.OriginPosition + new Vector2(2f, 1.5f)) * GameConfig.TileCellSize;
  268. door.ZIndex = GameConfig.TopMapLayer;
  269. break;
  270. case DoorDirection.N:
  271. door = ActivityObject.Create<RoomDoor>(ActivityObject.Ids.Id_other_door_n);
  272. door.Position = (doorInfo.OriginPosition + new Vector2(2f, -0.5f)) * GameConfig.TileCellSize;
  273. door.ZIndex = GameConfig.MiddleMapLayer;
  274. break;
  275. default:
  276. return;
  277. }
  278. doorInfo.Door = door;
  279. door.Init(doorInfo);
  280. door.PutDown(RoomLayerEnum.NormalLayer, false);
  281. }
  282. }
  283.  
  284. //创建房间归属区域
  285. private void CreateRoomAffiliation(RoomInfo roomInfo)
  286. {
  287. var affiliation = new AffiliationArea();
  288. affiliation.Name = "AffiliationArea" + roomInfo.Id;
  289. affiliation.Init(roomInfo, new Rect2(
  290. roomInfo.GetWorldPosition() + new Vector2(GameConfig.TileCellSize, GameConfig.TileCellSize),
  291. (roomInfo.Size - new Vector2I(2, 2)) * GameConfig.TileCellSize));
  292. roomInfo.AffiliationArea = affiliation;
  293. _world.AffiliationAreaRoot.AddChild(affiliation);
  294. }
  295.  
  296. //创建静态精灵画布
  297. private void CreateRoomStaticSpriteCanvas(RoomInfo roomInfo)
  298. {
  299. var worldPos = roomInfo.GetWorldPosition();
  300. var pos = new Vector2I((int)worldPos.X, (int)worldPos.Y);
  301. int minX = pos.X;
  302. int minY = pos.Y;
  303. int maxX = minX + roomInfo.GetWidth();
  304. int maxY = minY + roomInfo.GetHeight();
  305.  
  306. //遍历每一个连接的门, 计算计算canvas覆盖范围
  307. foreach (var doorInfo in roomInfo.Doors)
  308. {
  309. var connectDoor = doorInfo.ConnectDoor;
  310. switch (connectDoor.Direction)
  311. {
  312. case DoorDirection.E:
  313. case DoorDirection.W:
  314. {
  315. var (px1, py1) = connectDoor.GetWorldOriginPosition();
  316. var py2 = py1 + 4 * GameConfig.TileCellSize;
  317. if (px1 < minX)
  318. {
  319. minX = (int)px1;
  320. }
  321. else if (px1 > maxX)
  322. {
  323. maxX = (int)px1;
  324. }
  325.  
  326. if (py1 < minY)
  327. {
  328. minY = (int)py1;
  329. }
  330. else if (py1 > maxY)
  331. {
  332. maxY = (int)py1;
  333. }
  334. if (py2 < minY)
  335. {
  336. minY = (int)py2;
  337. }
  338. else if (py2 > maxY)
  339. {
  340. maxY = (int)py2;
  341. }
  342. }
  343. break;
  344. case DoorDirection.S:
  345. case DoorDirection.N:
  346. {
  347. var (px1, py1) = connectDoor.GetWorldOriginPosition();
  348. var px2 = px1 + 4 * GameConfig.TileCellSize;
  349. if (px1 < minX)
  350. {
  351. minX = (int)px1;
  352. }
  353. else if (px1 > maxX)
  354. {
  355. maxX = (int)px1;
  356. }
  357.  
  358. if (py1 < minY)
  359. {
  360. minY = (int)py1;
  361. }
  362. else if (py1 > maxY)
  363. {
  364. maxY = (int)py1;
  365. }
  366. if (px2 < minX)
  367. {
  368. minX = (int)px2;
  369. }
  370. else if (px2 > maxX)
  371. {
  372. maxX = (int)px2;
  373. }
  374. }
  375. break;
  376. }
  377. }
  378.  
  379. minX -= GameConfig.TileCellSize;
  380. minY -= GameConfig.TileCellSize;
  381. maxX += GameConfig.TileCellSize;
  382. maxY += GameConfig.TileCellSize;
  383. var staticSpriteCanvas = new RoomStaticImageCanvas(
  384. _world.StaticSpriteRoot,
  385. new Vector2I(minX, minY),
  386. maxX - minX, maxY - minY
  387. );
  388. staticSpriteCanvas.RoomOffset = new Vector2I(worldPos.X - minX, worldPos.Y - minY);
  389. roomInfo.StaticImageCanvas = staticSpriteCanvas;
  390. }
  391.  
  392. /// <summary>
  393. /// 玩家第一次进入某个房间回调
  394. /// </summary>
  395. private void OnPlayerFirstEnterRoom(object o)
  396. {
  397. var room = (RoomInfo)o;
  398. room.BeReady();
  399. //如果关门了, 那么房间外的敌人就会丢失目标
  400. if (room.IsSeclusion)
  401. {
  402. var playerAffiliationArea = Player.Current.AffiliationArea;
  403. foreach (var enemy in _world.Enemy_InstanceList)
  404. {
  405. //不与玩家处于同一个房间
  406. if (enemy.AffiliationArea != playerAffiliationArea)
  407. {
  408. if (enemy.StateController.CurrState != AiStateEnum.AiNormal)
  409. {
  410. enemy.StateController.ChangeState(AiStateEnum.AiNormal);
  411. }
  412. }
  413. }
  414. }
  415. }
  416.  
  417. /// <summary>
  418. /// 玩家进入某个房间回调
  419. /// </summary>
  420. private void OnPlayerEnterRoom(object o)
  421. {
  422. }
  423. /// <summary>
  424. /// 检测当前房间敌人是否已经消灭干净, 应当每秒执行一次
  425. /// </summary>
  426. private void OnCheckEnemy()
  427. {
  428. var activeRoom = ActiveRoom;
  429. if (activeRoom != null)// && //activeRoom.IsSeclusion)
  430. {
  431. if (activeRoom.IsSeclusion) //房间处于关上状态
  432. {
  433. if (activeRoom.IsCurrWaveOver()) //所有标记执行完成
  434. {
  435. //是否有存活的敌人
  436. var flag = ActiveAffiliationArea.ExistIncludeItem(
  437. activityObject => activityObject.CollisionWithMask(PhysicsLayer.Enemy)
  438. );
  439. //GD.Print("当前房间存活数量: " + count);
  440. if (!flag)
  441. {
  442. activeRoom.OnClearRoom();
  443. }
  444. }
  445. }
  446. }
  447. }
  448.  
  449. /// <summary>
  450. /// 更新敌人视野
  451. /// </summary>
  452. private void UpdateEnemiesView()
  453. {
  454. _world.Enemy_IsFindTarget = false;
  455. _world.Enemy_FindTargetAffiliationSet.Clear();
  456. for (var i = 0; i < _world.Enemy_InstanceList.Count; i++)
  457. {
  458. var enemy = _world.Enemy_InstanceList[i];
  459. var state = enemy.StateController.CurrState;
  460. if (state == AiStateEnum.AiFollowUp || state == AiStateEnum.AiSurround) //目标在视野内
  461. {
  462. if (!_world.Enemy_IsFindTarget)
  463. {
  464. _world.Enemy_IsFindTarget = true;
  465. _world.Enemy_FindTargetPosition = Player.Current.GetCenterPosition();
  466. _world.Enemy_FindTargetAffiliationSet.Add(Player.Current.AffiliationArea);
  467. }
  468. _world.Enemy_FindTargetAffiliationSet.Add(enemy.AffiliationArea);
  469. }
  470. }
  471. }
  472.  
  473. private void DisposeRoomInfo(RoomInfo roomInfo)
  474. {
  475. foreach (var activityMark in roomInfo.ActivityMarks)
  476. {
  477. activityMark.QueueFree();
  478. }
  479. roomInfo.ActivityMarks.Clear();
  480. }
  481. public override void _Draw()
  482. {
  483. if (GameApplication.Instance.Debug)
  484. {
  485. if (_dungeonTile != null)
  486. {
  487. //绘制ai寻路区域
  488. Utils.DrawNavigationPolygon(this, _roomStaticNavigationList.ToArray());
  489. }
  490. //绘制房间区域
  491. // if (_dungeonGenerator != null)
  492. // {
  493. // DrawRoomInfo(StartRoom);
  494. // }
  495. //绘制边缘线
  496. }
  497. }
  498. //绘制房间区域, debug 用
  499. private void DrawRoomInfo(RoomInfo room)
  500. {
  501. var cellSize = _world.TileRoot.CellQuadrantSize;
  502. var pos1 = (room.Position + room.Size / 2) * cellSize;
  503. //绘制下一个房间
  504. foreach (var nextRoom in room.Next)
  505. {
  506. var pos2 = (nextRoom.Position + nextRoom.Size / 2) * cellSize;
  507. DrawLine(pos1, pos2, Colors.Red);
  508. DrawRoomInfo(nextRoom);
  509. }
  510.  
  511. DrawString(ResourceManager.DefaultFont16Px, pos1 - new Vector2I(0, 10), "Id: " + room.Id.ToString());
  512. DrawString(ResourceManager.DefaultFont16Px, pos1 + new Vector2I(0, 10), "Layer: " + room.Layer.ToString());
  513.  
  514. //绘制门
  515. foreach (var roomDoor in room.Doors)
  516. {
  517. var originPos = roomDoor.OriginPosition * cellSize;
  518. switch (roomDoor.Direction)
  519. {
  520. case DoorDirection.E:
  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.W:
  526. DrawLine(originPos, originPos - new Vector2(3, 0) * cellSize, Colors.Yellow);
  527. DrawLine(originPos + new Vector2(0, 4) * cellSize, originPos - new Vector2(3, -4) * cellSize,
  528. Colors.Yellow);
  529. break;
  530. case DoorDirection.S:
  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. case DoorDirection.N:
  536. DrawLine(originPos, originPos - new Vector2(0, 3) * cellSize, Colors.Yellow);
  537. DrawLine(originPos + new Vector2(4, 0) * cellSize, originPos - new Vector2(-4, 3) * cellSize,
  538. Colors.Yellow);
  539. break;
  540. }
  541. //绘制房间区域
  542. DrawRect(new Rect2(room.Position * cellSize, room.Size * cellSize), Colors.Blue, false);
  543.  
  544. if (roomDoor.HasCross && roomDoor.RoomInfo.Id < roomDoor.ConnectRoom.Id)
  545. {
  546. DrawRect(new Rect2(roomDoor.Cross * cellSize, new Vector2(cellSize * 4, cellSize * 4)), Colors.Yellow, false);
  547. }
  548. }
  549. }
  550. }