Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditor / tileView / EditorTileMap.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Godot;
  5. using Godot.Collections;
  6. using UI.MapEditorTools;
  7.  
  8. namespace UI.MapEditor;
  9.  
  10. public partial class EditorTileMap : TileMap, IUiNodeScript
  11. {
  12. public enum MouseButtonType
  13. {
  14. /// <summary>
  15. /// 无状态
  16. /// </summary>
  17. None,
  18. /// <summary>
  19. /// 拖拽模式
  20. /// </summary>
  21. Drag,
  22. /// <summary>
  23. /// 笔
  24. /// </summary>
  25. Pen,
  26. /// <summary>
  27. /// 绘制区域模式
  28. /// </summary>
  29. Area,
  30. /// <summary>
  31. /// 编辑工具模式
  32. /// </summary>
  33. Edit,
  34. }
  35. public enum TileMapDrawMode
  36. {
  37. /// <summary>
  38. /// 无状态
  39. /// </summary>
  40. None,
  41. /// <summary>
  42. /// 自由绘制
  43. /// </summary>
  44. Free,
  45. /// <summary>
  46. /// 地形绘制
  47. /// </summary>
  48. Terrain,
  49. /// <summary>
  50. /// 组合绘制
  51. /// </summary>
  52. Combination
  53. }
  54.  
  55. /// <summary>
  56. /// 所属地图编辑器UI
  57. /// </summary>
  58. public MapEditorPanel MapEditorPanel { get; private set; }
  59. /// <summary>
  60. /// 编辑器工具UI
  61. /// </summary>
  62. public MapEditorToolsPanel MapEditorToolsPanel { get; set; }
  63. /// <summary>
  64. /// 左键功能
  65. /// </summary>
  66. public MouseButtonType MouseType { get; private set; } = MouseButtonType.Pen;
  67. //鼠标坐标
  68. private Vector2 _mousePosition;
  69. //鼠标所在的cell坐标
  70. private Vector2I _mouseCellPosition;
  71. //上一帧鼠标所在的cell坐标
  72. private Vector2I _prevMouseCellPosition = new Vector2I(-99999, -99999);
  73. //单次绘制是否改变过tile数据
  74. private bool _changeFlag = false;
  75. //左键开始按下时鼠标所在的坐标
  76. private Vector2I _mouseStartCellPosition;
  77. //鼠标中建是否按下
  78. private bool _isMiddlePressed = false;
  79. private Vector2 _moveOffset;
  80. //左键是否按下
  81. private bool _isLeftPressed = false;
  82. //右键是否按下
  83. private bool _isRightPressed = false;
  84. //绘制填充区域
  85. private bool _drawFullRect = false;
  86. //负责存储自动图块数据
  87. private InfiniteGrid<bool> _autoCellLayerGrid = new InfiniteGrid<bool>();
  88. //停止绘制多久后开始执行生成操作
  89. private float _generateInterval = 3f;
  90. //生成自动图块和导航网格的计时器
  91. private float _generateTimer = -1;
  92. //检测地形结果
  93. private bool _checkTerrainFlag = true;
  94. //错误地形位置
  95. private Vector2I _checkTerrainErrorPosition = Vector2I.Zero;
  96. //是否执行生成地形成功
  97. private bool _isGenerateTerrain = true;
  98. //导航网格数据
  99. private Vector2[][] _polygonData;
  100. private bool _initLayer = false;
  101.  
  102. //--------- 配置数据 -------------
  103. private int _mainSource = 0;
  104. private int _mainTerrainSet = 0;
  105. private int _mainTerrain = 0;
  106. private AutoTileConfig _autoTileConfig;
  107.  
  108. /// <summary>
  109. /// 正在编辑的房间数据
  110. /// </summary>
  111. public DungeonRoomSplit CurrRoomSplit;
  112. /// <summary>
  113. /// 数据是否脏了, 也就是是否有修改
  114. /// </summary>
  115. public bool IsDirty { get; private set; }
  116.  
  117. /// <summary>
  118. /// 地图是否有绘制错误
  119. /// </summary>
  120. public bool HasTerrainError => !_isGenerateTerrain;
  121. /// <summary>
  122. /// 当前选择的图层
  123. /// </summary>
  124. public TileMapLayerData CurrLayer { get; private set; }
  125. /// <summary>
  126. /// 当前正在使用的 TileSetSplit 数据
  127. /// </summary>
  128. public TileSetSplit CurrentTileSet { get; private set; }
  129. /// <summary>
  130. /// 当前正在使用的 Source
  131. /// </summary>
  132. public TileSetSourceInfo CurrSource => CurrentTileSet.TileSetInfo.Sources[CurrSourceIndex];
  133.  
  134. /// <summary>
  135. /// 当前正在使用的 Source 索引
  136. /// </summary>
  137. public int CurrSourceIndex { get; private set; }
  138. /// <summary>
  139. /// 当前笔刷类型
  140. /// </summary>
  141. public TileMapDrawMode CurrBrushType { get; private set; }
  142.  
  143. /// <summary>
  144. /// 当前笔刷使用的 AtlasCoords,, key: position, value: atlasCoords, 单位: 格
  145. /// </summary>
  146. public System.Collections.Generic.Dictionary<Vector2I, Vector2I> CurrBrush { get; } = new System.Collections.Generic.Dictionary<Vector2I, Vector2I>();
  147. /// <summary>
  148. /// 当前地形
  149. /// </summary>
  150. public TerrainData CurrTerrain { get; private set; }
  151. /// <summary>
  152. /// 当前笔刷使用的纹理
  153. /// </summary>
  154. public Texture2D CurrBrushTexture { get; private set; }
  155.  
  156. private int _brushStartX = 0;
  157. private int _brushStartY = 0;
  158. //笔刷宽度, 单位: 格
  159. private int _brushWidth = 0;
  160. //笔刷高度, 单位: 格
  161. private int _brushHeight = 0;
  162. //笔刷偏移, 单位: 格
  163. private Vector2I _brushOffset = Vector2I.Zero;
  164. //淡化其它层级
  165. private bool _desaltOtherLayer = false;
  166. //--------------------------------------- 变动过的数据 ---------------------------------------
  167. /// <summary>
  168. /// 地图位置, 单位: 格
  169. /// </summary>
  170. public Vector2I CurrRoomPosition { get; private set; }
  171. /// <summary>
  172. /// 当前地图大小, 单位: 格
  173. /// </summary>
  174. public Vector2I CurrRoomSize { get; private set; }
  175. /// <summary>
  176. /// 当前编辑的门数据
  177. /// </summary>
  178. public List<DoorAreaInfo> CurrDoorConfigs { get; } = new List<DoorAreaInfo>();
  179.  
  180. /// <summary>
  181. /// 是否绘制房间中的辅助标记
  182. /// </summary>
  183. public bool IsDrawMark { get; set; } = true;
  184.  
  185. //-------------------------------
  186. private MapEditor.TileMap _editorTileMap;
  187. private EventFactory _eventFactory;
  188. private Vector2I _cacheToolSizeData;
  189. public void SetUiNode(IUiNode uiNode)
  190. {
  191. _editorTileMap = (MapEditor.TileMap)uiNode;
  192. MapEditorPanel = _editorTileMap.UiPanel;
  193. MapEditorToolsPanel = _editorTileMap.UiPanel.S_MapEditorTools.Instance;
  194.  
  195. _editorTileMap.L_Brush.Instance.Draw += DrawGuides;
  196. _eventFactory = EventManager.CreateEventFactory();
  197. _eventFactory.AddEventListener(EventEnum.OnSelectDragTool, OnSelectHandTool);
  198. _eventFactory.AddEventListener(EventEnum.OnSelectPenTool, OnSelectPenTool);
  199. _eventFactory.AddEventListener(EventEnum.OnSelectRectTool, OnSelectRectTool);
  200. _eventFactory.AddEventListener(EventEnum.OnSelectEditTool, OnSelectEditTool);
  201. _eventFactory.AddEventListener(EventEnum.OnClickCenterTool, OnClickCenterTool);
  202. _eventFactory.AddEventListener(EventEnum.OnTileMapDirty, OnEditorDirty);
  203.  
  204. RenderingServer.FramePostDraw += OnFramePostDraw;
  205. var navigationRegion = _editorTileMap.L_NavigationRegion.Instance;
  206. navigationRegion.Visible = false;
  207. navigationRegion.NavigationPolygon.AgentRadius = GameConfig.NavigationAgentRadius;
  208. navigationRegion.BakeFinished += OnBakeFinished;
  209. }
  210.  
  211. public void OnDestroy()
  212. {
  213. _eventFactory.RemoveAllEventListener();
  214. RenderingServer.FramePostDraw -= OnFramePostDraw;
  215. }
  216.  
  217. public override void _Process(double delta)
  218. {
  219. if (!_initLayer)
  220. {
  221. return;
  222. }
  223. //触发绘制辅助线
  224. _editorTileMap.L_Brush.Instance.QueueRedraw();
  225. var newDelta = (float)delta;
  226. _drawFullRect = false;
  227. var position = GetLocalMousePosition();
  228. //绘制2x2地形
  229. if (CurrBrushType == TileMapDrawMode.Terrain && CurrTerrain != null && CurrTerrain.TerrainInfo.TerrainType == 1 && !_isRightPressed)
  230. {
  231. position -= new Vector2(GameConfig.TileCellSize / 2f, GameConfig.TileCellSize / 2f);
  232. }
  233. _mouseCellPosition = LocalToMap(position);
  234. _mousePosition = new Vector2(
  235. _mouseCellPosition.X * GameConfig.TileCellSize,
  236. _mouseCellPosition.Y * GameConfig.TileCellSize
  237. );
  238. if (!MapEditorToolsPanel.S_HBoxContainer.Instance.IsMouseInRect()) //不在Ui节点上
  239. {
  240. //左键绘制
  241. if (_isLeftPressed)
  242. {
  243. if (MouseType == MouseButtonType.Pen) //绘制单格
  244. {
  245. if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过
  246. {
  247. _changeFlag = true;
  248. _prevMouseCellPosition = _mouseCellPosition;
  249. //绘制图块
  250. SetSingleCell(_mouseCellPosition);
  251. }
  252. }
  253. else if (MouseType == MouseButtonType.Area) //绘制区域
  254. {
  255. _drawFullRect = true;
  256. }
  257. else if (MouseType == MouseButtonType.Drag) //拖拽
  258. {
  259. SetMapPosition(GetGlobalMousePosition() + _moveOffset);
  260. }
  261. }
  262. else if (_isRightPressed) //右键擦除
  263. {
  264. if (MouseType == MouseButtonType.Pen) //绘制单格
  265. {
  266. if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过
  267. {
  268. _changeFlag = true;
  269. _prevMouseCellPosition = _mouseCellPosition;
  270. EraseSingleCell(_mouseCellPosition);
  271. }
  272. }
  273. else if (MouseType == MouseButtonType.Area) //绘制区域
  274. {
  275. _drawFullRect = true;
  276. }
  277. else if (MouseType == MouseButtonType.Drag) //拖拽
  278. {
  279. SetMapPosition(GetGlobalMousePosition() + _moveOffset);
  280. }
  281. }
  282. else if (_isMiddlePressed) //中键移动
  283. {
  284. SetMapPosition(GetGlobalMousePosition() + _moveOffset);
  285. }
  286. }
  287.  
  288. //绘制停止指定时间后, 生成导航网格
  289. if (_generateTimer > 0)
  290. {
  291. _generateTimer -= newDelta;
  292. if (_generateTimer <= 0)
  293. {
  294. //检测地形
  295. RunCheckHandler();
  296. }
  297. }
  298. }
  299.  
  300. /// <summary>
  301. /// 绘制辅助线
  302. /// </summary>
  303. public void DrawGuides()
  304. {
  305. if (_hasPreviewImage)
  306. {
  307. return;
  308. }
  309. CanvasItem canvasItem = _editorTileMap.L_Brush.Instance;
  310. //轴线
  311. canvasItem.DrawLine(new Vector2(0, 2000), new Vector2(0, -2000), Colors.Green);
  312. canvasItem.DrawLine(new Vector2(2000, 0), new Vector2( -2000, 0), Colors.Red);
  313.  
  314. if (IsDrawMark)
  315. {
  316. //绘制房间区域
  317. if (CurrRoomSize.X != 0 && CurrRoomSize.Y != 0)
  318. {
  319. canvasItem.DrawRect(
  320. new Rect2(
  321. (CurrRoomPosition + new Vector2I(1, 2)) * GameConfig.TileCellSize,
  322. (CurrRoomSize - new Vector2I(2, 3)) * GameConfig.TileCellSize
  323. ),
  324. Colors.Aqua, false, 5f / Scale.X
  325. );
  326. }
  327.  
  328. //绘制导航网格
  329. if (_checkTerrainFlag && _isGenerateTerrain && _polygonData != null)
  330. {
  331. foreach (var vector2s in _polygonData)
  332. {
  333. canvasItem.DrawPolygon(vector2s, new Color(0,1,1, 0.3f).MakeArray(vector2s.Length));
  334. }
  335. }
  336. }
  337.  
  338. //绘制笔刷
  339. if (MouseType == MouseButtonType.Pen || MouseType == MouseButtonType.Area)
  340. {
  341. if (_drawFullRect) //绘制填充矩形
  342. {
  343. var size = TileSet.TileSize;
  344. var cellPos = _mouseStartCellPosition;
  345. var temp = size;
  346. if (_mouseStartCellPosition.X > _mouseCellPosition.X)
  347. {
  348. cellPos.X += 1;
  349. temp.X -= size.X;
  350. }
  351. if (_mouseStartCellPosition.Y > _mouseCellPosition.Y)
  352. {
  353. cellPos.Y += 1;
  354. temp.Y -= size.Y;
  355. }
  356.  
  357. var p = cellPos * size;
  358. var s = _mousePosition.AsVector2I() - p + temp;
  359. if (s.X < 0)
  360. {
  361. p.X += s.X;
  362. s.X *= -1;
  363. }
  364.  
  365. if (s.Y < 0)
  366. {
  367. p.Y += s.Y;
  368. s.Y *= -1;
  369. }
  370. //绘制边框
  371. canvasItem.DrawRect(new Rect2(p, s), Colors.White, false, 2f / Scale.X);
  372.  
  373. if (CurrLayer.Layer != MapLayer.AutoFloorLayer && (CurrBrushType == TileMapDrawMode.Free || CurrBrushType == TileMapDrawMode.Combination)) //自由绘制 或者 绘制组合
  374. {
  375. if (_isLeftPressed) //左键绘制
  376. {
  377. var w = s.X / GameConfig.TileCellSize;
  378. var h = s.Y / GameConfig.TileCellSize;
  379. for (var i = 0; i < w; i++)
  380. {
  381. for (var j = 0; j < h; j++)
  382. {
  383. var x = i % _brushWidth + _brushStartX;
  384. var y = j % _brushHeight + _brushStartY;
  385. if (CurrBrush.TryGetValue(new Vector2I(x, y), out var v))
  386. {
  387. var rect = new Rect2(p + (new Vector2I(i, j)) * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize);
  388. var srcRect = new Rect2(v * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize);
  389. canvasItem.DrawTextureRectRegion(CurrBrushTexture, rect, srcRect, new Color(1, 1, 1, 0.3f));
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. else //绘制单格
  397. {
  398. if (CurrLayer.Layer == MapLayer.AutoFloorLayer) //选择自动地板层
  399. {
  400. DrawCellOutline(canvasItem);
  401. }
  402. else //自定义层
  403. {
  404. if (CurrBrushType == TileMapDrawMode.Free || CurrBrushType == TileMapDrawMode.Combination) //自由绘制 或者 绘制组合
  405. {
  406. if (_isRightPressed) //按下了左键擦除
  407. {
  408. DrawCellOutline(canvasItem);
  409. }
  410. else //正常绘制
  411. {
  412. foreach (var item in CurrBrush)
  413. {
  414. var rect = new Rect2(_mousePosition + (item.Key + _brushOffset) * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize);
  415. var srcRect = new Rect2(item.Value * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize);
  416. canvasItem.DrawTextureRectRegion(CurrBrushTexture, rect, srcRect, new Color(1, 1, 1, 0.3f));
  417. }
  418. }
  419. }
  420. else if (CurrBrushType == TileMapDrawMode.Terrain) //绘制地形
  421. {
  422. if (CurrTerrain == null) //未选择地形
  423. {
  424. DrawCellOutline(canvasItem);
  425. }
  426. else if (CurrTerrain.TerrainInfo.TerrainType == 0) //3x3
  427. {
  428. DrawCellOutline(canvasItem);
  429. }
  430. else //2x2
  431. {
  432. DrawCellOutline(canvasItem);
  433. if (!_isRightPressed) //没按下左键擦除
  434. {
  435. DrawCellOutline(canvasItem, new Vector2I(GameConfig.TileCellSize, 0));
  436. DrawCellOutline(canvasItem, new Vector2I(0, GameConfig.TileCellSize));
  437. DrawCellOutline(canvasItem, new Vector2I(GameConfig.TileCellSize, GameConfig.TileCellSize));
  438. }
  439. }
  440. }
  441. }
  442. }
  443. }
  444. }
  445.  
  446. private void DrawCellOutline(CanvasItem canvasItem)
  447. {
  448. canvasItem.DrawRect(new Rect2(_mousePosition, TileSet.TileSize), Colors.White, false, 2f / Scale.X);
  449. }
  450. private void DrawCellOutline(CanvasItem canvasItem, Vector2I offset)
  451. {
  452. canvasItem.DrawRect(new Rect2(_mousePosition + offset, TileSet.TileSize), Colors.White, false, 2f / Scale.X);
  453. }
  454.  
  455. public override void _Input(InputEvent @event)
  456. {
  457. if (@event is InputEventMouseButton mouseButton)
  458. {
  459. if (mouseButton.ButtonIndex == MouseButton.Left) //左键
  460. {
  461. if (mouseButton.Pressed) //按下
  462. {
  463. _moveOffset = Position - GetGlobalMousePosition();
  464. _mouseStartCellPosition = LocalToMap(GetLocalMousePosition());
  465. }
  466. else
  467. {
  468. _changeFlag = false;
  469. if (_drawFullRect) //松开, 提交绘制的矩形区域
  470. {
  471. SetRectCell(_mouseStartCellPosition, _mouseCellPosition);
  472. _drawFullRect = false;
  473. }
  474. }
  475.  
  476. _isLeftPressed = mouseButton.Pressed;
  477. }
  478. else if (mouseButton.ButtonIndex == MouseButton.Right) //右键
  479. {
  480. if (mouseButton.Pressed) //按下
  481. {
  482. _moveOffset = Position - GetGlobalMousePosition();
  483. _mouseStartCellPosition = LocalToMap(GetLocalMousePosition());
  484. }
  485. else
  486. {
  487. _changeFlag = false;
  488. if (_drawFullRect) //松开, 提交擦除的矩形区域
  489. {
  490. EraseRectCell(_mouseStartCellPosition, _mouseCellPosition);
  491. _drawFullRect = false;
  492. }
  493. }
  494. _isRightPressed = mouseButton.Pressed;
  495. }
  496. else if (mouseButton.ButtonIndex == MouseButton.WheelDown)
  497. {
  498. //缩小
  499. Shrink();
  500. }
  501. else if (mouseButton.ButtonIndex == MouseButton.WheelUp)
  502. {
  503. //放大
  504. Magnify();
  505. }
  506. else if (mouseButton.ButtonIndex == MouseButton.Middle)
  507. {
  508. _isMiddlePressed = mouseButton.Pressed;
  509. if (_isMiddlePressed)
  510. {
  511. _moveOffset = Position - GetGlobalMousePosition();
  512. }
  513. }
  514. }
  515. }
  516.  
  517. /// <summary>
  518. /// 设置选择的layer
  519. /// </summary>
  520. public void SetCurrentLayer(TileMapLayerData layerData)
  521. {
  522. CurrLayer = layerData;
  523. MapEditorToolsPanel.S_CurrLayer.Instance.Text = "当前图层:" + layerData.Title;
  524. SetLayerModulate(MapLayer.AutoFloorLayer, GetEditorLayerModulate(MapLayer.AutoFloorLayer));
  525. SetLayerModulate(MapLayer.AutoMiddleLayer, GetEditorLayerModulate(MapLayer.AutoMiddleLayer));
  526. SetLayerModulate(MapLayer.AutoTopLayer, GetEditorLayerModulate(MapLayer.AutoTopLayer));
  527. SetLayerModulate(MapLayer.CustomFloorLayer1, GetEditorLayerModulate(MapLayer.CustomFloorLayer1));
  528. SetLayerModulate(MapLayer.CustomFloorLayer2, GetEditorLayerModulate(MapLayer.CustomFloorLayer2));
  529. SetLayerModulate(MapLayer.CustomFloorLayer3, GetEditorLayerModulate(MapLayer.CustomFloorLayer3));
  530. SetLayerModulate(MapLayer.CustomMiddleLayer1, GetEditorLayerModulate(MapLayer.CustomMiddleLayer1));
  531. SetLayerModulate(MapLayer.CustomMiddleLayer2, GetEditorLayerModulate(MapLayer.CustomMiddleLayer2));
  532. SetLayerModulate(MapLayer.CustomTopLayer, GetEditorLayerModulate(MapLayer.CustomTopLayer));
  533. }
  534.  
  535. /// <summary>
  536. /// 设置选中的 Source 索引
  537. /// </summary>
  538. public void SetCurrSourceIndex(int index)
  539. {
  540. CurrSourceIndex = index;
  541. }
  542.  
  543. /// <summary>
  544. /// 设置当前笔刷类型
  545. /// </summary>
  546. public void SetCurrBrushType(TileMapDrawMode mode)
  547. {
  548. CurrBrushType = mode;
  549. }
  550.  
  551. /// <summary>
  552. /// 添加笔刷绘制的数据, 单位: 格
  553. /// </summary>
  554. public void AddCurrBrushAtlasCoords(Vector2I pos, Vector2I atlasCoords)
  555. {
  556. if (!CurrBrush.ContainsKey(pos))
  557. {
  558. CurrBrush.Add(pos, atlasCoords);
  559. var xStart = int.MaxValue;
  560. var xEnd = int.MinValue;
  561. var yStart = int.MaxValue;
  562. var yEnd = int.MinValue;
  563.  
  564. //计算起始点和终点
  565. foreach (var kv in CurrBrush)
  566. {
  567. var cell = kv.Key;
  568. xStart = Mathf.Min(cell.X, xStart);
  569. yStart = Mathf.Min(cell.Y, yStart);
  570. xEnd = Mathf.Max(cell.X, xEnd);
  571. yEnd = Mathf.Max(cell.Y, yEnd);
  572. }
  573.  
  574. _brushStartX = xStart;
  575. _brushStartY = yStart;
  576. _brushWidth = xEnd - xStart + 1;
  577. _brushHeight = yEnd - yStart + 1;
  578. _brushOffset = new Vector2I(-(xStart + (xEnd - xStart) / 2), -(yStart + (yEnd - yStart) / 2));
  579. }
  580. }
  581. /// <summary>
  582. /// 移除笔刷绘制的数据, 单位: 格
  583. /// </summary>
  584. public void RemoveCurrBrushAtlasCoords(Vector2I pos)
  585. {
  586. CurrBrush.Remove(pos);
  587. }
  588. /// <summary>
  589. /// 清除笔刷
  590. /// </summary>
  591. public void ClearCurrBrushAtlasCoords()
  592. {
  593. CurrBrush.Clear();
  594. }
  595.  
  596. /// <summary>
  597. /// 设置笔刷使用的纹理
  598. /// </summary>
  599. public void SetCurrBrushTexture(Texture2D texture)
  600. {
  601. CurrBrushTexture = texture;
  602. }
  603.  
  604. /// <summary>
  605. /// 设置选中某个图层时是否淡化其他图层
  606. /// </summary>
  607. public void SetDesaltOtherLayer(bool flag)
  608. {
  609. _desaltOtherLayer = flag;
  610. SetCurrentLayer(CurrLayer);
  611. }
  612.  
  613. /// <summary>
  614. /// 尝试运行检查, 如果已经运行过了, 则没有效果
  615. /// </summary>
  616. public void TryRunCheckHandler()
  617. {
  618. if (_generateTimer > 0)
  619. {
  620. _generateTimer = -1;
  621. RunCheckHandler();
  622. }
  623. }
  624.  
  625. /// <summary>
  626. /// 设置当前地形
  627. /// </summary>
  628. public void SetCurrTerrain(TerrainData terrainData)
  629. {
  630. CurrBrushType = TileMapDrawMode.Terrain;
  631. CurrTerrain = terrainData;
  632. }
  633. //执行检测地形操作
  634. private void RunCheckHandler()
  635. {
  636. _isGenerateTerrain = false;
  637. //计算区域
  638. CalcTileRect(false);
  639. Debug.Log("开始检测是否可以生成地形...");
  640. if (CheckTerrain())
  641. {
  642. Debug.Log("开始绘制自动贴图...");
  643. GenerateTerrain();
  644. _isGenerateTerrain = true;
  645. }
  646. else
  647. {
  648. SetErrorCell(_checkTerrainErrorPosition);
  649. }
  650. }
  651.  
  652. //将指定自动地形层数据存入list中
  653. private void PushAutoLayerDataToList(int layer, List<int> list)
  654. {
  655. const int sourceId = 0; //这里指定0是因为 Main Source 的 id 为 0
  656. var layerArray = GetUsedCellsById(layer, sourceId);
  657. foreach (var pos in layerArray)
  658. {
  659. var atlasCoords = GetCellAtlasCoords(layer, pos);
  660. var tileCellData = _autoTileConfig.GetCellData(atlasCoords);
  661. if (tileCellData != null)
  662. {
  663. list.Add(pos.X);
  664. list.Add(pos.Y);
  665. list.Add((int)tileCellData.TerrainPeering);
  666. list.Add(tileCellData.TerrainType);
  667. }
  668. }
  669. }
  670. //将指定层数据存入list中
  671. private void PushLayerDataToList(int layer, List<int> list)
  672. {
  673. var layerArray = GetUsedCellsById(layer);
  674. foreach (var pos in layerArray)
  675. {
  676. var atlasCoords = GetCellAtlasCoords(layer, pos);
  677. list.Add(pos.X);
  678. list.Add(pos.Y);
  679. list.Add(GetCellSourceId(layer, pos));
  680. list.Add(atlasCoords.X);
  681. list.Add(atlasCoords.Y);
  682. }
  683. }
  684.  
  685. //设置自动地形层的数据
  686. private void SetAutoLayerDataFromList(int layer, List<int> list)
  687. {
  688. var terrainInfo = _autoTileConfig.TerrainInfo;
  689. var sourceId = _autoTileConfig.SourceId;
  690. for (var i = 0; i < list.Count; i += 4)
  691. {
  692. var pos = new Vector2I(list[i], list[i + 1]);
  693. var bit = (uint)list[i + 2];
  694. var type = (byte)list[i + 3];
  695. var index = terrainInfo.TerrainBitToIndex(bit, type);
  696. var terrainCell = terrainInfo.GetTerrainCell(index, type);
  697. var atlasCoords = terrainInfo.GetPosition(terrainCell);
  698. SetCell(layer, pos, sourceId, atlasCoords);
  699. if (layer == MapLayer.AutoFloorLayer)
  700. {
  701. _autoCellLayerGrid.Set(pos, true);
  702. }
  703. }
  704. }
  705. //设置自定义层的数据
  706. private void SetCustomLayerDataFromList(int layer, List<int> list)
  707. {
  708. //五个一组
  709. for (var i = 0; i < list.Count; i += 5)
  710. {
  711. var pos = new Vector2I(list[i], list[i + 1]);
  712. var sourceId = list[i + 2];
  713. var atlasCoords = new Vector2I(list[i + 3], list[i + 4]);
  714. SetCell(layer, pos, sourceId, atlasCoords);
  715. }
  716. }
  717.  
  718. /// <summary>
  719. /// 触发保存地图数据
  720. /// </summary>
  721. public void TriggerSave(RoomErrorType errorType, Action finish)
  722. {
  723. Debug.Log("保存地牢房间数据...");
  724. //执行创建预览图流程
  725. RunSavePreviewImage(() =>
  726. {
  727. //执行保存数据流程
  728. CurrRoomSplit.ErrorType = errorType;
  729. SaveRoomInfoConfig();
  730. SaveTileInfoConfig();
  731. SavePreinstallConfig();
  732. IsDirty = false;
  733. MapEditorPanel.SetTitleDirty(false);
  734. //派发保存事件
  735. EventManager.EmitEvent(EventEnum.OnTileMapSave);
  736. if (finish != null)
  737. {
  738. finish();
  739. }
  740. });
  741. }
  742.  
  743. /// <summary>
  744. /// 加载地牢, 返回是否加载成功
  745. /// </summary>
  746. public bool Load(DungeonRoomSplit roomSplit, TileSetSplit tileSetSplit)
  747. {
  748. InitTileSet(tileSetSplit);
  749. //重新加载数据
  750. roomSplit.ReloadRoomInfo();
  751. roomSplit.ReloadTileInfo();
  752. roomSplit.ReloadPreinstall();
  753. CurrRoomSplit = roomSplit;
  754. var roomInfo = roomSplit.RoomInfo;
  755. var tileInfo = roomSplit.TileInfo;
  756.  
  757. CurrRoomPosition = roomInfo.Position.AsVector2I();
  758. SetMapSize(roomInfo.Size.AsVector2I(), true);
  759. CurrDoorConfigs.Clear();
  760. foreach (var doorAreaInfo in roomInfo.DoorAreaInfos)
  761. {
  762. CurrDoorConfigs.Add(doorAreaInfo.Clone());
  763. }
  764. //读取地块数据
  765. SetAutoLayerDataFromList(MapLayer.AutoFloorLayer, tileInfo.Floor);
  766. SetAutoLayerDataFromList(MapLayer.AutoMiddleLayer, tileInfo.Middle);
  767. SetAutoLayerDataFromList(MapLayer.AutoTopLayer, tileInfo.Top);
  768. SetCustomLayerDataFromList(MapLayer.CustomFloorLayer1, tileInfo.CustomFloor1);
  769. SetCustomLayerDataFromList(MapLayer.CustomFloorLayer2, tileInfo.CustomFloor2);
  770. SetCustomLayerDataFromList(MapLayer.CustomFloorLayer3, tileInfo.CustomFloor3);
  771. SetCustomLayerDataFromList(MapLayer.CustomMiddleLayer1, tileInfo.CustomMiddle1);
  772. SetCustomLayerDataFromList(MapLayer.CustomMiddleLayer2, tileInfo.CustomMiddle2);
  773. SetCustomLayerDataFromList(MapLayer.CustomTopLayer, tileInfo.CustomTop);
  774.  
  775. //如果有图块错误, 则找出错误的点位
  776. if (roomSplit.ErrorType == RoomErrorType.TileError)
  777. {
  778. RunCheckHandler();
  779. }
  780. else
  781. {
  782. //导航网格
  783. if (tileInfo.NavigationPolygon != null && tileInfo.NavigationVertices != null)
  784. {
  785. var polygon = _editorTileMap.L_NavigationRegion.Instance.NavigationPolygon;
  786. polygon.Vertices = tileInfo.NavigationVertices.Select(v => v.AsVector2()).ToArray();
  787. foreach (var p in tileInfo.NavigationPolygon)
  788. {
  789. polygon.AddPolygon(p);
  790. }
  791.  
  792. OnBakeFinished();
  793. }
  794. }
  795. //聚焦
  796. //MapEditorPanel.CallDelay(0.1f, OnClickCenterTool);
  797. //CallDeferred(nameof(OnClickCenterTool), null);
  798. //加载门编辑区域
  799. foreach (var doorAreaInfo in CurrDoorConfigs)
  800. {
  801. MapEditorToolsPanel.CreateDoorTool(doorAreaInfo);
  802. }
  803. //聚焦 (需要延时一帧调用)
  804. this.CallDelayInNode(0, () => OnClickCenterTool(null));
  805. return true;
  806. }
  807. /// <summary>
  808. /// 初始化图块集。
  809. /// </summary>
  810. /// <param name="tileSetSplit">要初始化的图块集</param>
  811. private void InitTileSet(TileSetSplit tileSetSplit)
  812. {
  813. CurrentTileSet = tileSetSplit;
  814. TileSet = tileSetSplit.GetTileSet();
  815.  
  816. // 创建AutoTileConfig对象
  817. // 使用第一个图块集源作为参数
  818. _autoTileConfig = new AutoTileConfig(0, tileSetSplit.TileSetInfo.Sources[0].Terrain[0]);
  819. }
  820.  
  821. /// <summary>
  822. /// 初始化层数据
  823. /// </summary>
  824. public void InitLayer()
  825. {
  826. if (_initLayer)
  827. {
  828. return;
  829. }
  830.  
  831. _initLayer = true;
  832. //初始化层级数据
  833. AddLayer(MapLayer.AutoFloorLayer);
  834. SetLayerZIndex(MapLayer.AutoFloorLayer, MapLayer.AutoFloorLayer);
  835. AddLayer(MapLayer.CustomFloorLayer1);
  836. SetLayerZIndex(MapLayer.CustomFloorLayer1, MapLayer.CustomFloorLayer1);
  837. AddLayer(MapLayer.CustomFloorLayer2);
  838. SetLayerZIndex(MapLayer.CustomFloorLayer2, MapLayer.CustomFloorLayer2);
  839. AddLayer(MapLayer.CustomFloorLayer3);
  840. SetLayerZIndex(MapLayer.CustomFloorLayer3, MapLayer.CustomFloorLayer3);
  841. AddLayer(MapLayer.AutoMiddleLayer);
  842. SetLayerZIndex(MapLayer.AutoMiddleLayer, MapLayer.AutoMiddleLayer);
  843. AddLayer(MapLayer.CustomMiddleLayer1);
  844. SetLayerZIndex(MapLayer.CustomMiddleLayer1, MapLayer.CustomMiddleLayer1);
  845. AddLayer(MapLayer.CustomMiddleLayer2);
  846. SetLayerZIndex(MapLayer.CustomMiddleLayer2, MapLayer.CustomMiddleLayer2);
  847. AddLayer(MapLayer.AutoTopLayer);
  848. SetLayerZIndex(MapLayer.AutoTopLayer, MapLayer.AutoTopLayer);
  849. AddLayer(MapLayer.CustomTopLayer);
  850. SetLayerZIndex(MapLayer.CustomTopLayer, MapLayer.CustomTopLayer);
  851. }
  852.  
  853. //缩小
  854. private void Shrink()
  855. {
  856. var pos = GetLocalMousePosition();
  857. var scale = Scale / 1.1f;
  858. if (scale.LengthSquared() >= 0.5f)
  859. {
  860. Scale = scale;
  861. SetMapPosition(Position + pos * 0.1f * scale);
  862. }
  863. }
  864. //放大
  865. private void Magnify()
  866. {
  867. var pos = GetLocalMousePosition();
  868. var prevScale = Scale;
  869. var scale = prevScale * 1.1f;
  870. if (scale.LengthSquared() <= 2000)
  871. {
  872. Scale = scale;
  873. SetMapPosition(Position - pos * 0.1f * prevScale);
  874. }
  875. }
  876.  
  877. //绘制单个贴图
  878. private void SetSingleCell(Vector2I position)
  879. {
  880. if (CurrLayer.Layer == MapLayer.AutoFloorLayer) //选择自动地板层, 那么不管笔刷类型, 通通使用 Main Source 中的 Main Terrain
  881. {
  882. var tileCellData = _autoTileConfig.Floor;
  883. SetCell(MapLayer.AutoFloorLayer, position, tileCellData.SourceId, tileCellData.AutoTileCoords);
  884. if (!_autoCellLayerGrid.Contains(position.X, position.Y))
  885. {
  886. ResetGenerateTimer();
  887. _autoCellLayerGrid.Set(position.X, position.Y, true);
  888. }
  889. }
  890. else //自定义层
  891. {
  892. var dirty = false;
  893. if (CurrBrushType == TileMapDrawMode.Free || CurrBrushType == TileMapDrawMode.Combination) //自由绘制 或者 组合
  894. {
  895. foreach (var item in CurrBrush)
  896. {
  897. SetCell(CurrLayer.Layer, position + item.Key + _brushOffset, CurrSourceIndex, item.Value);
  898. dirty = true;
  899. }
  900. }
  901. else if (CurrBrushType == TileMapDrawMode.Terrain) //绘制地形
  902. {
  903. if (CurrTerrain == null || !CurrTerrain.TerrainInfo.Ready) //未选择地形或者存在错误就不绘制了
  904. {
  905. return;
  906. }
  907. if (CurrTerrain.TerrainInfo.TerrainType == 0) //3x3地形
  908. {
  909. //绘制自动图块
  910. SetCellsTerrainConnect(CurrLayer.Layer, new Array<Vector2I>() { position }, CurrTerrain.TerrainSetIndex, 0);
  911. dirty = true;
  912. }
  913. else if (CurrTerrain.TerrainInfo.TerrainType == 1) //2x2地形
  914. {
  915. var arr = new Array<Vector2I>()
  916. {
  917. position,
  918. position + new Vector2I(0, 1),
  919. position + new Vector2I(1, 1),
  920. position + new Vector2I(1, 0),
  921. };
  922. //绘制自动图块
  923. SetCellsTerrainConnect(CurrLayer.Layer, arr, CurrTerrain.TerrainSetIndex, 0, false);
  924. dirty = true;
  925. }
  926. }
  927.  
  928. if (dirty)
  929. {
  930. //标记有修改数据
  931. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  932. }
  933. }
  934. }
  935. //绘制区域贴图
  936. private void SetRectCell(Vector2I start, Vector2I end)
  937. {
  938. if (start.X > end.X)
  939. {
  940. var temp = end.X;
  941. end.X = start.X;
  942. start.X = temp;
  943. }
  944. if (start.Y > end.Y)
  945. {
  946. var temp = end.Y;
  947. end.Y = start.Y;
  948. start.Y = temp;
  949. }
  950.  
  951. var width = end.X - start.X + 1;
  952. var height = end.Y - start.Y + 1;
  953. if (CurrLayer.Layer == MapLayer.AutoFloorLayer) //选择自动地板层, 那么不管笔刷类型, 通通使用 Main Source 中的 Main Terrain
  954. {
  955. ResetGenerateTimer();
  956. for (var i = 0; i < width; i++)
  957. {
  958. for (var j = 0; j < height; j++)
  959. {
  960. var tileCellData = _autoTileConfig.Floor;
  961. SetCell(MapLayer.AutoFloorLayer, new Vector2I(start.X + i, start.Y + j), tileCellData.SourceId, tileCellData.AutoTileCoords);
  962. }
  963. }
  964.  
  965. _autoCellLayerGrid.SetRect(start, new Vector2I(width, height), true);
  966. }
  967. else //自定义层
  968. {
  969. var dirty = false;
  970. if (CurrBrushType == TileMapDrawMode.Free || CurrBrushType == TileMapDrawMode.Combination) //自由绘制 或者 组合
  971. {
  972. dirty = CurrBrush.Count > 0;
  973. for (var i = 0; i < width; i++)
  974. {
  975. for (var j = 0; j < height; j++)
  976. {
  977. var x = i % _brushWidth + _brushStartX;
  978. var y = j % _brushHeight + _brushStartY;
  979. if (CurrBrush.TryGetValue(new Vector2I(x, y), out var v))
  980. {
  981. SetCell(CurrLayer.Layer, new Vector2I(start.X + i, start.Y + j), CurrSourceIndex, v);
  982. }
  983. }
  984. }
  985. }
  986. else if (CurrBrushType == TileMapDrawMode.Terrain) //绘制地形
  987. {
  988. if (CurrTerrain == null || !CurrTerrain.TerrainInfo.Ready) //未选择地形或者存在错误就不绘制了
  989. {
  990. return;
  991. }
  992. var arr = new Array<Vector2I>();
  993. for (var i = 0; i < width; i++)
  994. {
  995. for (var j = 0; j < height; j++)
  996. {
  997. arr.Add(new Vector2I(start.X + i, start.Y + j));
  998. }
  999. }
  1000. if (CurrTerrain.TerrainInfo.TerrainType == 0) //3x3地形
  1001. {
  1002. SetCellsTerrainConnect(CurrLayer.Layer, arr, CurrTerrain.TerrainSetIndex, 0);
  1003. }
  1004. else if (CurrTerrain.TerrainInfo.TerrainType == 1) //2x2地形
  1005. {
  1006. SetCellsTerrainConnect(CurrLayer.Layer, arr, CurrTerrain.TerrainSetIndex, 0, false);
  1007. }
  1008. dirty = true;
  1009. }
  1010.  
  1011. if (dirty)
  1012. {
  1013. //标记有修改数据
  1014. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  1015. }
  1016. }
  1017. }
  1018.  
  1019. //擦除单个图块
  1020. private void EraseSingleCell(Vector2I position)
  1021. {
  1022. if (CurrLayer.Layer == MapLayer.AutoFloorLayer) //选择自动地板层, 那么不管笔刷类型, 通通使用 Main Source 中的 Main Terrain
  1023. {
  1024. EraseCell(MapLayer.AutoFloorLayer, position);
  1025. if (_autoCellLayerGrid.Remove(position.X, position.Y))
  1026. {
  1027. ResetGenerateTimer();
  1028. }
  1029. }
  1030. else //自定义层
  1031. {
  1032. if (CurrBrushType == TileMapDrawMode.Free || CurrBrushType == TileMapDrawMode.Combination) //自由绘制 或者 组合
  1033. {
  1034. EraseCell(CurrLayer.Layer, position);
  1035. }
  1036. else //绘制地形
  1037. {
  1038. if (CurrTerrain == null) //未选择地形
  1039. {
  1040. EraseCell(CurrLayer.Layer, position);
  1041. }
  1042. else if (CurrTerrain.TerrainInfo.TerrainType == 0 || CurrTerrain.TerrainInfo.TerrainType == 1) //2x2地形 / 3x3地形
  1043. {
  1044. EraseCell(CurrLayer.Layer, position);
  1045. var arr = new Array<Vector2I>();
  1046. //这里需要判断周围8格是否是同terrainSet
  1047. if (EqualsTerrainSet(position + new Vector2I(-1, -1)))
  1048. {
  1049. EraseCell(CurrLayer.Layer, position + new Vector2I(-1, -1));
  1050. arr.Add(position + new Vector2I(-1, -1));
  1051. }
  1052.  
  1053. if (EqualsTerrainSet(position + new Vector2I(0, -1)))
  1054. {
  1055. EraseCell(CurrLayer.Layer, position + new Vector2I(0, -1));
  1056. arr.Add(position + new Vector2I(0, -1));
  1057. }
  1058.  
  1059. if (EqualsTerrainSet(position + new Vector2I(1, -1)))
  1060. {
  1061. EraseCell(CurrLayer.Layer, position + new Vector2I(1, -1));
  1062. arr.Add(position + new Vector2I(1, -1));
  1063. }
  1064.  
  1065. if (EqualsTerrainSet(position + new Vector2I(-1, 0)))
  1066. {
  1067. EraseCell(CurrLayer.Layer, position + new Vector2I(-1, 0));
  1068. arr.Add(position + new Vector2I(-1, 0));
  1069. }
  1070.  
  1071. if (EqualsTerrainSet(position + new Vector2I(1, 0)))
  1072. {
  1073. EraseCell(CurrLayer.Layer, position + new Vector2I(1, 0));
  1074. arr.Add(position + new Vector2I(1, 0));
  1075. }
  1076.  
  1077. if (EqualsTerrainSet(position + new Vector2I(-1, 1)))
  1078. {
  1079. EraseCell(CurrLayer.Layer, position + new Vector2I(-1, 1));
  1080. arr.Add(position + new Vector2I(-1, 1));
  1081. }
  1082.  
  1083. if (EqualsTerrainSet(position + new Vector2I(0, 1)))
  1084. {
  1085. EraseCell(CurrLayer.Layer, position + new Vector2I(0, 1));
  1086. arr.Add(position + new Vector2I(0, 1));
  1087. }
  1088.  
  1089. if (EqualsTerrainSet(position + new Vector2I(1, 1)))
  1090. {
  1091. EraseCell(CurrLayer.Layer, position + new Vector2I(1, 1));
  1092. arr.Add(position + new Vector2I(1, 1));
  1093. }
  1094. SetCellsTerrainConnect(CurrLayer.Layer, arr, CurrTerrain.TerrainSetIndex, 0, false);
  1095. }
  1096. }
  1097. //标记有修改数据
  1098. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  1099. }
  1100. }
  1101. //擦除一个区域内的贴图
  1102. private void EraseRectCell(Vector2I start, Vector2I end)
  1103. {
  1104. if (start.X > end.X)
  1105. {
  1106. var temp = end.X;
  1107. end.X = start.X;
  1108. start.X = temp;
  1109. }
  1110. if (start.Y > end.Y)
  1111. {
  1112. var temp = end.Y;
  1113. end.Y = start.Y;
  1114. start.Y = temp;
  1115. }
  1116.  
  1117. var width = end.X - start.X + 1;
  1118. var height = end.Y - start.Y + 1;
  1119. if (CurrLayer.Layer == MapLayer.AutoFloorLayer) //选择自动地板层
  1120. {
  1121. ResetGenerateTimer();
  1122. for (var i = 0; i < width; i++)
  1123. {
  1124. for (var j = 0; j < height; j++)
  1125. {
  1126. EraseCell(MapLayer.AutoFloorLayer, new Vector2I(start.X + i, start.Y + j));
  1127. }
  1128. }
  1129. _autoCellLayerGrid.RemoveRect(start, new Vector2I(width, height));
  1130. }
  1131. else //自定义层
  1132. {
  1133. if (CurrBrushType == TileMapDrawMode.Free || CurrBrushType == TileMapDrawMode.Combination ||
  1134. (CurrBrushType == TileMapDrawMode.Terrain && CurrTerrain == null)) //自由绘制 或者 组合 或者 未选择地形
  1135. {
  1136. for (var i = 0; i < width; i++)
  1137. {
  1138. for (var j = 0; j < height; j++)
  1139. {
  1140. EraseCell(CurrLayer.Layer, new Vector2I(start.X + i, start.Y + j));
  1141. }
  1142. }
  1143. }
  1144. else if (CurrBrushType == TileMapDrawMode.Terrain) //绘制地形
  1145. {
  1146. var arr = new Array<Vector2I>();
  1147. //需要检测矩形向外一格的Cell
  1148. for (var i = -1; i < width + 1; i++)
  1149. {
  1150. for (var j = -1; j < height + 1; j++)
  1151. {
  1152. var pos = new Vector2I(start.X + i, start.Y + j);
  1153. if (i >= 0 && i < width && j >= 0 && j < height)
  1154. {
  1155. EraseCell(CurrLayer.Layer, pos);
  1156. }
  1157. else if (EqualsTerrainSet(pos))
  1158. {
  1159. EraseCell(CurrLayer.Layer, pos);
  1160. arr.Add(pos);
  1161. }
  1162. }
  1163. }
  1164. SetCellsTerrainConnect(CurrLayer.Layer, arr, CurrTerrain.TerrainSetIndex, 0, false);
  1165. }
  1166. //标记有修改数据
  1167. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  1168. }
  1169. }
  1170.  
  1171. //重置计时器
  1172. private void ResetGenerateTimer()
  1173. {
  1174. _generateTimer = _generateInterval;
  1175. _isGenerateTerrain = false;
  1176. ClearLayer(MapLayer.AutoTopLayer);
  1177. ClearLayer(MapLayer.AutoMiddleLayer);
  1178. CloseErrorCell();
  1179. //标记有修改数据
  1180. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  1181. }
  1182.  
  1183. //重新计算房间区域
  1184. private void CalcTileRect(bool refreshDoorTrans)
  1185. {
  1186. var rect = _autoCellLayerGrid.GetRect();
  1187. CurrRoomPosition = rect.Position - new Vector2I(2, 3);
  1188. SetMapSize(rect.Size + new Vector2I(4, 5), refreshDoorTrans);
  1189. }
  1190. //检测是否有不合规的图块, 返回true表示图块正常
  1191. private bool CheckTerrain()
  1192. {
  1193. _checkTerrainFlag = true;
  1194. _autoCellLayerGrid.ForEach((x, y, flag) =>
  1195. {
  1196. if (flag)
  1197. {
  1198. if (!_autoCellLayerGrid.Contains(x, y + 1) && _autoCellLayerGrid.Contains(x, y + 2))
  1199. {
  1200. _checkTerrainFlag = false;
  1201. _checkTerrainErrorPosition = new Vector2I(x, y + 1);
  1202. return false;
  1203. }
  1204. }
  1205.  
  1206. return true;
  1207. });
  1208. return _checkTerrainFlag;
  1209. }
  1210. //生成自动图块 (地形)
  1211. private void GenerateTerrain()
  1212. {
  1213. //ClearLayer(MapLayer.AutoFloorLayer);
  1214. var list = new List<Vector2I>();
  1215. var xStart = int.MaxValue;
  1216. var yStart = int.MaxValue;
  1217. var xEnd = int.MinValue;
  1218. var yEnd = int.MinValue;
  1219. _autoCellLayerGrid.ForEach((x, y, flag) =>
  1220. {
  1221. if (flag)
  1222. {
  1223. //计算范围
  1224. if (x < xStart)
  1225. xStart = x;
  1226. else if (x > xEnd)
  1227. xEnd = x;
  1228.  
  1229. if (y < yStart)
  1230. yStart = y;
  1231. else if (y > yEnd)
  1232. yEnd = y;
  1233. //填充墙壁
  1234. if (!_autoCellLayerGrid.Contains(x, y - 1))
  1235. {
  1236. var left = _autoCellLayerGrid.Contains(x - 1, y - 1);
  1237. var right = _autoCellLayerGrid.Contains(x + 1, y - 1);
  1238. TileCellData tileCellData;
  1239. if (left && right)
  1240. {
  1241. tileCellData = _autoTileConfig.Wall_Vertical_Single;
  1242. }
  1243. else if (left)
  1244. {
  1245. tileCellData = _autoTileConfig.Wall_Vertical_Left;
  1246. }
  1247. else if (right)
  1248. {
  1249. tileCellData = _autoTileConfig.Wall_Vertical_Right;
  1250. }
  1251. else
  1252. {
  1253. tileCellData = _autoTileConfig.Wall_Vertical_Center;
  1254. }
  1255. SetCell(MapLayer.AutoFloorLayer, new Vector2I(x, y - 1), tileCellData.SourceId, tileCellData.AutoTileCoords);
  1256. }
  1257. }
  1258.  
  1259. return true;
  1260. });
  1261. //绘制临时边界
  1262. var temp1 = new List<Vector2I>();
  1263. for (var x = xStart - 3; x <= xEnd + 3; x++)
  1264. {
  1265. var p1 = new Vector2I(x, yStart - 4);
  1266. var p2 = new Vector2I(x, yEnd + 3);
  1267. temp1.Add(p1);
  1268. temp1.Add(p2);
  1269. //上横
  1270. SetCell(MapLayer.AutoFloorLayer, p1, _autoTileConfig.TopMask.SourceId, _autoTileConfig.TopMask.AutoTileCoords);
  1271. //下横
  1272. SetCell(MapLayer.AutoFloorLayer, p2, _autoTileConfig.TopMask.SourceId, _autoTileConfig.TopMask.AutoTileCoords);
  1273. }
  1274. for (var y = yStart - 4; y <= yEnd + 3; y++)
  1275. {
  1276. var p1 = new Vector2I(xStart - 3, y);
  1277. var p2 = new Vector2I(xEnd + 3, y);
  1278. temp1.Add(p1);
  1279. temp1.Add(p2);
  1280. //左竖
  1281. SetCell(MapLayer.AutoFloorLayer, p1, _autoTileConfig.TopMask.SourceId, _autoTileConfig.TopMask.AutoTileCoords);
  1282. //右竖
  1283. SetCell(MapLayer.AutoFloorLayer, p2, _autoTileConfig.TopMask.SourceId, _autoTileConfig.TopMask.AutoTileCoords);
  1284. }
  1285. //计算需要绘制的图块
  1286. var temp2 = new List<Vector2I>();
  1287. for (var x = xStart - 2; x <= xEnd + 2; x++)
  1288. {
  1289. for (var y = yStart - 3; y <= yEnd + 2; y++)
  1290. {
  1291. if (!_autoCellLayerGrid.Contains(x, y) && !_autoCellLayerGrid.Contains(x, y + 1))
  1292. {
  1293. list.Add(new Vector2I(x, y));
  1294. if (!IsMaskCollisionGround(_autoCellLayerGrid, x, y))
  1295. {
  1296. temp2.Add(new Vector2I(x, y));
  1297. }
  1298. }
  1299. }
  1300. }
  1301. var arr = new Array<Vector2I>(list);
  1302. //绘制自动图块
  1303. SetCellsTerrainConnect(MapLayer.AutoFloorLayer, arr, _mainTerrainSet, _mainTerrain, false);
  1304. //擦除临时边界
  1305. for (var i = 0; i < temp1.Count; i++)
  1306. {
  1307. EraseCell(MapLayer.AutoFloorLayer, temp1[i]);
  1308. }
  1309.  
  1310. //计算区域
  1311. CalcTileRect(true);
  1312. //开始绘制导航网格
  1313. GenerateNavigation();
  1314.  
  1315. //擦除临时边界2
  1316. for (var i = 0; i < temp2.Count; i++)
  1317. {
  1318. EraseCell(MapLayer.AutoFloorLayer, temp2[i]);
  1319. }
  1320. //将墙壁移动到指定层
  1321. MoveTerrainCell();
  1322. }
  1323.  
  1324. private bool IsMaskCollisionGround(InfiniteGrid<bool> autoCellLayerGrid, int x, int y)
  1325. {
  1326. for (var i = -2; i <= 2; i++)
  1327. {
  1328. for (var j = -2; j <= 3; j++)
  1329. {
  1330. if (autoCellLayerGrid.Contains(x + i, y + j))
  1331. {
  1332. return true;
  1333. }
  1334. }
  1335. }
  1336.  
  1337. return false;
  1338. }
  1339. //将自动生成的图块从 MapLayer.AutoFloorLayer 移动到指定图层中
  1340. private void MoveTerrainCell()
  1341. {
  1342. ClearLayer(MapLayer.AutoTopLayer);
  1343. ClearLayer(MapLayer.AutoMiddleLayer);
  1344. var x = CurrRoomPosition.X;
  1345. var y = CurrRoomPosition.Y;
  1346. var w = CurrRoomSize.X;
  1347. var h = CurrRoomSize.Y;
  1348.  
  1349. for (var i = 0; i < w; i++)
  1350. {
  1351. for (var j = 0; j < h; j++)
  1352. {
  1353. var pos = new Vector2I(x + i, y + j);
  1354. if (!_autoCellLayerGrid.Contains(pos) && GetCellSourceId(MapLayer.AutoFloorLayer, pos) != -1)
  1355. {
  1356. var atlasCoords = GetCellAtlasCoords(MapLayer.AutoFloorLayer, pos);
  1357. var layer = _autoTileConfig.GetLayer(atlasCoords);
  1358. if (layer == MapLayer.AutoMiddleLayer)
  1359. {
  1360. layer = MapLayer.AutoMiddleLayer;
  1361. }
  1362. else if (layer == MapLayer.AutoTopLayer)
  1363. {
  1364. layer = MapLayer.AutoTopLayer;
  1365. }
  1366. else
  1367. {
  1368. Debug.LogError($"异常图块: {pos}, 这个图块的图集坐标'{atlasCoords}'不属于'MiddleMapLayer'和'TopMapLayer'!");
  1369. continue;
  1370. }
  1371. EraseCell(MapLayer.AutoFloorLayer, pos);
  1372. SetCell(layer, pos, _mainSource, atlasCoords);
  1373. }
  1374. }
  1375. }
  1376. }
  1377.  
  1378. //生成导航网格
  1379. private void GenerateNavigation()
  1380. {
  1381. var navigationRegion = _editorTileMap.L_NavigationRegion.Instance;
  1382. var navigationPolygon = navigationRegion.NavigationPolygon;
  1383. navigationPolygon.Clear();
  1384. navigationPolygon.ClearPolygons();
  1385. navigationPolygon.ClearOutlines();
  1386. var endPos = CurrRoomPosition + CurrRoomSize;
  1387. navigationPolygon.AddOutline(new []
  1388. {
  1389. CurrRoomPosition * GameConfig.TileCellSize,
  1390. new Vector2(endPos.X, CurrRoomPosition.Y) * GameConfig.TileCellSize,
  1391. endPos * GameConfig.TileCellSize,
  1392. new Vector2(CurrRoomPosition.X, endPos.Y) * GameConfig.TileCellSize
  1393. });
  1394. navigationRegion.BakeNavigationPolygon(false);
  1395. }
  1396.  
  1397. //设置显示的错误cell, 会标记上红色的闪烁动画
  1398. private void SetErrorCell(Vector2I pos)
  1399. {
  1400. MapEditorPanel.S_ErrorCell.Instance.Position = pos * GameConfig.TileCellSize;
  1401. MapEditorPanel.S_ErrorCellAnimationPlayer.Instance.Play(AnimatorNames.Show);
  1402. }
  1403.  
  1404. //关闭显示的错误cell
  1405. private void CloseErrorCell()
  1406. {
  1407. MapEditorPanel.S_ErrorCellAnimationPlayer.Instance.Stop();
  1408. }
  1409. /// <summary>
  1410. /// 选中拖拽功能
  1411. /// </summary>
  1412. private void OnSelectHandTool(object arg)
  1413. {
  1414. MouseType = MouseButtonType.Drag;
  1415. }
  1416. /// <summary>
  1417. /// 选中画笔攻击
  1418. /// </summary>
  1419. private void OnSelectPenTool(object arg)
  1420. {
  1421. MouseType = MouseButtonType.Pen;
  1422. }
  1423.  
  1424. /// <summary>
  1425. /// 选中绘制区域功能
  1426. /// </summary>
  1427. private void OnSelectRectTool(object arg)
  1428. {
  1429. MouseType = MouseButtonType.Area;
  1430. }
  1431.  
  1432. /// <summary>
  1433. /// 选择编辑门区域
  1434. /// </summary>
  1435. private void OnSelectEditTool(object arg)
  1436. {
  1437. MouseType = MouseButtonType.Edit;
  1438. }
  1439.  
  1440. /// <summary>
  1441. /// 聚焦
  1442. /// </summary>
  1443. private void OnClickCenterTool(object arg)
  1444. {
  1445. var pos = MapEditorPanel.S_SubViewport.Instance.Size / 2;
  1446. if (CurrRoomSize.X == 0 && CurrRoomSize.Y == 0) //聚焦原点
  1447. {
  1448. SetMapPosition(pos);
  1449. }
  1450. else //聚焦地图中心点
  1451. {
  1452. var roomPos = new Vector2(CurrRoomPosition.X, CurrRoomPosition.Y);
  1453. var roomSize = new Vector2(CurrRoomSize.X, CurrRoomSize.Y);
  1454. SetMapPosition(pos - (roomPos + roomSize / 2) * TileSet.TileSize * Scale);
  1455. }
  1456. }
  1457. //房间数据有修改
  1458. private void OnEditorDirty(object obj)
  1459. {
  1460. IsDirty = true;
  1461. MapEditorPanel.SetTitleDirty(true);
  1462. }
  1463.  
  1464. /// <summary>
  1465. /// 创建地牢房间门区域
  1466. /// </summary>
  1467. /// <param name="direction">门方向</param>
  1468. /// <param name="start">起始坐标, 单位: 像素</param>
  1469. /// <param name="end">结束坐标, 单位: 像素</param>
  1470. public DoorAreaInfo CreateDoorArea(DoorDirection direction, int start, int end)
  1471. {
  1472. var doorAreaInfo = new DoorAreaInfo();
  1473. doorAreaInfo.Direction = direction;
  1474. doorAreaInfo.Start = start;
  1475. doorAreaInfo.End = end;
  1476. //doorAreaInfo.CalcPosition(_roomPosition, _roomSize);
  1477. CurrDoorConfigs.Add(doorAreaInfo);
  1478. return doorAreaInfo;
  1479. }
  1480.  
  1481. /// <summary>
  1482. /// 检测门区域数据是否可以提交
  1483. /// </summary>
  1484. /// <param name="direction">门方向</param>
  1485. /// <param name="start">起始坐标, 单位: 像素</param>
  1486. /// <param name="end">结束坐标, 单位: 像素</param>
  1487. /// <returns></returns>
  1488. public bool CheckDoorArea(DoorDirection direction, int start, int end)
  1489. {
  1490. foreach (var item in CurrDoorConfigs)
  1491. {
  1492. if (item.Direction == direction)
  1493. {
  1494. if (CheckValueCollision(item.Start, item.End, start, end))
  1495. {
  1496. return false;
  1497. }
  1498. }
  1499. }
  1500.  
  1501. return true;
  1502. }
  1503. /// <summary>
  1504. /// 检测门区域数据是否可以提交
  1505. /// </summary>
  1506. /// <param name="target">需要检测的门</param>
  1507. /// <param name="start">起始坐标, 单位: 像素</param>
  1508. /// <param name="end">结束坐标, 单位: 像素</param>
  1509. public bool CheckDoorArea(DoorAreaInfo target, int start, int end)
  1510. {
  1511. foreach (var item in CurrDoorConfigs)
  1512. {
  1513. if (item.Direction == target.Direction && item != target)
  1514. {
  1515. if (CheckValueCollision(item.Start, item.End, start, end))
  1516. {
  1517. return false;
  1518. }
  1519. }
  1520. }
  1521.  
  1522. return true;
  1523. }
  1524. private bool CheckValueCollision(float o1, float o2, float h1, float h2)
  1525. {
  1526. var size = GameConfig.TileCellSize;
  1527. return !(h2 < o1 - 3 * size || o2 + 3 * size < h1);
  1528. }
  1529.  
  1530. /// <summary>
  1531. /// 移除门区域数据
  1532. /// </summary>
  1533. public void RemoveDoorArea(DoorAreaInfo doorAreaInfo)
  1534. {
  1535. CurrDoorConfigs.Remove(doorAreaInfo);
  1536. }
  1537. //保存房间配置
  1538. private void SaveRoomInfoConfig()
  1539. {
  1540. //存入本地
  1541. var roomInfo = CurrRoomSplit.RoomInfo;
  1542. if (!HasTerrainError) //没有绘制错误
  1543. {
  1544. roomInfo.Size = new SerializeVector2(CurrRoomSize);
  1545. roomInfo.Position = new SerializeVector2(CurrRoomPosition);
  1546. }
  1547. else
  1548. {
  1549. roomInfo.Position = new SerializeVector2(CurrRoomPosition - Vector2I.One);
  1550. roomInfo.Size = new SerializeVector2(CurrRoomSize + new Vector2I(2, 2));
  1551. }
  1552.  
  1553. roomInfo.DoorAreaInfos.Clear();
  1554. roomInfo.DoorAreaInfos.AddRange(CurrDoorConfigs);
  1555. roomInfo.ClearCompletionDoorArea();
  1556. MapProjectManager.SaveRoomInfo(CurrRoomSplit);
  1557. }
  1558.  
  1559. //保存地块数据
  1560. public void SaveTileInfoConfig()
  1561. {
  1562. //存入本地
  1563. var tileInfo = CurrRoomSplit.TileInfo;
  1564. if (tileInfo.NavigationPolygon == null)
  1565. {
  1566. tileInfo.NavigationPolygon = new List<int[]>();
  1567. }
  1568. else
  1569. {
  1570. tileInfo.NavigationPolygon.Clear();
  1571. }
  1572. if (tileInfo.NavigationVertices == null)
  1573. {
  1574. tileInfo.NavigationVertices = new List<SerializeVector2>();
  1575. }
  1576. else
  1577. {
  1578. tileInfo.NavigationVertices.Clear();
  1579. }
  1580. var polygon = _editorTileMap.L_NavigationRegion.Instance.NavigationPolygon;
  1581. tileInfo.NavigationPolygon.AddRange(polygon.Polygons);
  1582. tileInfo.NavigationVertices.AddRange(polygon.Vertices.Select(v => new SerializeVector2(v)));
  1583. tileInfo.Floor.Clear();
  1584. tileInfo.Middle.Clear();
  1585. tileInfo.Top.Clear();
  1586. tileInfo.CustomFloor1.Clear();
  1587. tileInfo.CustomFloor2.Clear();
  1588. tileInfo.CustomFloor3.Clear();
  1589. tileInfo.CustomMiddle1.Clear();
  1590. tileInfo.CustomMiddle2.Clear();
  1591. tileInfo.CustomTop.Clear();
  1592.  
  1593. //保存图块数据
  1594. PushAutoLayerDataToList(MapLayer.AutoFloorLayer, tileInfo.Floor);
  1595. PushAutoLayerDataToList(MapLayer.AutoMiddleLayer, tileInfo.Middle);
  1596. PushAutoLayerDataToList(MapLayer.AutoTopLayer, tileInfo.Top);
  1597. PushLayerDataToList(MapLayer.CustomFloorLayer1, tileInfo.CustomFloor1);
  1598. PushLayerDataToList(MapLayer.CustomFloorLayer2, tileInfo.CustomFloor2);
  1599. PushLayerDataToList(MapLayer.CustomFloorLayer3, tileInfo.CustomFloor3);
  1600. PushLayerDataToList(MapLayer.CustomMiddleLayer1, tileInfo.CustomMiddle1);
  1601. PushLayerDataToList(MapLayer.CustomMiddleLayer2, tileInfo.CustomMiddle2);
  1602. PushLayerDataToList(MapLayer.CustomTopLayer, tileInfo.CustomTop);
  1603. MapProjectManager.SaveRoomTileInfo(CurrRoomSplit);
  1604. }
  1605.  
  1606. /// <summary>
  1607. /// 保存预设数据
  1608. /// </summary>
  1609. public void SavePreinstallConfig()
  1610. {
  1611. //存入本地
  1612. MapProjectManager.SaveRoomPreinstall(CurrRoomSplit);
  1613. }
  1614.  
  1615. /// <summary>
  1616. /// 获取相机中心点坐标
  1617. /// </summary>
  1618. public Vector2I GetCenterPosition()
  1619. {
  1620. var pos = ToLocal(MapEditorPanel.S_SubViewport.Instance.Size / 2);
  1621. return new Vector2I((int)pos.X, (int)pos.Y);
  1622. }
  1623. /// <summary>
  1624. /// 设置相机看向的点
  1625. /// </summary>
  1626. public void SetLookPosition(Vector2 pos)
  1627. {
  1628. SetMapPosition(-pos * Scale + MapEditorPanel.S_SubViewport.Instance.Size / 2);
  1629. //SetMapPosition(pos * Scale);
  1630. //SetMapPosition(pos + MapEditorPanel.S_SubViewport.Instance.Size / 2);
  1631. }
  1632. /// <summary>
  1633. /// 设置地图坐标
  1634. /// </summary>
  1635. public void SetMapPosition(Vector2 pos)
  1636. {
  1637. Position = pos;
  1638. MapEditorToolsPanel.SetToolTransform(pos, Scale);
  1639. }
  1640.  
  1641. //设置地图大小
  1642. private void SetMapSize(Vector2I size, bool refreshDoorTrans)
  1643. {
  1644. CurrRoomSize = size;
  1645. if (refreshDoorTrans && _cacheToolSizeData != size)
  1646. {
  1647. _cacheToolSizeData = size;
  1648. MapEditorToolsPanel.SetDoorHoverToolTransform(CurrRoomPosition, CurrRoomSize);
  1649. }
  1650. }
  1651.  
  1652. private bool _hasPreviewImage = false;
  1653. private Action _previewFinish;
  1654. private int _previewIndex = 0;
  1655. private Vector2I _tempViewportSize;
  1656. private Vector2 _tempMapPos;
  1657. private Vector2 _tempMapScale;
  1658. private bool _tempAutoFloorLayer;
  1659. private bool _tempCustomFloorLayer1;
  1660. private bool _tempCustomFloorLayer2;
  1661. private bool _tempCustomFloorLayer3;
  1662. private bool _tempAutoMiddleLayer;
  1663. private bool _tempCustomMiddleLayer1;
  1664. private bool _tempCustomMiddleLayer2;
  1665. private bool _tempAutoTopLayer;
  1666. private bool _tempCustomTopLayer;
  1667.  
  1668. private void RunSavePreviewImage(Action action)
  1669. {
  1670. if (_hasPreviewImage)
  1671. {
  1672. return;
  1673. }
  1674.  
  1675. _previewIndex = 0;
  1676. _previewFinish = action;
  1677. _hasPreviewImage = true;
  1678. //先截图, 将图像数据放置到 S_MapView2 节点上
  1679. var subViewport = MapEditorPanel.S_SubViewport.Instance;
  1680. var viewportTexture = subViewport.GetTexture();
  1681. var tex = ImageTexture.CreateFromImage(viewportTexture.GetImage());
  1682. var textureRect = MapEditorPanel.S_MapView2.Instance;
  1683. textureRect.Texture = tex;
  1684. textureRect.Visible = true;
  1685. //调整绘制视图大小
  1686. _tempViewportSize = subViewport.Size;
  1687. subViewport.Size = new Vector2I(GameConfig.PreviewImageSize, GameConfig.PreviewImageSize);
  1688. //调整tileMap
  1689. _tempMapPos = Position;
  1690. _tempMapScale = Scale;
  1691. //中心点
  1692. var pos = new Vector2(GameConfig.PreviewImageSize / 2f, GameConfig.PreviewImageSize / 2f);
  1693. if (CurrRoomSize.X == 0 && CurrRoomSize.Y == 0) //聚焦原点
  1694. {
  1695. Position = pos;
  1696. }
  1697. else //聚焦地图中心点
  1698. {
  1699. var tempPos = new Vector2(CurrRoomSize.X, CurrRoomSize.Y);
  1700. //var tempPos = new Vector2(CurrRoomSize.X + 2, CurrRoomSize.Y + 2);
  1701. var mapSize = tempPos * TileSet.TileSize;
  1702. var axis = Mathf.Max(mapSize.X, mapSize.Y);
  1703. var targetScale = GameConfig.PreviewImageSize / axis;
  1704. Scale = new Vector2(targetScale, targetScale);
  1705. Position = pos - (CurrRoomPosition + tempPos / 2f) * TileSet.TileSize * targetScale;
  1706. }
  1707. //隐藏工具栏
  1708. MapEditorToolsPanel.Visible = false;
  1709. //显示所有层级
  1710. _tempAutoFloorLayer = IsLayerEnabled(MapLayer.AutoFloorLayer);
  1711. _tempCustomFloorLayer1 = IsLayerEnabled(MapLayer.CustomFloorLayer1);
  1712. _tempCustomFloorLayer2 = IsLayerEnabled(MapLayer.CustomFloorLayer2);
  1713. _tempCustomFloorLayer3 = IsLayerEnabled(MapLayer.CustomFloorLayer3);
  1714. _tempAutoMiddleLayer = IsLayerEnabled(MapLayer.AutoMiddleLayer);
  1715. _tempCustomMiddleLayer1 = IsLayerEnabled(MapLayer.CustomMiddleLayer1);
  1716. _tempCustomMiddleLayer2 = IsLayerEnabled(MapLayer.CustomMiddleLayer2);
  1717. _tempAutoTopLayer = IsLayerEnabled(MapLayer.AutoTopLayer);
  1718. _tempCustomTopLayer = IsLayerEnabled(MapLayer.CustomTopLayer);
  1719.  
  1720. SetLayerEnabled(MapLayer.AutoFloorLayer, true);
  1721. SetLayerEnabled(MapLayer.CustomFloorLayer1, true);
  1722. SetLayerEnabled(MapLayer.CustomFloorLayer2, true);
  1723. SetLayerEnabled(MapLayer.CustomFloorLayer3, true);
  1724. SetLayerEnabled(MapLayer.AutoMiddleLayer, true);
  1725. SetLayerEnabled(MapLayer.CustomMiddleLayer1, true);
  1726. SetLayerEnabled(MapLayer.CustomMiddleLayer2, true);
  1727. SetLayerEnabled(MapLayer.AutoTopLayer, true);
  1728. SetLayerEnabled(MapLayer.CustomTopLayer, true);
  1729. SetLayerModulate(MapLayer.AutoFloorLayer, Colors.White);
  1730. SetLayerModulate(MapLayer.CustomFloorLayer1, Colors.White);
  1731. SetLayerModulate(MapLayer.CustomFloorLayer2, Colors.White);
  1732. SetLayerModulate(MapLayer.CustomFloorLayer3, Colors.White);
  1733. SetLayerModulate(MapLayer.AutoMiddleLayer, Colors.White);
  1734. SetLayerModulate(MapLayer.CustomMiddleLayer1, Colors.White);
  1735. SetLayerModulate(MapLayer.CustomMiddleLayer2, Colors.White);
  1736. SetLayerModulate(MapLayer.AutoTopLayer, Colors.White);
  1737. SetLayerModulate(MapLayer.CustomTopLayer, Colors.White);
  1738. }
  1739.  
  1740. private void OnFramePostDraw()
  1741. {
  1742. if (_hasPreviewImage)
  1743. {
  1744. _previewIndex++;
  1745. if (_previewIndex == 2)
  1746. {
  1747. var textureRect = MapEditorPanel.S_MapView2.Instance;
  1748. var texture = textureRect.Texture;
  1749. textureRect.Texture = null;
  1750. texture.Dispose();
  1751. textureRect.Visible = false;
  1752. //还原工具栏
  1753. MapEditorToolsPanel.Visible = true;
  1754. //还原层级显示
  1755. SetLayerEnabled(MapLayer.AutoFloorLayer, _tempAutoFloorLayer);
  1756. SetLayerEnabled(MapLayer.AutoMiddleLayer, _tempAutoMiddleLayer);
  1757. SetLayerEnabled(MapLayer.AutoTopLayer, _tempAutoTopLayer);
  1758. SetLayerEnabled(MapLayer.CustomFloorLayer1, _tempCustomFloorLayer1);
  1759. SetLayerEnabled(MapLayer.CustomFloorLayer2, _tempCustomFloorLayer2);
  1760. SetLayerEnabled(MapLayer.CustomFloorLayer3, _tempCustomFloorLayer3);
  1761. SetLayerEnabled(MapLayer.CustomMiddleLayer1, _tempCustomMiddleLayer1);
  1762. SetLayerEnabled(MapLayer.CustomMiddleLayer2, _tempCustomMiddleLayer2);
  1763. SetLayerEnabled(MapLayer.CustomTopLayer, _tempCustomTopLayer);
  1764. SetCurrentLayer(CurrLayer);
  1765.  
  1766. //保存预览图
  1767. var subViewport = MapEditorPanel.S_SubViewport.Instance;
  1768. var viewportTexture = subViewport.GetTexture();
  1769. var image = viewportTexture.GetImage();
  1770. image.Resize(GameConfig.PreviewImageSize, GameConfig.PreviewImageSize, Image.Interpolation.Nearest);
  1771. CurrRoomSplit.PreviewImage = ImageTexture.CreateFromImage(image);
  1772. MapProjectManager.SaveRoomPreviewImage(CurrRoomSplit, image);
  1773. //还原tileMap
  1774. Position = _tempMapPos;
  1775. Scale = _tempMapScale;
  1776. //还原绘制视图
  1777. subViewport.Size = _tempViewportSize;
  1778. _previewFinish();
  1779. _hasPreviewImage = false;
  1780. }
  1781. }
  1782. }
  1783. private void OnBakeFinished()
  1784. {
  1785. var polygonData = _editorTileMap.L_NavigationRegion.Instance.NavigationPolygon;
  1786. var polygons = polygonData.Polygons;
  1787. var vertices = polygonData.Vertices;
  1788. _polygonData = new Vector2[polygons.Count][];
  1789. for (var i = 0; i < polygons.Count; i++)
  1790. {
  1791. var polygon = polygons[i];
  1792. var v2Array = new Vector2[polygon.Length];
  1793. for (var j = 0; j < polygon.Length; j++)
  1794. {
  1795. v2Array[j] = vertices[polygon[j]];
  1796. }
  1797. _polygonData[i] = v2Array;
  1798. }
  1799. }
  1800. private Color GetEditorLayerModulate(int layer)
  1801. {
  1802. if (!_desaltOtherLayer)
  1803. {
  1804. return Colors.White;
  1805. }
  1806.  
  1807. return layer == CurrLayer.Layer ? Colors.White : new Color(1, 1, 1, 0.25f);
  1808. }
  1809.  
  1810. private bool EqualsTerrainSet(Vector2I position)
  1811. {
  1812. if (GetCellSourceId(CurrLayer.Layer, position) == CurrSourceIndex)
  1813. {
  1814. var cellTileData = GetCellTileData(CurrLayer.Layer, position);
  1815. if (cellTileData != null && cellTileData.TerrainSet == CurrTerrain.TerrainSetIndex)
  1816. {
  1817. return true;
  1818. }
  1819. }
  1820.  
  1821. return false;
  1822. }
  1823. }