unity2D笔记-制作死亡功能和游戏重开

对之前的viewpanel的单例基类做了优化,这样不用在子类进行单例的创建和判断了
并且可以根据不同 单例类名进行实例化 实例化的物体和类名要一致

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

public class SingletonView<T> : ViewBase where T:SingletonView<T>
{
    // Start is called before the first frame update
    public static T  _instance;
    public static T Instance
    {
        get
        {
            if(_instance == null)
            {
                Debug.Log(typeof(T).Name);
                GameObject.Instantiate( Resources.Load<GameObject>("Prefabs/" + typeof(T).Name));
            }
            return _instance;
        }
    }
    protected virtual void Awake()
    {
        _instance = this as T;
        //因为继承该单例脚本的类名不一样 因此采用泛型 且泛型声明为需要继承SingletonView
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

1.受伤脚本

在之前的脚本中加入了受伤状态来判断是受伤还是死亡,并在事件调用中增加了受伤状态参数和复位的位置信息。

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

public class Damage : MonoBehaviour
{
    public int damage;//伤害
    public HurtType hurtType; //受伤类型
    public string ResetPos;//死亡复位

    //对一个游戏物体造成伤害
    public void onDamage(GameObject gameObject)
    {
        //是否能够受伤
        DamageAble dam = gameObject.GetComponent<DamageAble>();
        if (dam == null)
        {
            return;
        }
        dam.TakeDamage(this.damage, hurtType,ResetPos);
    }

    //对多个游戏物体造成伤害
    public void onDamage(GameObject[] gameObject)
    {
        for(int i = 0; i < gameObject.Length; i++)
        {
            onDamage(gameObject[i]);
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}


2.施加伤害脚本

这里设置玩家的重生点,和保存了初始生命值以便于游戏结束重新恢复。

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

public  enum HurtType
{
    Normal,
    Dead
}
public class DamageAble : MonoBehaviour
{
    public int health; //生命值
    public int default_health; //最初的生命值
    public Action<HurtType, string> OnHurt;
    public Action OnDead;
    public bool isEnable = true;
    public void Start()
    {
        default_health = this.health;
    }
    public void Enable()
    {
        isEnable = true;
    }
    public void DisEnable()
    {
        isEnable = false;
    }
    public void TakeDamage(int damage,HurtType ht,string pos)
    {
        if(isEnable == false) { return; }
        if (health < 0) return;
          //血量减少
        Debug.Log("SDA1" + health);
        health--;
        if (health == 0)
        {
            if(OnDead!=null)
            OnDead();
        }
        else
        {
            if(OnHurt!=null)
            OnHurt(ht,pos);
        }
        
    }

    public void ResetHealth()
    {
        this.health = this.default_health;
        Enable();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


3.玩家角色控制器脚本PlayController

类中的部分相关代码,依旧在之前的AWAKE函数中实现对受伤事件的注册

    
  public void onHurt(HurtType ht,string pos)
    {
        this.currestpos = pos;
        switch (ht)
        {
            case HurtType.Normal:  
                anim.SetTrigger("isHurt");//播放受伤动画
                //设置无敌状态
                SetWuDi(1);
                break;
            case HurtType.Dead:
                SetDead();
                //重置玩家位置
                Invoke("ResetDead", 1f);
                break;

        }
       
        GamePanel.Instance.UpdateHp(playerdam.health);
     
    }
    public void SetDead()//这里指的掉一条命的死亡
    {
      anim.SetBool("isDead", true);//播放死亡动画
      anim.SetTrigger("triggerdead");
      rgd2D.gravityScale = 0;
      rgd2D.velocity =Vector2.zero;
      PlayerInput.instance.SetEnable(false);
      //显示黑屏
      TipsPanel.Instance.showTips(null,TipsStyle.Style2);
    }
    private void SetWuDi(int time)
    {
        anim.SetBool("isWuDi", true);
        playerdam.DisEnable();
        Invoke("ResetDamageable", time);
    }
    public void ResetDead()
    {
        anim.SetBool("isDead", false);//播放死亡动画
        PlayerInput.instance.SetEnable(true);
        rgd2D.gravityScale = 5;
        //复活有无敌
        SetWuDi(1);
        //设置位置
        transform.position = GameObject.Find(this.currestpos).transform.position;
    }
    public void ResetDamageable()
    {
        anim.SetBool("isWuDi", false);
        playerdam.Enable();
    }
    public void onDead()//血量全没有的死亡
    {
        Debug.Log("GAME OVER");
        SetDead();
        GamePanel.Instance.UpdateHp(playerdam.health);
        //显示游戏结束界面
        Invoke("DelayShowGameOverPanel", 1f);
        
    }
    private void DelayShowGameOverPanel()
    {
        TipsPanel.Instance.showTips(null, TipsStyle.Style3);
        this.currestpos = "spawn1";
        ResetDead();
        GamePanel.Instance.ResetHp();
        playerdam.ResetHealth();
    }

4.提示面板

在提示面板中加入了死亡黑屏和 游戏结束界面

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

public enum TipsStyle
{
    Style1,//位于下方的提示
    Style2,//死亡黑屏
    Style3//游戏结束
}

public class TipsPanel : SingletonView<TipsPanel>
{
    // Start is called before the first frame update
    GameObject styleobj1;
    GameObject styleobj2;
    GameObject styleobj3;
    protected override void Awake()
    {
        base.Awake();
        styleobj1 = transform.Find("Style1").gameObject;
        styleobj1.SetActive(false);
        styleobj2 = transform.Find("Style2").gameObject;
        styleobj2.SetActive(false);
        styleobj3 = transform.Find("Style3").gameObject;
        styleobj3.SetActive(false);
    }
    public void showTips(string content, TipsStyle style)
    {
        switch (style)
        {
            case TipsStyle.Style1:
                styleobj1.SetActive(true);
                styleobj1.transform.Find("Content").GetComponent<Text>().text = content;
                break;
            case TipsStyle.Style2:
                styleobj2.SetActive(true);
                //1.5s隐藏
                Invoke("HideTips2", 1.5f);
                break;
            case TipsStyle.Style3:
                styleobj3.SetActive(true);
                //1.5s隐藏
                Invoke("HideTips3", 1f);
                break;
        }
    }
    private void HideTips2()
    {
        HideTips(TipsStyle.Style2);
    }
    private void HideTips3()
    {
        Debug.Log("hide tip3");
        HideTips(TipsStyle.Style3);
    }
    // Update is called once per frame
    public void HideTips(TipsStyle style)
    {
        switch (style)
        {
            case TipsStyle.Style1:
                styleobj1.SetActive(false);
                break;
            case TipsStyle.Style2:
                styleobj2.SetActive(false);
                break;
            case TipsStyle.Style3:
                styleobj3.SetActive(false);
                break;
        }
    }
}

5.游戏信息面板

加了血量重置代码

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

public class GamePanel : SingletonView<GamePanel>
{
    public GameObject hp_item_prefb;
    Transform hp_parent;
    GameObject[] hp_table;
     protected override void Awake()
    {
        base.Awake();
        hp_parent = transform.Find("Hp");
    }
    public void InitHp(int count)
    {
        hp_table = new GameObject[count];
        for (int i = 0; i < count; i++)
        {
            hp_table[i] =  GameObject.Instantiate(hp_item_prefb, hp_parent);//hp item
        }
    }
    public void UpdateHp(int count) //更新
    {
        for (int i = count; i <hp_table.Length; i++)
        {
            if (hp_table[i].GetComponent<Toggle>().isOn)
            {
                hp_table[i].GetComponent<Toggle>().isOn = false;
            }
            //显示特效
            
        }
    }
    public void ResetHp() //更新
    {
        for (int i = 0; i < hp_table.Length; i++)
        {
            hp_table[i].GetComponent<Toggle>().isOn = true;

        }
    }
}

你可能感兴趣的:(unity,动画,unity,c#)