코드 리팩토링

재사용성 및 확장성을 고려하여 코드 전반을 리팩토링함
This commit is contained in:
2026-01-21 01:45:15 +09:00
parent b4ac8f600f
commit db5db4b106
45 changed files with 2775 additions and 248 deletions

View File

@@ -204,35 +204,50 @@ public class PlayerNetworkController : NetworkBehaviour
if (_isGrounded) _velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
// 1. 액션 (좌클릭) - 대상이 없어도 나감
// PlayerNetworkController.cs 중 일부
// 1. Action (Left Click) - executes even without target
private void OnActionInput()
{
if (!IsOwner || _actionHandler.IsBusy) return;
ItemData selectedItem = _inventory.GetSelectedItemData();
if (selectedItem == null) return;
// 로그 1: 아이템 확인
if (selectedItem == null) { Debug.Log("선택된 아이템이 없음"); return; }
// 로그 2: 도구 여부 및 액션 데이터 확인
Debug.Log($"현재 아이템: {selectedItem.itemName}, 도구여부: {selectedItem.isTool}, 액션데이터: {selectedItem.toolAction != null}");
if (selectedItem.isTool && selectedItem.toolAction != null)
// Check if item has behavior (new system)
if (selectedItem.behavior != null)
{
if (_lastHighlightedBlock != null)
GameObject target = _lastHighlightedBlock != null ? _lastHighlightedBlock.gameObject : null;
// Use the new behavior system
if (selectedItem.CanUse(gameObject, target))
{
Debug.Log($"채광 시작: {_lastHighlightedBlock.name}");
_actionHandler.PerformAction(selectedItem.toolAction, _lastHighlightedBlock.gameObject);
}
else
{
Debug.Log("조준된 블록이 없음 (하이라이트 확인 필요)");
_actionHandler.PerformAction(selectedItem.toolAction, null);
// Get action descriptor and perform action
var actionDesc = selectedItem.GetUseAction();
if (actionDesc != null)
{
_actionHandler.PerformAction(
CreateActionDataFromDescriptor(actionDesc, selectedItem.behavior),
target
);
}
}
}
}
// Helper to bridge between new ActionDescriptor and legacy PlayerActionData
private PlayerActionData CreateActionDataFromDescriptor(ActionDescriptor desc, ItemBehavior behavior)
{
// Create a temporary runtime action data
var actionData = ScriptableObject.CreateInstance<BehaviorActionData>();
actionData.actionName = desc.ActionName;
actionData.duration = desc.Duration;
actionData.animTrigger = desc.AnimTrigger;
actionData.impactDelay = desc.ImpactDelay;
actionData.baseSpeed = desc.AnimSpeed;
actionData.canRepeat = desc.CanRepeat;
actionData.behavior = behavior;
return actionData;
}
// 2. 인터랙션 (F키) - 대상이 없으면 아예 시작 안 함
private void OnInteractTap()
{
@@ -250,13 +265,13 @@ public class PlayerNetworkController : NetworkBehaviour
{
if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(targetId, out var target))
{
if (target.TryGetComponent<MineableBlock>(out var block))
// Use IDamageable interface instead of MineableBlock directly
if (target.TryGetComponent<IDamageable>(out var damageable))
{
// 서버에서 최종 거리 검증 후 대미지 적용
// Server-side distance validation before applying damage
if (Vector3.Distance(transform.position, target.transform.position) <= attackRange + 1.0f)
{
block.TakeDamageRpc(miningDamage);
block.PlayHitEffectClientRpc();
damageable.TakeDamage(new DamageInfo(miningDamage, DamageType.Mining, gameObject));
}
}
}
@@ -487,10 +502,11 @@ public class PlayerNetworkController : NetworkBehaviour
private void HandleContinuousAction()
{
ItemData selectedItem = _inventory.GetSelectedItemData();
if (selectedItem == null || !selectedItem.isTool || selectedItem.toolAction == null) return;
if (selectedItem == null || selectedItem.behavior == null) return;
// [핵심] 반복 가능한 액션일 때만 Update에서 재실행
if (selectedItem.toolAction.canRepeat)
// Only repeat if action supports it
var actionDesc = selectedItem.GetUseAction();
if (actionDesc != null && actionDesc.CanRepeat)
{
TryExecuteAction();
}
@@ -501,15 +517,23 @@ public class PlayerNetworkController : NetworkBehaviour
if (_actionHandler.IsBusy) return;
ItemData selectedItem = _inventory.GetSelectedItemData();
if (selectedItem != null && selectedItem.isTool && selectedItem.toolAction != null)
if (selectedItem == null || selectedItem.behavior == null) return;
var actionDesc = selectedItem.GetUseAction();
if (actionDesc == null) return;
// Skip if non-repeatable action already executed once
if (!actionDesc.CanRepeat && _hasExecutedOnce) return;
GameObject target = _lastHighlightedBlock != null ? _lastHighlightedBlock.gameObject : null;
if (selectedItem.CanUse(gameObject, target))
{
// 단발성 액션인데 이미 한 번 실행했다면 스킵
if (!selectedItem.toolAction.canRepeat && _hasExecutedOnce) return;
GameObject target = _lastHighlightedBlock != null ? _lastHighlightedBlock.gameObject : null;
_actionHandler.PerformAction(selectedItem.toolAction, target);
_hasExecutedOnce = true; // 실행 기록 저장
_actionHandler.PerformAction(
CreateActionDataFromDescriptor(actionDesc, selectedItem.behavior),
target
);
_hasExecutedOnce = true;
}
}