中介者模式中的时间管理

在实际开发中,常常需要添加设定一些一段时间以后发生的事件,这里主要通过一个dictionary 来处理,通过Gamefacade来进行统一管理。

管理器基类

public  class BaseManager 
{
    protected GameFacade _facade;

    public BaseManager(GameFacade facade)
    {
        this._facade = facade;
    }

    public virtual void OnInit()
    { }

    public virtual void OnUpdate()
    { }

    public virtual void OnDestory()
    { }

}

 时间管理类,构造函数传入facade,方便后期管理

public class TimeManager : BaseManager
{
    public delegate void Interval();
    private Dictionary mDicinterval = new Dictionary();

    public TimeManager(GameFacade facade) : base(facade)
    {
    }

    public override void OnInit()
    {
        base.OnInit();
    }

    public override void OnUpdate()
    {
        if (mDicinterval.Count > 0)
        {
            List remove = new List();
            foreach (KeyValuePair KeyValue in mDicinterval)
            {
                if (KeyValue.Value <= Time.time)
                {
                    remove.Add(KeyValue.Key);
                }
            }
            for (int i = 0; i < remove.Count; i++)
            {
                remove[i]();
                mDicinterval.Remove(remove[i]);
            }
        }
    }

    public override void OnDestory()
    {
        base.OnDestory();
    }

    public void AddInterval(Interval interval, float time)
    {
        if (null != interval)
            mDicinterval[interval] = Time.time + time;
    }

    public void RemoveInterval(Interval interval)
    {
        if (null != interval)
        {
            if (mDicinterval.ContainsKey(interval))
            {
                mDicinterval.Remove(interval);
            }
        }
    }
}

facade类对管理类进行集合管理,此脚本挂在GameManager物体上,永远不消失,通过单利模式,其他类可以方便调用时间方法。

public class GameFacade : MonoBehaviour
{
    private static GameFacade _instance;

    public GameFacade Instance
    {
        get
        {
            if (_instance==null)
            {
                _instance=this;
            }
            return _instance;
        }
    }

    TimeManager timeMng;

    void Awake()
    {
        _instance = this;
        DontDestroyOnLoad(this.gameObject);
    }


	// Use this for initialization
	void Start ()
    {
        MngInit();
	}
	
	// Update is called once per frame
	void Update ()
    {
        MngUpdate();


        if (Input.GetKeyDown(KeyCode.A))
        {
            timeMng.AddInterval(SayOne, 10f);
            Debug.Log("添加1");
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            timeMng.AddInterval(SayTwo, 10f);
            Debug.Log("添加2");
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            timeMng.AddInterval(SayThree, 10f);
            Debug.Log("添加3");
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            timeMng.AddInterval(SayFour, 10f);
            Debug.Log("添加4");
        }
    }

    void OnDestory()
    {
        MngOnDestory();
    }

    private void MngInit()
    {
        timeMng = new TimeManager(this);

        timeMng.OnInit();
    }

    private void MngUpdate()
    {
        timeMng.OnUpdate();
    }

    private void MngOnDestory()
    {
        timeMng.OnDestory();
    }


    //要定时加入的方法
    public void SayOne()
    {
        Debug.Log("一只法克鱿");
    }
    public void SayTwo()
    {
        Debug.Log("二只法克鱿");
    }
    public void SayThree()
    {
        Debug.Log("三只法克鱿");
    }
    public void SayFour()
    {
        Debug.Log("四只法克鱿");
    }
}

 运行结果:

中介者模式中的时间管理_第1张图片

你可能感兴趣的:(中介者模式中的时间管理)