本次完成原创植物火焰草(周围范围僵尸附加高频的低伤害)
植物代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireGrass : PlantBase
{
public override float MaxHp
{
get
{
return 100;
}
}
protected override float attackCD => 0.5f;
protected override int attackValue => 40;
//是否正在攻击
private bool isFire = false;
// 是否可以攻击
private bool canAttack;
// private bool hasFire=false;
// 创建火焰偏移量
private Vector3 creatBulletOffsetPos = new Vector2(0, 0.17F);
protected override void OnInitForPlace()
{
canAttack = true;
// 可能要攻击
InvokeRepeating("Attack", 0, 0.02f);
}
/// <summary>
/// 攻击方法-循环检测
/// </summary>
private void Attack()
{
// 从僵尸管理器 获取一定范围内僵尸
List<ZombieBase> zombies = ZombieManager.Instance.GetZombies(transform.position, 2.25f);
// 没有僵尸,播放正常动画,正在攻击为false
if (zombies.Count == 0) {
animator.Play("Idle");
isFire = false;
return; }
//如果处于CD中,返回
if (canAttack == false) return;
//以下为可以攻击
//对每个僵尸附加伤害
for (int i = 0; i < zombies.Count; i++)
{
zombies[i].Hurt(attackValue);
}
//如果不处于攻击状态,则生成攻击特效,不加这句话会一直生成攻击特效
if (!isFire)
// 生成攻击特效
{
animator.Play("Attack");
isFire = true;
Fire fire = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.Fire).GetComponent<Fire>();
fire.Init(transform.position+creatBulletOffsetPos);
}
CDEnter();
canAttack = false;
}
/// <summary>
/// 进入CD
/// </summary>
private void CDEnter()
{
StartCoroutine(CalCD());
}
/// <summary>
/// 计算冷却时间
/// </summary>
IEnumerator CalCD()
{
yield return new WaitForSeconds(attackCD);
canAttack = true;
}
}
特效代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fire : MonoBehaviour
{
private Animator animator;
public void Init(Vector2 pos)
{
animator = GetComponent<Animator>();
transform.position = pos;
animator.Play("Fire");
}
void Update()
{
Check(); //检测周围是否有僵尸,如果没有则立即销毁
}
public void Check()
{
//获取周围僵尸,获取范围应该和火草一样大
List<ZombieBase> zombies = ZombieManager.Instance.GetZombies(transform.position, 2.25f);
if (zombies.Count == 0 )
PoolManager.Instance.PushObj(GameManager.Instance.GameConf.Fire, gameObject);
}
}