fix in autoweapon draw/ hide, bLock, bMeleeCombat and Input
This commit is contained in:
@@ -1,359 +1,467 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq; // Required for List.AddRange and ToArray
|
||||
using Invector.vCharacterController.AI.FSMBehaviour; // For vFSMBehaviourController
|
||||
using Beyond; // For GameStateManager and Player (ensure Player.cs is in this namespace or adjust)
|
||||
using System; // For Action
|
||||
using System.Linq;
|
||||
using Invector.vCharacterController.AI.FSMBehaviour;
|
||||
using Beyond;
|
||||
using System;
|
||||
|
||||
public class AutoTargetting : MonoBehaviour
|
||||
namespace Beyond
|
||||
{
|
||||
[Header("Targeting Parameters")]
|
||||
[Tooltip("Maximum distance AutoTargetting will consider an enemy for selection.")]
|
||||
public float maxTargetingDistance = 20f;
|
||||
[Tooltip("How often (in seconds) to re-evaluate for a new target during combat.")]
|
||||
public float targetingInterval = 0.25f;
|
||||
[Tooltip("Maximum angle (in degrees from player's forward) within which an enemy can be auto-targeted.")]
|
||||
public float targetingAngleThreshold = 90f;
|
||||
|
||||
[Header("Rotation Parameters")]
|
||||
[Tooltip("Speed at which the player rotates towards the current target when rotation is explicitly called (e.g., by MagicAttacks).")]
|
||||
public float playerRotationSpeed = 10f; // This will be used by MagicAttacks
|
||||
|
||||
[Header("Visuals")]
|
||||
[Tooltip("Name of the material color property to animate for Fresnel effect.")]
|
||||
public string materialHighlightPropertyName = "_FresnelColor";
|
||||
[Tooltip("HDR Color to use for Fresnel highlight when a target is selected (fade-in target).")]
|
||||
[ColorUsage(true, true)]
|
||||
public Color highlightColor = Color.white;
|
||||
[Tooltip("HDR Color to use for Fresnel when a target is deselected (fade-out target).")]
|
||||
[ColorUsage(true, true)]
|
||||
public Color deselectHighlightColor = Color.black;
|
||||
[Tooltip("Duration of the fade in/out animation for the highlight.")]
|
||||
public float highlightFadeDuration = 0.3f;
|
||||
[Tooltip("If true, was previously used to prefer SkinnedMeshRenderer. Now GetTargetRenderers collects both SkinnedMeshRenderers and MeshRenderers regardless of this flag. This flag might be repurposed or removed in the future.")]
|
||||
public bool preferSkinnedMeshRenderer = true; // Note: Its effect on GetTargetRenderers is changed.
|
||||
|
||||
public vFSMBehaviourController CurrentTarget { get; private set; }
|
||||
public event Action<vFSMBehaviourController> OnTargetSelected;
|
||||
public event Action<vFSMBehaviourController> OnTargetDeselected;
|
||||
|
||||
private GameStateManager _gameStateManager;
|
||||
private Coroutine _targetingLoopCoroutine;
|
||||
private Dictionary<Material, Color> _originalMaterialColors = new Dictionary<Material, Color>();
|
||||
private Dictionary<Material, Coroutine> _materialToFadeCoroutineMap = new Dictionary<Material, Coroutine>();
|
||||
private Transform _playerTransform;
|
||||
|
||||
void Start()
|
||||
public class AutoTargetting : MonoBehaviour
|
||||
{
|
||||
if (Player.Instance == null)
|
||||
{
|
||||
Debug.LogError("AutoTargetting: Player.Instance is not available at Start! Ensure Player script with static Instance exists and runs before AutoTargetting.");
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
_playerTransform = Player.Instance.transform;
|
||||
// ... (other headers and variables)
|
||||
[Header("Targeting Parameters")]
|
||||
[Tooltip("Maximum distance AutoTargetting will consider an enemy for selection.")]
|
||||
public float maxTargetingDistance = 20f;
|
||||
[Tooltip("How often (in seconds) to re-evaluate for a new target during combat.")]
|
||||
public float targetingInterval = 0.25f;
|
||||
[Tooltip("Maximum angle (in degrees from player's forward) within which an enemy can be auto-targeted.")]
|
||||
public float targetingAngleThreshold = 90f;
|
||||
|
||||
_gameStateManager = GameStateManager.Instance;
|
||||
if (_gameStateManager != null)
|
||||
{
|
||||
_gameStateManager.m_OnStateChanged.AddListener(HandleGameStateChanged);
|
||||
HandleGameStateChanged(_gameStateManager.CurrentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("AutoTargetting: GameStateManager.Instance not found! Disabling script.");
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
[Header("Rotation Parameters")]
|
||||
[Tooltip("Speed at which the player rotates towards the current target when rotation is explicitly called (e.g., by MagicAttacks).")]
|
||||
public float playerRotationSpeed = 10f;
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (_gameStateManager != null)
|
||||
{
|
||||
_gameStateManager.m_OnStateChanged.RemoveListener(HandleGameStateChanged);
|
||||
}
|
||||
StopAndClearAllFadeCoroutines();
|
||||
if (_targetingLoopCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_targetingLoopCoroutine);
|
||||
_targetingLoopCoroutine = null;
|
||||
}
|
||||
}
|
||||
[Header("Visuals")]
|
||||
// ... (visual parameters)
|
||||
public string materialHighlightPropertyName = "_FresnelColor";
|
||||
[ColorUsage(true, true)]
|
||||
public Color highlightColor = Color.white;
|
||||
[ColorUsage(true, true)]
|
||||
public Color deselectHighlightColor = Color.black;
|
||||
public float highlightFadeDuration = 0.3f;
|
||||
public bool preferSkinnedMeshRenderer = true;
|
||||
|
||||
private void StopAndClearAllFadeCoroutines()
|
||||
{
|
||||
foreach (var pair in _materialToFadeCoroutineMap)
|
||||
{
|
||||
if (pair.Value != null) StopCoroutine(pair.Value);
|
||||
}
|
||||
_materialToFadeCoroutineMap.Clear();
|
||||
}
|
||||
|
||||
private void HandleGameStateChanged(GameStateManager.State newState)
|
||||
{
|
||||
if (newState == GameStateManager.State.COMBAT)
|
||||
[Header("Lock-On Integration")]
|
||||
[Tooltip("If true, automatically locks onto the target selected by AutoTargetting.")]
|
||||
public bool autoLockSelectedTarget = false;
|
||||
[Tooltip("Reference to the bLockOn script, usually on the player. Will try to find if not set.")]
|
||||
public bLockOn targetLockSystem;
|
||||
[Tooltip("How long (in seconds) AutoTargetting will pause after a manual target switch before re-evaluating.")]
|
||||
public float manualSwitchCooldownDuration = 0.75f; // Cooldown duration
|
||||
|
||||
public vFSMBehaviourController CurrentTarget { get; private set; }
|
||||
public event Action<vFSMBehaviourController> OnTargetSelected;
|
||||
public event Action<vFSMBehaviourController> OnTargetDeselected;
|
||||
|
||||
private GameStateManager _gameStateManager;
|
||||
private Coroutine _targetingLoopCoroutine;
|
||||
private Dictionary<Material, Color> _originalMaterialColors = new Dictionary<Material, Color>();
|
||||
private Dictionary<Material, Coroutine> _materialToFadeCoroutineMap = new Dictionary<Material, Coroutine>();
|
||||
private Transform _playerTransform;
|
||||
|
||||
// Cooldown variables
|
||||
private bool _manualSwitchCooldownActive = false;
|
||||
private float _manualSwitchCooldownTimer = 0f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (_targetingLoopCoroutine == null)
|
||||
// ... (Start logic mostly the same)
|
||||
if (Player.Instance == null)
|
||||
{
|
||||
_targetingLoopCoroutine = StartCoroutine(TargetingLoop());
|
||||
Debug.LogError("AutoTargetting: Player.Instance is not available at Start!");
|
||||
enabled = false; return;
|
||||
}
|
||||
_playerTransform = Player.Instance.transform;
|
||||
|
||||
_gameStateManager = GameStateManager.Instance;
|
||||
if (_gameStateManager != null)
|
||||
{
|
||||
_gameStateManager.m_OnStateChanged.AddListener(HandleGameStateChanged);
|
||||
HandleGameStateChanged(_gameStateManager.CurrentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("AutoTargetting: GameStateManager.Instance not found!");
|
||||
enabled = false; return;
|
||||
}
|
||||
|
||||
if (targetLockSystem == null)
|
||||
{
|
||||
if (Player.Instance != null) targetLockSystem = Player.Instance.GetComponentInChildren<bLockOn>(true);
|
||||
if (targetLockSystem == null) targetLockSystem = GetComponent<bLockOn>(); // Fallback
|
||||
if (targetLockSystem == null)
|
||||
{
|
||||
Debug.LogWarning("AutoTargetting: bLockOn system not found. Auto-lock and target sync will be disabled.");
|
||||
autoLockSelectedTarget = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (targetLockSystem != null)
|
||||
{
|
||||
targetLockSystem.onLockOnTarget.AddListener(HandleLockOnSystemTargetChanged);
|
||||
targetLockSystem.onUnLockOnTarget.AddListener(HandleLockOnSystemTargetUnlocked);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
// ... (Cleanup logic)
|
||||
if (_gameStateManager != null)
|
||||
{
|
||||
_gameStateManager.m_OnStateChanged.RemoveListener(HandleGameStateChanged);
|
||||
}
|
||||
StopAndClearAllFadeCoroutines();
|
||||
if (_targetingLoopCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_targetingLoopCoroutine);
|
||||
_targetingLoopCoroutine = null;
|
||||
}
|
||||
if (CurrentTarget != null)
|
||||
|
||||
if (targetLockSystem != null)
|
||||
{
|
||||
SetNewTarget(null);
|
||||
targetLockSystem.onLockOnTarget.RemoveListener(HandleLockOnSystemTargetChanged);
|
||||
targetLockSystem.onUnLockOnTarget.RemoveListener(HandleLockOnSystemTargetUnlocked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator TargetingLoop()
|
||||
{
|
||||
while (true)
|
||||
private void StopAndClearAllFadeCoroutines()
|
||||
{
|
||||
if (_playerTransform == null && Player.Instance != null)
|
||||
foreach (var pair in _materialToFadeCoroutineMap)
|
||||
{
|
||||
_playerTransform = Player.Instance.transform;
|
||||
if (pair.Value != null) StopCoroutine(pair.Value);
|
||||
}
|
||||
_materialToFadeCoroutineMap.Clear();
|
||||
}
|
||||
|
||||
if (_playerTransform != null)
|
||||
|
||||
private void HandleGameStateChanged(GameStateManager.State newState)
|
||||
{
|
||||
if (newState == GameStateManager.State.COMBAT)
|
||||
{
|
||||
UpdateTarget();
|
||||
}
|
||||
yield return new WaitForSeconds(targetingInterval);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsTargetInAngle(Transform sourceTransform, vFSMBehaviourController targetAI, float angleThreshold)
|
||||
{
|
||||
if (targetAI == null || sourceTransform == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3 directionToTarget = (targetAI.transform.position - sourceTransform.position);
|
||||
directionToTarget.y = 0;
|
||||
|
||||
if (directionToTarget.sqrMagnitude < 0.0001f) return true;
|
||||
|
||||
directionToTarget.Normalize();
|
||||
float angle = Vector3.Angle(sourceTransform.forward, directionToTarget);
|
||||
return angle <= angleThreshold;
|
||||
}
|
||||
|
||||
private void UpdateTarget()
|
||||
{
|
||||
if (_playerTransform == null || _gameStateManager == null) return;
|
||||
|
||||
vFSMBehaviourController bestCandidate = null;
|
||||
float minDistanceSqr = maxTargetingDistance * maxTargetingDistance;
|
||||
|
||||
HashSet<vFSMBehaviourController> combatControllers = _gameStateManager.GetActiveCombatcontrollers();
|
||||
|
||||
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)
|
||||
{
|
||||
minDistanceSqr = distSqr;
|
||||
bestCandidate = controller;
|
||||
}
|
||||
}
|
||||
|
||||
if (CurrentTarget != bestCandidate)
|
||||
{
|
||||
SetNewTarget(bestCandidate);
|
||||
}
|
||||
else if (CurrentTarget != null && !IsTargetValid(CurrentTarget))
|
||||
{
|
||||
SetNewTarget(null);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsTargetValid(vFSMBehaviourController target)
|
||||
{
|
||||
if (target == null || !target.gameObject.activeInHierarchy || target.aiController.currentHealth <= 0)
|
||||
return false;
|
||||
|
||||
if (_playerTransform == null) return false;
|
||||
|
||||
if (!IsTargetInAngle(_playerTransform, target, targetingAngleThreshold))
|
||||
return false;
|
||||
|
||||
float distSqr = (target.transform.position - _playerTransform.position).sqrMagnitude;
|
||||
return distSqr <= maxTargetingDistance * maxTargetingDistance;
|
||||
}
|
||||
|
||||
private void SetNewTarget(vFSMBehaviourController newTarget)
|
||||
{
|
||||
if (CurrentTarget == newTarget) return;
|
||||
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
ApplyHighlight(CurrentTarget, false);
|
||||
OnTargetDeselected?.Invoke(CurrentTarget);
|
||||
}
|
||||
|
||||
CurrentTarget = newTarget;
|
||||
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
ApplyHighlight(CurrentTarget, true);
|
||||
OnTargetSelected?.Invoke(CurrentTarget);
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteRotationTowardsCurrentTarget(float deltaTime)
|
||||
{
|
||||
if (CurrentTarget == null || _playerTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 directionToTarget = CurrentTarget.transform.position - _playerTransform.position;
|
||||
directionToTarget.y = 0f;
|
||||
|
||||
if (directionToTarget.sqrMagnitude < 0.0001f) return;
|
||||
|
||||
directionToTarget.Normalize();
|
||||
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget);
|
||||
_playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, targetRotation, deltaTime * playerRotationSpeed);
|
||||
}
|
||||
|
||||
public float GetCurrentTargetHealth()
|
||||
{
|
||||
if (CurrentTarget != null && CurrentTarget.aiController != null)
|
||||
{
|
||||
return CurrentTarget.aiController.currentHealth;
|
||||
}
|
||||
return -1f;
|
||||
}
|
||||
|
||||
public void ClearTarget(bool findNewOneImmediately)
|
||||
{
|
||||
SetNewTarget(null);
|
||||
if (findNewOneImmediately && _gameStateManager != null && _gameStateManager.CurrentState == GameStateManager.State.COMBAT)
|
||||
{
|
||||
UpdateTarget();
|
||||
}
|
||||
}
|
||||
|
||||
// *** MODIFIED GetTargetRenderers METHOD ***
|
||||
private Renderer[] GetTargetRenderers(vFSMBehaviourController targetController)
|
||||
{
|
||||
if (targetController == null) return new Renderer[0];
|
||||
|
||||
List<Renderer> collectedRenderers = new List<Renderer>();
|
||||
|
||||
// Collect all SkinnedMeshRenderers
|
||||
SkinnedMeshRenderer[] smrs = targetController.GetComponentsInChildren<SkinnedMeshRenderer>(true);
|
||||
if (smrs != null && smrs.Length > 0)
|
||||
{
|
||||
collectedRenderers.AddRange(smrs);
|
||||
}
|
||||
|
||||
// Collect all MeshRenderers
|
||||
MeshRenderer[] mrs = targetController.GetComponentsInChildren<MeshRenderer>(true);
|
||||
if (mrs != null && mrs.Length > 0)
|
||||
{
|
||||
collectedRenderers.AddRange(mrs);
|
||||
}
|
||||
|
||||
// If no specific SMRs or MRs were found, fall back to all Renderers (optional, but good for safety)
|
||||
// Or, if the goal is *only* SMRs and MRs, this fallback can be removed.
|
||||
// For now, let's assume we want to highlight *something* if possible.
|
||||
// If you strictly want ONLY SMRs and MRs, and nothing else, remove this 'if' block.
|
||||
if (collectedRenderers.Count == 0)
|
||||
{
|
||||
Renderer[] allRenderers = targetController.GetComponentsInChildren<Renderer>(true);
|
||||
if (allRenderers != null && allRenderers.Length > 0)
|
||||
{
|
||||
collectedRenderers.AddRange(allRenderers);
|
||||
}
|
||||
}
|
||||
|
||||
// The preferSkinnedMeshRenderer flag is not directly used here for filtering types anymore.
|
||||
// It could potentially be used for ordering or other logic if needed in the future.
|
||||
|
||||
return collectedRenderers.Distinct().ToArray(); // Distinct() to avoid duplicates if an object somehow has multiple relevant renderer types
|
||||
}
|
||||
|
||||
|
||||
private void ApplyHighlight(vFSMBehaviourController targetController, bool fadeIn)
|
||||
{
|
||||
if (targetController == null || string.IsNullOrEmpty(materialHighlightPropertyName)) return;
|
||||
|
||||
Renderer[] renderers = GetTargetRenderers(targetController);
|
||||
|
||||
foreach (Renderer rend in renderers)
|
||||
{
|
||||
if (rend == null) continue;
|
||||
|
||||
foreach (Material mat in rend.materials)
|
||||
{
|
||||
if (mat == null || !mat.HasProperty(materialHighlightPropertyName)) continue;
|
||||
|
||||
if (_materialToFadeCoroutineMap.TryGetValue(mat, out Coroutine existingCoroutine) && existingCoroutine != null)
|
||||
if (_targetingLoopCoroutine == null)
|
||||
{
|
||||
StopCoroutine(existingCoroutine);
|
||||
_targetingLoopCoroutine = StartCoroutine(TargetingLoop());
|
||||
}
|
||||
|
||||
Color currentColor = mat.GetColor(materialHighlightPropertyName);
|
||||
Color targetColor = fadeIn ? highlightColor : deselectHighlightColor;
|
||||
|
||||
if (fadeIn)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_targetingLoopCoroutine != null)
|
||||
{
|
||||
if (!_originalMaterialColors.ContainsKey(mat) ||
|
||||
(_originalMaterialColors[mat] != currentColor && currentColor != deselectHighlightColor && currentColor != highlightColor) )
|
||||
StopCoroutine(_targetingLoopCoroutine);
|
||||
_targetingLoopCoroutine = null;
|
||||
}
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
SetNewTarget(null, true); // Force immediate update if leaving combat
|
||||
}
|
||||
_manualSwitchCooldownActive = false; // Reset cooldown when leaving combat
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator TargetingLoop()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (_manualSwitchCooldownActive)
|
||||
{
|
||||
_manualSwitchCooldownTimer -= targetingInterval; // Or Time.deltaTime if loop is faster
|
||||
if (_manualSwitchCooldownTimer <= 0)
|
||||
{
|
||||
_originalMaterialColors[mat] = currentColor;
|
||||
_manualSwitchCooldownActive = false;
|
||||
// Debug.Log("AutoTargetting: Manual switch cooldown ended.");
|
||||
}
|
||||
}
|
||||
Coroutine newFadeCoroutine = StartCoroutine(FadeMaterialPropertyCoroutine(mat, currentColor, targetColor, highlightFadeDuration));
|
||||
_materialToFadeCoroutineMap[mat] = newFadeCoroutine;
|
||||
|
||||
if (!_manualSwitchCooldownActive) // Only update target if cooldown is not active
|
||||
{
|
||||
if (_playerTransform == null && Player.Instance != null)
|
||||
{
|
||||
_playerTransform = Player.Instance.transform;
|
||||
}
|
||||
|
||||
if (_playerTransform != null)
|
||||
{
|
||||
UpdateTarget();
|
||||
}
|
||||
}
|
||||
yield return new WaitForSeconds(targetingInterval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FadeMaterialPropertyCoroutine(Material material, Color fromValue, Color toValue, float duration)
|
||||
{
|
||||
float timer = 0f;
|
||||
|
||||
while (timer < duration)
|
||||
public bool IsTargetInAngle(Transform sourceTransform, vFSMBehaviourController targetAI, float angleThreshold)
|
||||
{
|
||||
if (material == null) yield break;
|
||||
|
||||
timer += Time.deltaTime;
|
||||
float progress = Mathf.Clamp01(timer / duration);
|
||||
material.SetColor(materialHighlightPropertyName, Color.Lerp(fromValue, toValue, progress));
|
||||
yield return null;
|
||||
// ... (IsTargetInAngle logic - unchanged)
|
||||
if (targetAI == null || sourceTransform == null) return false;
|
||||
Vector3 directionToTarget = (targetAI.transform.position - sourceTransform.position);
|
||||
directionToTarget.y = 0;
|
||||
if (directionToTarget.sqrMagnitude < 0.0001f) return true;
|
||||
directionToTarget.Normalize();
|
||||
float angle = Vector3.Angle(sourceTransform.forward, directionToTarget);
|
||||
return angle <= angleThreshold;
|
||||
}
|
||||
|
||||
if (material != null)
|
||||
|
||||
private void UpdateTarget()
|
||||
{
|
||||
material.SetColor(materialHighlightPropertyName, toValue);
|
||||
if (_playerTransform == null || _gameStateManager == null) return;
|
||||
if (_manualSwitchCooldownActive) return; // Double check, though TargetingLoop should handle it
|
||||
|
||||
vFSMBehaviourController bestCandidate = null;
|
||||
float minDistanceSqr = maxTargetingDistance * maxTargetingDistance;
|
||||
HashSet<vFSMBehaviourController> combatControllers = _gameStateManager.GetActiveCombatcontrollers();
|
||||
|
||||
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)
|
||||
{
|
||||
minDistanceSqr = distSqr;
|
||||
bestCandidate = controller;
|
||||
}
|
||||
}
|
||||
|
||||
if (CurrentTarget != bestCandidate)
|
||||
{
|
||||
SetNewTarget(bestCandidate);
|
||||
}
|
||||
else if (CurrentTarget != null && !IsTargetValid(CurrentTarget))
|
||||
{
|
||||
SetNewTarget(null);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsTargetValid(vFSMBehaviourController target)
|
||||
{
|
||||
// ... (IsTargetValid logic - unchanged)
|
||||
if (target == null || !target.gameObject.activeInHierarchy || target.aiController.currentHealth <= 0) return false;
|
||||
if (_playerTransform == null) return false;
|
||||
if (!IsTargetInAngle(_playerTransform, target, targetingAngleThreshold)) return false;
|
||||
float distSqr = (target.transform.position - _playerTransform.position).sqrMagnitude;
|
||||
return distSqr <= (maxTargetingDistance * maxTargetingDistance);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new target for AutoTargetting.
|
||||
/// </summary>
|
||||
/// <param name="newTarget">The new target. Can be null.</param>
|
||||
/// <param name="forceLockSystemUpdate">If true, ManuallySetLockOnTarget will be called even if newTarget is same as CurrentTarget (used for re-assertion).</param>
|
||||
private void SetNewTarget(vFSMBehaviourController newTarget, bool forceLockSystemUpdate = false)
|
||||
{
|
||||
if (_manualSwitchCooldownActive && !forceLockSystemUpdate) // Don't change target if cooldown is active, unless forced
|
||||
{
|
||||
// If cooldown is active and this SetNewTarget call is NOT from a HandleLockOnSystem... event,
|
||||
// it means AutoTargeting's own loop tried to change target. We prevent this.
|
||||
// If it IS from HandleLockOnSystem... it means bLockOn initiated, and we need to sync.
|
||||
// The 'isManualSwitch' parameter in HandleLockOnSystemTargetChanged handles activating cooldown.
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (CurrentTarget == newTarget && !forceLockSystemUpdate)
|
||||
{
|
||||
// If auto-lock is enabled, ensure the bLockOn system is actually locked on this target.
|
||||
if (autoLockSelectedTarget && targetLockSystem != null && CurrentTarget != null)
|
||||
{
|
||||
if (targetLockSystem.GetCurrentLockOnTarget() != CurrentTarget.transform)
|
||||
{
|
||||
targetLockSystem.ManuallySetLockOnTarget(CurrentTarget.transform, true);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
ApplyHighlight(CurrentTarget, false);
|
||||
OnTargetDeselected?.Invoke(CurrentTarget);
|
||||
if (autoLockSelectedTarget && targetLockSystem != null &&
|
||||
targetLockSystem.GetCurrentLockOnTarget() == CurrentTarget.transform &&
|
||||
(newTarget == null || newTarget.transform != CurrentTarget.transform))
|
||||
{
|
||||
targetLockSystem.ManuallySetLockOnTarget(null, false);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentTarget = newTarget;
|
||||
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
ApplyHighlight(CurrentTarget, true);
|
||||
OnTargetSelected?.Invoke(CurrentTarget);
|
||||
if (autoLockSelectedTarget && targetLockSystem != null)
|
||||
{
|
||||
targetLockSystem.ManuallySetLockOnTarget(CurrentTarget.transform, true);
|
||||
}
|
||||
}
|
||||
else // CurrentTarget is now null
|
||||
{
|
||||
// Deselection logic above handles unlocking if autoLockSelectedTarget was true
|
||||
// and if bLockOn was on the CurrentTarget that just became null.
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleLockOnSystemTargetChanged(Transform lockedTargetTransform)
|
||||
{
|
||||
if (lockedTargetTransform == null || targetLockSystem == null) return;
|
||||
|
||||
// This event means bLockOn changed target (e.g., player pressed Next/Previous)
|
||||
// Activate the cooldown for AutoTargetting.
|
||||
_manualSwitchCooldownActive = true;
|
||||
_manualSwitchCooldownTimer = manualSwitchCooldownDuration;
|
||||
// Debug.Log($"AutoTargetting: Manual switch detected via bLockOn. Cooldown activated for {manualSwitchCooldownDuration}s.");
|
||||
|
||||
vFSMBehaviourController fsmTarget = lockedTargetTransform.GetComponentInParent<vFSMBehaviourController>();
|
||||
if (fsmTarget == null) fsmTarget = lockedTargetTransform.GetComponentInChildren<vFSMBehaviourController>(true);
|
||||
|
||||
|
||||
if (CurrentTarget != null && CurrentTarget.transform == lockedTargetTransform)
|
||||
{
|
||||
// AutoTargetting was already on this target, but bLockOn re-confirmed.
|
||||
// Cooldown is still active due to bLockOn's event.
|
||||
return;
|
||||
}
|
||||
|
||||
if (fsmTarget != null)
|
||||
{
|
||||
// Debug.Log($"AutoTargeting: Syncing to bLockOn's new target: {fsmTarget.name}.");
|
||||
// We need to update AutoTargetting's CurrentTarget to match bLockOn,
|
||||
// even during cooldown, because bLockOn IS the source of truth now.
|
||||
// The 'forceLockSystemUpdate = true' ensures highlights are updated correctly.
|
||||
SetNewTarget(fsmTarget, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// bLockOn locked something without an FSM, or unlocked.
|
||||
// If AutoTargeting had a target, it should now clear it to sync.
|
||||
// Debug.LogWarning($"AutoTargeting: bLockOn locked {lockedTargetTransform.name} (no FSM) or unlocked. Clearing AutoTarget.");
|
||||
SetNewTarget(null, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleLockOnSystemTargetUnlocked(Transform previouslyLockedTargetTransform)
|
||||
{
|
||||
if (targetLockSystem == null) return;
|
||||
// This event means bLockOn explicitly unlocked (e.g., player pressed Tab again).
|
||||
|
||||
// If player manually unlocks, we should respect that and also stop AutoTargetting's autoLock behavior for a bit.
|
||||
// Or, if autoLockSelectedTarget is false, this is just a notification.
|
||||
_manualSwitchCooldownActive = true; // Treat manual unlock like a manual switch
|
||||
_manualSwitchCooldownTimer = manualSwitchCooldownDuration;
|
||||
// Debug.Log($"AutoTargeting: Manual unlock detected via bLockOn. Cooldown activated.");
|
||||
|
||||
string targetName = previouslyLockedTargetTransform ? previouslyLockedTargetTransform.name : "an unknown target";
|
||||
// Debug.Log($"AutoTargeting: bLockOn unlocked from '{targetName}'.");
|
||||
|
||||
if (CurrentTarget != null && previouslyLockedTargetTransform == CurrentTarget.transform)
|
||||
{
|
||||
// AutoTargeting's current target was the one that bLockOn just unlocked.
|
||||
// We should clear AutoTargeting's target to be in sync.
|
||||
// The cooldown will prevent immediate re-auto-targeting.
|
||||
SetNewTarget(null, true);
|
||||
}
|
||||
// If previouslyLockedTargetTransform was something else, AutoTargeting might already be on null or a different target.
|
||||
// The cooldown ensures AutoTargeting doesn't fight this unlock.
|
||||
}
|
||||
|
||||
|
||||
// ... (ExecuteRotationTowardsCurrentTarget, GetCurrentTargetHealth, ClearTarget, Renderer methods, Fade coroutine remain the same)
|
||||
public void ExecuteRotationTowardsCurrentTarget(float deltaTime)
|
||||
{
|
||||
if (CurrentTarget == null || _playerTransform == null) return;
|
||||
Vector3 directionToTarget = CurrentTarget.transform.position - _playerTransform.position;
|
||||
directionToTarget.y = 0f;
|
||||
if (directionToTarget.sqrMagnitude < 0.0001f) return;
|
||||
directionToTarget.Normalize();
|
||||
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget);
|
||||
_playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, targetRotation, deltaTime * playerRotationSpeed);
|
||||
}
|
||||
|
||||
public float GetCurrentTargetHealth()
|
||||
{
|
||||
if (CurrentTarget != null && CurrentTarget.aiController != null)
|
||||
{
|
||||
return CurrentTarget.aiController.currentHealth;
|
||||
}
|
||||
return -1f;
|
||||
}
|
||||
|
||||
public void ClearTarget(bool findNewOneImmediately)
|
||||
{
|
||||
SetNewTarget(null, true); // Force update if clearing target
|
||||
if (findNewOneImmediately && _gameStateManager != null && _gameStateManager.CurrentState == GameStateManager.State.COMBAT)
|
||||
{
|
||||
// UpdateTarget will respect cooldown, so immediate find might be delayed
|
||||
// If you need it to bypass cooldown here, more logic is needed.
|
||||
// For now, let's assume standard behavior.
|
||||
if (!_manualSwitchCooldownActive) UpdateTarget();
|
||||
}
|
||||
}
|
||||
|
||||
private Renderer[] GetTargetRenderers(vFSMBehaviourController targetController)
|
||||
{
|
||||
if (targetController == null) return new Renderer[0];
|
||||
List<Renderer> collectedRenderers = new List<Renderer>();
|
||||
SkinnedMeshRenderer[] smrs = targetController.GetComponentsInChildren<SkinnedMeshRenderer>(true);
|
||||
if (smrs != null && smrs.Length > 0) collectedRenderers.AddRange(smrs);
|
||||
MeshRenderer[] mrs = targetController.GetComponentsInChildren<MeshRenderer>(true);
|
||||
if (mrs != null && mrs.Length > 0) collectedRenderers.AddRange(mrs);
|
||||
if (collectedRenderers.Count == 0)
|
||||
{
|
||||
Renderer[] allRenderers = targetController.GetComponentsInChildren<Renderer>(true);
|
||||
if (allRenderers != null && allRenderers.Length > 0) collectedRenderers.AddRange(allRenderers);
|
||||
}
|
||||
return collectedRenderers.Distinct().ToArray();
|
||||
}
|
||||
|
||||
|
||||
private void ApplyHighlight(vFSMBehaviourController targetController, bool fadeIn)
|
||||
{
|
||||
if (targetController == null || string.IsNullOrEmpty(materialHighlightPropertyName)) return;
|
||||
Renderer[] renderers = GetTargetRenderers(targetController);
|
||||
foreach (Renderer rend in renderers)
|
||||
{
|
||||
if (rend == null) continue;
|
||||
foreach (Material mat in rend.materials)
|
||||
{
|
||||
if (mat == null || !mat.HasProperty(materialHighlightPropertyName)) continue;
|
||||
if (_materialToFadeCoroutineMap.TryGetValue(mat, out Coroutine existingCoroutine) && existingCoroutine != null)
|
||||
{
|
||||
StopCoroutine(existingCoroutine);
|
||||
}
|
||||
Color currentColor = mat.GetColor(materialHighlightPropertyName);
|
||||
Color targetColor = fadeIn ? highlightColor : deselectHighlightColor;
|
||||
if (fadeIn)
|
||||
{
|
||||
if (!_originalMaterialColors.ContainsKey(mat) ||
|
||||
(_originalMaterialColors[mat] != currentColor && currentColor != deselectHighlightColor && currentColor != highlightColor))
|
||||
{
|
||||
_originalMaterialColors[mat] = currentColor;
|
||||
}
|
||||
}
|
||||
Coroutine newFadeCoroutine = StartCoroutine(FadeMaterialPropertyCoroutine(mat, currentColor, targetColor, highlightFadeDuration));
|
||||
_materialToFadeCoroutineMap[mat] = newFadeCoroutine;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FadeMaterialPropertyCoroutine(Material material, Color fromValue, Color toValue, float duration)
|
||||
{
|
||||
float timer = 0f;
|
||||
while (timer < duration)
|
||||
{
|
||||
if (material == null) yield break;
|
||||
timer += Time.deltaTime;
|
||||
float progress = Mathf.Clamp01(timer / duration);
|
||||
material.SetColor(materialHighlightPropertyName, Color.Lerp(fromValue, toValue, progress));
|
||||
yield return null;
|
||||
}
|
||||
if (material != null)
|
||||
{
|
||||
material.SetColor(materialHighlightPropertyName, toValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user