자원 오브젝트가 일꾼에게 자신이 가지고 있는 것 보다 많은 자원을 지급하는 문제 수정

This commit is contained in:
2026-02-02 20:44:02 +09:00
parent 958ae1cb75
commit 602cb2985c
3 changed files with 71 additions and 8 deletions

View File

@@ -105,7 +105,8 @@ MonoBehaviour:
miningSpeed: 1 miningSpeed: 1
followDistance: 3 followDistance: 3
movementSpeed: 21.41 movementSpeed: 21.41
resourcesPerMining: 5 resourcesPerMining: 29
recruitmentCost: 10
interactionAnimationTrigger: interactionAnimationTrigger:
miningEffectPrefab: {fileID: 0} miningEffectPrefab: {fileID: 0}
depositEffectPrefab: {fileID: 0} depositEffectPrefab: {fileID: 0}

View File

@@ -78,21 +78,21 @@ namespace Northbound
return _currentWorkerId.Value == workerId; return _currentWorkerId.Value == workerId;
} }
public void TakeResourcesForWorker(int amount, ulong workerId) public int TakeResourcesForWorker(int amount, ulong workerId)
{ {
if (!IsServer) return; if (!IsServer) return 0;
if (!allowMultipleWorkers) if (!allowMultipleWorkers)
{ {
if (_currentWorkerId.Value != ulong.MaxValue && _currentWorkerId.Value != workerId) if (_currentWorkerId.Value != ulong.MaxValue && _currentWorkerId.Value != workerId)
return; return 0;
} }
int availableResources = _currentResources.Value; int availableResources = _currentResources.Value;
int actualAmount = Mathf.Min(amount, availableResources); int actualAmount = Mathf.Min(amount, availableResources);
if (actualAmount <= 0) if (actualAmount <= 0)
return; return 0;
_currentResources.Value -= actualAmount; _currentResources.Value -= actualAmount;
_lastGatheringTime = Time.time; _lastGatheringTime = Time.time;
@@ -108,6 +108,8 @@ namespace Northbound
} }
ShowGatheringEffectClientRpc(); ShowGatheringEffectClientRpc();
return actualAmount;
} }
private NetworkVariable<int> _currentResources = new NetworkVariable<int>( private NetworkVariable<int> _currentResources = new NetworkVariable<int>(

View File

@@ -8,7 +8,8 @@ namespace Northbound
Idle = 0, Idle = 0,
Following = 1, Following = 1,
Mining = 2, Mining = 2,
Returning = 3 Returning = 3,
WaitingForResource = 4
} }
[RequireComponent(typeof(Collider))] [RequireComponent(typeof(Collider))]
@@ -136,6 +137,9 @@ namespace Northbound
case WorkerState.Returning: case WorkerState.Returning:
HandleReturning(); HandleReturning();
break; break;
case WorkerState.WaitingForResource:
HandleWaitingForResource();
break;
} }
} }
@@ -186,6 +190,13 @@ namespace Northbound
return; return;
} }
if (!_targetResource.HasResourcesAvailable())
{
SetState(WorkerState.WaitingForResource);
PlayIdleAnimation();
return;
}
float distance = GetDistanceToCollider(_targetResource.GetComponent<Collider>()); float distance = GetDistanceToCollider(_targetResource.GetComponent<Collider>());
if (distance > 2f) if (distance > 2f)
@@ -225,6 +236,41 @@ namespace Northbound
} }
} }
private void HandleWaitingForResource()
{
if (_targetResource == null)
{
SetState(WorkerState.Following);
PlayIdleAnimation();
return;
}
if (IsBagFull)
{
SetState(WorkerState.Returning);
PlayIdleAnimation();
return;
}
if (_targetResource.HasResourcesAvailable())
{
SetState(WorkerState.Mining);
PlayMoveAnimation();
return;
}
float distance = GetDistanceToCollider(_targetResource.GetComponent<Collider>());
if (distance > 3f)
{
MoveTowards(_targetResource.transform.position);
}
else
{
PlayIdleAnimation();
}
}
private float GetDistanceToCollider(Collider targetCollider) private float GetDistanceToCollider(Collider targetCollider)
{ {
if (targetCollider == null) if (targetCollider == null)
@@ -274,8 +320,8 @@ namespace Northbound
if (mineAmount > 0) if (mineAmount > 0)
{ {
_targetResource.TakeResourcesForWorker(mineAmount, NetworkObject.NetworkObjectId); int actualAmount = _targetResource.TakeResourcesForWorker(mineAmount, NetworkObject.NetworkObjectId);
_currentBagResources.Value += mineAmount; _currentBagResources.Value += actualAmount;
_lastMiningTime = Time.time; _lastMiningTime = Time.time;
ShowMiningEffectClientRpc(); ShowMiningEffectClientRpc();
@@ -303,6 +349,10 @@ namespace Northbound
{ {
SetState(WorkerState.Mining); SetState(WorkerState.Mining);
} }
else if (_targetResource != null)
{
SetState(WorkerState.WaitingForResource);
}
else else
{ {
SetState(WorkerState.Following); SetState(WorkerState.Following);
@@ -427,6 +477,7 @@ namespace Northbound
WorkerState.Following => "따라가는 중", WorkerState.Following => "따라가는 중",
WorkerState.Mining => "채굴 중", WorkerState.Mining => "채굴 중",
WorkerState.Returning => "반환 중", WorkerState.Returning => "반환 중",
WorkerState.WaitingForResource => "자원 대기 중",
_ => "대기 중" _ => "대기 중"
}; };
return $"워커 (가방: {_currentBagResources.Value}/{maxBagCapacity}) - {stateText}"; return $"워커 (가방: {_currentBagResources.Value}/{maxBagCapacity}) - {stateText}";
@@ -507,6 +558,9 @@ namespace Northbound
case WorkerState.Returning: case WorkerState.Returning:
PlayMoveAnimation(); PlayMoveAnimation();
break; break;
case WorkerState.WaitingForResource:
PlayIdleAnimation();
break;
} }
} }
@@ -642,6 +696,12 @@ namespace Northbound
Gizmos.DrawLine(transform.position, _targetResource.transform.position); Gizmos.DrawLine(transform.position, _targetResource.transform.position);
} }
if (_currentState.Value == WorkerState.WaitingForResource && _targetResource != null)
{
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, _targetResource.transform.position);
}
if (_currentState.Value == WorkerState.Returning && _core != null) if (_currentState.Value == WorkerState.Returning && _core != null)
{ {
Gizmos.color = Color.green; Gizmos.color = Color.green;