new autotargettinf script, dash

This commit is contained in:
2025-05-14 18:23:34 +02:00
parent 6a9c064b8a
commit 86d35d8eaa
12 changed files with 1862 additions and 1143 deletions

View File

@@ -0,0 +1,377 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
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
public class AutoTargetting : MonoBehaviour
{
[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.")]
public float playerRotationSpeed = 10f;
[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, will try to find a SkinnedMeshRenderer first, then MeshRenderer. If false, affects all renderers.")]
public bool preferSkinnedMeshRenderer = true;
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()
{
if (Player.Instance == null) // Player.Instance should be set by its own Awake
{
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;
_gameStateManager = GameStateManager.Instance;
if (_gameStateManager != null)
{
_gameStateManager.m_OnStateChanged.AddListener(HandleGameStateChanged);
HandleGameStateChanged(_gameStateManager.CurrentState); // Initialize based on current state
}
else
{
Debug.LogError("AutoTargetting: GameStateManager.Instance not found! Disabling script.");
enabled = false;
}
}
void OnDestroy()
{
if (_gameStateManager != null)
{
_gameStateManager.m_OnStateChanged.RemoveListener(HandleGameStateChanged);
}
StopAndClearAllFadeCoroutines();
if (_targetingLoopCoroutine != null)
{
StopCoroutine(_targetingLoopCoroutine);
_targetingLoopCoroutine = null;
}
}
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)
{
if (_targetingLoopCoroutine == null)
{
_targetingLoopCoroutine = StartCoroutine(TargetingLoop());
}
}
else // State is NORMAL or other non-combat
{
if (_targetingLoopCoroutine != null)
{
StopCoroutine(_targetingLoopCoroutine);
_targetingLoopCoroutine = null;
}
if (CurrentTarget != null) // If there was a target, deselect it
{
vFSMBehaviourController oldTarget = CurrentTarget;
SetNewTarget(null); // This will handle fade out and event
}
}
}
private IEnumerator TargetingLoop()
{
while (true)
{
if (_playerTransform == null && Player.Instance != null) // Defensive: re-cache if player was respawned or similar
{
_playerTransform = Player.Instance.transform;
}
if (_playerTransform != null) // Only proceed if we have a valid player transform
{
UpdateTarget();
}
yield return new WaitForSeconds(targetingInterval);
}
}
/// <summary>
/// Checks if the given AI target is within the specified angle from the source transform's forward direction.
/// </summary>
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; // Consider only horizontal angle
// If target is effectively at the same horizontal position, consider it in angle.
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); // No enemies, clear current
return;
}
foreach (var controller in combatControllers)
{
if (controller == null || !controller.gameObject.activeInHierarchy || controller.aiController.currentHealth <= 0)
{
continue;
}
// Check 1: Is target within selection angle?
if (!IsTargetInAngle(_playerTransform, controller, targetingAngleThreshold))
{
continue;
}
// Check 2: Is target within selection distance?
float distSqr = (controller.transform.position - _playerTransform.position).sqrMagnitude;
if (distSqr <= minDistanceSqr) // distSqr must also be <= maxTargetingDistance^2 due to minDistanceSqr initialization
{
// If multiple targets are at similar very close distances, this might pick the "last one" processed.
// For more refined "closest", ensure this is strictly '<' for new candidates,
// or add a secondary sort criterion if multiple are at exact same minDistanceSqr.
// Current logic is fine for most cases.
minDistanceSqr = distSqr;
bestCandidate = controller;
}
}
if (CurrentTarget != bestCandidate)
{
SetNewTarget(bestCandidate);
}
else if (CurrentTarget != null && !IsTargetValid(CurrentTarget)) // Current target became invalid (e.g. died, moved out of range/angle)
{
SetNewTarget(null);
}
}
/// <summary>
/// Checks if a given target is still valid according to AutoTargetting's rules.
/// </summary>
private bool IsTargetValid(vFSMBehaviourController target)
{
if (target == null || !target.gameObject.activeInHierarchy || target.aiController.currentHealth <= 0)
return false;
if (_playerTransform == null) return false; // Should not happen if script is active
// Check 1: Angle (using AutoTargetting's own targetingAngleThreshold)
if (!IsTargetInAngle(_playerTransform, target, targetingAngleThreshold))
return false;
// Check 2: Distance (using AutoTargetting's own maxTargetingDistance)
float distSqr = (target.transform.position - _playerTransform.position).sqrMagnitude;
return distSqr <= maxTargetingDistance * maxTargetingDistance;
}
private void SetNewTarget(vFSMBehaviourController newTarget)
{
if (CurrentTarget == newTarget) return;
// Deselect previous target
if (CurrentTarget != null)
{
ApplyHighlight(CurrentTarget, false); // Fade out
OnTargetDeselected?.Invoke(CurrentTarget);
}
CurrentTarget = newTarget;
// Select new target
if (CurrentTarget != null)
{
ApplyHighlight(CurrentTarget, true); // Fade in
OnTargetSelected?.Invoke(CurrentTarget);
}
}
/// <summary>
/// Smoothly rotates the player character towards the CurrentTarget on the horizontal plane.
/// </summary>
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; // Already at target or too close to get a direction
directionToTarget.Normalize();
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget);
_playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, targetRotation, deltaTime * playerRotationSpeed);
}
/// <summary>
/// Gets the health of the CurrentTarget.
/// </summary>
/// <returns>The health of the CurrentTarget, or -1f if no target or target has no health component.</returns>
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(); // Attempt to find a new one if in combat
}
}
private Renderer[] GetTargetRenderers(vFSMBehaviourController targetController)
{
if (targetController == null) return new Renderer[0];
if (preferSkinnedMeshRenderer)
{
SkinnedMeshRenderer smr = targetController.GetComponentInChildren<SkinnedMeshRenderer>();
if (smr != null) return new Renderer[] { smr };
MeshRenderer mr = targetController.GetComponentInChildren<MeshRenderer>();
if (mr != null) return new Renderer[] { mr };
}
return targetController.GetComponentsInChildren<Renderer>(true); // Include inactive renderers if any
}
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;
// Use rend.materials to get instances for modification
foreach (Material mat in rend.materials)
{
if (mat == null || !mat.HasProperty(materialHighlightPropertyName)) continue;
// Stop any existing fade coroutine for this specific material
if (_materialToFadeCoroutineMap.TryGetValue(mat, out Coroutine existingCoroutine) && existingCoroutine != null)
{
StopCoroutine(existingCoroutine);
}
Color currentColor = mat.GetColor(materialHighlightPropertyName);
Color targetColor = fadeIn ? highlightColor : deselectHighlightColor;
// Smartly store original color only if not already a highlight/deselect color.
if (fadeIn)
{
if (!_originalMaterialColors.ContainsKey(mat) ||
(_originalMaterialColors[mat] != currentColor && currentColor != deselectHighlightColor && currentColor != highlightColor) )
{
_originalMaterialColors[mat] = currentColor;
}
}
// When fading out, if an original was stored, we could potentially use it instead of always deselectHighlightColor.
// However, for a consistent "off" state, deselectHighlightColor (e.g., black) is usually desired.
// If fading out and original exists and isn't black:
// else if (_originalMaterialColors.TryGetValue(mat, out Color original) && original != deselectHighlightColor)
// {
// targetColor = original; // Fade back to true original
// }
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;
// yield return null; // Not strictly necessary here as StopCoroutine handles overlaps.
while (timer < duration)
{
if (material == null) yield break; // Material might have been destroyed
timer += Time.deltaTime;
float progress = Mathf.Clamp01(timer / duration);
material.SetColor(materialHighlightPropertyName, Color.Lerp(fromValue, toValue, progress));
yield return null;
}
if (material != null) // Final set
{
material.SetColor(materialHighlightPropertyName, toValue);
}
// Optional: Remove from map if coroutine completed naturally and is still the one in the map.
// if (_materialToFadeCoroutineMap.TryGetValue(material, out Coroutine currentMappedCoroutine) && currentMappedCoroutine == /*this specific instance, tricky to get*/)
// {
// _materialToFadeCoroutineMap.Remove(material);
// }
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b8fd6dab0eb7c40d59fd6631516b3b51

View File

@@ -159,6 +159,9 @@ namespace Beyond
public float CurrentHealth => m_vController.currentHealth;
public float MaxHealth => m_vController.MaxHealth;
public AutoTargetting AutoTarget { get; internal set; }
private AutoTargetting m_autoTargetting;
private void Awake()
{
if (s_instance == null)
@@ -182,6 +185,7 @@ namespace Beyond
m_playerConfessionController = GetComponent<PlayerConfessionController>();
m_meleeManager = GetComponent<vMeleeManager>();
m_magicAttacks = GetComponent<MagicAttacks>();
m_autoTargetting = GetComponent<AutoTargetting>();
}

View File

@@ -260,7 +260,7 @@ namespace Beyond
switch (state)
{
case State.COMBAT:
SetRollInputOnJump();
//SetRollInputOnJump();
if (m_autoDrawWeapon && m_weaponDraw != null)
{
@@ -269,7 +269,7 @@ namespace Beyond
break;
case State.NORMAL:
SetDefaultInputsForRollJump();
//SetDefaultInputsForRollJump();
if (m_autoDrawWeapon && m_weaponDraw != null)
{

View File

@@ -17,6 +17,8 @@ namespace Beyond
public GameObject ComsumableButton;
public GameObject ComsumableFaithButton;
public GameObject powerButtonsParent;
public Button DashButton;
public Button RunButton;
public Button TargetButton;
public Button JumpButton;
@@ -58,54 +60,55 @@ namespace Beyond
private List<TriggerDescriptor> m_Triggers = new List<TriggerDescriptor>();
private GameStateManager.State m_prevGameState = GameStateManager.State.NORMAL;
/*
private List<TriggerObject> m_triggers = new List<TriggerObject>();
public enum TriggerType
{
Dialogue,
Ladder,
Generic,
Collectable,
COUNT
};
private List<TriggerObject> m_triggers = new List<TriggerObject>();
public enum TriggerType
{
Dialogue,
Ladder,
Generic,
Collectable,
COUNT
};
public struct TriggerObject : IComparable<TriggerObject>, IComparable
{
public GameObject obj;
public TriggerType type;
public struct TriggerObject : IComparable<TriggerObject>, IComparable
{
public GameObject obj;
public TriggerType type;
public int CompareTo(TriggerObject other)
{
return type.CompareTo(other.type);
}
public int CompareTo(TriggerObject other)
{
return type.CompareTo(other.type);
}
public int CompareTo(object obj)
{
if (ReferenceEquals(null, obj)) return 1;
return obj is TriggerObject other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TriggerObject)}");
}
public int CompareTo(object obj)
{
if (ReferenceEquals(null, obj)) return 1;
return obj is TriggerObject other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TriggerObject)}");
}
public static bool operator <(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) < 0;
}
public static bool operator <(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) < 0;
}
public static bool operator >(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) > 0;
}
public static bool operator >(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) > 0;
}
public static bool operator <=(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) <= 0;
}
public static bool operator <=(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) <= 0;
}
public static bool operator >=(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) >= 0;
}
}
*/
public static bool operator >=(TriggerObject left, TriggerObject right)
{
return left.CompareTo(right) >= 0;
}
}
*/
private void Initialize()
{
@@ -174,10 +177,12 @@ namespace Beyond
// JumpButton.interactable = !activate;
}
private void Awake()
private void Start()
{
ActionTriggerEnter += OnInvokeActionTriggerEnter;
ActionTriggerExit += OnInvokeActionTriggerExit;
GameStateManager.Instance.m_OnStateChanged.AddListener(OnGameStateChanged);
/*
LadderTriggerEnter += OnInvokeLadderTriggerEnter;
LadderTriggerExit += OnInvokeLadderTriggerExit;
@@ -194,6 +199,8 @@ namespace Beyond
{
ActionTriggerEnter -= OnInvokeActionTriggerEnter;
ActionTriggerExit -= OnInvokeActionTriggerExit;
if (GameStateManager.Instance)
GameStateManager.Instance.m_OnStateChanged.RemoveListener(OnGameStateChanged);
/*
LadderTriggerEnter -= OnInvokeLadderTriggerEnter;
LadderTriggerExit -= OnInvokeLadderTriggerExit;
@@ -204,6 +211,18 @@ namespace Beyond
*/
}
void OnGameStateChanged(GameStateManager.State state)
{
if (state == GameStateManager.State.COMBAT)
{
DashButton.gameObject.SetActive(true);
}
else if (state == GameStateManager.State.NORMAL)
{
DashButton.gameObject.SetActive(false);
}
}
private void DebugLog(string text)
{
if (debugPrint)

View File

@@ -233,13 +233,13 @@ namespace Beyond
{
return !isAttacking && base.JumpConditions();
}
/*
protected override bool RollConditions()
{
return base.RollConditions() && !isAttacking && !cc.animator.IsInTransition(cc.upperBodyLayer) &&
!cc.animator.IsInTransition(cc.fullbodyLayer);
}
*/
#endregion Conditions
#region Update Animations
@@ -355,20 +355,20 @@ namespace Beyond
protected override void RollInput()
{
if (rollInput.GetButtonDown())
if (rollInput.GetButtonDown() && RollConditions())
{
if (horizontalInput.GetAxis() != 0f || verticallInput.GetAxis() != 0f)
cc.Roll();
/*
if (horizontalInput.GetAxis() != 0f && verticallInput.GetAxis() != 0f)
{
if (RollConditions())
{
cc.Roll();
}
cc.Roll();
}
else
{
bThirdPersonController beyondController = (bThirdPersonController)cc;
beyondController.Dash();
}
*/
}
}

View File

@@ -70,8 +70,8 @@ namespace Beyond
public virtual void Dash()
{
// OnRoll.Invoke();
// isRolling = true;
OnRoll.Invoke();
isRolling = true;
animator.CrossFadeInFixedTime("Dash", rollTransition, baseLayer);
ReduceStamina(rollStamina, false);