NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Map/GridEditor.cs

424 lines
15 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Gameplay;
using TGS;
using UnityEngine;
using Sirenix.OdinInspector;
[ExecuteAlways]
public class GridEditor : MonoBehaviour
{
// private TerrainGridSystem _tgs;
// private GameObject _ground;
// private Map _map;
// [HideInInspector]
// public MapData currentMapData = new();
//
// private static readonly Color regionColor = MapUtils.GetRGBAColor("FF960072");
// private static MapData.Region area;
// private static bool canEditorRegion = false;
// private bool EditorNewRegion = false;
//
// public class RegionArea
// {
// [ReadOnly] public int id;
// [ReadOnly] public int regionCount;
//
// [Button("编辑"), HorizontalGroup("")]
// private void EditorRegion()
// {
// var map = _map;
// var regionslist = map.MapData.GetAllRegions();
// for(int i = 0; i < regionslist.Count; i++)
// {
// EditorMap.Instance.Current.SetRegionColor(i, false, regionColor);
// }
// map.SetRegionColor(id, true, regionColor);
// var regionlist = map.MapData.GetRegion(id);
// area = new MapData.Region();
// foreach (var region in regionlist.positions)
// {
// area.positions.Add(region);
// }
// EditorMap.Instance.Current.MapData.AddRegion(area, id);
// regionCount = area.positions.Count;
// canEditorRegion = true;
// }
// }
//
// [VerticalGroup("地图编辑器/Split/Right/地区信息/RegionInfo"), Indent]
// [TableList(AlwaysExpanded = false, DrawScrollView = false, IsReadOnly = true)]
// public List<RegionArea> RegionList = new List<RegionArea>();
//
// [BoxGroup("地图编辑器/Split/Right/地区信息")]
// [HorizontalGroup("地图编辑器/Split/Right/地区信息/Region")]
// [LabelText("编辑地区"), Indent]
// [OnValueChanged("OnCanEditorAreaTypeChanged")]
// [LabelWidth(100)]
// [SerializeField] private bool CanEditorAreaType;
//
// private void OnCanEditorAreaTypeChanged()
// {
// if (CanEditorAreaType)
// {
// CanBrushTerrainType = false;
// }
// else
// {
// for (int i = 0; i < currentMapData.GetAllRegions().Count; i++)
// {
// _map.SetRegionColor(i, false, regionColor);
// }
// canEditorRegion = false;
// }
// UpdateRegion();
// }
//
// [HideIf("EditorNewRegion")]
// [HorizontalGroup("地图编辑器/Split/Right/地区信息/Region")]
// [EnableIf("CanEditorAreaType")]
// [Button("添加地区")]
// private void BrushArea()
// {
// if (EditorMap.Instance.Current != null)
// {
// canEditorRegion = true;
// EditorNewRegion = true;
// area = new MapData.Region();
// }
// }
//
// [ShowIf("EditorNewRegion")]
// [HorizontalGroup("地图编辑器/Split/Right/地区信息/Region")]
// [Button("保存添加")]
// private void SaveArea()
// {
// if (area.positions.Count > 0)
// {
// var map = EditorMap.Instance.Current;
// map.MapData.AddRegion(area);
// foreach (var cell in area.positions)
// {
// map.TerrainGridSystem.CellHideRegionSurface(map.BlockPosition2TGSCellIndex(cell));
// }
// UpdateRegion();
// }
// EditorNewRegion = false;
// canEditorRegion = false;
// area = null;
// }
//
// [HorizontalGroup("地图编辑器/Split/Right/地区信息/Region")]
// [EnableIf("CanEditorAreaType")]
// [Button("删除地区")]
// private void DeleteRegion()
// {
// var map = EditorMap.Instance.Current;
// if (RegionList.Count > 0)
// {
// map.SetRegionColor(RegionList.Count - 1, false, regionColor);
// map.MapData.RemoveRegion(RegionList.Count - 1);
// UpdateRegion();
// }
// }
//
// [BoxGroup("地图编辑器/Split/Right/地块信息")]
// [LabelText("刷地块"), Indent]
// [OnValueChanged("OnCanCanBrushTerrainTypeChanged")]
// [LabelWidth(100)]
// [SerializeField] private bool CanBrushTerrainType;
//
// private void OnCanCanBrushTerrainTypeChanged()
// {
// if (CanBrushTerrainType)
// {
// CanEditorAreaType = false;
// _tgs.colorizeTerritories = true;
// }
// }
//
// [HorizontalGroup("地图编辑器/Split/Right/地块信息/Territory")]
// [ValueDropdown("GetTerrainTypeProperties")]
// [Indent]
// [LabelText("选择地块")]
// [ShowIf("CanBrushTerrainType")]
// public int TerrainTypeIndex = 0;
//
// [ShowIf("CanBrushTerrainType")]
// [HorizontalGroup("地图编辑器/Split/Right/地块信息/Territory")]
// [Button(SdfIconType.GearFill, "编辑地块")]
// public void EditTerrainTypeConfig()
// {
// var window = GetWindow<TerrainTypeConfigWindow>();
// window.Show();
// }
//
// private void UpdateRegion()
// {
// RegionList.Clear();
// if (EditorMap.Instance.Current != null)
// {
// var regions = EditorMap.Instance.Current.MapData.GetAllRegions();
//
// var idList = RegionList.Select(region => region.id).ToList();
// foreach (var region in regions)
// {
// if (!idList.Contains(region.Key))
// {
// RegionArea area = new RegionArea();
// area.id = region.Key;
// area.regionCount = region.Value.positions.Count;
// RegionList.Add(area);
// }
// }
// }
//
// }
//
// [VerticalGroup("地图编辑器/Split/Right/InitialCameraPosition")]
// [LabelText("摄像机初始视角")]
// [LabelWidth(150)]
// [OnValueChanged("OnInitialCameraPositionChanged")]
// public Vector2Int initialCameraPosition;
//
// private void OnInitialCameraPositionChanged()
// {
// if (Camera.main != null && currentMapData !=null)
// {
// initialCameraPosition.x = Math.Clamp(initialCameraPosition.x, 0, currentMapData.height - 1);
// initialCameraPosition.y = Math.Clamp(initialCameraPosition.y, 0, currentMapData.width - 1);
// var cameraController = Camera.main.gameObject.GetComponent<CameraController>();
// currentMapData.initialCameraPosition = initialCameraPosition;
// cameraController.InitializeCameraPosition(initialCameraPosition);
// }
//
// }
//
// private void Awake()
// {
// _tgs = FindObjectOfType<TerrainGridSystem>();
// if (_tgs == null)
// {
// Debug.LogError("Terrain Grid System Is Null!");
// }
//
// _map = Map.Create(_tgs,currentMapData);
// EnhancedTouchSupport.Enable();
// TerrainTypeConfig.Load();
// EditorMap.Instance.onMapChanged += OnMapChanged;
// Selection.selectionChanged += OnSelectionChanged;
// EditorApplication.update += OnUpadte;
// UpdateMapInfo();
// }
//
// private void OnUpadte()
// {
// GameObject currentSelectedGameObject = Selection.activeGameObject;
//
// if (currentSelectedGameObject != null && currentSelectedGameObject != selectedGameObject)
// {
// selectedGameObject = currentSelectedGameObject;
// previousPosition = selectedGameObject.transform.position;
// previousRotation = selectedGameObject.transform.eulerAngles;
// }
//
// if (selectedGameObject != null)
// {
// var transform = selectedGameObject.transform;
//
// if (Vector3.Distance(transform.position, previousPosition) >= 0.01f)
// {
// transform.position = new Vector3(transform.position.x, 0f, transform.position.z);
// previousPosition = transform.position;
// MapEditorUtils.ObjectAutoAdsorptionCell(selectedGameObject);
// }
//
// if (Vector3.Distance(transform.eulerAngles, previousRotation) >= 0.1f && !Mouse.current.leftButton.isPressed)
// {
// int index = (int)(transform.eulerAngles.y / FixedRotationValue);
// float fixedRotation = FixedRotationValue;
// if ((transform.eulerAngles.y - index * fixedRotation) > ((index + 1) * fixedRotation - transform.eulerAngles.y))
// {
// transform.eulerAngles = new Vector3(0f, fixedRotation * (index + 1), 0f);
// }
// else
// {
// transform.eulerAngles = new Vector3(0f, fixedRotation * index, 0f);
// }
// previousRotation = transform.eulerAngles;
// MapEditorUtils.ObjectAutoAdsorptionCell(selectedGameObject);
// }
// }
// }
//
// private void OnSelectionChanged()
// {
// if (EditorMap.Instance.Current != null)
// {
// List<GameObject> MapObjects = new List<GameObject>();
//
// MeshFilter[] meshRenderers = EditorMap.Instance.Current.GameObject.GetComponentsInChildren<MeshFilter>();
//
// foreach (MeshFilter renderer in meshRenderers)
// {
// Transform parentTransform = renderer.transform.parent;
// if (parentTransform != null)
// {
// MapObjects.Add(parentTransform.gameObject);
// }
// }
// GameObject removedMapObj = null;
//
// MapEditorUtils.mapObjectOccupyCells.ForEach(cell =>
// {
// if (!MapObjects.Contains(cell.Key))
// {
// removedMapObj = cell.Key;
// EditorMap.Instance.Current.TerrainGridSystem.CellSetColor(cell.Value, Color.clear);
// }
// });
//
// if (removedMapObj != null)
// {
// MapEditorUtils.mapObjectOccupyCells.Remove(removedMapObj);
// }
// }
// }
//
// private GameObject selectedGameObject;
// private Vector3 previousPosition;
// private Vector3 previousRotation;
//
// private void UpdateMapInfo()
// {
// var map = EditorMap.Instance.Current;
// if (map != null)
// {
// currentMapData = map.MapData;
// initialCameraPosition = currentMapData.initialCameraPosition;
// }
// }
//
// private void OnMapChanged()
// {
// UpdateMapInfo();
// UpdateRegion();
// var currentMap = EditorMap.Instance.Current;
// if (currentMap != null)
// {
// currentMap.TerrainGridSystem.OnCellClick += OnCellClick;
// MapScale = new Vector2(currentMap.TerrainGridSystem.transform.localScale.x,
// currentMap.TerrainGridSystem.transform.localScale.y);
// groundMaterial = currentMap.GroundMeshRenderer.sharedMaterial;
//
// }
// }
//
// private void OnCellClick(TerrainGridSystem tgs, int cellIndex, int buttonIndex)
// {
// if (!CanBrushTerrainType && !CanEditorAreaType)
// {
// return;
// }
//
// var map = EditorMap.Instance.Current;
//
// int leftButton = 0;
// int rightButton = 1;
//
// if (buttonIndex == leftButton && TerrainTypeIndex >= 0 && CanBrushTerrainType)
// {
// SetCellTerritory(tgs, cellIndex, TerrainTypeIndex);
// }
// else if (buttonIndex == rightButton && CanBrushTerrainType)
// {
// ClearCellTerritory(tgs, cellIndex);
// }
//
// if (buttonIndex == leftButton && canEditorRegion)
// {
// if (!area.positions.Contains(map.MapData.BlockIndex2Position(cellIndex)))
// {
// area.positions.Add(map.MapData.BlockIndex2Position(cellIndex));
// map.TerrainGridSystem.CellToggleRegionSurface(cellIndex, true, regionColor);
// }
// }
// else if (buttonIndex == rightButton && canEditorRegion)
// {
// ClearCellRegion(cellIndex);
// }
// }
//
// private void SetCellTerritory(TerrainGridSystem tgs, int cellIndex ,int TerrainTypeIndex)
// {
// var map = EditorMap.Instance.Current;
// // Key: TerrainTypeIndex Value: tgs.territories.index
// if (!map.TerrainTypeIndexDic.ContainsKey(TerrainTypeIndex))
// {
// Territory territory = tgs.TerritoryCreate(cellIndex);
// map.TerrainTypeIndexDic.Add(TerrainTypeIndex, map.TerrainTypeIndexDic.Count);
// territory.fillColor = _terrainTypeConfig[TerrainTypeIndex].Color;
// }
// if (_terrainTypeConfig[TerrainTypeIndex].emptyBlock)
// {
// map.TerrainGridSystem.CellSetBorderVisible(cellIndex, false);
// }
//
// map?.SetTerrainType(cellIndex, TerrainTypeIndex);
// tgs.CellSetTerritory(cellIndex, map.TerrainTypeIndexDic[TerrainTypeIndex]);
// Debug.Log($"Set Territory, cellIndex ({cellIndex}), terrain type ({TerrainTypeIndex}), name ({_terrainTypeConfig[TerrainTypeIndex].Color})");
// }
//
// private void ClearCellRegion(int cellIndex)
// {
// var map = EditorMap.Instance.Current;
// if (area.positions.Contains(map.MapData.BlockIndex2Position(cellIndex)))
// {
// area.positions.Remove(map.MapData.BlockIndex2Position(cellIndex));
// map.TerrainGridSystem.CellHideRegionSurface(cellIndex);
// }
// }
//
// private void ClearCellTerritory(TerrainGridSystem tgs, int cellIndex)
// {
// var map = EditorMap.Instance.Current;
// map.TerrainGridSystem.CellSetBorderVisible(cellIndex, true);
//
// var tIndex = tgs.CellGetTerritoryIndex(cellIndex);
// if (tIndex != -1)
// {
// tgs.CellSetTerritory(cellIndex, -1);
// map?.SetTerrainType(cellIndex, -1);
// Debug.Log($"Cell Clear Territory, cellIndex({cellIndex})");
//
// }
// }
//
// [Button("刷新网格")]
// private void Refresh()
// {
// }
// public void OnMapScaleChanged(Vector2 MapScale)
// {
// _tgs.regularHexagons = false;
// currentMapData.width = GetHexCount(MapScale.x, _tgs.regularHexagonsWidth, false);
// currentMapData.height = GetHexCount(MapScale.y, _tgs.regularHexagonsWidth, true);
// _tgs.rowCount = currentMapData.height;
// _tgs.columnCount = currentMapData.width;
// _tgs.regularHexagons = true;
// }
private int GetHexCount(float totalLength, float hexWidth, bool isDiagonal)
{
if (isDiagonal)
return Mathf.FloorToInt((4 * totalLength) / (3 * hexWidth) - 1 / 3f);
else
{
var sqrt3 = Mathf.Sqrt(3);
var countInFloat = (totalLength - (sqrt3 / 4 * hexWidth)) / (Mathf.Sqrt(3) / 2 * hexWidth);
return Mathf.FloorToInt(countInFloat);
}
}
}