当前位置:网站首页>项目实训-克隆门

项目实训-克隆门

2022-06-11 08:05:00 azzin

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

代码

子弹

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;

}

原网站

版权声明
本文为[azzin]所创,转载请带上原文链接,感谢
https://blog.csdn.net/azzin/article/details/125107140