using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.VFX;
using static GridSelector;
///格子信息 - 绿色块
///功能:加载格子数据,重新计算格子,显示格子,隐藏格子
public class UICellInfo : MonoBehaviour
{
public static UICellInfo Instance;
public Text txtMapWidth;
public Text txtMapHeight;
public Text txtTotalCells;
public Text txtMoveCells;
public InputField txtCellRows;
public InputField txtCellCols;
public InputField txtCellWidth;
public InputField txtCellHeight;
public bool bMapOpened;
public Text txtSelectInfo;
private void Awake()
{
Instance = this;
}
private void Start()
{
Cleanup();
}
void Cleanup()
{
txtMapWidth.text = string.Empty;
txtMapHeight.text = string.Empty;
txtTotalCells.text = string.Empty;
txtMoveCells.text = string.Empty;
txtCellRows.text = string.Empty;
txtCellCols.text = string.Empty;
txtCellWidth.text = string.Empty;
txtCellHeight.text = string.Empty;
bMapOpened = false;
}
public void CloseMap()
{
Cleanup();
UIWindow.Instance.uiCellEditor.togEdit.isOn = false;
UIWindow.Instance.uiCellEditor.togPathTest.isOn = false;
}
public void ShowCells()
{
MapManager.Instance.ShowMapGrid();
}
public void HideCells()
{
MapManager.Instance.HideMapGrid();
}
public void ShowMapCellInfo()
{
if (MapManager.Instance.map == null || MapManager.Instance.map.selector == null)
return;
if(MapManager.Instance._curMapRegions == null) return;
txtMapWidth.text = MapManager.Instance._curMapRegions.regionRowNum.ToString();
txtMapHeight.text = MapManager.Instance._curMapRegions.regionColNum.ToString();
txtCellRows.text = MapManager.Instance.map.selector.horizontalNumber.ToString();
txtCellCols.text = MapManager.Instance.map.selector.verticalNumber.ToString();
txtTotalCells.text = MapManager.Instance.map.selector.totalNumber.ToString();
txtCellWidth.text = MapManager.widthPixel.ToString();
txtCellHeight.text = MapManager.heightPixel.ToString();
txtMoveCells.text = MapManager.Instance.map.selector.moveNum.ToString();
}
///
/// 加载格子
///
public void LoadCell()
{
MapManager.Instance.LoadMapObs(MapManager.Instance._curOpenMapId);
ShowMapCellInfo();
}
///
/// 重新计算格子
///
public void ReCalculateCell()
{
int oldhorizontalNumber = MapManager.Instance.map?.selector?.horizontalNumber ?? 0;
int oldverticalNumber = MapManager.Instance.map?.selector?.verticalNumber ?? 0;
var oldRendData = MapManager.Instance.map?.selector?.GetGridData();
int width = int.Parse(txtCellRows.text);
int height = int.Parse(txtCellCols.text);
float cellW = int.Parse(txtCellWidth.text) / 100.0f;
float cellH = int.Parse(txtCellHeight.text) / 100.0f;
MapManager.Instance.GenerateMap(MapManager.Instance._curOpenMapId,width * cellW, height * cellH, cellW, cellH);
var newRendData = MapManager.Instance.map?.selector?.GetGridData();
for (int i = 0; i < MapManager.Instance.map?.selector.horizontalNumber; i++)
{
for (int j = 0; j < MapManager.Instance.map?.selector.verticalNumber; j++)
{
if (i < oldhorizontalNumber && j < oldverticalNumber)
{
int oldindex = i + j * oldhorizontalNumber;
//取出老的数据
int index = MapManager.Instance.map.selector.GetIndexByXY(i, j);
newRendData[index] = oldRendData[oldindex];
}
else {//新格子
int index = MapManager.Instance.map.selector.GetIndexByXY(i, j);
newRendData[index] = new RenderData();
newRendData[index].barrier = (int)CellType.Obstacle;
}
}
}
MapManager.Instance.map.selector.RefreshPlaneRender();
ShowMapCellInfo();
}
public void Update()
{
bool isOpen = false;
do {
if (MapManager.Instance.map == null) break;
if (MapManager.Instance.map.selector == null) break;
if (MapManager.Instance.map.selector.selectedGridIndex.Count == 1)
{
int beginIndex = MapManager.Instance.map.selector.selectedGridIndex[0];
MapManager.Instance.map.selector.GetXyByIndex(beginIndex, out int x, out int y);
txtSelectInfo.text = string.Format("所选点:{0},{1} 信息:{2}", x, y, CellTypeColors.GetAreaStr((MapManager.Instance.map.selector.dataArray[beginIndex].barrier)));
isOpen = true;
}
} while (false);
txtSelectInfo.transform.parent.gameObject.SetActive(isOpen);
}
}