RiftMayhem/Assets/Scripts/AbilitySystem/Base/CastingStateController.cs
2025-02-21 18:35:51 +00:00

122 lines
3.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Events;
public class CastingStateController : MonoBehaviour
{
public bool isCasting;
public PlayerMovement playerMovement;
public LayerMask movementMask;
private MovementSpeedModifierEffectInstance movementSpeedModifier;
Camera cam;
Ray ray;
RaycastHit hit;
ProjectileSpawnLocationController projectileSpawnLocationController;
public UnityEvent<BaseAbility> OnAbilityQueued = new UnityEvent<BaseAbility>();
private void Awake()
{
cam = Camera.main;
movementSpeedModifier = playerMovement.GetComponent<MovementSpeedModifierEffectInstance>();
projectileSpawnLocationController = transform.root.GetComponentInChildren<ProjectileSpawnLocationController>();
}
private void Start()
{
}
public void RequestAbilityCast(BaseAbility ability, Action abilityExecution)
{
if (isCasting) return;
if (!ability.castableWhileMoving)
{
movementSpeedModifier.ToggleCastPenalty(true);
//playerMovement.ToggleAgentMoving(true);
}
OnAbilityQueued.Invoke(ability);
StartCoroutine(Casting(ability, abilityExecution));
}
public void RequestAbilityChannel(BaseAbility channeledAbility, Action abilityChannelExecution)
{
if (isCasting) return;
if (!channeledAbility.castableWhileMoving)
{
movementSpeedModifier.ToggleCastPenalty(true);
//playerMovement.ToggleAgentMoving(true);
}
OnAbilityQueued.Invoke(channeledAbility);
StartCoroutine(Channeling(channeledAbility, abilityChannelExecution));
}
private IEnumerator Casting(BaseAbility ability, Action abilityExecution)
{
isCasting = true;
CastBarHandler.Instance.ShowCastBar(ability, abilityExecution);
playerMovement.InstantFaceCast(projectileSpawnLocationController.GetLookat());
//cast animation
yield return new WaitForSeconds(ability.castTime);
if (!GameConstants.Animation.IsAnimationEventBasedAbility(ability.animationType))
{
abilityExecution.Invoke();
}
isCasting = false;
if (!ability.castableWhileMoving)
{
movementSpeedModifier.ToggleCastPenalty(false);
//playerMovement.ToggleAgentMoving(false);
}
//Debug.Log("$$sCastbar Done casting");
}
private IEnumerator Channeling(BaseAbility channeledAbility, Action abilityChannelExecution)
{
isCasting = true;
CastBarHandler.Instance.ShowChannelingBar(channeledAbility, abilityChannelExecution);
playerMovement.InstantFaceCast(projectileSpawnLocationController.GetLookat());
abilityChannelExecution.Invoke();
if (!channeledAbility.castableWhileMoving)
{
movementSpeedModifier.ToggleCastPenalty(true);
//playerMovement.ToggleAgentMoving(true);
}
yield return null;
}
public void ResetChannelingCast()
{
isCasting = false;
movementSpeedModifier.ToggleCastPenalty(false);
//playerMovement.ToggleAgentMoving(false);
}
public void ResetCastingOnMeleeAnimationEvent()
{
isCasting = false;
movementSpeedModifier.ToggleCastPenalty(false);
//playerMovement.ToggleAgentMoving(false);
StopAllCoroutines();
//Debug.Log("$$ResetCastingState On Event");
}
}