2025-10-08 20:48:27 +08:00

60 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapBuildPlacer : MonoBehaviour
{
[Tooltip("地图SO数据")] public HomeMapSO SO_HomeMap;
[Tooltip("坐标起始点")] public GameObject startPositionObject;
public int cellSize = 2;
private void Update()
{
// 跟随鼠标移动
Vector3 mouse_world_position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouse_world_position.z = 0;
transform.position = mouse_world_position;
// // 计算距离最近的格子坐标
// Vector2Int nearest = get_nearest_buildable_cell(mouse_world_position);
// if (nearest != Vector2Int.zero)
// {
// // 吸附到格子位置
// Vector3 cell_world_pos = startPositionObject.transform.position +
// new Vector3(nearest.y, nearest.x, 0) * cellSize;
// transform.position = cell_world_pos;
// }
// else
// {
// // 不在可建造范围内,跟随鼠标自由移动
// transform.position = mouse_world_position;
// }
}
// private Vector2Int get_nearest_buildable_cell(Vector3 mouse_pos)
// {
// var buildable_cells = SO_HomeMap.get_position_by_can_build();
//
// if (buildable_cells == null || buildable_cells.Count == 0)
// return Vector2Int.zero;
//
// float min_distance = float.MaxValue;
// Vector2Int nearest = Vector2Int.zero;
//
// foreach (var cell in buildable_cells)
// {
// Vector3 cell_world_pos = startPositionObject.transform.position +
// new Vector3(cell.y, cell.x, 0) * cellSize;
// float dist = Vector2.Distance(mouse_pos, cell_world_pos);
//
// if (dist < cellSize * 0.5f && dist < min_distance)
// {
// min_distance = dist;
// nearest = cell;
// }
// }
// return nearest;
// }
}