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

88 lines
2.6 KiB
C#
Raw Normal View History

2023-11-15 13:30:13 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using Framework;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using TGS;
using Debug = DebugUtil;
using Object = UnityEngine.Object;
using UnityEditor;
[ExecuteAlways]
public class MapSceneEditor : MonoBehaviour
{
2023-11-15 16:24:53 +08:00
private const float UPP = 100;
2023-11-15 13:30:13 +08:00
private TerrainGridSystem _tgs;
private GridEditor _gridEditor;
2023-11-15 16:24:53 +08:00
2023-11-15 13:30:13 +08:00
[SerializeField]
[HideInInspector]
2023-11-15 16:24:53 +08:00
private string _mapDataJson = "";
2023-11-15 13:30:13 +08:00
private MapData _mapData = new();
public MapData mapData => _mapData;
2023-11-15 16:24:53 +08:00
//[BoxGroup("地图编辑器/Split/Right/地图基本结构")]
[TableColumnWidth(57, Resizable = false)]
[PreviewField(Alignment = ObjectFieldAlignment.Center)]
[AssetList(Path = (Constants.MAP_TEXTURE_PATH_WITHOUT_ASSET))]
//[EnableIf("canRebuild")]
[OnValueChanged("OnMapMaterialChanged")]
[LabelText("地面图")]
[Indent]
public Material groundMaterial;
[LabelText("地图尺寸")]
[LabelWidth(100)]
[Indent]
[ReadOnly]
public Vector2 MapScale;
private void OnMapMaterialChanged()
2023-11-15 13:30:13 +08:00
{
2023-11-15 16:24:53 +08:00
if (groundMaterial != null&&groundMaterial.mainTexture!=null)
2023-11-15 13:30:13 +08:00
{
2023-11-15 16:24:53 +08:00
_tgs.GetComponent<MeshRenderer>().sharedMaterial = groundMaterial;
MapScale = (new Vector2(groundMaterial.mainTexture.width,
groundMaterial.mainTexture.height) / UPP);
_tgs.transform.localScale = new Vector3(MapScale.x,MapScale.y,1) ;
_tgs.regularHexagons = false;
var width = GetHexCount(MapScale.x, _tgs.regularHexagonsWidth, false);
var height = GetHexCount(MapScale.y, _tgs.regularHexagonsWidth, true);
_tgs.rowCount = height;
_tgs.columnCount = width;
_tgs.regularHexagons = true;
2023-11-15 13:30:13 +08:00
}
2023-11-15 16:24:53 +08:00
}
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);
}
}
private void Awake()
{
2023-11-15 13:30:13 +08:00
_tgs = Object.FindObjectOfType<TerrainGridSystem>();
if (_tgs == null)
{
Debug.LogError("Terrain Grid System Is Null!");
}
_gridEditor = Object.FindObjectOfType<GridEditor>();
if (_gridEditor == null)
{
Debug.LogError("Grid Editor is null!");
}
}
}