项目实训-克隆门

本次完成了传送门的编写,主要作用是在飞行物如豌豆、杨桃子弹等经过的时候生成一个一样的子弹

代码

子弹

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private Rigidbody2D rigidbody;
    private SpriteRenderer spriteRenderer;
    // 攻击力
    private int attackValue;
    // 是否击中
    private bool isHit;


    public bool isCloneDoor=false;
    private Grid currGrid;
    private Vector3 offset = new Vector3(-0.4f, 0, 0);

    public void Init(int attackValue,Vector2 pos)
    {
        transform.position = pos;
        rigidbody = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        rigidbody.AddForce(Vector2.right*300);
        this.attackValue = attackValue;
        rigidbody.gravityScale = 0;
        isHit = false;
        // 修改成正常状态的图片
        spriteRenderer.sprite = GameManager.Instance.GameConf.Bullet1Nor;
    }

    void Update()
    {
        currGrid = GridManager.Instance.GetGridByWorldPos(transform.position);
        if (currGrid.currPlantBase == null) return;
        if (currGrid.currPlantBase.name == "CloneDoor(Clone)" && !isCloneDoor)
        {
            isCloneDoor = true;
            Bullet bullet = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.Bullet1).GetComponent<Bullet>();
            bullet.isCloneDoor = true;                        //让子弹也变成true 否则这个子弹也会被复制
            bullet.Init(attackValue, transform.position+offset );
        }




        if (isHit) return;
        if (transform.position.x>7.7F)
        {
            Destroy();
            return;
        }
        transform.Rotate(new Vector3(0,0,-15f));
    }

    private void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.GetComponentInParent<ZombieBase>() == null) return;
        if (isHit) return;
        if (coll.tag == "Zombie")
        {
            isHit = true;
            if(coll.GetComponentInParent<ZombieBase>().name=="BucketheadZombie(Clone)"|| coll.GetComponentInParent<ZombieBase>().name == "IceZombie(Clone)"
                || coll.GetComponentInParent<ZombieBase>().name == "ScreenDoorZombie(Clone)")
                AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.SteelHurtByPea);
            // 播放僵尸被豌豆攻击的音效
            else
            AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.ZombieHurtForPea);

            // 让僵尸受伤
            coll.GetComponentInParent<ZombieBase>().Hurt(attackValue);
            // 修改成击中图片
            spriteRenderer.sprite = GameManager.Instance.GameConf.Bullet1Hit;

            // 暂停自身的运动
            rigidbody.velocity = Vector2.zero;

            // 下落
            rigidbody.gravityScale = 1;

            // 销毁自身
            Invoke("Destroy", 0.5f);

        }
    }

    private void Destroy()
    {
        isCloneDoor = false;  
        // 取消延迟调用
        CancelInvoke();
        // 把自己放进缓存池
        PoolManager.Instance.PushObj(GameManager.Instance.GameConf.Bullet1,gameObject);
    }
}

克隆门

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CloneDoor : PlantBase
{
    public override float MaxHp => 300;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值