et框架之自写模块smarttimermodule(代码片段)

linxmouse linxmouse     2022-12-23     797

关键词:

1.代码结构图

技术分享图片

2.SmartTimer 模块Entity:

技术分享图片
  1 using System;
  2 
  3 namespace ETModel
  4 
  5     [ObjectSystem]
  6     public class SmartTimerAwakeSystem: AwakeSystem<SmartTimer, float, uint, Action>
  7     
  8         public override void Awake(SmartTimer self, float a, uint b, Action c)
  9         
 10             self.Awake(a, b, c);
 11         
 12     
 13 
 14     public sealed class SmartTimer: Entity
 15     
 16         public const uint INFINITE = uint.MaxValue;
 17 
 18         private float m_Interval = 0;
 19         private bool m_InfiniteLoops = false;
 20         private uint m_LoopsCount = 1;
 21         private Action m_Action = null;
 22         private Action m_Delegate = null;
 23         private bool m_bIsPaused = false;
 24         private uint m_CurrentLoopsCount = 0;
 25         private float m_ElapsedTime = 0;
 26         private long m_TickedTime = 0;
 27         private float m_CurrentCycleElapsedTime = 0;
 28 
 29         public void Awake(float interval, uint loopsCount, Action action)
 30         
 31             if (m_Interval < 0)
 32                 m_Interval = 0;
 33 
 34             m_TickedTime = DateTime.Now.Ticks;
 35             m_Interval = interval;
 36             m_LoopsCount = Math.Max(loopsCount, 1);
 37             m_Delegate = action;
 38         
 39 
 40         public override void Dispose()
 41         
 42             if (this.IsDisposed) return;
 43             this.m_Delegate = null;
 44             this.m_Action = null;
 45             base.Dispose();
 46         
 47 
 48         internal void UpdateActionFromAction()
 49         
 50             if (m_InfiniteLoops)
 51                 m_LoopsCount = INFINITE;
 52 
 53             if (m_Action != null)
 54                 m_Delegate = delegate  m_Action.Invoke(); ;
 55         
 56 
 57         internal void UpdateTimer()
 58         
 59             if (m_bIsPaused)
 60                 return;
 61 
 62             if (m_Delegate == null || m_Interval < 0)
 63             
 64                 m_Interval = 0;
 65                 return;
 66             
 67 
 68             if (m_CurrentLoopsCount >= m_LoopsCount && m_LoopsCount != INFINITE)
 69             
 70                 m_ElapsedTime = m_Interval * m_LoopsCount;
 71                 m_CurrentCycleElapsedTime = m_Interval;
 72             
 73             else
 74             
 75                 m_ElapsedTime += (DateTime.Now.Ticks - this.m_TickedTime) / 10000000f;
 76                 m_TickedTime = DateTime.Now.Ticks;
 77                 m_CurrentCycleElapsedTime = m_ElapsedTime - m_CurrentLoopsCount * m_Interval;
 78 
 79                 if (m_CurrentCycleElapsedTime > m_Interval)
 80                 
 81                     m_CurrentCycleElapsedTime -= m_Interval;
 82                     m_CurrentLoopsCount++;
 83                     m_Delegate.Invoke();
 84                 
 85             
 86         
 87 
 88         /// <summary>
 89         /// Get interval
 90         /// </summary>
 91         public float Interval()  return m_Interval; 
 92 
 93         /// <summary>
 94         /// Get total loops count (INFINITE (which is uint.MaxValue) if is constantly looping) 
 95         /// </summary>
 96         public uint LoopsCount()  return m_LoopsCount; 
 97 
 98         /// <summary>
 99         /// Get how many loops were completed
100         /// </summary>
101         public uint CurrentLoopsCount()  return m_CurrentLoopsCount; 
102 
103         /// <summary>
104         /// Get how many loops remained to completion
105         /// </summary>
106         public uint RemainingLoopsCount()  return m_LoopsCount - m_CurrentLoopsCount; 
107 
108         /// <summary>
109         /// Get total duration, (INFINITE if it‘s constantly looping)
110         /// </summary>
111         public float Duration()  return (m_LoopsCount == INFINITE) ? INFINITE : (m_LoopsCount * m_Interval); 
112 
113         /// <summary>
114         /// Get the delegate to execute
115         /// </summary>
116         public Action Delegate()  return m_Delegate; 
117 
118         /// <summary>
119         /// Get total remaining time
120         /// </summary>
121         public float RemainingTime()  return (m_LoopsCount == INFINITE && m_Interval > 0f) ? INFINITE : Math.Max(m_LoopsCount * m_Interval - m_ElapsedTime, 0f); 
122 
123         /// <summary>
124         /// Get total elapsed time
125         /// </summary>
126         public float ElapsedTime()  return m_ElapsedTime; 
127 
128         /// <summary>
129         /// Get elapsed time in current loop
130         /// </summary>
131         public float CurrentCycleElapsedTime()  return m_CurrentCycleElapsedTime; 
132 
133         /// <summary>
134         /// Get remaining time in current loop
135         /// </summary>
136         public float CurrentCycleRemainingTime()  return Math.Max(m_Interval - m_CurrentCycleElapsedTime, 0); 
137 
138         /// <summary>
139         /// Checks whether this timer is ok to be removed
140         /// </summary>
141         public bool ShouldClear()  return (m_Delegate == null || RemainingTime() == 0); 
142 
143         /// <summary>
144         /// Checks if the timer is paused
145         /// </summary>
146         public bool IsPaused()  return m_bIsPaused; 
147 
148         /// <summary>
149         /// Pause / Inpause timer
150         /// </summary>
151         public void SetPaused(bool bPause)  m_bIsPaused = bPause; 
152 
153         /// <summary>
154         ///     Compare frequency (calls per second)
155         /// </summary>
156         public static bool operator >(SmartTimer A, SmartTimer B)      return (A == null || B == null) || A.Interval() < B.Interval(); 
157 
158         /// <summary>
159         ///     Compare frequency (calls per second)
160         /// </summary>
161         public static bool operator <(SmartTimer A, SmartTimer B)      return (A == null || B == null) || A.Interval() > B.Interval(); 
162 
163         /// <summary>
164         ///     Compare frequency (calls per second)
165         /// </summary>
166         public static bool operator >=(SmartTimer A, SmartTimer B)     return (A == null || B == null) || A.Interval() <= B.Interval(); 
167 
168         /// <summary>
169         ///     Compare frequency (calls per second)
170         /// </summary>
171         public static bool operator <=(SmartTimer A, SmartTimer B)     return (A == null || B == null) || A.Interval() >= B.Interval(); 
172     
173 
View Code

3.SmartTimerComponent 模块Manager:

技术分享图片
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 
  5 namespace ETModel
  6 
  7     [ObjectSystem]
  8     public class SmartTimerComponentAwakeSystem: AwakeSystem<SmartTimerComponent>
  9     
 10         public override void Awake(SmartTimerComponent self)
 11         
 12             self.Awake();
 13         
 14     
 15 
 16     [ObjectSystem]
 17     public class SmartTimerComponentUpdateSystem: UpdateSystem<SmartTimerComponent>
 18     
 19         public override void Update(SmartTimerComponent self)
 20         
 21             self.Update();
 22         
 23     
 24 
 25     public class SmartTimerComponent: Component
 26     
 27         // Ensure we only have a single instance of the SmartTimerComponent loaded (singleton pattern).
 28         public static SmartTimerComponent Instance = null;
 29         private IList<SmartTimer> smartTimers = new List<SmartTimer>();
 30 
 31         // Whether the game is paused
 32         public bool Paused  get; set;  = false;
 33 
 34         public void Awake()
 35         
 36             Instance = this;
 37         
 38 
 39         public void Update()
 40         
 41             if (this.Paused)
 42                 return;
 43 
 44             foreach (SmartTimer smartTimer in this.smartTimers.ToArray())
 45             
 46                 smartTimer.UpdateTimer();
 47                 if (smartTimer.ShouldClear() || smartTimer.IsDisposed)
 48                 
 49                     this.smartTimers.Remove(smartTimer);
 50                 
 51             
 52         
 53 
 54         public void Add(SmartTimer smartTimer)
 55         
 56             this.smartTimers.Add(smartTimer);
 57         
 58 
 59         public SmartTimer Get(Action action)
 60         
 61             foreach (SmartTimer smartTimer in this.smartTimers)
 62             
 63                 if (smartTimer.Delegate() == action) return smartTimer;
 64             
 65 
 66             return null;
 67         
 68 
 69         public void Remove(SmartTimer smartTimer)
 70         
 71             this.smartTimers.Remove(smartTimer);
 72             smartTimer.Dispose();
 73         
 74 
 75         public void Remove(Action action)
 76         
 77             foreach (SmartTimer smartTimer in this.smartTimers.ToArray())
 78             
 79                 if (smartTimer.Delegate() == action)
 80                 
 81                     Remove(smartTimer);
 82                     break;
 83                 
 84             
 85         
 86 
 87         public int Count => this.smartTimers.Count;
 88 
 89         public override void Dispose()
 90         
 91             if (this.IsDisposed) return;
 92             base.Dispose();
 93 
 94             foreach (SmartTimer smartTimer in this.smartTimers)
 95             
 96                 smartTimer.Dispose();
 97             
 98             this.smartTimers.Clear();
 99             Instance = null;
100         
101 
102         /// <summary>
103         /// Get timer interval. Returns 0 if not found.
104         /// </summary>
105         /// <param name="action">Delegate name</param>
106         public float Interval(Action action)  SmartTimer timer = this.Get(action); return timer?.Interval() ?? 0f; 
107 
108         /// <summary>
109         /// Get total loops count (INFINITE (which is uint.MaxValue) if is constantly looping) 
110         /// </summary>
111         /// <param name="action">Delegate name</param>
112         public uint LoopsCount(Action action)  SmartTimer timer = this.Get(action); return timer?.LoopsCount() ?? 0; 
113 
114         /// <summary>
115         /// Get how many loops were completed
116         /// </summary>
117         /// <param name="action">Delegate name</param>
118         public uint CurrentLoopsCount(Action action)  SmartTimer timer = this.Get(action); return timer?.CurrentLoopsCount() ?? 0; 
119 
120         /// <summary>
121         /// Get how many loops remained to completion
122         /// </summary>
123         /// <param name="action">Delegate name</param>
124         public uint RemainingLoopsCount(Action action)  SmartTimer timer = this.Get(action); return timer?.RemainingLoopsCount() ?? 0; 
125 
126         /// <summary>
127         /// Get total remaining time
128         /// </summary>
129         /// <param name="action">Delegate name</param>
130         public float RemainingTime(Action action)  SmartTimer timer = this.Get(action); return timer?.RemainingTime() ?? -1f; 
131 
132         /// <summary>
133         /// Get total elapsed time
134         /// </summary>
135         /// <param name="action">Delegate name</param>
136         public float ElapsedTime(Action action)  SmartTimer timer = this.Get(action); return timer?.ElapsedTime() ?? -1f; 
137 
138         /// <summary>
139         /// Get elapsed time in current loop
140         /// </summary>
141         /// <param name="action">Delegate name</param>
142         public float CurrentCycleElapsedTime(Action action)  SmartTimer timer = this.Get(action); return timer?.CurrentCycleElapsedTime() ?? -1f; 
143 
144         /// <summary>
145         /// Get remaining time in current loop
146         /// </summary>
147         /// <param name="action">Delegate name</param>
148         public float CurrentCycleRemainingTime(Action action)  SmartTimer timer = this.Get(action); return timer?.CurrentCycleRemainingTime() ?? -1f; 
149 
150         /// <summary>
151         /// Verifies whether the timer exits
152         /// </summary>
153         /// <param name="action">Delegate name</param>
154         public bool IsTimerActive(Action action)  SmartTimer timer = this.Get(action); return timer != null; 
155 
156         /// <summary>
157         /// Checks if the timer is paused
158         /// </summary>
159         /// <param name="action">Delegate name</param>
160         public bool IsTimerPaused(Action action)  SmartTimer timer = this.Get(action); return timer?.IsPaused() ?? false; 
161 
162         /// <summary>
163         /// Pause / Unpause timer
164         /// </summary>
165         /// <param name="action">Delegate name</param>
166         ///  <param name="bPause">true - pause, false - unpause</param>
167         public void SetPaused(Action action, bool bPause)  SmartTimer timer = this.Get(action); if (timer != null) timer.SetPaused(bPause); 
168 
169         /// <summary>
170         /// Get total duration, (INFINITE if it‘s constantly looping)
171         /// </summary>
172         /// <param name="action">Delegate name</param>
173         public float Duration(Action action)  SmartTimer timer = this.Get(action); return timer?.Duration() ?? 0f; 
174     
175 
View Code

4.SmartTimerFactory 模块工厂:

技术分享图片
 1 using System;
 2 
 3 namespace ETModel
 4 
 5     public static class SmartTimerFactory
 6     
 7         public static SmartTimer Create(float interval, uint loopsCount, Action action)
 8         
 9             SmartTimer smartTimer = ComponentFactory.Create<SmartTimer, float, uint, Action>(interval, loopsCount, action);
10             SmartTimerComponent smartTimerComponent = Game.Scene.GetComponent<SmartTimerComponent>();
11             smartTimerComponent.Add(smartTimer);
12             return smartTimer;
13         
14     
15 
View Code

5.Example:

技术分享图片

技术分享图片

salt之自定义模块

 salt默认模块路径[[email protected] base]# ll /usr/lib/python2.6/site-packages/salt/modules/saltstack自定义salt模块[[email protected] _grains]# cd /srv/salt/base/[[em 查看详情

pycharm中导入自写模块时,模块下出现红线

问题描述:    在pycharm中导入自己写的模块时,得不到智能提示,并在模块名下出现下红线,但是代码可以执行,错误提示为下图所示: 解决办法:    出现 以上情况,是因为文件目录设置不当导致,pycharm中... 查看详情

mybatis进阶之自定义mybatis框架(代码片段)

MyBatis进阶之自定义MyBatis框架1.自定义MyBatis框架流程分析2.自定义框架原理介绍3.准备工作:1)创建maven工程Groupid:com.itheimaArtifactId:custom-mybatisPacking:jar2)添加pom依赖:<dependency><groupId>mysql</groupId&g 查看详情

struts2框架之自定义拦截器和配置

  struts框架中也存在拦截器,只不过系统自动调用。框架自带的拦截器的配置文件所在的位置为:  javaResources--->Libraries--->struts2-core-2.3.36.jar(核心包)--->struts-default.xml  这个配置文件中放置的是框架所有的拦截器,... 查看详情

drf框架之自定义action(代码片段)

一、自定义action使用action装饰器methods支持的请求方式,为一个列表,默认为[‘get‘]detail必传参数,要处理的是否是详情资源对象(即是否通过url路径获取主键)True表示需要传递主键id,使用通过URL获取的主键对应的数据对象Fal... 查看详情

帝国cms之自定义系统模型

系统模型就是通常所说的系统模块,如:新闻系统,下载系统,商城系统等。而自定义系统模型就是用户可以根据需要自由扩展各种系统模块。自定义系统模型一般步骤:1、系统分析;2、建立数据表;3、建立字段;4、建立系统模... 查看详情

jvm进阶之自定义类加载器(代码片段)

...加载器1.作用2.场景3.注意4.实现1.作用隔离加载类在某些框架内进行中间件与应用的模块隔离,把类加载到不同的环境。修改类加载的方式类的加载模型并非强制的,应该根据实际情况在某个时间点按需进行动态加载。扩... 查看详情

jvm进阶之自定义类加载器(代码片段)

...加载器1.作用2.场景3.注意4.实现1.作用隔离加载类在某些框架内进行中间件与应用的模块隔离,把类加载到不同的环境。修改类加载的方式类的加载模型并非强制的,应该根据实际情况在某个时间点按需进行动态加载。扩... 查看详情

wpf编程之自定义button控件样式(代码片段)

  自.NETFramework3.0以后,WPF编程框架可使开发人员开发出更加令人耳目一新的桌面应用程序。它使开发工作更加方便快捷,它将设计人员和编程人员的工作分离开来。至于WPF的背景历史、框架特点、框架结构这里就不再赘述。... 查看详情

自写网站——第一部

...码去做样式,是小瞧了CSS的功能了。直到自己写了大概的框架,只是给框架用了一点定位内容,才感觉CSS是个神器。培训老师也没有讲到定位的内容,自己也是了解不多,在中间遇到了很多问题,一步步摸索着去敲代码,一 查看详情

pythonxml模块

xml模块  自己创建xml文档importxml.etree.cElementTreeasETnew_xml=ET.Element("personinfolist")personinfo=ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"})name=ET.SubElement(personinfo,"nam 查看详情

flask框架从入门到精通之自定义response(代码片段)

知识点:1、自定义响应信息2、返回Json一、概况我们都知道当浏览器发起一个请求时,服务器会给一个响应。这个响应包含了返回的内容类型,状态码,服务器版本等一些。如下图:如果我们不进行修改这里... 查看详情

et框架简介

...题,这感觉非常糟糕,这么多年也没人解决这个问题。ET框架使用了类似守望先锋的组件设计,所有服务端内容都拆成了一个个组件,启动时根据服务器类型挂载自 查看详情

android知识要点整理(17)----gradle之自定义构建(代码片段)

通过Gradle,我们可以灵活地定制构建中的变量,从而方便灵活地控制构建过程。1.理解三个文件Gradle项目有3个重要的文件需要深入理解:项目根目录的build.gradle,settings.gradle和模块目录的build.gradle。1.settings.gradle文件会... 查看详情

xml模块

xml创建importxml.etree.ElementTreeasETnew_xml=ET.Element("namelist")name=ET.SubElement(new_xml,"name",attrib="enrolled":"yes")age=ET.SubElement(name,"age&q 查看详情

php来自et的推荐补丁为博客模块帖子生成方形特色图像。(代码片段)

查看详情

saltstack学习系列之自定义grains

Master端打开存放自定义grains的目录vim/etc/salt/masterfile_roots:base:-/srv/salt/建立自定义模块cd/srv/saltmkdir_grainscd_grains编写自定义grainscatdisk.pyimportosdefdisk():grains={}disk=os.popen(‘fdisk-l|grep‘Disk‘|grep-v‘ 查看详情

qt之自绘制饼图

1、说明   最近在搞绘图方面的工作,说实话C++的第三方绘图库并不算多,总之我了解的有:qtcharts、ChartDirector、qwt、kdchart和QCustomPlot。这几个库各有利弊。qtcharts:qt5.7之后才开源的模块,支持绘制各种图标,并且功... 查看详情