WXMC/.svn/pristine/71/710b2fee31b79a81da45adbe55b60416c9c95030.svn-base

142 lines
3.0 KiB
Plaintext
Raw Permalink Normal View History

2024-12-04 16:18:46 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
public class PHomePetAnimation : MonoBehaviour
{
[SerializeField]
private PHomePet m_pet;
[SerializeField]
private SkeletonAnimation m_animation;
[SerializeField]
[SpineAnimation]
private string m_runningAnimation = "Running";
[SerializeField]
[SpineAnimation]
private string m_idleAnimation = "Idle 1";
[SerializeField]
[SpineAnimation]
private string m_attackAnimation = "";
[SerializeField]
private float m_attackAnimTime;
private float m_attackAnimTimer;
private List<PHomeWeapon> m_registeredWeapons;
private float m_defaultXScale;
private bool Moving
{
get
{
return m_pet.CurrentVelocity.sqrMagnitude >= 0.01f;
}
}
private bool Attacking
{
get
{
return m_attackAnimTimer > 0.0f;
}
}
private void Update_Animation()
{
if (Attacking)
{
//m_animation.AnimationName = m_attackAnimation;
}
else
{
string animName = Moving ? m_runningAnimation : m_idleAnimation;
m_animation.AnimationName = animName;
}
}
private void Update_Attack()
{
if (Attacking)
{
m_attackAnimTimer -= Time.deltaTime;
}
}
private void Update_Rotation()
{
if (!Moving)
{
return;
}
// Moving
Vector3 scale = transform.localScale;
if (m_pet.CurrentVelocity.x < 0.0f)
{
scale.x = m_defaultXScale * -1.0f;
}
else
{
scale.x = m_defaultXScale;
}
transform.localScale = scale;
}
private void Awake()
{
m_defaultXScale = transform.localScale.x;
}
private void Start()
{
m_pet.OnAfterWeaponsChanged += OnAfterWeaponsChanged;
OnAfterWeaponsChanged();
}
private void OnDestroy()
{
m_pet.OnAfterWeaponsChanged -= OnAfterWeaponsChanged;
}
private void OnAfterWeaponsChanged()
{
if (m_registeredWeapons != null)
{
foreach (var item in m_registeredWeapons)
{
item.OnWeaponStartFiring.RemoveListener(OnAfterWeaponFired);
}
}
m_registeredWeapons = new List<PHomeWeapon>(m_pet.Weapons);
foreach (var item in m_registeredWeapons)
{
item.OnWeaponStartFiring.AddListener(OnAfterWeaponFired);
}
}
private void OnAfterWeaponFired()
{
m_attackAnimTimer = m_attackAnimTime;
if (m_attackAnimTimer > 0)
{
m_animation.state.SetAnimation(0, m_attackAnimation, false);
//m_animation.AnimationName = m_attackAnimation;
}
//print("Fired");
}
private void Update()
{
Update_Attack();
Update_Rotation();
Update_Animation();
}
}