123
This commit is contained in:
1 parent
2241642a97
commit
4216d27310
132 files changed
+3575
-137
No files matched your search
+102
-2
@@ -290,6 +290,53 @@ public class UnitBase : MonoBehaviour
|
||||
return 0f;
|
||||
}
|
||||
|
||||
// 查找当前 Animator 中使用的技能动画时长。
|
||||
private float GetSkillAnimationDuration()
|
||||
{
|
||||
if (animator == null || animator.runtimeAnimatorController == null)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
var overrideController = animator.runtimeAnimatorController as AnimatorOverrideController;
|
||||
if (overrideController != null)
|
||||
{
|
||||
var overrides = new List<KeyValuePair<AnimationClip, AnimationClip>>();
|
||||
overrideController.GetOverrides(overrides);
|
||||
|
||||
foreach (var pair in overrides)
|
||||
{
|
||||
if (IsSkillAnimation(pair.Key))
|
||||
{
|
||||
return pair.Value != null ? pair.Value.length : pair.Key.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var clip in animator.runtimeAnimatorController.animationClips)
|
||||
{
|
||||
if (IsSkillAnimation(clip))
|
||||
{
|
||||
return clip.length;
|
||||
}
|
||||
}
|
||||
|
||||
return 0f;
|
||||
}
|
||||
|
||||
// 根据动画名称判断是否为技能动画。
|
||||
private bool IsSkillAnimation(AnimationClip clip)
|
||||
{
|
||||
if (clip == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string clipName = clip.name.ToLowerInvariant();
|
||||
return clipName.Contains("skill")
|
||||
|| clipName.Contains("技能");
|
||||
}
|
||||
|
||||
//根据动画名称判断是否为攻击动画
|
||||
private bool IsAttackAnimation(AnimationClip clip)
|
||||
{
|
||||
@@ -322,6 +369,8 @@ public class UnitBase : MonoBehaviour
|
||||
{
|
||||
target = friend;
|
||||
|
||||
FaceTarget(friend);
|
||||
|
||||
//根据目标攻击时长加速或减速完整攻击动画
|
||||
SetAttackAnimationSpeed();
|
||||
|
||||
@@ -331,6 +380,24 @@ public class UnitBase : MonoBehaviour
|
||||
attackStartTime = Time.time;
|
||||
}
|
||||
|
||||
private void FaceTarget(GameObject opponent)
|
||||
{
|
||||
if (opponent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 direction = opponent.transform.position - transform.position;
|
||||
direction.y = 0f;
|
||||
if (direction.sqrMagnitude <= 0.0001f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
|
||||
transform.rotation = Quaternion.Euler(0f, targetRotation.eulerAngles.y, 0f);
|
||||
}
|
||||
|
||||
//由攻击动画中最合适的帧通过 Animation Event 调用。
|
||||
public void OnAttackEffect()
|
||||
{
|
||||
@@ -365,6 +432,26 @@ public class UnitBase : MonoBehaviour
|
||||
RegisterAttackHit();
|
||||
}
|
||||
|
||||
public void ApplySkillHit(float damageMultiplier, bool registerEnergy)
|
||||
{
|
||||
if (target == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
WorldHealthBar healthBar = target.GetComponentInParent<WorldHealthBar>();
|
||||
if (healthBar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
healthBar.TakeDamage(attackPower * Mathf.Max(0f, damageMultiplier));
|
||||
if (registerEnergy)
|
||||
{
|
||||
RegisterAttackHit();
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterAttackHit()
|
||||
{
|
||||
AddAttackEnergy();
|
||||
@@ -432,7 +519,18 @@ public class UnitBase : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public void PlaySkillAnimation(System.Action onFinished)
|
||||
private float GetSkillAnimationSpeed(float targetDuration)
|
||||
{
|
||||
float animationDuration = GetSkillAnimationDuration();
|
||||
if (animationDuration <= 0f || targetDuration <= 0f)
|
||||
{
|
||||
return 1f;
|
||||
}
|
||||
|
||||
return animationDuration / targetDuration;
|
||||
}
|
||||
|
||||
public void PlaySkillAnimation(System.Action onFinished, float targetDuration = 0f)
|
||||
{
|
||||
if (isPlayingSkill)
|
||||
{
|
||||
@@ -442,7 +540,7 @@ public class UnitBase : MonoBehaviour
|
||||
skillFinishedCallback = onFinished;
|
||||
isPlayingSkill = true;
|
||||
state = State.Skill;
|
||||
animator.speed = 1f;
|
||||
animator.speed = GetSkillAnimationSpeed(targetDuration);
|
||||
animator.SetInteger("speed", 0);
|
||||
animator.SetTrigger("skill");
|
||||
attackStartTime = Time.time;
|
||||
@@ -520,6 +618,8 @@ public class UnitBase : MonoBehaviour
|
||||
target = FindNearestOpponent();
|
||||
if (target != null && IsWithinAttackReach(target))
|
||||
{
|
||||
// 攻击结束后可能已切换到新的近距离目标,先更新朝向再开始下一次攻击。
|
||||
FaceTarget(target);
|
||||
attack(target);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user