block improvements, autolock improvements and auto off, block button auto on/off, recoil anim improvement
This commit is contained in:
@@ -13,7 +13,15 @@ namespace Beyond
|
||||
#region Fields & Properties
|
||||
|
||||
[Header("Targeting Parameters")]
|
||||
[Tooltip("The maximum distance to highlight a potential target.")]
|
||||
public float maxTargetingDistance = 20f;
|
||||
|
||||
[Tooltip("The distance within which the system will automatically lock on to the highlighted target.")]
|
||||
public float autoLockOnDistance = 15f;
|
||||
|
||||
[Tooltip("The distance at which a locked-on target will be automatically unlocked.")]
|
||||
public float unlockDistanceThreshold = 25f;
|
||||
|
||||
public float targetingInterval = 0.25f;
|
||||
public float targetingAngleThreshold = 90f;
|
||||
|
||||
@@ -98,7 +106,7 @@ namespace Beyond
|
||||
{
|
||||
if (_gameStateManager != null) _gameStateManager.m_OnStateChanged.RemoveListener(HandleGameStateChanged);
|
||||
|
||||
StopAndClearAllFadeCoroutines(); // This call is correct
|
||||
StopAndClearAllFadeCoroutines();
|
||||
|
||||
if (_targetingLoopCoroutine != null) StopCoroutine(_targetingLoopCoroutine);
|
||||
|
||||
@@ -113,7 +121,6 @@ namespace Beyond
|
||||
|
||||
#region Core Logic
|
||||
|
||||
// --- THIS IS THE MISSING METHOD THAT HAS BEEN RESTORED ---
|
||||
private void StopAndClearAllFadeCoroutines()
|
||||
{
|
||||
foreach (var pair in _materialToFadeCoroutineMap)
|
||||
@@ -125,7 +132,6 @@ namespace Beyond
|
||||
}
|
||||
_materialToFadeCoroutineMap.Clear();
|
||||
}
|
||||
// --- END OF RESTORED METHOD ---
|
||||
|
||||
private void HandleGameStateChanged(GameStateManager.State newState)
|
||||
{
|
||||
@@ -164,41 +170,83 @@ namespace Beyond
|
||||
}
|
||||
}
|
||||
|
||||
// REFACTORED: This method now has a clearer, more robust flow.
|
||||
private void UpdateTarget()
|
||||
{
|
||||
if (_playerTransform == null || _gameStateManager == null || _manualSwitchCooldownActive) return;
|
||||
|
||||
// Step 1: Always find the absolute best candidate in range right now.
|
||||
vFSMBehaviourController bestCandidate = null;
|
||||
float minDistanceSqr = maxTargetingDistance * maxTargetingDistance;
|
||||
HashSet<vFSMBehaviourController> combatControllers = _gameStateManager.GetActiveCombatcontrollers();
|
||||
|
||||
if (combatControllers == null || combatControllers.Count == 0)
|
||||
if (combatControllers != null && combatControllers.Count > 0)
|
||||
{
|
||||
if (CurrentTarget != null) SetNewTarget(null);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var controller in combatControllers)
|
||||
{
|
||||
if (controller == null || !controller.gameObject.activeInHierarchy || controller.aiController.currentHealth <= 0) continue;
|
||||
if (!IsTargetInAngle(_playerTransform, controller, targetingAngleThreshold)) continue;
|
||||
|
||||
float distSqr = (controller.transform.position - _playerTransform.position).sqrMagnitude;
|
||||
if (distSqr <= minDistanceSqr)
|
||||
foreach (var controller in combatControllers)
|
||||
{
|
||||
minDistanceSqr = distSqr;
|
||||
bestCandidate = controller;
|
||||
if (controller == null || !controller.gameObject.activeInHierarchy || controller.aiController.currentHealth <= 0) continue;
|
||||
if (!IsTargetInAngle(_playerTransform, controller, targetingAngleThreshold)) continue;
|
||||
|
||||
float distSqr = (controller.transform.position - _playerTransform.position).sqrMagnitude;
|
||||
if (distSqr <= minDistanceSqr)
|
||||
{
|
||||
minDistanceSqr = distSqr;
|
||||
bestCandidate = controller;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CurrentTarget != bestCandidate) SetNewTarget(bestCandidate);
|
||||
else if (CurrentTarget != null && !IsTargetValid(CurrentTarget)) SetNewTarget(null);
|
||||
// Step 2: If the best candidate is different from our current one, switch the highlight.
|
||||
if (CurrentTarget != bestCandidate)
|
||||
{
|
||||
SetNewTarget(bestCandidate);
|
||||
}
|
||||
|
||||
// Step 3: Every update, evaluate and apply the correct lock-on state for the current target.
|
||||
UpdateLockOnState();
|
||||
}
|
||||
|
||||
// NEW: This method exclusively handles the logic for locking on and off.
|
||||
private void UpdateLockOnState()
|
||||
{
|
||||
if (targetLockSystem == null || _playerTransform == null) return;
|
||||
|
||||
// Determine if the target *should* be locked based on distance rules.
|
||||
bool shouldBeLocked = false;
|
||||
if (CurrentTarget != null && (autoLockSelectedTarget || alwaysLockOnInCombat))
|
||||
{
|
||||
float distanceToTarget = Vector3.Distance(_playerTransform.position, CurrentTarget.transform.position);
|
||||
|
||||
// This is a hysteresis check: use different distances for locking and unlocking to prevent flickering.
|
||||
if (targetLockSystem.isLockingOn)
|
||||
{
|
||||
// If already locked, stay locked unless we are beyond the unlock threshold.
|
||||
shouldBeLocked = distanceToTarget <= unlockDistanceThreshold;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not locked, we only engage the lock if we are within the auto-lock distance.
|
||||
shouldBeLocked = distanceToTarget <= autoLockOnDistance;
|
||||
}
|
||||
}
|
||||
|
||||
// Synchronize the desired state with the lock-on system.
|
||||
Transform desiredLockTarget = shouldBeLocked ? CurrentTarget.transform : null;
|
||||
|
||||
// The lock-on system should be smart enough to not do anything if the target hasn't changed.
|
||||
// We send the desired target (or null) every update.
|
||||
targetLockSystem.ManuallySetLockOnTarget(desiredLockTarget, true);
|
||||
|
||||
if (alwaysLockOnInCombat && desiredLockTarget != null && !targetLockSystem.isLockingOn)
|
||||
{
|
||||
targetLockSystem.SetLockOn(true);
|
||||
}
|
||||
}
|
||||
|
||||
// SIMPLIFIED: This method now only handles changing the CurrentTarget reference and its visual highlight.
|
||||
private void SetNewTarget(vFSMBehaviourController newTarget, bool forceLockSystemUpdate = false)
|
||||
{
|
||||
if (_manualSwitchCooldownActive && !forceLockSystemUpdate) return;
|
||||
if (CurrentTarget == newTarget && !forceLockSystemUpdate) return;
|
||||
if (CurrentTarget == newTarget) return;
|
||||
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
@@ -208,16 +256,6 @@ namespace Beyond
|
||||
|
||||
CurrentTarget = newTarget;
|
||||
|
||||
if (targetLockSystem != null)
|
||||
{
|
||||
bool shouldLock = (autoLockSelectedTarget || alwaysLockOnInCombat) && CurrentTarget != null;
|
||||
if (alwaysLockOnInCombat && CurrentTarget != null && !targetLockSystem.isLockingOn)
|
||||
{
|
||||
targetLockSystem.SetLockOn(true);
|
||||
}
|
||||
targetLockSystem.ManuallySetLockOnTarget(shouldLock ? CurrentTarget.transform : null, true);
|
||||
}
|
||||
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
ApplyHighlight(CurrentTarget, true);
|
||||
@@ -256,9 +294,14 @@ namespace Beyond
|
||||
vFSMBehaviourController fsmTarget = lockedTargetTransform.GetComponentInParent<vFSMBehaviourController>();
|
||||
if (fsmTarget == null) fsmTarget = lockedTargetTransform.GetComponentInChildren<vFSMBehaviourController>(true);
|
||||
|
||||
if (CurrentTarget != null && CurrentTarget.transform == lockedTargetTransform) return;
|
||||
if (fsmTarget != null) SetNewTarget(fsmTarget, true);
|
||||
else SetNewTarget(null, true);
|
||||
if (fsmTarget != null && CurrentTarget != fsmTarget)
|
||||
{
|
||||
SetNewTarget(fsmTarget, true);
|
||||
}
|
||||
else if (fsmTarget == null && CurrentTarget != null)
|
||||
{
|
||||
SetNewTarget(null, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleLockOnSystemTargetUnlocked(Transform previouslyLockedTargetTransform)
|
||||
@@ -266,10 +309,9 @@ namespace Beyond
|
||||
if (targetLockSystem == null) return;
|
||||
_manualSwitchCooldownActive = true;
|
||||
_manualSwitchCooldownTimer = manualSwitchCooldownDuration;
|
||||
if (CurrentTarget != null && previouslyLockedTargetTransform == CurrentTarget.transform)
|
||||
{
|
||||
SetNewTarget(null, true);
|
||||
}
|
||||
|
||||
// After manually unlocking, we let the main UpdateTarget loop find the next best candidate.
|
||||
// If the previously locked target is still the closest, it will be re-highlighted automatically.
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user