180 lines
7.8 KiB
C#
180 lines
7.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class MapBuilding : MonoBehaviour
|
|
{
|
|
[HideInInspector] public Camera mainCamera;
|
|
[HideInInspector] public GameObject startPositionObject;
|
|
[HideInInspector] public float cellSize = 2;
|
|
[HideInInspector] public MapListener mapListener;
|
|
private Transform neighbor_container;
|
|
|
|
private bool isBuild = false;
|
|
private MapListener.RoomPrefabs choose_room_prefab = null;
|
|
private MapPlacerItem map_placer_item;
|
|
private MapBuildPlacer build_Placer;
|
|
private MapListener.RoomPrefabs single_room_dataclass;
|
|
private MapListener.RoomPrefabs area_room_dataclass;
|
|
private MapListener.RoomPrefabs cage_room_dataclass;
|
|
|
|
private void Start()
|
|
{
|
|
neighbor_container = startPositionObject.transform.Find("neighborContainer");
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
this.m_self_on_mouse_down_right_cancel(); // 鼠标右键取消创建
|
|
this.self_on_click_down_left_yes(); // 鼠标左键确认创建
|
|
}
|
|
|
|
public void m_recv_initialize_room_prefabs_list(List<MapListener.RoomPrefabs> room_dataclass_list)
|
|
{
|
|
single_room_dataclass = mm_self_find_room_prefabs_dataclass(HomeMapSO.RoomType.SingleRoom);
|
|
cage_room_dataclass = mm_self_find_room_prefabs_dataclass(HomeMapSO.RoomType.CageRoom);
|
|
area_room_dataclass = mm_self_find_room_prefabs_dataclass(HomeMapSO.RoomType.AreaRoom);
|
|
|
|
return;
|
|
|
|
MapListener.RoomPrefabs mm_self_find_room_prefabs_dataclass(HomeMapSO.RoomType _room_type)
|
|
{
|
|
return room_dataclass_list.Find(item => item.roomType == _room_type);
|
|
}
|
|
}
|
|
|
|
|
|
private void self_on_click_down_left_yes()
|
|
{
|
|
if (!Input.GetMouseButtonDown(0)) return; // 鼠标左键按下
|
|
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; // 是否与已有建筑物有交集
|
|
Vector2Int snap_grid_position = build_Placer.m_recv_placer_get_snap_grid_position_for_SO();
|
|
mapListener.m_send_SOMap_create_a_room(snap_grid_position, choose_room_prefab.prefab);
|
|
this.mm_self__destroy_temp_grid_and_variable(); // 销毁临时格子
|
|
print("hello");
|
|
}
|
|
|
|
|
|
private void m_self_on_mouse_down_right_cancel() // 鼠标右键取消创建
|
|
{
|
|
if (!Input.GetMouseButtonDown(1)) return; // 鼠标右键按下
|
|
if (!isBuild) return; // 当前不在创建状态
|
|
this.mm_self__destroy_temp_grid_and_variable(); // 销毁临时格子
|
|
return;
|
|
}
|
|
|
|
private void mm_self__destroy_temp_grid_and_variable()
|
|
{
|
|
foreach (Transform child in neighbor_container) // 清理方向格子, 移除容器下所有格子
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
isBuild = false; // 取消创建状态
|
|
choose_room_prefab = null; // 清理已选择房间的预制体
|
|
Destroy(build_Placer.gameObject);
|
|
}
|
|
|
|
private void m_self_create_target_room_follow_mouse(GameObject roomPrefab) // 生成跟随鼠标移动的建筑物
|
|
{
|
|
Vector3 mouse_world_position = mainCamera.ScreenToWorldPoint(Input.mousePosition);
|
|
mouse_world_position.z = 0;
|
|
GameObject build_target_room = Instantiate(roomPrefab, mouse_world_position, Quaternion.identity);
|
|
build_target_room.name = "BuildingPreview";
|
|
build_target_room.transform.SetParent(startPositionObject.transform);
|
|
build_target_room.layer = LayerMask.NameToLayer("UseBuild");
|
|
mm_self__create_target_room(build_target_room); // 生成目标房间
|
|
return;
|
|
|
|
void mm_self__create_target_room(GameObject _build_room) // 挂载脚本
|
|
{
|
|
build_Placer = _build_room.AddComponent<MapBuildPlacer>();
|
|
build_Placer.startPositionObject = startPositionObject;
|
|
build_Placer.cellSize = cellSize;
|
|
build_Placer.mainCamera = mainCamera;
|
|
build_Placer.mapListener = mapListener;
|
|
mmm_self__set_placer_sort_layer("UseBuild");
|
|
}
|
|
|
|
void mmm_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 m_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 has_room_position in has_room_position_list) // 从已有房间的坐标推导出方向坐标列表
|
|
{
|
|
foreach (var dir_position in direction_list)
|
|
{
|
|
var neighbor_position = has_room_position + dir_position;
|
|
if (neighbor_position.y >= 1) continue; // 地面上的空间不生成
|
|
if (has_room_position_list.Contains(neighbor_position)) continue; // 已有房间的格子不生成
|
|
neighbor_position_list.Add(neighbor_position);
|
|
}
|
|
}
|
|
|
|
foreach (var _position in neighbor_position_list) // 生成四周的空间预制件
|
|
{
|
|
mm_self_create_a_area_room(_position);
|
|
}
|
|
|
|
return;
|
|
|
|
GameObject mm_self_create_a_area_room(Vector2Int __position)
|
|
{
|
|
GameObject _room = Instantiate<GameObject>(area_room_dataclass.prefab,
|
|
position: (startPositionObject.transform.position +
|
|
new Vector3(__position.x, __position.y, 0) * cellSize),
|
|
rotation: Quaternion.identity);
|
|
neighbor_container = startPositionObject.transform.Find("neighborContainer");
|
|
_room.name = $"AreaGrid_{__position.x}.{__position.y}";
|
|
_room.transform.SetParent(neighbor_container);
|
|
MapBuildableCell roomScriptsComponent = _room.GetComponent<MapBuildableCell>();
|
|
roomScriptsComponent.m_set_grid_position(__position);
|
|
return _room;
|
|
}
|
|
}
|
|
|
|
public void m_ui_button_start_build()
|
|
{
|
|
if (isBuild) return;
|
|
choose_room_prefab = single_room_dataclass;
|
|
map_placer_item = choose_room_prefab.prefab.GetComponent<MapPlacerItem>();
|
|
m_self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
|
|
m_self_create_target_room_follow_mouse(choose_room_prefab.prefab); // 生成一个跟随鼠标移动的建筑物
|
|
isBuild = true;
|
|
}
|
|
|
|
|
|
public void m_ui_button_start_build_two_cell()
|
|
{
|
|
if (isBuild) return;
|
|
choose_room_prefab = cage_room_dataclass;
|
|
map_placer_item = choose_room_prefab.prefab.GetComponent<MapPlacerItem>();
|
|
m_self_create_direction_grid(); // 在已有建筑物四周生成可建造的格子
|
|
m_self_create_target_room_follow_mouse(choose_room_prefab.prefab);
|
|
isBuild = true;
|
|
}
|
|
} |