using System.Collections;
using System.Collections.Generic;
using TGS;
using UnityEngine;
using UnityEngine.InputSystem;
///
/// 新营地建造Demo
///
public class NewCampBuildDemo : MonoBehaviour
{
///
/// 状态
///
private enum E_State
{
None,
///
/// 拖动状态
///
Drag,
///
/// 停止状态
///
Stop,
}
///
/// 网格系统
///
[SerializeField]
private TerrainGridSystem gridSystem;
///
/// 测试建筑
///
[SerializeField]
private Transform tsGo;
///
/// 格子状态字典
///
private Dictionary dicGridState;
///
/// 记录所有格子字典
///
private Dictionary dicAllGrid;
///
/// 格子拖动状态编辑
///
private HashSet setGridDragEdit;
///
/// 测试建筑物
///
private Building building;
///
/// 射线检测点列表
///
private RaycastHit[] hits;
///
/// 当前状态
///
private E_State state;
private void Awake()
{
setGridDragEdit = new HashSet();
dicAllGrid = new Dictionary();
dicGridState = new Dictionary();
foreach (Cell cell in gridSystem.cells)
{
Vector2Int vector2Int = new(cell.row, cell.column);
dicAllGrid[vector2Int] = cell;
dicGridState[vector2Int] = E_GridState.Empty;
}
building = new Building(tsGo.gameObject, Vector2Int.zero, Vector2Int.up);
gridSystem.allowHighlightWhileDragging = true;
hits = new RaycastHit[1];
state = E_State.None;
}
private void Update()
{
UpdateInput();
if (state == E_State.Drag)
{
ClearDragCell(); // 还原拖动的格子
Vector2 mousePos = Mouse.current.position.ReadValue();
// 发射射线
Ray ray = Camera.main.ScreenPointToRay(mousePos);
int hitNum = Physics.RaycastNonAlloc(ray.origin, ray.direction, hits);
if (hitNum > 0)
{
RaycastHit firstHit = hits[0];
Cell centerCell = gridSystem.CellGetAtPosition(firstHit.point, true); // 中心点
if (null != centerCell)
{
foreach (Vector2Int offset in building.GridOffsets)
{
Vector2Int newPos = new Vector2Int(centerCell.row, centerCell.column) + offset;
if (dicAllGrid.TryGetValue(newPos, out Cell cell))
{
setGridDragEdit.Add(cell);
gridSystem.CellSetColor(cell, Color.green); // 绿色
Vector3 pos = gridSystem.CellGetPosition(cell);
if (Vector2Int.zero == offset)
tsGo.position = new Vector3(pos.x, tsGo.position.y, pos.z);
}
}
}
}
}
if (state == E_State.Drag && Input.GetMouseButtonDown(0))
{
Build();
}
}
private void UpdateInput()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
gridSystem.showCells = false;
gridSystem.showTerritories = false;
gridSystem.showTerritoriesInteriorBorders = false;
gridSystem.HideAll();
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
gridSystem.showCells = true;
gridSystem.showTerritories = true;
gridSystem.showTerritoriesInteriorBorders = true;
gridSystem.ShowAll();
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
state = E_State.Drag;
Debug.Log("进入拖动状态");
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
state = E_State.None;
Debug.Log("进入普通状态");
}
}
///
/// 清除拖动的格子
///
private void ClearDragCell()
{
foreach (Cell cell in setGridDragEdit)
{
if (dicGridState.TryGetValue(new Vector2Int(cell.row, cell.column), out E_GridState cellState))
SetCellState(cell, cellState);
}
setGridDragEdit.Clear();
}
///
/// 校验是否可以放置
///
///
private bool CheckBuild(Cell centerCell, Vector2Int[] offsets, out List listCell)
{
listCell = new List();
if (null == centerCell)
return false;
foreach (Vector2Int offset in offsets)
{
Vector2Int newPos = new Vector2Int(centerCell.row, centerCell.column) + offset;
if (!dicGridState.TryGetValue(newPos, out E_GridState gridState))
return false;
if (E_GridState.Using == gridState)
return false;
if(dicAllGrid.TryGetValue(newPos,out Cell cell))
listCell.Add(cell);
}
return true;
}
///
/// 建造
///
private void Build()
{
// 左键按下
Vector2 mousePos = Mouse.current.position.ReadValue();
// 发射射线
Ray ray = Camera.main.ScreenPointToRay(mousePos);
int hitNum = Physics.RaycastNonAlloc(ray.origin, ray.direction, hits);
if (hitNum <= 0)
return;
RaycastHit firstHit = hits[0];
Cell centerCell = gridSystem.CellGetAtPosition(firstHit.point, true); // 中心点
if (!CheckBuild(centerCell, building.GridOffsets, out List| listCell))
{
Debug.LogError("该位置不能放置");
return;
}
foreach (Cell cell in listCell)
{
SetCellState(cell, E_GridState.Using);
}
setGridDragEdit.Clear();
state = E_State.None;
}
///
/// 设置格子状态
///
private void SetCellState(Cell cell, E_GridState gridState)
{
dicGridState[new Vector2Int(cell.row, cell.column)] = gridState;
switch (gridState)
{
case E_GridState.Empty:
{
gridSystem.CellSetColor(cell, new Color(0f, 0f, 0f, 0f)); // 透明
}
break;
case E_GridState.Drag:
{
gridSystem.CellSetColor(cell, Color.green); // 绿色
}
break;
case E_GridState.DragNot:
{
gridSystem.CellSetColor(cell, Color.red); // 红色
}
break;
case E_GridState.Using:
{
gridSystem.CellSetColor(cell, Color.blue); // 蓝色
}
break;
}
}
}
///
/// 建筑物
///
public class Building
{
///
/// 格子偏移
///
public Vector2Int[] GridOffsets;
public GameObject Go;
///
/// 格子索引
///
public int CellIndex = -1;
public Building(GameObject go, params Vector2Int[] gridOffsets)
{
GridOffsets = gridOffsets;
Go = go;
}
}
///
/// 格子状态
///
public enum E_GridState
{
///
/// 空
///
Empty,
///
/// 拖动可用
///
Drag,
///
/// 拖动不可用
///
DragNot,
///
/// 已经使用
///
Using,
} | | | | |