터널 연장 로직 개선
This commit is contained in:
@@ -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} 선택됨");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,33 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PlayerInteractionController : MonoBehaviour
|
||||
{
|
||||
[Header("Detection Settings")]
|
||||
[SerializeField] private float range = 3f;
|
||||
[SerializeField] private LayerMask interactableLayer; // Tunnel용 레이어
|
||||
[SerializeField] private LayerMask constructionLayer; // 건설 토대용 레이어
|
||||
[SerializeField] private LayerMask interactableLayer; // TunnelNode 감지용
|
||||
[SerializeField] private LayerMask constructionLayer; // ConstructionSite 감지용
|
||||
|
||||
[Header("Build Settings")]
|
||||
[SerializeField] private float buildSpeedMultiplier = 2f;
|
||||
|
||||
private PlayerInputActions _inputActions;
|
||||
private bool _isHoldingInteract = false;
|
||||
private TunnelTraveler _traveler;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_inputActions = new PlayerInputActions();
|
||||
_traveler = GetComponent<TunnelTraveler>();
|
||||
|
||||
// 탭(짧게 누르기) 시점 체크
|
||||
_inputActions.Player.Interact.performed += OnInteractTap;
|
||||
// [최신화] 탭/홀드 로직을 Input Action 이벤트에 직접 바인딩
|
||||
// 1. 단일 상호작용 (터널 진입 등)
|
||||
_inputActions.Player.Interact.performed += OnInteractPerformed;
|
||||
|
||||
// 홀드(꾹 누르기) 시작/종료 체크
|
||||
_inputActions.Player.Interact.started += ctx => {
|
||||
_isHoldingInteract = true;
|
||||
Debug.Log("인터랙션 버튼 누르기 시작 (건설 가속 준비)");
|
||||
};
|
||||
_inputActions.Player.Interact.canceled += ctx => {
|
||||
_isHoldingInteract = false;
|
||||
Debug.Log("인터랙션 버튼 뗌");
|
||||
};
|
||||
// 2. 지속 상호작용 (건설 가속)
|
||||
_inputActions.Player.Interact.started += ctx => _isHoldingInteract = true;
|
||||
_inputActions.Player.Interact.canceled += ctx => _isHoldingInteract = false;
|
||||
}
|
||||
|
||||
void OnEnable() => _inputActions.Enable();
|
||||
@@ -37,52 +35,65 @@ public class PlayerInteractionController : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_isHoldingInteract)
|
||||
// 홀드 중일 때만 건설 지원 로직 실행
|
||||
if (_isHoldingInteract && !(_traveler != null && _traveler.IsTraveling))
|
||||
{
|
||||
PerformConstructionSupport();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInteractTap(InputAction.CallbackContext context)
|
||||
private void OnInteractPerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
Debug.Log("E 키 탭 감지! 주변 탐색 시작...");
|
||||
// [로그 1] 버튼 입력 자체가 인식되는지 확인
|
||||
Debug.Log("<color=white>1. E 키 입력 감지됨</color>");
|
||||
|
||||
if (_traveler != null && _traveler.IsTraveling)
|
||||
{
|
||||
Debug.Log("<color=gray>이동 중이라 상호작용 무시됨</color>");
|
||||
return;
|
||||
}
|
||||
|
||||
// [로그 2] 탐색 시작 위치와 범위 확인
|
||||
Debug.Log($"<color=white>2. 탐색 시작 - 위치: {transform.position}, 범위: {range}, 레이어: {interactableLayer.value}</color>");
|
||||
|
||||
// 1. 주변 모든 콜라이더 가져오기 (레이어 마스크 없이 먼저 테스트해보고 싶다면 0 대신 ~0 입력)
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, range, interactableLayer);
|
||||
|
||||
Debug.Log($"주변 {interactableLayer.value} 레이어에서 {colliders.Length}개의 오브젝트 감지됨");
|
||||
// [로그 3] 감지된 콜라이더 개수 확인
|
||||
Debug.Log($"<color=cyan>3. 감지된 콜라이더 개수: {colliders.Length}</color>");
|
||||
|
||||
foreach (var col in colliders)
|
||||
{
|
||||
// 2. 인터페이스 찾기 (본인 또는 부모에게서)
|
||||
// [로그 4] 감지된 오브젝트의 이름 출력
|
||||
Debug.Log($"<color=yellow>4. 감지된 오브젝트: {col.name}</color>");
|
||||
|
||||
IInteractable interactable = col.GetComponentInParent<IInteractable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
Debug.Log($"[성공] {col.name}에서 IInteractable 발견! 터널 진입합니다.");
|
||||
Debug.Log("<color=green>5. IInteractable 발견! Interact 실행</color>");
|
||||
interactable.Interact(gameObject);
|
||||
_isHoldingInteract = false; // 이동 중에는 건설 지원 중단
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"[실패] {col.name} 감지되었으나 IInteractable 스크립트가 없음");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PerformConstructionSupport()
|
||||
{
|
||||
// 주변의 건설 토대 감지
|
||||
Collider[] targets = Physics.OverlapSphere(transform.position, range, constructionLayer);
|
||||
|
||||
foreach (var col in targets)
|
||||
{
|
||||
ConstructionSite site = col.GetComponent<ConstructionSite>();
|
||||
// 토대 스크립트 탐색 (부모 또는 본인)
|
||||
ConstructionSite site = col.GetComponentInParent<ConstructionSite>();
|
||||
if (site != null)
|
||||
{
|
||||
// 최신화된 AdvanceConstruction 호출
|
||||
site.AdvanceConstruction(Time.deltaTime * buildSpeedMultiplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 에디터 시각화
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
|
||||
@@ -1,96 +1,56 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ConstructionSite : MonoBehaviour
|
||||
{
|
||||
[Header("UI Settings")]
|
||||
[SerializeField] private GameObject uiPrefab; // Canvas 프리팹
|
||||
[SerializeField] private Vector3 uiOffset = new Vector3(0, 2f, 0); // 머리 위 높이
|
||||
|
||||
private Slider _progressSlider;
|
||||
private GameObject _finalPrefab; // 이름을 _finalPrefab으로 통일하여 에러 방지
|
||||
private GameObject _finalPrefab;
|
||||
private float _buildTime;
|
||||
private float _currentProgress = 0f;
|
||||
private Vector2Int _size;
|
||||
private float _timer;
|
||||
private Vector3Int _gridPos;
|
||||
private bool _isCompleted = false; // 중복 완공 방지 플래그
|
||||
|
||||
public void Initialize(GameObject finalPrefab, float time, Vector2Int size)
|
||||
public void Initialize(GameObject final, float time, Vector3Int pos)
|
||||
{
|
||||
_finalPrefab = finalPrefab;
|
||||
_finalPrefab = final;
|
||||
_buildTime = time;
|
||||
_size = size;
|
||||
|
||||
// UI 생성 및 초기화
|
||||
if (uiPrefab != null)
|
||||
{
|
||||
GameObject uiObj = Instantiate(uiPrefab, transform.position + uiOffset, Quaternion.identity, transform);
|
||||
_progressSlider = uiObj.GetComponentInChildren<Slider>();
|
||||
if (_progressSlider != null)
|
||||
{
|
||||
_progressSlider.maxValue = 1f;
|
||||
_progressSlider.value = 0f;
|
||||
}
|
||||
}
|
||||
_gridPos = pos;
|
||||
_timer = 0;
|
||||
_isCompleted = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_isCompleted) return;
|
||||
|
||||
// 매 프레임 자동으로 시간이 흐름
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer >= _buildTime) Complete();
|
||||
}
|
||||
|
||||
// [핵심] 플레이어가 상호작용 버튼을 꾹 누를 때 호출되는 함수
|
||||
public void AdvanceConstruction(float amount)
|
||||
{
|
||||
_currentProgress += amount;
|
||||
float ratio = Mathf.Clamp01(_currentProgress / _buildTime);
|
||||
if (_isCompleted) return;
|
||||
|
||||
// 슬라이더 업데이트
|
||||
if (_progressSlider != null)
|
||||
{
|
||||
_progressSlider.value = ratio;
|
||||
}
|
||||
|
||||
if (_currentProgress >= _buildTime)
|
||||
{
|
||||
CompleteBuild();
|
||||
}
|
||||
_timer += amount;
|
||||
if (_timer >= _buildTime) Complete();
|
||||
}
|
||||
|
||||
private void CompleteBuild()
|
||||
private void Complete()
|
||||
{
|
||||
// 1. 실제 타워 생성
|
||||
GameObject finalTurret = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
||||
// 1. 터널 생성
|
||||
GameObject tunnel = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
||||
TunnelNode node = tunnel.GetComponentInChildren<TunnelNode>();
|
||||
|
||||
// 2. BuildManager를 통한 지면 정렬 (스케일 반영을 위해 호출)
|
||||
if (BuildManager.Instance != null)
|
||||
if (node != null)
|
||||
{
|
||||
BuildManager.Instance.AlignToGround(finalTurret, 0f);
|
||||
// 2. [수정] 완공된 터널의 루트 위치로 좌표 계산
|
||||
Vector3Int myGridPos = BuildManager.Instance.WorldToGrid3D(tunnel.transform.position);
|
||||
|
||||
// 3. 레지스트리 등록 및 연결
|
||||
BuildManager.Instance.RegisterTunnel(myGridPos, node);
|
||||
node.LinkVertical();
|
||||
}
|
||||
|
||||
// 3. 터널 노드 연결 갱신 (터널인 경우에만 작동)
|
||||
UpdateTunnelConnections(finalTurret);
|
||||
|
||||
// 4. 토대 제거
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private void UpdateTunnelConnections(GameObject newTurret)
|
||||
{
|
||||
// 3-1. 새로 생성된 타워 내부의 모든 TunnelNode를 찾아 주변 탐색 실행
|
||||
TunnelNode[] newNodes = newTurret.GetComponentsInChildren<TunnelNode>();
|
||||
if (newNodes.Length > 0)
|
||||
{
|
||||
foreach (var node in newNodes)
|
||||
{
|
||||
node.FindNeighborNode();
|
||||
}
|
||||
|
||||
// 3-2. 주변에 이미 설치된 다른 터널 노드들도 새로운 터널을 인식하도록 재탐색 명령
|
||||
// 5f는 격자 크기에 맞춰 적절히 조절 (일반적으로 격자 한 칸 이상이면 충분)
|
||||
Collider[] neighbors = Physics.OverlapSphere(transform.position, 5f, LayerMask.GetMask("Tunnel"));
|
||||
foreach (var col in neighbors)
|
||||
{
|
||||
TunnelNode neighborNode = col.GetComponentInParent<TunnelNode>();
|
||||
if (neighborNode != null && !System.Array.Exists(newNodes, x => x == neighborNode))
|
||||
{
|
||||
neighborNode.FindNeighborNode();
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log($"[Construction] {newTurret.name} 터널 연결 갱신 완료");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,41 +2,64 @@ using UnityEngine;
|
||||
|
||||
public class TunnelNode : MonoBehaviour, IInteractable
|
||||
{
|
||||
public TunnelNode connectedNode;
|
||||
public TunnelNode otherEndOfThisSegment;
|
||||
public float detectionRadius = 0.5f;
|
||||
public LayerMask tunnelLayer;
|
||||
public TunnelNode aboveNode;
|
||||
public TunnelNode belowNode;
|
||||
|
||||
// [중요] 이 노드가 속한 터널의 최상단 부모를 캐싱합니다.
|
||||
private Transform _tunnelRoot;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// 이름이 "Tunnel"로 시작하는 부모를 찾거나, 단순히 부모의 부모를 참조합니다.
|
||||
// 여기서는 이미지 구조에 맞게 부모(Visual)의 부모(Tunnel (1))를 찾습니다.
|
||||
_tunnelRoot = transform.parent.parent;
|
||||
}
|
||||
|
||||
public void LinkVertical()
|
||||
{
|
||||
// 루트(Tunnel 1)의 위치로 내 위치 파악
|
||||
Transform myRoot = transform.parent.parent;
|
||||
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(myRoot.position);
|
||||
|
||||
// 위/아래 터널 찾기
|
||||
TunnelNode targetAbove = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.up);
|
||||
TunnelNode targetBelow = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.down);
|
||||
|
||||
if (targetAbove != null)
|
||||
{
|
||||
this.aboveNode = targetAbove;
|
||||
targetAbove.belowNode = this; // 상대방의 아래도 나로 설정 (양방향)
|
||||
}
|
||||
|
||||
if (targetBelow != null)
|
||||
{
|
||||
this.belowNode = targetBelow;
|
||||
targetBelow.aboveNode = this; // 상대방의 위도 나로 설정 (양방향)
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// 연결되었다면 Scene 뷰에서 선을 그림
|
||||
if (aboveNode != null)
|
||||
{
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawLine(transform.position, aboveNode.transform.position);
|
||||
Gizmos.DrawSphere(aboveNode.transform.position, 0.2f);
|
||||
}
|
||||
if (belowNode != null)
|
||||
{
|
||||
Gizmos.color = Color.cyan;
|
||||
Gizmos.DrawLine(transform.position, belowNode.transform.position);
|
||||
Gizmos.DrawSphere(belowNode.transform.position, 0.2f);
|
||||
}
|
||||
}
|
||||
|
||||
// --- IInteractable 구현부 ---
|
||||
public void Interact(GameObject user)
|
||||
{
|
||||
// 상호작용한 플레이어의 TunnelTraveler를 찾아 이동 시작!
|
||||
TunnelTraveler traveler = user.GetComponent<TunnelTraveler>();
|
||||
if (traveler != null)
|
||||
{
|
||||
traveler.StartTravel(this);
|
||||
}
|
||||
var traveler = user.GetComponent<TunnelTraveler>();
|
||||
if (traveler && !traveler.IsTraveling) traveler.StartTravel(this);
|
||||
}
|
||||
|
||||
public string GetInteractionText()
|
||||
{
|
||||
return "통로 진입";
|
||||
}
|
||||
// ----------------------------
|
||||
|
||||
void Start() => FindNeighborNode();
|
||||
|
||||
public void FindNeighborNode()
|
||||
{
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, detectionRadius, tunnelLayer);
|
||||
foreach (var col in colliders)
|
||||
{
|
||||
TunnelNode neighbor = col.GetComponent<TunnelNode>();
|
||||
if (neighbor != null && neighbor != this && neighbor != otherEndOfThisSegment)
|
||||
{
|
||||
connectedNode = neighbor;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public string GetInteractionText() => "터널 이용 [E]";
|
||||
}
|
||||
@@ -4,87 +4,62 @@ using System.Collections.Generic;
|
||||
|
||||
public class TunnelTraveler : MonoBehaviour
|
||||
{
|
||||
public float travelSpeed = 25f; private bool _isTraveling = false;
|
||||
// 외부에서 읽기 전용으로 접근할 수 있게 합니다.
|
||||
public float travelSpeed = 20f;
|
||||
private bool _isTraveling;
|
||||
public bool IsTraveling => _isTraveling;
|
||||
|
||||
private CharacterController _controller;
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
private IEnumerator TravelRoutine(List<Vector3> path, TunnelNode startNode)
|
||||
{
|
||||
_isTraveling = true;
|
||||
|
||||
if (_controller != null) _controller.enabled = false;
|
||||
if (_rigidbody != null) _rigidbody.isKinematic = true;
|
||||
|
||||
// 1. 캐릭터 높이 보정값 계산
|
||||
float heightOffset = 0f;
|
||||
if (_controller != null) heightOffset = _controller.height / 2f;
|
||||
|
||||
// 2. [입구 정렬] 시작 노드의 정확한 중앙 위치로 플레이어를 즉시 이동시킵니다.
|
||||
// 플레이어의 '발바닥' 위치를 노드 중앙보다 heightOffset만큼 아래로 맞춤
|
||||
Vector3 entryPosition = new Vector3(startNode.transform.position.x,
|
||||
startNode.transform.position.y - heightOffset,
|
||||
startNode.transform.position.z);
|
||||
|
||||
// 시작 지점으로 순간이동 또는 아주 빠르게 정렬
|
||||
transform.position = entryPosition;
|
||||
|
||||
// 3. 경로 이동 시작
|
||||
foreach (Vector3 targetPos in path)
|
||||
{
|
||||
Vector3 adjustedTarget = new Vector3(targetPos.x, targetPos.y - heightOffset, targetPos.z);
|
||||
|
||||
while (Vector3.Distance(transform.position, adjustedTarget) > 0.01f)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, adjustedTarget, travelSpeed * Time.deltaTime);
|
||||
yield return null;
|
||||
}
|
||||
transform.position = adjustedTarget;
|
||||
}
|
||||
|
||||
if (_rigidbody != null) _rigidbody.isKinematic = false;
|
||||
if (_controller != null) _controller.enabled = true;
|
||||
|
||||
_isTraveling = false;
|
||||
}
|
||||
|
||||
// StartTravel 함수에서 startNode를 코루틴에 넘겨주도록 수정
|
||||
public void StartTravel(TunnelNode startNode)
|
||||
public void StartTravel(TunnelNode start)
|
||||
{
|
||||
if (_isTraveling) return;
|
||||
|
||||
List<Vector3> path = GeneratePath(startNode);
|
||||
// [디버그 1] 상호작용한 노드의 연결 상태 확인
|
||||
Debug.Log($"<color=white>[Travel] 시작 노드: {start.name} | 위: {(start.aboveNode != null)} | 아래: {(start.belowNode != null)}</color>");
|
||||
|
||||
List<Vector3> path = new List<Vector3>();
|
||||
|
||||
// 이동 방향 결정 (아래가 있으면 아래로, 없으면 위로)
|
||||
bool goDown = start.belowNode != null;
|
||||
TunnelNode curr = goDown ? start.belowNode : start.aboveNode;
|
||||
|
||||
while (curr != null)
|
||||
{
|
||||
path.Add(curr.transform.position);
|
||||
curr = goDown ? curr.belowNode : curr.aboveNode;
|
||||
}
|
||||
|
||||
// [디버그 2] 최종 경로 개수 확인
|
||||
Debug.Log($"<color=yellow>[Travel] 생성된 경로 포인트 개수: {path.Count}</color>");
|
||||
|
||||
if (path.Count > 0)
|
||||
{
|
||||
// startNode 정보를 함께 넘김
|
||||
StartCoroutine(TravelRoutine(path, startNode));
|
||||
StartCoroutine(Travel(path));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("<color=red>[Travel] 이동할 경로가 없습니다! 노드 연결을 확인하세요.</color>");
|
||||
}
|
||||
}
|
||||
|
||||
// GeneratePath 함수는 기존과 동일하게 유지합니다.
|
||||
private List<Vector3> GeneratePath(TunnelNode startNode)
|
||||
private IEnumerator Travel(List<Vector3> path)
|
||||
{
|
||||
List<Vector3> path = new List<Vector3>();
|
||||
TunnelNode currentNode = startNode;
|
||||
HashSet<TunnelNode> visited = new HashSet<TunnelNode>();
|
||||
Debug.Log("<color=green>[Travel] 코루틴 이동 시작!</color>");
|
||||
_isTraveling = true;
|
||||
var cc = GetComponent<CharacterController>();
|
||||
if (cc) cc.enabled = false;
|
||||
|
||||
while (currentNode != null && !visited.Contains(currentNode))
|
||||
foreach (var point in path)
|
||||
{
|
||||
visited.Add(currentNode);
|
||||
TunnelNode exitNode = currentNode.otherEndOfThisSegment;
|
||||
if (exitNode == null) break;
|
||||
|
||||
path.Add(exitNode.transform.position);
|
||||
currentNode = exitNode.connectedNode;
|
||||
Vector3 target = new Vector3(point.x, point.y - (cc ? cc.height / 2 : 0), point.z);
|
||||
while (Vector3.Distance(transform.position, target) > 0.1f)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, target, travelSpeed * Time.deltaTime);
|
||||
yield return null;
|
||||
}
|
||||
transform.position = target;
|
||||
}
|
||||
return path;
|
||||
|
||||
if (cc) cc.enabled = true;
|
||||
_isTraveling = false;
|
||||
Debug.Log("<color=green>[Travel] 이동 완료!</color>");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user