74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class ArcherSkill : UnitSkill
|
|
{
|
|
[Min(1)]
|
|
public int volleyArrowCount = 3;
|
|
|
|
[Min(0f)]
|
|
public float volleyArrowDamageMultiplier = 0.7f;
|
|
|
|
[Min(0f)]
|
|
public float volleyArrowInterval = 0.15f;
|
|
|
|
private Coroutine volleyCoroutine;
|
|
|
|
public override void ReleaseSkill()
|
|
{
|
|
if (volleyCoroutine != null)
|
|
{
|
|
StopCoroutine(volleyCoroutine);
|
|
}
|
|
|
|
volleyCoroutine = StartCoroutine(FireVolley());
|
|
}
|
|
|
|
private IEnumerator FireVolley()
|
|
{
|
|
for (int i = 0; i < volleyArrowCount; i++)
|
|
{
|
|
// 三连射优先攻击当前目标;目标阵亡后剩余箭矢改打最近敌人。
|
|
GameObject volleyTarget = unit.Target != null ? unit.Target : FindNearestOpponent();
|
|
if (volleyTarget == null)
|
|
{
|
|
break;
|
|
}
|
|
|
|
unit.FireSkillProjectile(volleyTarget, volleyArrowDamageMultiplier);
|
|
yield return new WaitForSeconds(volleyArrowInterval);
|
|
}
|
|
|
|
volleyCoroutine = null;
|
|
}
|
|
|
|
private GameObject FindNearestOpponent()
|
|
{
|
|
string opponentTag = unit.OpponentTag;
|
|
if (string.IsNullOrEmpty(opponentTag))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
GameObject nearestOpponent = null;
|
|
float nearestSqrDistance = float.PositiveInfinity;
|
|
GameObject[] opponents = GameObject.FindGameObjectsWithTag(opponentTag);
|
|
foreach (GameObject opponent in opponents)
|
|
{
|
|
if (opponent == null || opponent.transform.root == unit.transform.root)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float sqrDistance = (opponent.transform.position - unit.transform.position).sqrMagnitude;
|
|
if (sqrDistance < nearestSqrDistance)
|
|
{
|
|
nearestSqrDistance = sqrDistance;
|
|
nearestOpponent = opponent;
|
|
}
|
|
}
|
|
|
|
return nearestOpponent;
|
|
}
|
|
}
|