Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditor / TileView / EditorTileMap.cs
@小李xl 小李xl on 20 Jul 2023 24 KB 地图编辑器开发中...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using Godot;
  7. using Godot.Collections;
  8.  
  9. namespace UI.MapEditor;
  10.  
  11. public partial class EditorTileMap : TileMap
  12. {
  13. /// <summary>
  14. /// 自动图块地板层
  15. /// </summary>
  16. public const int AutoFloorLayer = 0;
  17. /// <summary>
  18. /// 自定义图块地板层
  19. /// </summary>
  20. public const int CustomFloorLayer = 1;
  21. /// <summary>
  22. /// 自动图块中间层
  23. /// </summary>
  24. public const int AutoMiddleLayer = 2;
  25. /// <summary>
  26. /// 自定义图块中间层
  27. /// </summary>
  28. public const int CustomMiddleLayer = 3;
  29. /// <summary>
  30. /// 自动图块顶层
  31. /// </summary>
  32. public const int AutoTopLayer = 4;
  33. /// <summary>
  34. /// 自定义图块顶层
  35. /// </summary>
  36. public const int CustomTopLayer = 5;
  37. /// <summary>
  38. /// 所属地图编辑器UI
  39. /// </summary>
  40. public MapEditorPanel MapEditorPanel { get; set; }
  41. //鼠标坐标
  42. private Vector2 _mousePosition;
  43. //鼠标所在的cell坐标
  44. private Vector2I _mouseCellPosition;
  45. //上一帧鼠标所在的cell坐标
  46. private Vector2I _prevMouseCellPosition = new Vector2I(-99999, -99999);
  47. //单次绘制是否改变过tile数据
  48. private bool _changeFlag = false;
  49. //左键开始按下时鼠标所在的坐标
  50. private Vector2I _mouseStartCellPosition;
  51. //鼠标中建是否按下
  52. private bool _isMiddlePressed = false;
  53. private Vector2 _moveOffset;
  54. //左键是否按下
  55. private bool _isLeftPressed = false;
  56. //右键是否按下
  57. private bool _isRightPressed = false;
  58. //绘制填充区域
  59. private bool _drawFullRect = false;
  60. //负责存储自动图块数据
  61. private Grid<bool> _autoCellLayerGrid = new Grid<bool>();
  62. //用于生成导航网格
  63. private DungeonTileMap _dungeonTileMap;
  64. //停止绘制多久后开始执行生成操作
  65. private float _generateInterval = 3f;
  66. //生成自动图块和导航网格的计时器
  67. private float _generateTimer = -1;
  68. //检测地形结果
  69. private bool _checkTerrainFlag = true;
  70. //错误地形位置
  71. private Vector2I _checkTerrainErrorPosition = Vector2I.Zero;
  72. //是否执行生成地形成功
  73. private bool _isGenerateTerrain = false;
  74.  
  75. private bool _initLayer = false;
  76. //--------- 配置数据 -------------
  77. private int _sourceId = 0;
  78. private int _terrainSet = 0;
  79. private int _terrain = 0;
  80. private AutoTileConfig _autoTileConfig = new AutoTileConfig();
  81.  
  82. private string _dir;
  83. private string _groupName = "testGroup1";
  84. private string _fileName = "Room2";
  85. private Vector2I _roomPosition;
  86. private Vector2I _roomSize;
  87. private List<DoorAreaInfo> _doorConfigs = new List<DoorAreaInfo>();
  88. private DungeonRoomType _roomType = DungeonRoomType.Battle;
  89. private int _weight = 100;
  90. //-------------------------------
  91.  
  92. public override void _Ready()
  93. {
  94. InitLayer();
  95. }
  96.  
  97. public override void _Process(double delta)
  98. {
  99. var newDelta = (float)delta;
  100. _drawFullRect = false;
  101. var position = GetLocalMousePosition();
  102. _mouseCellPosition = LocalToMap(position);
  103. _mousePosition = new Vector2(
  104. _mouseCellPosition.X * GameConfig.TileCellSize,
  105. _mouseCellPosition.Y * GameConfig.TileCellSize
  106. );
  107.  
  108. if (!MapEditorPanel.ToolsPanel.S_HBoxContainer.Instance.IsPositionOver(GetGlobalMousePosition())) //不在Ui节点上
  109. {
  110. //左键绘制
  111. if (_isLeftPressed)
  112. {
  113. if (Input.IsKeyPressed(Key.Shift)) //按住shift绘制矩形
  114. {
  115. _drawFullRect = true;
  116. }
  117. else if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过
  118. {
  119. _changeFlag = true;
  120. _prevMouseCellPosition = _mouseCellPosition;
  121. //绘制自动图块
  122. SetSingleAutoCell(_mouseCellPosition);
  123. }
  124. }
  125. else if (_isRightPressed) //右键擦除
  126. {
  127. if (Input.IsKeyPressed(Key.Shift)) //按住shift擦除矩形
  128. {
  129. _drawFullRect = true;
  130. }
  131. else if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过
  132. {
  133. _changeFlag = true;
  134. _prevMouseCellPosition = _mouseCellPosition;
  135. EraseSingleAutoCell(_mouseCellPosition);
  136. }
  137. }
  138. else if (_isMiddlePressed) //中建移动
  139. {
  140. //GD.Print("移动...");
  141. Position = GetGlobalMousePosition() + _moveOffset;
  142. }
  143. }
  144.  
  145. //绘制停止指定时间后, 生成导航网格
  146. if (_generateTimer > 0)
  147. {
  148. _generateTimer -= newDelta;
  149. if (_generateTimer <= 0)
  150. {
  151. //计算区域
  152. CalcTileRect();
  153. GD.Print("开始检测是否可以生成地形...");
  154. if (CheckTerrain())
  155. {
  156. GD.Print("开始绘制导航网格...");
  157. if (GenerateNavigation())
  158. {
  159. GD.Print("开始绘制自动贴图...");
  160. GenerateTerrain();
  161. _isGenerateTerrain = true;
  162. }
  163. }
  164. else
  165. {
  166. SetErrorCell(_checkTerrainErrorPosition);
  167. }
  168. }
  169. }
  170. }
  171.  
  172. /// <summary>
  173. /// 绘制辅助线
  174. /// </summary>
  175. public void DrawGuides(CanvasItem canvasItem)
  176. {
  177. //轴线
  178. canvasItem.DrawLine(new Vector2(0, 2000), new Vector2(0, -2000), Colors.Green);
  179. canvasItem.DrawLine(new Vector2(2000, 0), new Vector2( -2000, 0), Colors.Red);
  180. //绘制房间区域
  181. if (_roomSize.X != 0 && _roomSize.Y != 0)
  182. {
  183. var size = TileSet.TileSize;
  184. canvasItem.DrawRect(new Rect2((_roomPosition - Vector2I.One) * size, (_roomSize + new Vector2I(2, 2)) * size),
  185. Colors.Aqua, false, 5f / Scale.X);
  186. }
  187. if (_checkTerrainFlag) //已经通过地形检测
  188. {
  189. //绘制导航网格
  190. var result = _dungeonTileMap.GetGenerateNavigationResult();
  191. if (result != null && result.Success)
  192. {
  193. var polygonData = _dungeonTileMap.GetPolygonData();
  194. Utils.DrawNavigationPolygon(canvasItem, polygonData, 3f / Scale.X);
  195. }
  196. }
  197.  
  198. if (_drawFullRect) //绘制填充矩形
  199. {
  200. var size = TileSet.TileSize;
  201. var cellPos = _mouseStartCellPosition;
  202. var temp = size;
  203. if (_mouseStartCellPosition.X > _mouseCellPosition.X)
  204. {
  205. cellPos.X += 1;
  206. temp.X -= size.X;
  207. }
  208. if (_mouseStartCellPosition.Y > _mouseCellPosition.Y)
  209. {
  210. cellPos.Y += 1;
  211. temp.Y -= size.Y;
  212. }
  213.  
  214. var pos = cellPos * size;
  215. canvasItem.DrawRect(new Rect2(pos, _mousePosition - pos + temp), Colors.White, false, 2f / Scale.X);
  216. }
  217. else //绘制单格
  218. {
  219. canvasItem.DrawRect(new Rect2(_mousePosition, TileSet.TileSize), Colors.White, false, 2f / Scale.X);
  220. }
  221. }
  222.  
  223. public override void _Input(InputEvent @event)
  224. {
  225. if (@event is InputEventMouseButton mouseButton)
  226. {
  227. if (mouseButton.ButtonIndex == MouseButton.Left) //左键
  228. {
  229. if (mouseButton.Pressed) //按下
  230. {
  231. _mouseStartCellPosition = LocalToMap(GetLocalMousePosition());
  232. }
  233. else
  234. {
  235. _changeFlag = false;
  236. if (_drawFullRect) //松开, 提交绘制的矩形区域
  237. {
  238. SetRectAutoCell(_mouseStartCellPosition, _mouseCellPosition);
  239. _drawFullRect = false;
  240. }
  241. }
  242.  
  243. _isLeftPressed = mouseButton.Pressed;
  244. }
  245. else if (mouseButton.ButtonIndex == MouseButton.Right) //右键
  246. {
  247. if (mouseButton.Pressed) //按下
  248. {
  249. _mouseStartCellPosition = LocalToMap(GetLocalMousePosition());
  250. }
  251. else
  252. {
  253. _changeFlag = false;
  254. if (_drawFullRect) //松开, 提交擦除的矩形区域
  255. {
  256. EraseRectAutoCell(_mouseStartCellPosition, _mouseCellPosition);
  257. _drawFullRect = false;
  258. }
  259. }
  260. _isRightPressed = mouseButton.Pressed;
  261. }
  262. else if (mouseButton.ButtonIndex == MouseButton.WheelDown)
  263. {
  264. //缩小
  265. Shrink();
  266. }
  267. else if (mouseButton.ButtonIndex == MouseButton.WheelUp)
  268. {
  269. //放大
  270. Magnify();
  271. }
  272. else if (mouseButton.ButtonIndex == MouseButton.Middle)
  273. {
  274. _isMiddlePressed = mouseButton.Pressed;
  275. if (_isMiddlePressed)
  276. {
  277. _moveOffset = Position - GetGlobalMousePosition();
  278. }
  279. }
  280. }
  281. else if (@event is InputEventKey eventKey)
  282. {
  283. if (eventKey.Pressed && eventKey.Keycode == Key.M)
  284. {
  285. GD.Print("测试保存地牢房间数据...");
  286. TriggerSave();
  287. }
  288. }
  289. }
  290.  
  291. //将指定层数据存入list中
  292. private void PushLayerDataToList(int layer, int sourceId, List<int> list)
  293. {
  294. var layerArray = GetUsedCellsById(layer, sourceId);
  295. foreach (var pos in layerArray)
  296. {
  297. var atlasCoords = GetCellAtlasCoords(layer, pos);
  298. list.Add(pos.X);
  299. list.Add(pos.Y);
  300. list.Add(_sourceId);
  301. list.Add(atlasCoords.X);
  302. list.Add(atlasCoords.Y);
  303. }
  304. }
  305.  
  306. private void SetLayerDataFromList(int layer, List<int> list)
  307. {
  308. for (var i = 0; i < list.Count; i += 5)
  309. {
  310. var pos = new Vector2I(list[i], list[i + 1]);
  311. var sourceId = list[i + 2];
  312. var atlasCoords = new Vector2I(list[i + 3], list[i + 4]);
  313. SetCell(layer, pos, sourceId, atlasCoords);
  314. if (layer == AutoFloorLayer)
  315. {
  316. _autoCellLayerGrid.Set(pos, true);
  317. }
  318. }
  319. }
  320.  
  321. //保存地牢
  322. private void TriggerSave()
  323. {
  324. SaveRoomInfoConfig();
  325. SaveTileInfoConfig();
  326. }
  327.  
  328. /// <summary>
  329. /// 加载地牢, 返回是否加载成功
  330. /// </summary>
  331. /// <param name="dir">文件夹路径</param>
  332. /// <param name="groupName">房间组名</param>
  333. /// <param name="roomType">房间类型</param>
  334. /// <param name="roomName">房间名称</param>
  335. public bool Load(string dir, string groupName, DungeonRoomType roomType, string roomName)
  336. {
  337. _dir = dir;
  338. _groupName = groupName;
  339. _roomType = roomType;
  340. _fileName = roomName;
  341.  
  342. var path = GetConfigPath(dir, groupName, roomType, roomName) + "/";
  343. var tileInfoConfigPath = path + GetTileInfoConfigName(_fileName);
  344. var roomInfoConfigPath = path + GetRoomInfoConfigName(_fileName);
  345.  
  346. if (!File.Exists(tileInfoConfigPath))
  347. {
  348. GD.PrintErr("地牢编辑器加载地牢时未找到文件: " + tileInfoConfigPath);
  349. return false;
  350. }
  351. if (!File.Exists(roomInfoConfigPath))
  352. {
  353. GD.PrintErr("地牢编辑器加载地牢时未找到文件: " + roomInfoConfigPath);
  354. return false;
  355. }
  356. var text = ResourceManager.LoadText(tileInfoConfigPath);
  357. var tileInfo = JsonSerializer.Deserialize<DungeonTileInfo>(text);
  358. var text2 = ResourceManager.LoadText(roomInfoConfigPath);
  359. var roomInfo = JsonSerializer.Deserialize<DungeonRoomInfo>(text2);
  360.  
  361. _roomPosition = roomInfo.Position.AsVector2I();
  362. _roomSize = roomInfo.Size.AsVector2I();
  363. _doorConfigs.Clear();
  364. _doorConfigs.AddRange(roomInfo.DoorAreaInfos);
  365. _weight = roomInfo.Weight;
  366. //初始化层级数据
  367. InitLayer();
  368. //地块数据
  369. SetLayerDataFromList(AutoFloorLayer, tileInfo.Floor);
  370. SetLayerDataFromList(AutoMiddleLayer, tileInfo.Middle);
  371. SetLayerDataFromList(AutoTopLayer, tileInfo.Top);
  372. //导航网格数据
  373. _dungeonTileMap.SetPolygonData(tileInfo.NavigationList);
  374.  
  375. //聚焦
  376. //MapEditorPanel.CallDelay(0.1f, OnClickCenterTool);
  377. CallDeferred(nameof(OnClickCenterTool));
  378. return true;
  379. }
  380.  
  381. private void InitLayer()
  382. {
  383. if (_initLayer)
  384. {
  385. return;
  386. }
  387.  
  388. _initLayer = true;
  389. //初始化层级数据
  390. AddLayer(CustomFloorLayer);
  391. SetLayerZIndex(CustomFloorLayer, CustomFloorLayer);
  392. AddLayer(AutoMiddleLayer);
  393. SetLayerZIndex(AutoMiddleLayer, AutoMiddleLayer);
  394. AddLayer(CustomMiddleLayer);
  395. SetLayerZIndex(CustomMiddleLayer, CustomMiddleLayer);
  396. AddLayer(AutoTopLayer);
  397. SetLayerZIndex(AutoTopLayer, AutoTopLayer);
  398. AddLayer(CustomTopLayer);
  399. SetLayerZIndex(CustomTopLayer, CustomTopLayer);
  400.  
  401. _dungeonTileMap = new DungeonTileMap(this);
  402. _dungeonTileMap.SetFloorAtlasCoords(new List<Vector2I>(new []{ _autoTileConfig.Floor.AutoTileCoord }));
  403. }
  404.  
  405. //缩小
  406. private void Shrink()
  407. {
  408. var pos = GetLocalMousePosition();
  409. var scale = Scale / 1.1f;
  410. if (scale.LengthSquared() >= 0.5f)
  411. {
  412. Scale = scale;
  413. Position += pos * 0.1f * scale;
  414. }
  415. else
  416. {
  417. GD.Print("太小了");
  418. }
  419. }
  420. //放大
  421. private void Magnify()
  422. {
  423. var pos = GetLocalMousePosition();
  424. var prevScale = Scale;
  425. var scale = prevScale * 1.1f;
  426. if (scale.LengthSquared() <= 2000)
  427. {
  428. Scale = scale;
  429. Position -= pos * 0.1f * prevScale;
  430. }
  431. else
  432. {
  433. GD.Print("太大了");
  434. }
  435. }
  436.  
  437. //绘制单个自动贴图
  438. private void SetSingleAutoCell(Vector2I position)
  439. {
  440. SetCell(GetFloorLayer(), position, _sourceId, _autoTileConfig.Floor.AutoTileCoord);
  441. if (!_autoCellLayerGrid.Contains(position.X, position.Y))
  442. {
  443. ResetGenerateTimer();
  444. _autoCellLayerGrid.Set(position.X, position.Y, true);
  445. }
  446. }
  447. //绘制区域自动贴图
  448. private void SetRectAutoCell(Vector2I start, Vector2I end)
  449. {
  450. ResetGenerateTimer();
  451. if (start.X > end.X)
  452. {
  453. var temp = end.X;
  454. end.X = start.X;
  455. start.X = temp;
  456. }
  457. if (start.Y > end.Y)
  458. {
  459. var temp = end.Y;
  460. end.Y = start.Y;
  461. start.Y = temp;
  462. }
  463.  
  464. var width = end.X - start.X + 1;
  465. var height = end.Y - start.Y + 1;
  466. for (var i = 0; i < width; i++)
  467. {
  468. for (var j = 0; j < height; j++)
  469. {
  470. SetCell(GetFloorLayer(), new Vector2I(start.X + i, start.Y + j), _sourceId, _autoTileConfig.Floor.AutoTileCoord);
  471. }
  472. }
  473.  
  474. _autoCellLayerGrid.SetRect(start, new Vector2I(width, height), true);
  475. }
  476.  
  477. //擦除单个自动图块
  478. private void EraseSingleAutoCell(Vector2I position)
  479. {
  480. EraseCell(GetFloorLayer(), position);
  481. if (_autoCellLayerGrid.Remove(position.X, position.Y))
  482. {
  483. ResetGenerateTimer();
  484. }
  485. }
  486. //擦除一个区域内的自动贴图
  487. private void EraseRectAutoCell(Vector2I start, Vector2I end)
  488. {
  489. ResetGenerateTimer();
  490. if (start.X > end.X)
  491. {
  492. var temp = end.X;
  493. end.X = start.X;
  494. start.X = temp;
  495. }
  496. if (start.Y > end.Y)
  497. {
  498. var temp = end.Y;
  499. end.Y = start.Y;
  500. start.Y = temp;
  501. }
  502.  
  503. var width = end.X - start.X + 1;
  504. var height = end.Y - start.Y + 1;
  505. for (var i = 0; i < width; i++)
  506. {
  507. for (var j = 0; j < height; j++)
  508. {
  509. EraseCell(GetFloorLayer(), new Vector2I(start.X + i, start.Y + j));
  510. }
  511. }
  512. _autoCellLayerGrid.RemoveRect(start, new Vector2I(width, height));
  513. }
  514.  
  515. //重置计时器
  516. private void ResetGenerateTimer()
  517. {
  518. _generateTimer = _generateInterval;
  519. _isGenerateTerrain = false;
  520. _dungeonTileMap.ClearPolygonData();
  521. ClearLayer(AutoTopLayer);
  522. ClearLayer(AutoMiddleLayer);
  523. }
  524.  
  525. //从新计算房间区域
  526. private void CalcTileRect()
  527. {
  528. var rect = GetUsedRect();
  529. _roomPosition = rect.Position;
  530. _roomSize = rect.Size;
  531. }
  532. //检测是否有不合规的图块, 返回true表示图块正常
  533. private bool CheckTerrain()
  534. {
  535. var x = _roomPosition.X;
  536. var y = _roomPosition.Y;
  537. var w = _roomSize.X;
  538. var h = _roomSize.Y;
  539.  
  540. for (var i = 0; i < w; i++)
  541. {
  542. for (var j = 0; j < h; j++)
  543. {
  544. var pos = new Vector2I(x + i, y + j);
  545. if (GetCellSourceId(AutoFloorLayer, pos) == -1)
  546. {
  547. //先检测对角是否有地板
  548. var left = _autoCellLayerGrid.Get(pos.X - 1, pos.Y);
  549. var right = _autoCellLayerGrid.Get(pos.X + 1, pos.Y);
  550. var top = _autoCellLayerGrid.Get(pos.X, pos.Y + 1);
  551. var down = _autoCellLayerGrid.Get(pos.X, pos.Y - 1);
  552. if ((left && right) || (top && down))
  553. {
  554. _checkTerrainFlag = false;
  555. _checkTerrainErrorPosition = pos;
  556. return false;
  557. }
  558. }
  559. }
  560. }
  561.  
  562. _checkTerrainFlag = true;
  563. return true;
  564. }
  565. //生成自动图块 (地形)
  566. private void GenerateTerrain()
  567. {
  568. ClearLayer(AutoFloorLayer);
  569. var list = new List<Vector2I>();
  570. _autoCellLayerGrid.ForEach((x, y, data) =>
  571. {
  572. if (data)
  573. {
  574. list.Add(new Vector2I(x, y));
  575. }
  576. });
  577. var arr = new Array<Vector2I>(list);
  578. SetCellsTerrainConnect(AutoFloorLayer, arr, _terrainSet, _terrain, false);
  579. MoveTerrainCell();
  580. }
  581.  
  582. //将自动生成的图块从 AutoFloorLayer 移动到指定图层中
  583. private void MoveTerrainCell()
  584. {
  585. ClearLayer(AutoTopLayer);
  586. ClearLayer(AutoMiddleLayer);
  587. var x = _roomPosition.X;
  588. var y = _roomPosition.Y;
  589. var w = _roomSize.X;
  590. var h = _roomSize.Y;
  591.  
  592. for (var i = 0; i < w; i++)
  593. {
  594. for (var j = 0; j < h; j++)
  595. {
  596. var pos = new Vector2I(x + i, y + j);
  597. if (!_autoCellLayerGrid.Contains(pos) && GetCellSourceId(AutoFloorLayer, pos) != -1)
  598. {
  599. var atlasCoords = GetCellAtlasCoords(AutoFloorLayer, pos);
  600. var layer = _autoTileConfig.GetLayer(atlasCoords);
  601. if (layer == GameConfig.MiddleMapLayer)
  602. {
  603. layer = AutoMiddleLayer;
  604. }
  605. else if (layer == GameConfig.TopMapLayer)
  606. {
  607. layer = AutoTopLayer;
  608. }
  609. else
  610. {
  611. GD.PrintErr($"异常图块: {pos}, 这个图块的图集坐标'{atlasCoords}'不属于'MiddleMapLayer'和'TopMapLayer'!");
  612. continue;
  613. }
  614. EraseCell(AutoFloorLayer, pos);
  615. SetCell(layer, pos, _sourceId, atlasCoords);
  616. }
  617. }
  618. }
  619. }
  620.  
  621. //生成导航网格
  622. private bool GenerateNavigation()
  623. {
  624. _dungeonTileMap.GenerateNavigationPolygon(AutoFloorLayer);
  625. var result = _dungeonTileMap.GetGenerateNavigationResult();
  626. if (result.Success)
  627. {
  628. CloseErrorCell();
  629. }
  630. else
  631. {
  632. SetErrorCell(result.Exception.Point);
  633. }
  634.  
  635. return result.Success;
  636. }
  637.  
  638. //设置显示的错误cell, 会标记上红色的闪烁动画
  639. private void SetErrorCell(Vector2I pos)
  640. {
  641. MapEditorPanel.S_ErrorCell.Instance.Position = pos * CellQuadrantSize;
  642. MapEditorPanel.S_ErrorCellAnimationPlayer.Instance.Play(AnimatorNames.Show);
  643. }
  644.  
  645. //关闭显示的错误cell
  646. private void CloseErrorCell()
  647. {
  648. MapEditorPanel.S_ErrorCellAnimationPlayer.Instance.Stop();
  649. }
  650.  
  651. private int GetFloorLayer()
  652. {
  653. return AutoFloorLayer;
  654. }
  655.  
  656. private int GetMiddleLayer()
  657. {
  658. return AutoMiddleLayer;
  659. }
  660.  
  661. private int GetTopLayer()
  662. {
  663. return AutoTopLayer;
  664. }
  665.  
  666. /// <summary>
  667. /// 选中拖拽功能
  668. /// </summary>
  669. public void OnSelectHandTool()
  670. {
  671. }
  672. /// <summary>
  673. /// 选中画笔攻击
  674. /// </summary>
  675. public void OnSelectPenTool()
  676. {
  677. }
  678.  
  679. /// <summary>
  680. /// 选中绘制区域功能
  681. /// </summary>
  682. public void OnSelectRectTool()
  683. {
  684. }
  685.  
  686. /// <summary>
  687. /// 聚焦
  688. /// </summary>
  689. public void OnClickCenterTool()
  690. {
  691. var pos = MapEditorPanel.S_SubViewport.Instance.Size / 2;
  692. if (_roomSize.X == 0 && _roomSize.Y == 0) //聚焦原点
  693. {
  694. Position = pos;
  695. }
  696. else //聚焦地图中心点
  697. {
  698. Position = pos - (_roomPosition + _roomSize / 2) * TileSet.TileSize * Scale;
  699. }
  700. }
  701.  
  702. //保存房间配置
  703. private void SaveRoomInfoConfig()
  704. {
  705. //存入本地
  706. var path = GetConfigPath(_dir, _groupName, _roomType, _fileName);
  707. if (!Directory.Exists(path))
  708. {
  709. Directory.CreateDirectory(path);
  710. }
  711. var roomInfo = new DungeonRoomInfo();
  712. roomInfo.Position = new SerializeVector2(_roomPosition);
  713. roomInfo.Size = new SerializeVector2(_roomSize);
  714. roomInfo.DoorAreaInfos = _doorConfigs;
  715. roomInfo.RoomType = _roomType;
  716. roomInfo.GroupName = _groupName;
  717. roomInfo.FileName = _fileName;
  718. roomInfo.Weight = _weight;
  719.  
  720. path += "/" + GetRoomInfoConfigName(_fileName);
  721. var jsonStr = JsonSerializer.Serialize(roomInfo);
  722. File.WriteAllText(path, jsonStr);
  723. }
  724.  
  725. //保存地块数据
  726. public void SaveTileInfoConfig()
  727. {
  728. //存入本地
  729. var path = GetConfigPath(_dir, _groupName, _roomType, _fileName);
  730. if (!Directory.Exists(path))
  731. {
  732. Directory.CreateDirectory(path);
  733. }
  734. var tileInfo = new DungeonTileInfo();
  735. tileInfo.NavigationList = _dungeonTileMap.GetPolygonData().ToList();
  736. tileInfo.Floor = new List<int>();
  737. tileInfo.Middle = new List<int>();
  738. tileInfo.Top = new List<int>();
  739.  
  740. PushLayerDataToList(AutoFloorLayer, _sourceId, tileInfo.Floor);
  741. PushLayerDataToList(AutoMiddleLayer, _sourceId, tileInfo.Middle);
  742. PushLayerDataToList(AutoTopLayer, _sourceId, tileInfo.Top);
  743. path += "/" + GetTileInfoConfigName(_fileName);
  744. var jsonStr = JsonSerializer.Serialize(tileInfo);
  745. File.WriteAllText(path, jsonStr);
  746. }
  747.  
  748. private string GetConfigPath(string dir, string groupName, DungeonRoomType roomType, string fileName)
  749. {
  750. return dir + groupName + "/" + DungeonManager.DungeonRoomTypeToString(roomType) + "/" + fileName;
  751. }
  752.  
  753. private string GetTileInfoConfigName(string roomName)
  754. {
  755. return roomName + "_tileInfo.json";
  756. }
  757. private string GetRoomInfoConfigName(string roomName)
  758. {
  759. return roomName + "_roomInfo.json";
  760. }
  761. }