건물 건설 모드 및 건설 인터랙션

This commit is contained in:
2026-01-12 17:21:07 +09:00
parent 87340317ab
commit c75c5bd868
25 changed files with 3747 additions and 311 deletions

View File

@@ -0,0 +1,17 @@
using UnityEngine;
public class Billboard : MonoBehaviour
{
private Transform _camTransform;
void Start()
{
_camTransform = Camera.main.transform;
}
void LateUpdate()
{
// UI가 항상 카메라를 정면으로 바라보게 함
transform.LookAt(transform.position + _camTransform.rotation * Vector3.forward, _camTransform.rotation * Vector3.up);
}
}

View File

@@ -0,0 +1,188 @@
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class BuildManager : MonoBehaviour
{
public static BuildManager Instance;
[System.Serializable]
public struct TurretData // 타워별 정보를 담는 구조체
{
public string turretName;
public GameObject finalPrefab; // 완공 후 실제 타워
public GameObject ghostPrefab; // 건설 모드 시 미리보기
public float buildTime; // 건설 소요 시간
public Vector2Int size; // 점유 칸수
}
[Header("Settings")]
[SerializeField] private float cellSize = 1f;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private GameObject constructionSitePrefab; // 공용 토대 프리팹
[Header("Current Selection")]
[SerializeField] private TurretData selectedTurret; // 현재 선택된 타워 데이터
[SerializeField] private bool isBuildMode = false;
private GameObject _ghostInstance;
private Material _ghostMaterial;
private HashSet<Vector2Int> _occupiedNodes = new HashSet<Vector2Int>();
private PlayerInputActions _inputActions;
void Awake()
{
Instance = this;
_inputActions = new PlayerInputActions();
}
void OnEnable()
{
_inputActions.Player.Build.performed += OnBuildPerformed;
_inputActions.Player.Cancel.performed += OnCancelPerformed;
_inputActions.Player.ToggleBuild.performed += OnTogglePerformed;
_inputActions.Enable();
}
void OnDisable()
{
_inputActions.Disable();
}
void Update()
{
if (isBuildMode)
{
UpdateGhost();
}
}
// --- Input Callbacks ---
private void OnTogglePerformed(InputAction.CallbackContext context) => ToggleBuildMode();
private void OnCancelPerformed(InputAction.CallbackContext context)
{
if (isBuildMode) ToggleBuildMode();
}
private void OnBuildPerformed(InputAction.CallbackContext context)
{
if (!isBuildMode || EventSystem.current.IsPointerOverGameObject()) return;
Vector2 mousePos = Mouse.current.position.ReadValue();
Ray ray = Camera.main.ScreenPointToRay(mousePos);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer))
{
Vector2Int gridPos = WorldToGrid(hit.point);
if (CanBuild(gridPos, selectedTurret.size))
{
Build(gridPos, selectedTurret);
}
}
}
// --- Core Logic ---
private void ToggleBuildMode()
{
isBuildMode = !isBuildMode;
if (isBuildMode) CreateGhost();
else DestroyGhost();
}
private void CreateGhost()
{
if (_ghostInstance != null) Destroy(_ghostInstance);
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
}
private void DestroyGhost()
{
if (_ghostInstance != null) Destroy(_ghostInstance);
}
private void UpdateGhost()
{
if (_ghostInstance == null) return;
Vector2 mousePos = Mouse.current.position.ReadValue();
Ray ray = Camera.main.ScreenPointToRay(mousePos);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer))
{
_ghostInstance.SetActive(true);
Vector2Int gridPos = WorldToGrid(hit.point);
_ghostInstance.transform.position = GridToWorld(gridPos, selectedTurret.size);
bool canBuild = CanBuild(gridPos, selectedTurret.size);
_ghostMaterial.color = canBuild ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.5f);
}
else
{
_ghostInstance.SetActive(false);
}
}
private void Build(Vector2Int gridPos, TurretData data)
{
// 1. 프리팹 할당 여부 체크
if (constructionSitePrefab == null)
{
Debug.LogError("BuildManager: Construction Site Prefab이 할당되지 않았습니다!");
return;
}
if (data.finalPrefab == null)
{
Debug.LogError($"BuildManager: {data.turretName}의 Final Prefab이 할당되지 않았습니다!");
return;
}
// 2. 점유 노드 등록
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));
// 3. 토대 생성
GameObject siteObj = Instantiate(constructionSitePrefab, GridToWorld(gridPos, data.size), Quaternion.identity);
// 4. 컴포넌트 존재 여부 체크
ConstructionSite siteScript = siteObj.GetComponent<ConstructionSite>();
if (siteScript != null)
{
siteScript.Initialize(data.finalPrefab, data.buildTime);
}
else
{
Debug.LogError("BuildManager: 생성된 토대 프리팹에 ConstructionSite 스크립트가 없습니다!");
}
ToggleBuildMode();
}
// --- Utilities ---
private bool CanBuild(Vector2Int startPos, Vector2Int size)
{
for (int x = 0; x < size.x; x++)
for (int y = 0; y < size.y; y++)
if (_occupiedNodes.Contains(new Vector2Int(startPos.x + x, startPos.y + y))) return false;
return true;
}
private Vector2Int WorldToGrid(Vector3 worldPos) => new Vector2Int(Mathf.RoundToInt(worldPos.x / cellSize), Mathf.RoundToInt(worldPos.z / cellSize));
private 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);
}
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));
}
}

View File

@@ -0,0 +1,55 @@
using UnityEngine;
using UnityEngine.UI; // 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 _finalTurretPrefab;
private float _buildTime;
private float _currentProgress = 0f;
public void Initialize(GameObject finalPrefab, float time)
{
_finalTurretPrefab = finalPrefab;
_buildTime = time;
// 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;
}
}
}
public void AdvanceConstruction(float amount)
{
_currentProgress += amount;
float ratio = _currentProgress / _buildTime;
// 슬라이더 업데이트
if (_progressSlider != null)
{
_progressSlider.value = ratio;
}
if (_currentProgress >= _buildTime)
{
CompleteBuild();
}
}
private void CompleteBuild()
{
Instantiate(_finalTurretPrefab, transform.position, transform.rotation);
Destroy(gameObject);
}
}

View File

@@ -0,0 +1,58 @@
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBuildInteract : MonoBehaviour
{
[Header("Interaction Settings")]
[SerializeField] private float interactRange = 3f; // 건설 가능 거리
[SerializeField] private float buildSpeedMultiplier = 1f; // 건설 속도 배율
[SerializeField] private LayerMask constructionLayer; // 토대 레이어 (선택 사항)
private PlayerInputActions _inputActions;
private bool _isInteracting = false;
void Awake()
{
_inputActions = new PlayerInputActions();
// Interact 액션 연결 (Hold 방식)
_inputActions.Player.Interact.started += ctx => _isInteracting = true;
_inputActions.Player.Interact.canceled += ctx => _isInteracting = false;
}
void OnEnable() => _inputActions.Enable();
void OnDisable() => _inputActions.Disable();
void Update()
{
// 키를 누르고 있을 때만 실행
if (_isInteracting)
{
PerformConstruction();
}
}
void PerformConstruction()
{
// 주변의 모든 콜라이더 검사
Collider[] targets = Physics.OverlapSphere(transform.position, interactRange, constructionLayer);
foreach (var col in targets)
{
// 토대 컴포넌트가 있는지 확인
ConstructionSite site = col.GetComponent<ConstructionSite>();
if (site != null)
{
// 드디어 여기서 호출합니다!
site.AdvanceConstruction(Time.deltaTime * buildSpeedMultiplier);
}
}
}
// 에디터에서 상호작용 범위를 확인하기 위함
void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, interactRange);
}
}

View File

@@ -1,36 +0,0 @@
using UnityEngine;
using UnityEngine.InputSystem; // New Input System 사용
public class PlayerMovement : MonoBehaviour
{
private Rigidbody _rb;
private Vector2 _inputVector;
[SerializeField] private float moveSpeed = 5f;
void Awake()
{
_rb = GetComponent<Rigidbody>();
}
// Player Input 컴포넌트의 Behavior가 'Send Messages'일 때 자동 호출됨
// Input Action 이름이 "Move"라면 "OnMove"라는 메서드를 찾습니다.
void OnMove(InputValue value)
{
_inputVector = value.Get<Vector2>();
}
void FixedUpdate()
{
// 물리 연산은 프레임 독립적인 FixedUpdate에서 수행
Move();
}
void Move()
{
Vector3 movement = new Vector3(_inputVector.x, 0, _inputVector.y) * moveSpeed;
// Rigidbody의 속도를 직접 제어하거나 MovePosition 사용
// 등속도 이동을 위해 velocity의 x, z만 변경 (y는 중력 유지를 위해 기존 값 사용)
_rb.linearVelocity = new Vector3(movement.x, _rb.linearVelocity.y, movement.z);
}
}

View File

@@ -0,0 +1,36 @@
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
private Vector2 _moveInput;
private CharacterController _controller;
private PlayerInputActions _inputActions;
void Awake()
{
_controller = GetComponent<CharacterController>();
_inputActions = new PlayerInputActions();
// Move 액션 연결 (Vector2 타입으로 설정되어 있어야 함)
_inputActions.Player.Move.performed += ctx => _moveInput = ctx.ReadValue<Vector2>();
_inputActions.Player.Move.canceled += ctx => _moveInput = Vector2.zero;
}
void OnEnable() => _inputActions.Enable();
void OnDisable() => _inputActions.Disable();
void Update()
{
// 입력받은 방향으로 이동 계산
Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y);
_controller.Move(move * Time.deltaTime * moveSpeed);
// 이동 중일 때 바라보는 방향 전환 (선택 사항)
if (move != Vector3.zero)
{
transform.forward = move;
}
}
}