터널 연장 로직 개선

This commit is contained in:
2026-01-15 22:46:40 +09:00
parent 2e57fe09ce
commit 394bbe64a2
10 changed files with 935 additions and 618 deletions

View File

@@ -11,33 +11,88 @@ public class BuildManager : MonoBehaviour
public struct TurretData
{
public string turretName;
public bool isTunnel; // [추가] 터널 여부 체크
public bool isTunnel;
public GameObject finalPrefab;
public GameObject ghostPrefab;
public float buildTime;
public Vector2Int size;
}
public bool IsBuildMode => isBuildMode;
[Header("Settings")]
[SerializeField] private float cellSize = 1f;
public float cellSize = 1f;
public float tunnelHeight = 3f;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private GameObject constructionSitePrefab; // 공용 토대 프리팹
[Header("Current Selection")]
[SerializeField] private TurretData selectedTurret; // 현재 선택된 타워 데이터
[SerializeField] private bool isBuildMode = false;
[SerializeField] private LayerMask playerLayer; // 플레이어의 레이어
[SerializeField] private GameObject constructionSitePrefab;
[SerializeField] private LayerMask playerLayer;
[Header("Turret Library")]
[SerializeField] private List<TurretData> turretLibrary; // 인스펙터에서 여러 타워 등록
[SerializeField] private List<TurretData> turretLibrary;
[SerializeField] private TurretData selectedTurret;
private GameObject _ghostInstance;
private Material _ghostMaterial;
private HashSet<Vector2Int> _occupiedNodes = new HashSet<Vector2Int>();
private bool _isBuildMode = false;
public bool IsBuildMode => _isBuildMode;
// 좌표 레지스트리 (물리 탐색 대체)
private Dictionary<Vector3Int, TunnelNode> _tunnelRegistry = new Dictionary<Vector3Int, TunnelNode>();
private HashSet<Vector3Int> _occupiedNodes = new HashSet<Vector3Int>();
private PlayerInputActions _inputActions;
private Vector3Int _currentGridPos;
private float _currentY;
// 게임 시작 시 기존 터널들 등록 (반드시 필요!)
void Start()
{
// 1. 씬에 배치된 모든 터널을 찾아 등록하고 연결합니다.
RegisterAllExistingTunnels();
}
public void RegisterTunnel(Vector3Int pos, TunnelNode node)
{
if (!_tunnelRegistry.ContainsKey(pos))
{
_tunnelRegistry.Add(pos, node);
_occupiedNodes.Add(pos);
Debug.Log($"<color=green>[Registry]</color> {pos} 좌표에 {node.name} 등록 완료");
}
}
// 씬에 이미 배치된 터널들을 등록하는 함수 (Start에서 호출)
public void RegisterAllExistingTunnels()
{
_tunnelRegistry.Clear();
_occupiedNodes.Clear();
TunnelNode[] nodes = FindObjectsByType<TunnelNode>(FindObjectsSortMode.None);
HashSet<Transform> roots = new HashSet<Transform>();
foreach (var node in nodes)
{
Transform root = node.transform.parent.parent; // Tunnel (1)
if (!roots.Contains(root))
{
Vector3Int pos = WorldToGrid3D(root.position);
RegisterTunnel(pos, node);
roots.Add(root);
}
}
// 모든 등록이 끝난 후 '동시에' 연결
foreach (var node in nodes) node.LinkVertical();
}
// [핵심 수정] 노드의 위치가 아닌, 터널 부모의 위치를 넣어도 정확한 격자가 나오도록 함
public Vector3Int WorldToGrid3D(Vector3 worldPos)
{
int x = Mathf.FloorToInt(worldPos.x / cellSize);
int z = Mathf.FloorToInt(worldPos.z / cellSize);
// Y값은 tunnelHeight로 나눈 뒤 반올림(Round)하여 오차 극복
int y = Mathf.RoundToInt(worldPos.y / tunnelHeight);
return new Vector3Int(x, y, z);
}
void Awake()
{
@@ -47,234 +102,112 @@ public class BuildManager : MonoBehaviour
void OnEnable()
{
_inputActions.Player.Build.performed += OnBuildPerformed;
_inputActions.Player.Cancel.performed += OnCancelPerformed;
_inputActions.Player.ToggleBuild.performed += OnTogglePerformed;
// 최신 Input System 콜백 등록
_inputActions.Player.Build.performed += ctx => OnBuildRequested();
_inputActions.Player.Cancel.performed += ctx => ExitBuildMode();
_inputActions.Player.ToggleBuild.performed += ctx => ToggleBuildMode();
_inputActions.Enable();
}
void OnDisable()
{
_inputActions.Disable();
}
void OnDisable() => _inputActions.Disable();
void Update()
{
if (!isBuildMode || _ghostInstance == null) return;
if (!_isBuildMode || _ghostInstance == null) return;
Vector2 mousePos = Mouse.current.position.ReadValue();
Ray ray = Camera.main.ScreenPointToRay(mousePos);
UpdateGhostPosition();
}
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer))
private void UpdateGhostPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
int tunnelMask = LayerMask.GetMask("Tunnel");
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer | tunnelMask))
{
Vector2Int gridPos = WorldToGrid(hit.point);
_ghostInstance.transform.position = GridToWorld(gridPos, selectedTurret.size);
Vector2Int xz = WorldToGrid(hit.point);
float targetY = 0.05f;
int floor = 0;
// [수정] CanBuild 호출 인자를 selectedTurret 전체로 변경
bool canPlace = CanBuild(gridPos, selectedTurret);
// 고스트 색상 변경
if (_ghostMaterial != null)
// 터널 조준 시 지하로 스냅
if (((1 << hit.collider.gameObject.layer) & tunnelMask) != 0)
{
_ghostMaterial.color = canPlace ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.5f);
xz = WorldToGrid(hit.collider.transform.position);
targetY = hit.collider.transform.position.y - tunnelHeight;
floor = Mathf.RoundToInt(targetY / tunnelHeight);
}
// 미리보기 타워의 사거리 표시기 제어
TowerRangeOverlay overlay = _ghostInstance.GetComponentInChildren<TowerRangeOverlay>();
if (overlay != null)
{
overlay.ShowRange(true);
overlay.UpdateRangeScale(); // 매 프레임 스케일 보정
}
_currentGridPos = new Vector3Int(xz.x, floor, xz.y);
_currentY = targetY;
_ghostInstance.transform.position = new Vector3(xz.x * cellSize, targetY, xz.y * cellSize);
bool canBuild = CanBuildVertical(_currentGridPos);
_ghostMaterial.color = canBuild ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.5f);
}
}
// --- Input Callbacks ---
private void OnTogglePerformed(InputAction.CallbackContext context) => ToggleBuildMode();
private void OnCancelPerformed(InputAction.CallbackContext context)
private bool CanBuildVertical(Vector3Int pos)
{
if (isBuildMode) ToggleBuildMode();
if (_occupiedNodes.Contains(pos)) return false;
// 지하 건설 시 위층 터널 존재 여부 확인
if (pos.y < 0 && !_occupiedNodes.Contains(pos + Vector3Int.up)) return false;
// 플레이어와 겹치는지 확인
return !Physics.CheckBox(new Vector3(pos.x * cellSize, pos.y * tunnelHeight + 1f, pos.z * cellSize),
new Vector3(0.45f, 0.5f, 0.45f), Quaternion.identity, playerLayer);
}
private void OnBuildPerformed(InputAction.CallbackContext context)
private void OnBuildRequested()
{
if (!isBuildMode || EventSystem.current.IsPointerOverGameObject()) return;
if (!_isBuildMode || EventSystem.current.IsPointerOverGameObject()) return;
if (!CanBuildVertical(_currentGridPos)) return;
Vector2 mousePos = Mouse.current.position.ReadValue();
Ray ray = Camera.main.ScreenPointToRay(mousePos);
_occupiedNodes.Add(_currentGridPos);
GameObject site = Instantiate(constructionSitePrefab, _ghostInstance.transform.position, Quaternion.identity);
site.GetComponent<ConstructionSite>().Initialize(selectedTurret.finalPrefab, selectedTurret.buildTime, _currentGridPos);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer))
{
Vector2Int gridPos = WorldToGrid(hit.point);
// [수정] CanBuild 호출 인자 변경
if (CanBuild(gridPos, selectedTurret))
{
Build(gridPos, selectedTurret);
}
}
ExitBuildMode();
}
// --- Core Logic ---
private void ToggleBuildMode()
public TunnelNode GetTunnelAt(Vector3Int pos) => _tunnelRegistry.GetValueOrDefault(pos);
public Vector2Int WorldToGrid(Vector3 pos) => new Vector2Int(Mathf.FloorToInt(pos.x / cellSize), Mathf.FloorToInt(pos.z / cellSize));
private void ToggleBuildMode() { if (_isBuildMode) ExitBuildMode(); else EnterBuildMode(); }
private void EnterBuildMode()
{
isBuildMode = !isBuildMode;
if (isBuildMode) CreateGhost();
else DestroyGhost();
}
private void CreateGhost()
{
if (selectedTurret.ghostPrefab == null) return;
if (_ghostInstance != null) Destroy(_ghostInstance);
_isBuildMode = true;
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
// 고스트의 머티리얼 참조 (색상 변경용)
Renderer ghostRenderer = _ghostInstance.GetComponentInChildren<Renderer>();
if (ghostRenderer != null) _ghostMaterial = ghostRenderer.material;
// 1. 비주얼 스케일 조절
Transform visual = _ghostInstance.transform.Find("Visual");
if (visual != null)
{
visual.localScale = new Vector3(selectedTurret.size.x, 1f, selectedTurret.size.y);
}
// 2. 바닥 정렬
AlignToGround(_ghostInstance, 0f);
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
}
private void DestroyGhost()
private void ExitBuildMode()
{
if (_ghostInstance != null) Destroy(_ghostInstance);
_ghostMaterial = null;
}
private void Build(Vector2Int gridPos, TurretData data)
{
if (constructionSitePrefab == null || data.finalPrefab == null)
{
Debug.LogError("BuildManager: 프리팹 할당 상태를 확인하세요!");
return;
}
// 점유 노드 등록
for (int x = 0; x < data.size.x; x++)
for (int y = 0; y < data.size.y; y++)
_occupiedNodes.Add(new Vector2Int(gridPos.x + x, gridPos.y + y));
// 토대 생성
GameObject siteObj = Instantiate(constructionSitePrefab, GridToWorld(gridPos, data.size), Quaternion.identity);
// 토대 비주얼 스케일 조절
Transform visual = siteObj.transform.Find("Visual");
if (visual != null)
{
visual.localScale = new Vector3(data.size.x, 1f, data.size.y);
}
// 토대 바닥 정렬
AlignToGround(siteObj, 0f);
// 컴포넌트 초기화
ConstructionSite siteScript = siteObj.GetComponent<ConstructionSite>();
if (siteScript != null)
{
siteScript.Initialize(data.finalPrefab, data.buildTime, data.size);
}
ToggleBuildMode();
}
// --- Utilities ---
private bool CanBuild(Vector2Int startPos, TurretData data)
{
// 1. 기존 점유 노드 및 플레이어 충돌 체크
for (int x = 0; x < data.size.x; x++)
{
for (int y = 0; y < data.size.y; y++)
{
if (_occupiedNodes.Contains(new Vector2Int(startPos.x + x, startPos.y + y)))
return false;
}
}
// 플레이어와 겹치는지 체크
Vector3 center = GridToWorld(startPos, data.size);
center.y = 1f;
Vector3 halfExtents = new Vector3(data.size.x * 0.45f, 0.5f, data.size.y * 0.45f);
if (Physics.CheckBox(center, halfExtents, Quaternion.identity, playerLayer))
return false;
// 2. 터널 연결 제약 조건 체크
if (data.isTunnel)
{
return IsConnectedToExistingTunnel(startPos, data);
}
return true;
}
private bool IsConnectedToExistingTunnel(Vector2Int startPos, TurretData data)
{
// 첫 번째 터널은 자유롭게 설치 (또는 특정 구역 제한)
if (_occupiedNodes.Count == 0) return true;
Vector3 ghostWorldPos = GridToWorld(startPos, data.size);
float checkRadius = cellSize * 1.5f;
Collider[] neighbors = Physics.OverlapSphere(ghostWorldPos, checkRadius, LayerMask.GetMask("Tunnel"));
foreach (var col in neighbors)
{
TunnelNode node = col.GetComponent<TunnelNode>();
if (node != null && node.gameObject.activeInHierarchy)
{
return true;
}
}
return false;
}
public Vector2Int WorldToGrid(Vector3 worldPos) => new Vector2Int(Mathf.FloorToInt(worldPos.x / cellSize), Mathf.FloorToInt(worldPos.z / cellSize));
public Vector3 GridToWorld(Vector2Int gridPos, Vector2Int size)
{
return new Vector3(gridPos.x * cellSize + (size.x - 1) * cellSize * 0.5f, 0.05f, gridPos.y * cellSize + (size.y - 1) * cellSize * 0.5f);
}
public void AlignToGround(GameObject obj, float yOffset)
{
Transform visual = obj.transform.Find("Visual");
if (visual == null) return;
MeshRenderer[] renderers = visual.GetComponentsInChildren<MeshRenderer>();
if (renderers.Length == 0) return;
Bounds bounds = renderers[0].bounds;
foreach (var renderer in renderers)
{
bounds.Encapsulate(renderer.bounds);
}
float bottomY = bounds.min.y - obj.transform.position.y;
visual.localPosition = new Vector3(0, -bottomY + yOffset, 0);
}
private void OnDrawGizmos()
{
Gizmos.color = new Color(1, 1, 1, 0.1f);
for (int x = -10; x <= 10; x++)
for (int z = -10; z <= 10; z++)
Gizmos.DrawWireCube(new Vector3(x * cellSize, 0, z * cellSize), new Vector3(cellSize, 0.01f, cellSize));
_isBuildMode = false;
if (_ghostInstance) Destroy(_ghostInstance);
}
// BuildManager.cs 내부
public void SelectTurret(int index)
{
if (index >= 0 && index < turretLibrary.Count)
{
selectedTurret = turretLibrary[index];
if (isBuildMode) CreateGhost();
// 현재 건설 모드가 아니라면 건설 모드로 진입
if (!_isBuildMode)
{
EnterBuildMode();
}
else
{
// 이미 건설 모드라면 고스트만 교체
if (_ghostInstance) Destroy(_ghostInstance);
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
}
Debug.Log($"{selectedTurret.turretName} 선택됨");
}
}
}