【委托事件】一个订阅玩家死亡的事件

using System.Collections;
using System;

public class NewBehaviourScript2 : MonoBehaviour {


        void Start () 
        {
                Player player = new Player ();

                player.PlayerHp += GameManage.HeroDeath;
                player.PlayerHp += AduioManage.HeroDeath;

                player.Dmage ();

        }
        
        public class Player
        {
                public class DieEventArgs:EventArgs
                {
                        public DieEventArgs()
                        {
                                Debug.Log("你挂了");
                        }
                }

                public delegate void PlayerHpEventHandle(object sender,DieEventArgs e);
                public event PlayerHpEventHandle PlayerHp;

                private int curHp = 100;

                public void Dmage()
                {

                        while(curHp!=0)
                        {
                                curHp -= 1;
                                if(curHp==0)
                                {
                                        DieEventArgs e = new DieEventArgs();
                                        Die(e);
                                }
                        }

                }

                protected virtual void Die(DieEventArgs e)
                {
                        if(PlayerHp!=null)
                        {
                                PlayerHp(this,e);
                        }
                }

        }

        public class GameManage
        {
                public static void HeroDeath(object sender,Player.DieEventArgs e)
                {
                        Debug.Log ("英雄死亡,用户失去控制英雄的权限");
                }
        }
        
        public class AduioManage
        {
                public static void HeroDeath(object sender,Player.DieEventArgs e)
                {
                        Debug.Log ("英雄死亡,播放游戏失败的音乐");
                }
        }

        

}

  

posted on 2015-08-19 11:27  快要结束了  阅读(147)  评论(0编辑  收藏  举报

导航