162 lines
5.8 KiB
C#
162 lines
5.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class MapBuilding : MonoBehaviour
|
|
{
|
|
[HideInInspector] public Camera mainCamera;
|
|
[HideInInspector] public HomeMapSO SOHomeMap;
|
|
[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;
|
|
private MapBuildPlacer buildPlacer = null;
|
|
|
|
private void Start()
|
|
{
|
|
neighbor_container = startPositionObject.transform.Find("neighborContainer");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
this.self_on_mouse_down_right_cancel(); // 鼠标右键取消创建
|
|
this.self_on_click_down_left_yes();
|
|
}
|
|
|
|
private void self_on_click_down_left_yes()
|
|
{
|
|
if (!isBuild) return;
|
|
if (!buildPlacer.m_get_placer_is_snap()) return;
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
print("hello");
|
|
}
|
|
}
|
|
|
|
private void self_on_mouse_down_right_cancel() // 鼠标右键取消创建
|
|
{
|
|
if (!isBuild) return;
|
|
if (!Input.GetMouseButtonDown(1)) return;
|
|
// 清理方向格子
|
|
foreach (Transform child in neighbor_container)
|
|
{
|
|
// 移除容器下所有格子
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
// 清理临时生成的建筑物
|
|
if (current_build_room)
|
|
{
|
|
Destroy(current_build_room);
|
|
}
|
|
|
|
isBuild = false;
|
|
buildPlacer = null;
|
|
}
|
|
|
|
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) // 下
|
|
};
|
|
// 收集已有房间的格子坐标
|
|
List<Vector2Int> has_room_position_list = SOHomeMap.m_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>(
|
|
AreaRoomPrefab,
|
|
position: (startPositionObject.transform.position +
|
|
new Vector3(_position.y, _position.x, 0) * cellSize),
|
|
rotation: Quaternion.identity);
|
|
neighbor_container = startPositionObject.transform.Find("neighborContainer");
|
|
room.name = $"Cell_{_position.x}.{_position.y}";
|
|
room.transform.SetParent(neighbor_container);
|
|
|
|
MapBuildableCell roomScriptsComponent = room.GetComponent<MapBuildableCell>();
|
|
roomScriptsComponent.m_set_grid_position(_position);
|
|
}
|
|
}
|
|
|
|
// 生成跟随鼠标移动的建筑物
|
|
private void self_create_room_follow_mouse(GameObject roomPrefab)
|
|
{
|
|
Vector3 mouse_world_position = mainCamera.ScreenToWorldPoint(Input.mousePosition);
|
|
mouse_world_position.z = 0;
|
|
current_build_room = Instantiate(roomPrefab, mouse_world_position, Quaternion.identity);
|
|
current_build_room.name = "BuildingPreview";
|
|
current_build_room.transform.SetParent(startPositionObject.transform);
|
|
current_build_room.layer = LayerMask.NameToLayer("UseBuild");
|
|
this.m__create_target_room(); // 生成目标房间
|
|
}
|
|
|
|
private void m__create_target_room()
|
|
{
|
|
// 挂载脚本
|
|
buildPlacer = current_build_room.GetComponent<MapBuildPlacer>();
|
|
buildPlacer.startPositionObject = startPositionObject;
|
|
buildPlacer.cellSize = cellSize;
|
|
buildPlacer.mainCamera = mainCamera;
|
|
buildPlacer.mapListener = mapListener;
|
|
|
|
// 设置排序图层
|
|
Transform placer_sprite_render = buildPlacer.transform.Find("RenderContainer/RoomSpriteRender");
|
|
Transform child_ui = buildPlacer.transform.Find("RenderContainer/UI");
|
|
Transform render_container = buildPlacer.transform.Find("RenderContainer");
|
|
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";
|
|
}
|
|
}
|
|
|
|
|
|
// UI function
|
|
public void ui_button_start_build()
|
|
{
|
|
if (isBuild) return;
|
|
self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
|
|
self_create_room_follow_mouse(singleRoomPrefab); // 生成一个跟随鼠标移动的建筑物
|
|
isBuild = true;
|
|
}
|
|
|
|
public void ui_button_start_build_two_cell()
|
|
{
|
|
if (isBuild) return;
|
|
self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
|
|
self_create_room_follow_mouse(CageRoomPrefab);
|
|
isBuild = true;
|
|
}
|
|
} |