멀티플레이어 지원
이동, 건설, 인터랙션, 공격 등
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.EventSystems;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class BuildManager : MonoBehaviour
|
||||
public class BuildManager : NetworkBehaviour
|
||||
{
|
||||
public static BuildManager Instance;
|
||||
|
||||
@@ -11,113 +12,81 @@ public class BuildManager : MonoBehaviour
|
||||
public struct TurretData
|
||||
{
|
||||
public string turretName;
|
||||
public bool isTunnel;
|
||||
public GameObject finalPrefab;
|
||||
public GameObject ghostPrefab;
|
||||
public GameObject finalPrefab; // NetworkObject 필수
|
||||
public GameObject ghostPrefab; // 로컬 프리뷰용
|
||||
public float buildTime;
|
||||
public Vector2Int size;
|
||||
}
|
||||
|
||||
[Header("Settings")]
|
||||
[Header("Grid Settings")]
|
||||
public float cellSize = 1f;
|
||||
public float tunnelHeight = 3f;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[SerializeField] private float yOffset = 0.5f; // 터널 중심점 오프셋
|
||||
|
||||
[Header("Prefabs & Layers")]
|
||||
[SerializeField] private GameObject constructionSitePrefab;
|
||||
[SerializeField] private List<TurretData> turretLibrary = new List<TurretData>();
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[SerializeField] private LayerMask playerLayer;
|
||||
|
||||
[Header("Turret Library")]
|
||||
[SerializeField] private List<TurretData> turretLibrary;
|
||||
[SerializeField] private TurretData selectedTurret;
|
||||
|
||||
private GameObject _ghostInstance;
|
||||
private Material _ghostMaterial;
|
||||
private int _selectedTurretIndex = 0;
|
||||
private bool _isBuildMode = false;
|
||||
public bool IsBuildMode => _isBuildMode;
|
||||
private GameObject _ghostInstance;
|
||||
private Vector3Int _currentGridPos;
|
||||
private PlayerInputActions _inputActions;
|
||||
|
||||
// 좌표 레지스트리 (물리 탐색 대체)
|
||||
// 데이터 레지스트리 (다른 코드에서 참조)
|
||||
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()
|
||||
{
|
||||
Instance = this;
|
||||
if (Instance == null) Instance = this;
|
||||
else Destroy(gameObject);
|
||||
|
||||
_inputActions = new PlayerInputActions();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 최신 Input System 콜백 등록
|
||||
// 최신 Action-based 이벤트 바인딩
|
||||
_inputActions.Player.ToggleBuild.performed += ctx => ToggleBuildMode();
|
||||
_inputActions.Player.Build.performed += ctx => OnBuildRequested();
|
||||
_inputActions.Player.Cancel.performed += ctx => ExitBuildMode();
|
||||
_inputActions.Player.ToggleBuild.performed += ctx => ToggleBuildMode();
|
||||
_inputActions.Enable();
|
||||
}
|
||||
|
||||
void OnDisable() => _inputActions.Disable();
|
||||
// 숫자키 슬롯 선택 (Input Action 에셋 설정에 따라 Select1, Select2... 등 사용)
|
||||
_inputActions.Player.Select1.performed += ctx => SelectTurret(0);
|
||||
_inputActions.Player.Select2.performed += ctx => SelectTurret(1);
|
||||
_inputActions.Player.Select3.performed += ctx => SelectTurret(2);
|
||||
|
||||
_inputActions.Enable();
|
||||
|
||||
// 시작 시 씬에 배치된 터널 자동 등록
|
||||
RegisterAllExistingTunnels();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!_isBuildMode || _ghostInstance == null) return;
|
||||
|
||||
UpdateGhostPosition();
|
||||
}
|
||||
|
||||
#region Selection & Build Logic
|
||||
|
||||
public void SelectTurret(int index)
|
||||
{
|
||||
if (index < 0 || index >= turretLibrary.Count) return;
|
||||
|
||||
_selectedTurretIndex = index;
|
||||
Debug.Log($"타워 선택: {turretLibrary[_selectedTurretIndex].turretName}");
|
||||
|
||||
if (_isBuildMode)
|
||||
{
|
||||
if (_ghostInstance != null) Destroy(_ghostInstance);
|
||||
_ghostInstance = Instantiate(turretLibrary[_selectedTurretIndex].ghostPrefab);
|
||||
_ghostInstance.transform.position = GridToWorld(_currentGridPos);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGhostPosition()
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
@@ -125,61 +94,112 @@ public class BuildManager : MonoBehaviour
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer | tunnelMask))
|
||||
{
|
||||
Vector2Int xz = WorldToGrid(hit.point);
|
||||
float targetY = 0.05f;
|
||||
int floor = 0;
|
||||
Vector3Int gridPos = WorldToGrid3D(hit.point);
|
||||
|
||||
// 터널 조준 시 지하로 스냅
|
||||
// 터널 조준 시 해당 터널의 한 칸 아래로 스냅
|
||||
if (((1 << hit.collider.gameObject.layer) & tunnelMask) != 0)
|
||||
{
|
||||
xz = WorldToGrid(hit.collider.transform.position);
|
||||
targetY = hit.collider.transform.position.y - tunnelHeight;
|
||||
floor = Mathf.RoundToInt(targetY / tunnelHeight);
|
||||
gridPos = WorldToGrid3D(hit.collider.transform.position) + Vector3Int.down;
|
||||
}
|
||||
|
||||
_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);
|
||||
_currentGridPos = gridPos;
|
||||
_ghostInstance.transform.position = GridToWorld(gridPos);
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanBuildVertical(Vector3Int pos)
|
||||
{
|
||||
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 OnBuildRequested()
|
||||
{
|
||||
if (!_isBuildMode || EventSystem.current.IsPointerOverGameObject()) return;
|
||||
if (!CanBuildVertical(_currentGridPos)) return;
|
||||
|
||||
_occupiedNodes.Add(_currentGridPos);
|
||||
GameObject site = Instantiate(constructionSitePrefab, _ghostInstance.transform.position, Quaternion.identity);
|
||||
site.GetComponent<ConstructionSite>().Initialize(selectedTurret.finalPrefab, selectedTurret.buildTime, _currentGridPos);
|
||||
// 서버에 건설 가능 여부 확인 및 생성 요청
|
||||
RequestBuildRpc(_selectedTurretIndex, _currentGridPos);
|
||||
|
||||
ExitBuildMode();
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
private void RequestBuildRpc(int index, Vector3Int gridPos)
|
||||
{
|
||||
// 서버 측 중복 점유 확인
|
||||
if (_occupiedNodes.Contains(gridPos)) return;
|
||||
|
||||
Vector3 spawnPos = GridToWorld(gridPos);
|
||||
|
||||
// 1. 토대 생성 및 네트워크 스폰
|
||||
GameObject siteObj = Instantiate(constructionSitePrefab, spawnPos, Quaternion.identity);
|
||||
siteObj.GetComponent<NetworkObject>().Spawn();
|
||||
|
||||
// 2. 토대 데이터 초기화
|
||||
ConstructionSite site = siteObj.GetComponent<ConstructionSite>();
|
||||
if (site != null)
|
||||
{
|
||||
site.Initialize(index, gridPos);
|
||||
}
|
||||
|
||||
_occupiedNodes.Add(gridPos);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Functions (External References)
|
||||
|
||||
// ConstructionSite에서 프리팹 정보를 가져갈 때 사용
|
||||
public TurretData GetTurretData(int index) => turretLibrary[index];
|
||||
|
||||
// TunnelNode가 스폰될 때 자신을 등록하기 위해 사용
|
||||
public void RegisterTunnel(Vector3Int pos, TunnelNode node)
|
||||
{
|
||||
if (!_tunnelRegistry.ContainsKey(pos))
|
||||
{
|
||||
_tunnelRegistry.Add(pos, node);
|
||||
_occupiedNodes.Add(pos); // 건설된 구역으로 마킹
|
||||
}
|
||||
}
|
||||
|
||||
// TunnelNode가 위아래 노드를 찾기 위해 사용
|
||||
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));
|
||||
// 좌표 변환: 월드 -> 격자 인덱스 (0.5 오프셋 반영)
|
||||
public Vector3Int WorldToGrid3D(Vector3 worldPos)
|
||||
{
|
||||
return new Vector3Int(
|
||||
Mathf.FloorToInt(worldPos.x / cellSize),
|
||||
Mathf.RoundToInt((worldPos.y - yOffset) / tunnelHeight),
|
||||
Mathf.FloorToInt(worldPos.z / cellSize)
|
||||
);
|
||||
}
|
||||
|
||||
private void ToggleBuildMode() { if (_isBuildMode) ExitBuildMode(); else EnterBuildMode(); }
|
||||
// 좌표 변환: 격자 인덱스 -> 월드 (0.5 오프셋 반영)
|
||||
public Vector3 GridToWorld(Vector3Int gridPos)
|
||||
{
|
||||
return new Vector3(
|
||||
gridPos.x * cellSize,
|
||||
(gridPos.y * tunnelHeight) + yOffset,
|
||||
gridPos.z * cellSize
|
||||
);
|
||||
}
|
||||
|
||||
// 씬에 미리 배치된 터널들을 한꺼번에 등록
|
||||
public void RegisterAllExistingTunnels()
|
||||
{
|
||||
TunnelNode[] nodes = FindObjectsByType<TunnelNode>(FindObjectsSortMode.None);
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
Vector3Int pos = WorldToGrid3D(node.transform.position);
|
||||
RegisterTunnel(pos, node);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mode Switching
|
||||
|
||||
public void ToggleBuildMode() { if (_isBuildMode) ExitBuildMode(); else EnterBuildMode(); }
|
||||
private void EnterBuildMode()
|
||||
{
|
||||
if (turretLibrary.Count == 0) return;
|
||||
_isBuildMode = true;
|
||||
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
|
||||
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
|
||||
_ghostInstance = Instantiate(turretLibrary[_selectedTurretIndex].ghostPrefab);
|
||||
}
|
||||
private void ExitBuildMode()
|
||||
{
|
||||
@@ -187,27 +207,7 @@ public class BuildManager : MonoBehaviour
|
||||
if (_ghostInstance) Destroy(_ghostInstance);
|
||||
}
|
||||
|
||||
// BuildManager.cs 내부
|
||||
public void SelectTurret(int index)
|
||||
{
|
||||
if (index >= 0 && index < turretLibrary.Count)
|
||||
{
|
||||
selectedTurret = turretLibrary[index];
|
||||
#endregion
|
||||
|
||||
// 현재 건설 모드가 아니라면 건설 모드로 진입
|
||||
if (!_isBuildMode)
|
||||
{
|
||||
EnterBuildMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 이미 건설 모드라면 고스트만 교체
|
||||
if (_ghostInstance) Destroy(_ghostInstance);
|
||||
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
|
||||
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
|
||||
}
|
||||
|
||||
Debug.Log($"{selectedTurret.turretName} 선택됨");
|
||||
}
|
||||
}
|
||||
public override void OnNetworkDespawn() => _inputActions.Disable();
|
||||
}
|
||||
54
Assets/Scripts/MineableBlock.cs
Normal file
54
Assets/Scripts/MineableBlock.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class MineableBlock : NetworkBehaviour
|
||||
{
|
||||
[Header("Block Stats")]
|
||||
[SerializeField] private int maxHp = 100;
|
||||
// [동기화] 모든 플레이어가 동일한 블록 체력을 보게 함
|
||||
private NetworkVariable<int> _currentHp = new NetworkVariable<int>();
|
||||
|
||||
[Header("Visuals")]
|
||||
[SerializeField] private GameObject breakEffectPrefab; // 파괴 시 파티클
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (IsServer)
|
||||
{
|
||||
_currentHp.Value = maxHp;
|
||||
}
|
||||
}
|
||||
|
||||
// 서버에서만 대미지를 처리하도록 제한
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
public void TakeDamageRpc(int damageAmount)
|
||||
{
|
||||
if (_currentHp.Value <= 0) return;
|
||||
|
||||
_currentHp.Value -= damageAmount;
|
||||
Debug.Log($"블록 대미지! 남은 체력: {_currentHp.Value}");
|
||||
|
||||
if (_currentHp.Value <= 0)
|
||||
{
|
||||
DestroyBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void DestroyBlock()
|
||||
{
|
||||
// 1. 모든 클라이언트에게 파괴 이펙트 재생 요청
|
||||
PlayBreakEffectRpc();
|
||||
|
||||
// 2. 서버에서 네트워크 오브젝트 제거 (모든 클라이언트에서 사라짐)
|
||||
GetComponent<NetworkObject>().Despawn();
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Everyone)]
|
||||
private void PlayBreakEffectRpc()
|
||||
{
|
||||
if (breakEffectPrefab != null)
|
||||
{
|
||||
Instantiate(breakEffectPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/MineableBlock.cs.meta
Normal file
2
Assets/Scripts/MineableBlock.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8e42f98781f84c42855fa4e989d3c1b
|
||||
13
Assets/Scripts/OwnerNetworkAnimator.cs
Normal file
13
Assets/Scripts/OwnerNetworkAnimator.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Unity.Netcode.Components;
|
||||
using UnityEngine;
|
||||
|
||||
// 이 스크립트를 파일로 저장하세요.
|
||||
[DisallowMultipleComponent]
|
||||
public class OwnerNetworkAnimator : NetworkAnimator
|
||||
{
|
||||
// 이동 권한이 Owner에게 있으므로, 애니메이션 권한도 Owner가 갖도록 설정합니다.
|
||||
protected override bool OnIsServerAuthoritative()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/OwnerNetworkAnimator.cs.meta
Normal file
2
Assets/Scripts/OwnerNetworkAnimator.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7154bdbb6b349e7468f33b4e16cd11b1
|
||||
173
Assets/Scripts/Player/PlayerNetworkController.cs
Normal file
173
Assets/Scripts/Player/PlayerNetworkController.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Unity.Netcode; // NGO 필수 네임스페이스
|
||||
using Unity.Netcode.Components;
|
||||
|
||||
[RequireComponent(typeof(NetworkObject))]
|
||||
public class PlayerNetworkController : NetworkBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
public float moveSpeed = 5f;
|
||||
public float rotationSpeed = 10f;
|
||||
public float jumpHeight = 1.5f;
|
||||
public float gravity = -19.62f;
|
||||
|
||||
[Header("Interaction Settings")]
|
||||
[SerializeField] private float interactRange = 3f;
|
||||
[SerializeField] private LayerMask interactableLayer;
|
||||
[SerializeField] private LayerMask constructionLayer;
|
||||
[SerializeField] private float buildSpeedMultiplier = 2f;
|
||||
|
||||
[Header("Mining Settings")]
|
||||
[SerializeField] private float attackRange = 1.5f;
|
||||
[SerializeField] private int miningDamage = 25;
|
||||
[SerializeField] private LayerMask mineableLayer; // 'Mineable' 레이어 설정 필요
|
||||
|
||||
private CharacterController _controller;
|
||||
private PlayerInputActions _inputActions;
|
||||
private Animator _animator;
|
||||
private TunnelTraveler _traveler;
|
||||
|
||||
private Vector2 _moveInput;
|
||||
private Vector3 _velocity;
|
||||
private Vector3 _currentMoveDir;
|
||||
private bool _isGrounded;
|
||||
private bool _isHoldingInteract = false;
|
||||
|
||||
// NGO에서 Start 대신 사용하는 네트워크 초기화 메서드
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 내 캐릭터가 아니라면 입력을 활성화하지 않습니다.
|
||||
if (!IsOwner) return;
|
||||
|
||||
_inputActions = new PlayerInputActions();
|
||||
_inputActions.Player.Jump.performed += ctx => OnJump();
|
||||
_inputActions.Player.Attack.performed += ctx => OnAttackServerRpc(); // 서버에 공격 요청
|
||||
_inputActions.Player.Interact.performed += ctx => OnInteractTap();
|
||||
|
||||
_inputActions.Player.Interact.started += ctx => _isHoldingInteract = true;
|
||||
_inputActions.Player.Interact.canceled += ctx => _isHoldingInteract = false;
|
||||
|
||||
_inputActions.Enable();
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_animator = GetComponent<Animator>();
|
||||
_traveler = GetComponent<TunnelTraveler>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// [중요] '나'의 캐릭터가 아니면 조종 로직을 아예 실행하지 않습니다.
|
||||
if (!IsOwner) return;
|
||||
if (_traveler != null && _traveler.IsTraveling) return;
|
||||
|
||||
HandleGravity();
|
||||
HandleMovement();
|
||||
|
||||
// 건설 가속 로직
|
||||
if (_isHoldingInteract) PerformConstructionSupport();
|
||||
}
|
||||
|
||||
private void HandleMovement()
|
||||
{
|
||||
_isGrounded = _controller.isGrounded;
|
||||
_animator.SetBool("isGrounded", _isGrounded);
|
||||
|
||||
bool isAttacking = _animator.GetCurrentAnimatorStateInfo(0).IsTag("Attack");
|
||||
_moveInput = _inputActions.Player.Move.ReadValue<Vector2>();
|
||||
Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y).normalized;
|
||||
|
||||
// 지상/공중 및 공격 중 관성 처리
|
||||
if (isAttacking) move = _isGrounded ? Vector3.zero : _currentMoveDir;
|
||||
else if (move.magnitude > 0.1f) _currentMoveDir = move;
|
||||
|
||||
if (move.magnitude >= 0.1f)
|
||||
{
|
||||
if (!isAttacking)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(move);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
_controller.Move(move * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
_animator.SetFloat("MoveSpeed", isAttacking && _isGrounded ? 0 : move.magnitude);
|
||||
}
|
||||
|
||||
private void HandleGravity()
|
||||
{
|
||||
if (_isGrounded && _velocity.y < 0) _velocity.y = -2f;
|
||||
_velocity.y += gravity * Time.deltaTime;
|
||||
_controller.Move(_velocity * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnJump()
|
||||
{
|
||||
if (_isGrounded) _velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
// 기존 OnAttackServerRpc를 수정하거나 호출되는 시점에 아래 로직 포함
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Owner)]
|
||||
private void OnAttackServerRpc()
|
||||
{
|
||||
// 모든 유저에게 애니메이션 재생 신호 전송
|
||||
OnAttackClientRpc();
|
||||
|
||||
// [채광 판정] 서버에서 물리 연산을 통해 전방의 블록 탐색
|
||||
Collider[] hitBlocks = Physics.OverlapSphere(transform.position + transform.forward, attackRange, mineableLayer);
|
||||
|
||||
foreach (var col in hitBlocks)
|
||||
{
|
||||
MineableBlock block = col.GetComponentInParent<MineableBlock>();
|
||||
if (block != null)
|
||||
{
|
||||
// 서버가 직접 블록의 대미지 함수 호출
|
||||
block.TakeDamageRpc(miningDamage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void OnAttackClientRpc()
|
||||
{
|
||||
_animator.SetTrigger("Attack");
|
||||
}
|
||||
|
||||
private void OnInteractTap()
|
||||
{
|
||||
_animator.SetTrigger("Interact");
|
||||
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, interactRange, interactableLayer);
|
||||
foreach (var col in colliders)
|
||||
{
|
||||
IInteractable interactable = col.GetComponentInParent<IInteractable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interact(gameObject);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PerformConstructionSupport()
|
||||
{
|
||||
Collider[] targets = Physics.OverlapSphere(transform.position, interactRange, constructionLayer);
|
||||
foreach (var col in targets)
|
||||
{
|
||||
ConstructionSite site = col.GetComponentInParent<ConstructionSite>();
|
||||
if (site != null)
|
||||
{
|
||||
// 건설 진행도는 서버에서 관리하는 것이 안전하므로 나중에 RPC로 전환 필요
|
||||
site.AdvanceConstruction(Time.deltaTime * buildSpeedMultiplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
if (IsOwner) _inputActions.Disable();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Player/PlayerNetworkController.cs.meta
Normal file
2
Assets/Scripts/Player/PlayerNetworkController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a5866e584e3482645a906bd152cd00fe
|
||||
@@ -1,56 +1,45 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class ConstructionSite : MonoBehaviour
|
||||
public class ConstructionSite : NetworkBehaviour
|
||||
{
|
||||
private GameObject _finalPrefab;
|
||||
private float _buildTime;
|
||||
private float _timer;
|
||||
private NetworkVariable<float> _currentTimer = new NetworkVariable<float>(0f);
|
||||
private NetworkVariable<int> _syncTurretIndex = new NetworkVariable<int>();
|
||||
private Vector3Int _gridPos;
|
||||
private bool _isCompleted = false; // 중복 완공 방지 플래그
|
||||
private float _targetBuildTime;
|
||||
private bool _isCompleted = false;
|
||||
|
||||
public void Initialize(GameObject final, float time, Vector3Int pos)
|
||||
public void Initialize(int index, Vector3Int pos)
|
||||
{
|
||||
_finalPrefab = final;
|
||||
_buildTime = time;
|
||||
if (!IsServer) return;
|
||||
_syncTurretIndex.Value = index;
|
||||
_gridPos = pos;
|
||||
_timer = 0;
|
||||
_isCompleted = false;
|
||||
_targetBuildTime = BuildManager.Instance.GetTurretData(index).buildTime;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_isCompleted) return;
|
||||
if (!IsServer || _isCompleted) return;
|
||||
|
||||
// 매 프레임 자동으로 시간이 흐름
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer >= _buildTime) Complete();
|
||||
_currentTimer.Value += Time.deltaTime;
|
||||
if (_currentTimer.Value >= _targetBuildTime) CompleteBuild();
|
||||
}
|
||||
|
||||
// [핵심] 플레이어가 상호작용 버튼을 꾹 누를 때 호출되는 함수
|
||||
public void AdvanceConstruction(float amount)
|
||||
public void AdvanceConstruction(float amount) => AdvanceBuildRpc(amount);
|
||||
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
private void AdvanceBuildRpc(float amount) => _currentTimer.Value += amount;
|
||||
|
||||
private void CompleteBuild()
|
||||
{
|
||||
if (_isCompleted) return;
|
||||
_isCompleted = true;
|
||||
var data = BuildManager.Instance.GetTurretData(_syncTurretIndex.Value);
|
||||
|
||||
_timer += amount;
|
||||
if (_timer >= _buildTime) Complete();
|
||||
}
|
||||
// [중요] 저장된 그리드 좌표로부터 정확한 월드 좌표 복원
|
||||
Vector3 finalPos = BuildManager.Instance.GridToWorld(_gridPos);
|
||||
GameObject finalObj = Instantiate(data.finalPrefab, finalPos, Quaternion.identity);
|
||||
|
||||
private void Complete()
|
||||
{
|
||||
// 1. 터널 생성
|
||||
GameObject tunnel = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
||||
TunnelNode node = tunnel.GetComponentInChildren<TunnelNode>();
|
||||
|
||||
if (node != null)
|
||||
{
|
||||
// 2. [수정] 완공된 터널의 루트 위치로 좌표 계산
|
||||
Vector3Int myGridPos = BuildManager.Instance.WorldToGrid3D(tunnel.transform.position);
|
||||
|
||||
// 3. 레지스트리 등록 및 연결
|
||||
BuildManager.Instance.RegisterTunnel(myGridPos, node);
|
||||
node.LinkVertical();
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
finalObj.GetComponent<NetworkObject>().Spawn();
|
||||
GetComponent<NetworkObject>().Despawn();
|
||||
}
|
||||
}
|
||||
@@ -1,58 +1,34 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class TunnelNode : MonoBehaviour, IInteractable
|
||||
public class TunnelNode : NetworkBehaviour, IInteractable
|
||||
{
|
||||
public TunnelNode aboveNode;
|
||||
public TunnelNode belowNode;
|
||||
|
||||
// [중요] 이 노드가 속한 터널의 최상단 부모를 캐싱합니다.
|
||||
private Transform _tunnelRoot;
|
||||
|
||||
void Awake()
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 이름이 "Tunnel"로 시작하는 부모를 찾거나, 단순히 부모의 부모를 참조합니다.
|
||||
// 여기서는 이미지 구조에 맞게 부모(Visual)의 부모(Tunnel (1))를 찾습니다.
|
||||
_tunnelRoot = transform.parent.parent;
|
||||
// 모든 클라이언트에서 각자 장부에 등록
|
||||
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(transform.position);
|
||||
BuildManager.Instance.RegisterTunnel(myPos, this);
|
||||
|
||||
// 모든 노드가 등록될 시간을 벌기 위해 서버에서 약간 지연 후 연결 명령
|
||||
if (IsServer) Invoke(nameof(SyncLinks), 0.2f);
|
||||
}
|
||||
|
||||
private void SyncLinks() => LinkVerticalRpc();
|
||||
|
||||
[Rpc(SendTo.Everyone)]
|
||||
private void LinkVerticalRpc() => LinkVertical();
|
||||
|
||||
public void LinkVertical()
|
||||
{
|
||||
// 루트(Tunnel 1)의 위치로 내 위치 파악
|
||||
Transform myRoot = transform.parent.parent;
|
||||
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(myRoot.position);
|
||||
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(transform.position);
|
||||
aboveNode = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.up);
|
||||
belowNode = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.down);
|
||||
|
||||
// 위/아래 터널 찾기
|
||||
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);
|
||||
}
|
||||
if (aboveNode != null) aboveNode.belowNode = this;
|
||||
if (belowNode != null) belowNode.aboveNode = this;
|
||||
}
|
||||
|
||||
public void Interact(GameObject user)
|
||||
|
||||
Reference in New Issue
Block a user