175 lines
6.9 KiB
C#
175 lines
6.9 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 GameObject startPositionObject;
|
|
[HideInInspector] public MapListener.RoomPrefabs AreaRoomPrefab;
|
|
[HideInInspector] public MapListener.RoomPrefabs singleRoomPrefab;
|
|
[HideInInspector] public MapListener.RoomPrefabs 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 MapListener.RoomPrefabs choose_room_prefab = null;
|
|
private MapBuildPlacer build_Placer;
|
|
|
|
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 (!build_Placer.m_recv_placer_is_snap_to_grid()) return; // 是否正在吸附单元格
|
|
if (build_Placer.m_recv_is_trigger_with_has_build()) return; // 是否与已有建筑物有交集
|
|
if (!Input.GetMouseButtonDown(0)) return;
|
|
Vector2Int snap_grid_position = build_Placer.m_recv_placer_get_snap_grid_position_for_SO();
|
|
// print("snap_grid_position: " + snap_grid_position);
|
|
// print("choose_room_type: " + choose_room_prefab.roomType);
|
|
// mapListener.m_send_SOMap_create_a_room(snap_grid_position, choose_room_prefab.roomType);
|
|
// self__destroy_temp_grid_and_variable();
|
|
// print("hello");
|
|
}
|
|
|
|
|
|
private void self__destroy_temp_grid_and_variable()
|
|
{
|
|
foreach (Transform child in neighbor_container) // 清理方向格子, 移除容器下所有格子
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
if (current_build_room) // 清理临时生成的建筑物
|
|
{
|
|
Destroy(current_build_room);
|
|
}
|
|
|
|
isBuild = false;
|
|
choose_room_prefab = null;
|
|
}
|
|
|
|
private void self_on_mouse_down_right_cancel() // 鼠标右键取消创建
|
|
{
|
|
if (!isBuild) return;
|
|
if (!Input.GetMouseButtonDown(1)) return;
|
|
self__destroy_temp_grid_and_variable();
|
|
}
|
|
|
|
private void self_create_direction_grid()
|
|
{
|
|
// 需要创建格子的坐标
|
|
List<Vector2Int> neighbor_position_list = new List<Vector2Int>();
|
|
// 方向列表:上下左右
|
|
List<Vector2Int> direction_list = new List<Vector2Int>
|
|
{
|
|
new Vector2Int(0, 1), // 上
|
|
new Vector2Int(0, -1), // 下
|
|
new Vector2Int(-1, 0), // 左
|
|
new Vector2Int(1, 0), // 右
|
|
};
|
|
// 收集已有房间的格子坐标
|
|
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;
|
|
// print("neighbor_position: " + neighbor_position);
|
|
if (neighbor_position.y >= 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.prefab,
|
|
position: (startPositionObject.transform.position +
|
|
new Vector3(_position.x, _position.y, 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.self__create_target_room(); // 生成目标房间
|
|
}
|
|
|
|
// 设置排序图层
|
|
private void self__set_placer_sort_layer(string layerName)
|
|
{
|
|
Transform placer_sprite_render = build_Placer.transform.Find("RenderContainer/RoomSpriteRender");
|
|
Transform child_ui = build_Placer.transform.Find("RenderContainer/UI");
|
|
Transform render_container = build_Placer.transform.Find("RenderContainer");
|
|
render_container.gameObject.SetActive(false);
|
|
placer_sprite_render.GetComponent<SpriteRenderer>().sortingLayerName = layerName;
|
|
foreach (Transform child in child_ui.transform)
|
|
{
|
|
if (!child.GetComponent<SpriteRenderer>()) continue;
|
|
child.GetComponent<SpriteRenderer>().sortingLayerName = layerName;
|
|
}
|
|
}
|
|
|
|
private void self__create_target_room()
|
|
{
|
|
// 挂载脚本
|
|
build_Placer = current_build_room.AddComponent<MapBuildPlacer>();
|
|
build_Placer.startPositionObject = startPositionObject;
|
|
build_Placer.cellSize = cellSize;
|
|
build_Placer.mainCamera = mainCamera;
|
|
build_Placer.buildRoomType = choose_room_prefab.roomType;
|
|
build_Placer.mapListener = mapListener;
|
|
// build_Placer.buildableCellLayer = LayerMask.NameToLayer("BuildableCell");
|
|
this.self__set_placer_sort_layer("UseBuild");
|
|
}
|
|
|
|
public void m_ui_button_start_build()
|
|
{
|
|
if (isBuild) return;
|
|
choose_room_prefab = singleRoomPrefab;
|
|
self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
|
|
self_create_room_follow_mouse(choose_room_prefab.prefab); // 生成一个跟随鼠标移动的建筑物
|
|
isBuild = true;
|
|
}
|
|
|
|
public void m_ui_button_start_build_two_cell()
|
|
{
|
|
if (isBuild) return;
|
|
choose_room_prefab = CageRoomPrefab;
|
|
self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
|
|
self_create_room_follow_mouse(choose_room_prefab.prefab);
|
|
isBuild = true;
|
|
}
|
|
} |