모든 네트워크 오브젝트의 소유권을 서버가 갖도록 함
Distributable -> None 관련 사이드 이펙트로 인한 버그 수정
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Unity.Netcode;
|
||||
using Unity.Netcode.Components;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System.Collections.Generic;
|
||||
@@ -19,17 +20,23 @@ namespace Northbound
|
||||
private PlayerInputActions _inputActions;
|
||||
private Dictionary<string, IAction> _actions = new Dictionary<string, IAction>();
|
||||
private Animator _animator;
|
||||
private NetworkAnimator _networkAnimator;
|
||||
private NetworkPlayerController _networkPlayerController;
|
||||
|
||||
// 로컬 플레이어인지 확인
|
||||
private bool IsLocalPlayer => _networkPlayerController != null && _networkPlayerController.IsLocalPlayer;
|
||||
private ulong LocalPlayerId => _networkPlayerController != null ? _networkPlayerController.OwnerPlayerId : OwnerClientId;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_animator = GetComponent<Animator>();
|
||||
_networkAnimator = GetComponent<NetworkAnimator>();
|
||||
_networkPlayerController = GetComponent<NetworkPlayerController>();
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (!IsOwner) return;
|
||||
|
||||
// 액션 컴포넌트들을 딕셔너리에 등록
|
||||
// 액션 컴포넌트들을 딕셔너리에 등록 (모든 클라이언트에서)
|
||||
foreach (var component in actionComponents)
|
||||
{
|
||||
if (component is IAction action)
|
||||
@@ -38,15 +45,39 @@ namespace Northbound
|
||||
}
|
||||
}
|
||||
|
||||
// _ownerPlayerId 변경 이벤트 구독
|
||||
if (_networkPlayerController != null)
|
||||
{
|
||||
_networkPlayerController.OnOwnerChanged += OnOwnerPlayerIdChanged;
|
||||
}
|
||||
|
||||
// 이미 로컬 플레이어면 입력 초기화
|
||||
TryInitializeInput();
|
||||
}
|
||||
|
||||
private void OnOwnerPlayerIdChanged(ulong newOwnerId)
|
||||
{
|
||||
TryInitializeInput();
|
||||
}
|
||||
|
||||
private void TryInitializeInput()
|
||||
{
|
||||
if (!IsLocalPlayer) return;
|
||||
if (_inputActions != null) return;
|
||||
|
||||
_inputActions = new PlayerInputActions();
|
||||
_inputActions.Player.Attack.performed += OnAttack;
|
||||
// 다른 액션들도 여기에 바인딩
|
||||
_inputActions.Enable();
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
if (IsOwner && _inputActions != null)
|
||||
if (_networkPlayerController != null)
|
||||
{
|
||||
_networkPlayerController.OnOwnerChanged -= OnOwnerPlayerIdChanged;
|
||||
}
|
||||
|
||||
if (_inputActions != null)
|
||||
{
|
||||
_inputActions.Player.Attack.performed -= OnAttack;
|
||||
_inputActions.Disable();
|
||||
@@ -63,23 +94,30 @@ namespace Northbound
|
||||
{
|
||||
if (_actions.TryGetValue(actionName, out IAction action))
|
||||
{
|
||||
if (action.CanExecute(OwnerClientId))
|
||||
if (action.CanExecute(LocalPlayerId))
|
||||
{
|
||||
// 애니메이션 재생 (액션 실행 전)
|
||||
if (playAnimations && _animator != null)
|
||||
string animTrigger = action.GetActionAnimation();
|
||||
|
||||
// 서버에서 애니메이션 실행 (동기화를 위해)
|
||||
if (playAnimations && !string.IsNullOrEmpty(animTrigger))
|
||||
{
|
||||
string animTrigger = action.GetActionAnimation();
|
||||
if (!string.IsNullOrEmpty(animTrigger))
|
||||
{
|
||||
_animator.SetTrigger(animTrigger);
|
||||
}
|
||||
PlayAnimationServerRpc(animTrigger);
|
||||
}
|
||||
|
||||
action.Execute(OwnerClientId);
|
||||
action.Execute(LocalPlayerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void PlayAnimationServerRpc(string animTrigger)
|
||||
{
|
||||
if (_networkAnimator != null && !string.IsNullOrEmpty(animTrigger))
|
||||
{
|
||||
_networkAnimator.SetTrigger(animTrigger);
|
||||
}
|
||||
}
|
||||
|
||||
override public void OnDestroy()
|
||||
{
|
||||
if (_inputActions != null)
|
||||
|
||||
Reference in New Issue
Block a user