单机游戏:时间型的View与Controller

  • 倒计时的其实并不多。枚举方式,字符串方式用字典存储一下。
  • 倒计时单位:s

  • 倒计时回调

    • View

      • 注意责任制,UI不负责保存,删除效果等逻辑,只负责显示刷新的逻辑。
      • 倒计时开始:UI显示绑定了,才是UI部分的倒计时开始。
      • 倒计时中: 一般UI负责实时显示的,所以每秒刷新,当然取决于需求。
      • 倒计时结束:UI方面的处理,关闭还是什么
    • Con中

      • 每秒刷新的类型
      • 倒计时开始:需要保存的可以在这个时候保存,然后游戏中倒计时,离线上线后,如果有倒计时可以计算得到剩余时间继续计算。
      • 倒计时中:非每秒刷新的类型,如buff类,结束就结束了。都不需要实时刷新
      • 倒计时结束:buff移除等,时间清除。
    • offline和online

      • offline:离线,保存离线时间就行了,其他的其实没必要,因为上线后会重新计算。
      • online:上线,调用所有保存的时间。有些不仅仅要间隔,还需要计算离线前的剩余时间,如双倍金币加成,上线的离线收益就需要用到这个离线前剩余时间,得到后计算离线的时间中包含多长的双倍收益的时间。
      • 时间间隔:online-offline
      • 离线前剩余时间: offline-buff保存时间
  • buff剩余时间计算

  • 方法一:buff总时间 -(上线时间-离线时间) =时间剩余 好处是:知道离线时间,但是公式相对麻烦一点【滑稽】 本人这种,因为有离线收益

  • 方法二:结束时间【保存的是结束时间】-当前时间=剩余时间 好处是:计算方便,需要开始计算出buff结束时间,并保存。

  • 大致结构

class{
    key;
    conCallBack<long>;
    viewCallBack<long>;//剩余时间
    isConSecCallBack;//是否每秒回调
    isViewSecCallBack;//是否每秒回调
}  
  • 参考脚本 :实际没有用这个,想要做到逻辑和显示一起控制的,但是有点简单奔着复杂去了

 /// <summary>
    /// </summary>
    internal static class TimerCheck
    {
        static Dictionary<string, TimerEventBase> _dicTime = new Dictionary<string, TimerEventBase>();

        private static TimerEventBase _tempValue;
        private static string _tempKey;

        private static List<string> _waitRemoveKey = new List<string>();

        public static void UpdatePerSecond()
        {
            var dic = GetDicEnumerator();
            while (dic.MoveNext())
            {
                _tempKey = dic.Current.Key;
                _tempValue = dic.Current.Value;

                if (!_tempValue.IsPause)
                {
                    ExecuteTimerDurationSub(_tempKey, _tempValue, 1);
                }
            }

            //迭代器内无法删除
            for (int i = 0; i < _waitRemoveKey.Count; i++)
            {
                RemoveTimerEvent(_waitRemoveKey[i]);
            }
        }

        /// <summary>
        /// 减去离线时间
        /// </summary>
        /// <param name="interval"></param>
        public static void OfflineInterval(long interval)
        {
            var dic = GetDicEnumerator();
            while (dic.MoveNext())
            {
                _tempKey = dic.Current.Key;
                _tempValue = dic.Current.Value;
                if (!_tempValue.IsDisableOfflineExt && _tempValue.UpdatePerSec != null)
                {
                    ExecuteTimerDurationSub(_tempKey, _tempValue, interval);
                }
            }
        }

        private static void ExecuteTimerDurationSub(string key, TimerEventBase eventBase, long factor)
        {
            if (eventBase.SetDuration(-factor) > 0 || eventBase.IsForever)
            {
                eventBase.UpdatePerSec?.Invoke(eventBase.Duration);
                eventBase.UpdatePerSecView?.Invoke(eventBase.Duration);
            }
            else
            {
                if (!eventBase.IsCycle)
                {
                    _waitRemoveKey.Add(key);
                }

                eventBase.OnCompleted?.Invoke(); //再回调,防止回调里面添加这个事件
            }
        }


        public static void AddTimerEvent(string key, TimerEventBase timerEvent)
        {
            if (_dicTime.ContainsKey(key))
            {
                Action<long> temp = _dicTime[key].UpdatePerSecView;
                _dicTime[key] = timerEvent;
                _dicTime[key].AddViewPerSec(temp);
            }
            else
            {
                _dicTime.Add(key, timerEvent);
            }
        }

        #region  view中的刷新

        /// <summary>
        /// 预添加计时器,用于被动刷新,如UI加载出来了,显示过程中某些事件准备好了,那就需要即时刷新到界面上
        /// </summary>
        /// <param name="key"></param>
        /// <param name="timerEvent"></param>
        public static void AddViewCallBack(string key, Action<long> perUpsec)
        {
            if (_dicTime.ContainsKey(key))
            {
                _dicTime[key].AddViewPerSec(perUpsec);
            }
            else
            {
                _dicTime.Add(key, new TimerEventBase(perUpsec));
            }
        }

        public static void RemoveViewCallBack(string key, Action<long> perUpsec)
        {
            if (_dicTime.ContainsKey(key))
            {
                _dicTime[key].RemoveViewPerSec(perUpsec);
            }
        }

        #endregion


        /// <summary>
        /// 强制结束某个计时器
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isExcuteCompleteCallBack"></param>
        public static void RemoveTimerEventByForce(string key, bool isExcuteCompleteCallBack = true)
        {
            if (_dicTime.ContainsKey(key))
            {
                if (isExcuteCompleteCallBack)
                {
                    _dicTime[key].OnCompleted?.Invoke();
                }

                _dicTime.Remove(key);
            }
        }

        public static bool TryGetTimerEvent(string key, out TimerEventBase timerEventBase)
        {
            if (_dicTime.ContainsKey(key))
            {
                timerEventBase = _dicTime[key];
                return true;
            }

            timerEventBase = null;
            return false;
        }


        private static Dictionary<string, TimerEventBase>.Enumerator GetDicEnumerator()
        {
            return _dicTime.GetEnumerator();
        }

        private static void RemoveTimerEvent(string key)
        {
            if (_dicTime.ContainsKey(key))
            {
                _dicTime.Remove(key);
            }
        }
    }

参考2:


using System;
using System.Collections.Generic;
using System.Data.Common;
using CatLib.Custom.FairyGUI;
using FairyGUI;
using JetBrains.Annotations;
//总的控制
namespace Game.Timer
{
    public delegate void DUpdatePerSec(long remain, object obj = null);

    public class TimerCon
    {
        static Dictionary<string, DUpdatePerSec> _dic = new Dictionary<string, DUpdatePerSec>();

        static Dictionary<Type, Dictionary<string, DUpdatePerSec>> _dicMain = new Dictionary<Type, Dictionary<string, DUpdatePerSec>>();

        //view层检测
        public static void DetectGComp(string dataKey, DUpdatePerSec callback)
        {
            Add(dataKey, callback);
        }

        //view层检测
        public static void UnDetectGComp(string dataKey, DUpdatePerSec callback)
        {
            Remove(dataKey, callback);
        }

        //分发所有检测,移除所有检测,数据层倒计时结束
        public static void UnDetectAll(string dataKey, long remain, object obj = null)
        {
            DistributeGComp(dataKey, remain, obj);
            Remove(dataKey);
        }

        //数据层分发
        public static void DistributeGComp(string datakey, long remain, object obj = null)
        {
            if (_dic.ContainsKey(datakey))
            {
                _dic[datakey]?.Invoke(remain, obj);
            }
        }

        private static void Add(string dataKey, DUpdatePerSec callback)
        {
            if (_dic.ContainsKey(dataKey))
            {
                _dic[dataKey] += callback;
            }
            else
            {
                _dic.Add(dataKey, callback);
            }
        }

        private static void Remove(string dataKey, DUpdatePerSec callback)
        {
            if (_dic.ContainsKey(dataKey))
            {
                _dic[dataKey] -= callback;
            }
        }

        private static void Remove(string dataKey)
        {
            if (_dic.ContainsKey(dataKey))
            {
                _dic.Remove(dataKey);
            }
        }
    }
}

   /// <summary>
    /// 界面式创建,方便界面销毁,统一删除相关回调,一个界面很多的时间检测的用这个更方便,移除
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class TimerView<T> : IEnumerable where T : GComponent, ILayout
    {
        private Dictionary<string, DUpdatePerSec> _dic;

        public TimerView()
        {
            _dic = new Dictionary<string, DUpdatePerSec>();
        }

        public void Detect(string dataKey, DUpdatePerSec callback)
        {
            try
            {
                _dic.Add(dataKey, callback);
            }
            catch (Exception e)
            {
                Debug.LogError("dataKey重复");
            }

            TimerCon.DetectGComp(dataKey, callback);
        }

        //view层检测 删除所有与当前view相关
        public void UnDetect()
        {
            foreach (var item in _dic)
            {
                TimerCon.UnDetectGComp(item.Key, item.Value);
            }

            _dic.Clear();
        }

        public IEnumerator GetEnumerator()
        {
            return _dic.GetEnumerator();
        }

        public void Add(string dataKey, DUpdatePerSec callback)
        {
            Detect(dataKey, callback);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值