RiftMayhem/Assets/Scripts/CharacterAnimatorController.cs
Pedro Gomes 5ce9c0b3a7 Bugfixes + Weapon Visual handler
- bugfix build manager on starter builds not correctly assigning keybinders with defaults
- added 2h ranged shoot animation
- added shoot animation type for abilities
- new weapon visual handler swaps visual weapons based on ability queues
2025-01-13 20:48:15 +00:00

229 lines
6.4 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Events;
public class CharacterAnimatorController : MonoBehaviour
{
Animator anim;
NavMeshAgent agent;
PhotonView photonView;
CharacterAnimatorParent parentController;
SimpleRotationFX characterSpin;
bool isCasting;
bool wasSpinning = false;
public UnityEvent onFishingStarted = new UnityEvent();
public UnityEvent onFishingEnding = new UnityEvent();
public UnityEvent onFishingEnded = new UnityEvent();
private void Awake()
{
anim = GetComponent<Animator>();
agent = GetComponentInParent<NavMeshAgent>();
photonView = GetComponentInParent<PhotonView>();
parentController = GetComponentInParent<CharacterAnimatorParent>();
characterSpin = photonView.GetComponentInChildren<SimpleRotationFX>();
}
// Start is called before the first frame update
void Start()
{
if (!photonView.IsMine)
{
return;
}
}
private void OnEnable()
{
parentController = GetComponentInParent<CharacterAnimatorParent>();
//Debug.Log("OnEnable: parentController is " + (parentController != null ? "not null" : "null"));
if (photonView.IsMine)
{
CastBarHandler.Instance.OnCastingStateChanged.AddListener(UpdateIsCastingState);
//Debug.Log("Adding Listener for character: " + this.gameObject.name);
}
}
private void OnDisable()
{
if (photonView.IsMine)
CastBarHandler.Instance.OnCastingStateChanged.RemoveListener(UpdateIsCastingState);
}
// Update is called once per frame
void Update()
{
if (!photonView.IsMine)
{
if (characterSpin != null)
characterSpin.enabled = anim.GetBool("spinning");
return;
}
anim.SetFloat("movementSpeed", agent.velocity.magnitude);
}
private void UpdateIsCastingState(bool isCasting)
{
this.isCasting = isCasting;
if (isCasting)
{
anim.SetFloat("throwingTime", (1 / CastBarHandler.Instance.currentCastTime));
SetTriggerBasedOnAbility(CastBarHandler.Instance.currentAbility.animationType, CastBarHandler.Instance.currentAbility.animationType == AbilityAnimationType.Spin || CastBarHandler.Instance.currentAbility.animationType == AbilityAnimationType.Casting);
if (parentController == null)
{
parentController = GetComponentInParent<CharacterAnimatorParent>();
//Debug.Log("OnEnable: parentController is " + (parentController != null ? "not null" : "null"));
}
//Debug.Log("NULL: " + parentController);
parentController.SetRemoteTriggerBasedOnAbility((int)CastBarHandler.Instance.currentAbility.animationType);
}
else if (wasSpinning)
{
SetTriggerBasedOnAbility(AbilityAnimationType.Spin, false);
SetTriggerBasedOnAbility(AbilityAnimationType.Casting, false);
}
}
public void OnDeath()
{
SetDeadTrigger();
parentController.SetRemoteDead();
}
public void OnRevived()
{
SetRevivedTrigger();
parentController.SetRemoteRevived();
}
public void OnFish()
{
SetFishTrigger();
parentController.SetRemoteFish();
}
public void OnPickup()
{
SetPickupTrigger();
parentController.SetRemotePickup();
}
public void SetDeadTrigger()
{
anim.SetTrigger("dead");
}
public void SetRevivedTrigger()
{
anim.SetTrigger("revived");
}
public void SetFishTrigger()
{
anim.SetTrigger("fish");
onFishingStarted.Invoke();
}
public void SetPickupTrigger()
{
anim.SetTrigger("pickup");
}
public void OnFishingEnding()
{
onFishingEnding.Invoke();
if (!photonView.IsMine) return;
}
public void OnFishingEnded()
{
onFishingEnded.Invoke();
if (!photonView.IsMine) return;
}
public void OnAnimationEvent()
{
if (!photonView.IsMine) return;
//Debug.Log("$$melee Animation event");
//if (!isCasting) return;
//Debug.Log("$$melee Animation event casting true");
if (CastBarHandler.Instance.currentAbility.animationType == AbilityAnimationType.Melee)
{
CastBarHandler.Instance.ExecuteQueuedCastOnAnimationEvent();
parentController.castingStateController.ResetCastingOnMeleeAnimationEvent();
}
else if(CastBarHandler.Instance.currentAbility.animationType == AbilityAnimationType.Shoot)
{
CastBarHandler.Instance.ExecuteQueuedCastOnAnimationEvent();
parentController.castingStateController.ResetCastingOnMeleeAnimationEvent();
}
}
public void SetTriggerBasedOnAbility(AbilityAnimationType animationType, bool spinning = false)
{
switch (animationType)
{
case AbilityAnimationType.Throw:
anim.SetTrigger("throw");
break;
case AbilityAnimationType.Spell:
anim.SetTrigger("cast");
break;
case AbilityAnimationType.Melee:
anim.SetTrigger("melee");
break;
case AbilityAnimationType.Spin:
wasSpinning = spinning;
anim.SetBool("spinning", spinning);
if (characterSpin != null)
characterSpin.enabled = spinning;
break;
case AbilityAnimationType.Summon:
anim.SetTrigger("summon");
break;
case AbilityAnimationType.Jump:
anim.SetTrigger("jump");
break;
case AbilityAnimationType.Potion:
anim.SetTrigger("potion");
break;
case AbilityAnimationType.Casting:
wasSpinning = spinning;
anim.SetBool("spellcasting", spinning);
break;
case AbilityAnimationType.Shoot:
anim.SetTrigger("shoot");
break;
default:
break;
}
}
}
public enum AbilityAnimationType
{
Throw,
Spell,
Melee,
Spin,
Summon,
Jump,
Casting,
Potion,
Shoot
}