156 lines
5.9 KiB
C#
Raw Normal View History

2025-10-09 22:33:05 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class MapBuilding : MonoBehaviour
{
2025-10-09 22:33:05 +08:00
[HideInInspector] public Camera mainCamera;
[HideInInspector] public GameObject startPositionObject;
[HideInInspector] public GameObject AreaRoomPrefab;
[HideInInspector] public GameObject singleRoomPrefab;
[HideInInspector] public GameObject CageRoomPrefab;
[HideInInspector] public float cellSize = 2;
[HideInInspector] public MapListener mapListener;
private Transform neighbor_container;
private GameObject current_build_room = null;
private bool isBuild = false;
2025-10-09 23:57:21 +08:00
private MapBuildPlacer buildPlacer = null;
2025-10-09 22:33:05 +08:00
private void Start()
{
neighbor_container = startPositionObject.transform.Find("neighborContainer");
}
private void Update()
{
this.self_on_mouse_down_right_cancel(); // 鼠标右键取消创建
2025-10-09 23:57:21 +08:00
this.self_on_click_down_left_yes();
}
private void self_on_click_down_left_yes()
{
if (!isBuild) return;
2025-10-10 00:53:27 +08:00
if (!buildPlacer.m_recv_placer_is_snap_to_grid()) return; // 是否正在吸附单元格
if (buildPlacer.m_recv_is_trigger_with_has_build()) return; // 是否与已有建筑物有交集
2025-10-09 23:57:21 +08:00
if (Input.GetMouseButtonDown(0))
{
print("hello");
}
2025-10-09 22:33:05 +08:00
}
private void self_on_mouse_down_right_cancel() // 鼠标右键取消创建
{
if (!isBuild) return;
if (!Input.GetMouseButtonDown(1)) return;
2025-10-10 00:53:27 +08:00
foreach (Transform child in neighbor_container) // 清理方向格子, 移除容器下所有格子
2025-10-09 22:33:05 +08:00
{
Destroy(child.gameObject);
}
2025-10-10 00:53:27 +08:00
if (current_build_room) // 清理临时生成的建筑物
2025-10-09 22:33:05 +08:00
{
Destroy(current_build_room);
}
isBuild = false;
2025-10-09 23:57:21 +08:00
buildPlacer = null;
2025-10-09 22:33:05 +08:00
}
private void self_create_direction_grid()
{
// 需要创建格子的坐标
List<Vector2Int> neighbor_position_list = new List<Vector2Int>();
// 方向列表:上下左右
List<Vector2Int> direction_list = new List<Vector2Int>
{
new Vector2Int(1, 0), // 右
new Vector2Int(0, 1), // 上
new Vector2Int(-1, 0), // 左
new Vector2Int(0, -1) // 下
};
// 收集已有房间的格子坐标
2025-10-10 00:53:27 +08:00
List<Vector2Int> has_room_position_list = mapListener.m_send_SOMap_get_position_by_has_room();
// 从已有房间的坐标推导出方向坐标列表
foreach (var hr_position in has_room_position_list)
{
foreach (var dir_position in direction_list)
{
var neighbor_position = hr_position + dir_position;
if (neighbor_position.x >= 1) continue; // 地面上的空间不生成
if (!has_room_position_list.Contains(neighbor_position))
{
neighbor_position_list.Add(neighbor_position);
}
}
}
// 生成四周的空间预制件
foreach (var _position in neighbor_position_list)
{
GameObject room = Instantiate<GameObject>(
2025-10-09 22:33:05 +08:00
AreaRoomPrefab,
position: (startPositionObject.transform.position +
new Vector3(_position.y, _position.x, 0) * cellSize),
rotation: Quaternion.identity);
2025-10-09 22:33:05 +08:00
neighbor_container = startPositionObject.transform.Find("neighborContainer");
room.name = $"Cell_{_position.x}.{_position.y}";
room.transform.SetParent(neighbor_container);
2025-10-09 04:03:32 +08:00
MapBuildableCell roomScriptsComponent = room.GetComponent<MapBuildableCell>();
2025-10-09 22:33:05 +08:00
roomScriptsComponent.m_set_grid_position(_position);
}
}
2025-10-08 20:48:27 +08:00
// 生成跟随鼠标移动的建筑物
2025-10-09 22:33:05 +08:00
private void self_create_room_follow_mouse(GameObject roomPrefab)
2025-10-08 20:48:27 +08:00
{
2025-10-09 04:03:32 +08:00
Vector3 mouse_world_position = mainCamera.ScreenToWorldPoint(Input.mousePosition);
2025-10-08 20:48:27 +08:00
mouse_world_position.z = 0;
2025-10-09 23:57:21 +08:00
current_build_room = Instantiate(roomPrefab, mouse_world_position, Quaternion.identity);
2025-10-09 22:33:05 +08:00
current_build_room.name = "BuildingPreview";
current_build_room.transform.SetParent(startPositionObject.transform);
current_build_room.layer = LayerMask.NameToLayer("UseBuild");
2025-10-09 23:57:21 +08:00
this.m__create_target_room(); // 生成目标房间
2025-10-09 22:33:05 +08:00
}
2025-10-08 20:48:27 +08:00
2025-10-09 23:57:21 +08:00
private void m__create_target_room()
2025-10-09 22:33:05 +08:00
{
2025-10-09 04:03:32 +08:00
// 挂载脚本
2025-10-09 23:57:21 +08:00
buildPlacer = current_build_room.GetComponent<MapBuildPlacer>();
buildPlacer.startPositionObject = startPositionObject;
buildPlacer.cellSize = cellSize;
buildPlacer.mainCamera = mainCamera;
buildPlacer.mapListener = mapListener;
2025-10-09 22:33:05 +08:00
// 设置排序图层
2025-10-09 23:57:21 +08:00
Transform placer_sprite_render = buildPlacer.transform.Find("RenderContainer/RoomSpriteRender");
Transform child_ui = buildPlacer.transform.Find("RenderContainer/UI");
Transform render_container = buildPlacer.transform.Find("RenderContainer");
2025-10-09 22:33:05 +08:00
render_container.gameObject.SetActive(false);
placer_sprite_render.GetComponent<SpriteRenderer>().sortingLayerName = "UseBuild";
foreach (Transform child in child_ui.transform)
{
if (!child.GetComponent<SpriteRenderer>()) continue;
child.GetComponent<SpriteRenderer>().sortingLayerName = "UseBuild";
}
2025-10-08 20:48:27 +08:00
}
2025-10-10 00:53:27 +08:00
public void m_ui_button_start_build()
{
2025-10-09 22:33:05 +08:00
if (isBuild) return;
self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
self_create_room_follow_mouse(singleRoomPrefab); // 生成一个跟随鼠标移动的建筑物
isBuild = true;
2025-10-09 04:03:32 +08:00
}
2025-10-10 00:53:27 +08:00
public void m_ui_button_start_build_two_cell()
2025-10-09 04:03:32 +08:00
{
2025-10-09 22:33:05 +08:00
if (isBuild) return;
self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
self_create_room_follow_mouse(CageRoomPrefab);
isBuild = true;
}
}