46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class PoisonEffect : MonoBehaviour
|
|
{
|
|
private Damagable damagable;
|
|
private Coroutine poisonRoutine;
|
|
|
|
private float damagePerTick;
|
|
private float duration;
|
|
private float interval;
|
|
|
|
private float endTime;
|
|
|
|
private void Awake()
|
|
{
|
|
damagable = GetComponent<Damagable>();
|
|
}
|
|
|
|
public void StartPoison(float damage, float duration, float interval)
|
|
{
|
|
damagePerTick = damage;
|
|
this.duration = duration;
|
|
this.interval = interval;
|
|
endTime = Time.time + duration;
|
|
|
|
if (poisonRoutine != null)
|
|
{
|
|
StopCoroutine(poisonRoutine);
|
|
}
|
|
poisonRoutine = StartCoroutine(PoisonRoutine());
|
|
}
|
|
|
|
private IEnumerator PoisonRoutine()
|
|
{
|
|
while (Time.time < endTime)
|
|
{
|
|
if (damagable != null)
|
|
{
|
|
damagable.ApplyDamage(damagePerTick, -1, transform.position, transform);
|
|
}
|
|
yield return new WaitForSeconds(interval);
|
|
}
|
|
Destroy(this); // 中毒结束后自动移除组件
|
|
}
|
|
} |