项目实训2022-4-21(火焰草)

本次完成原创植物火焰草(周围范围僵尸附加高频的低伤害)

植物代码

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);
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值