ET框架之自写模块SmartTimerModule
1.代码结构图
2.SmartTimer 模块Entity:
using System; namespace ETModel
{
[ObjectSystem]
public class SmartTimerAwakeSystem: AwakeSystem<SmartTimer, float, uint, Action>
{
public override void Awake(SmartTimer self, float a, uint b, Action c)
{
self.Awake(a, b, c);
}
} public sealed class SmartTimer: Entity
{
public const uint INFINITE = uint.MaxValue; private float m_Interval = ;
private bool m_InfiniteLoops = false;
private uint m_LoopsCount = ;
private Action m_Action = null;
private Action m_Delegate = null;
private bool m_bIsPaused = false;
private uint m_CurrentLoopsCount = ;
private float m_ElapsedTime = ;
private long m_TickedTime = ;
private float m_CurrentCycleElapsedTime = ; public void Awake(float interval, uint loopsCount, Action action)
{
if (m_Interval < )
m_Interval = ; m_TickedTime = DateTime.Now.Ticks;
m_Interval = interval;
m_LoopsCount = Math.Max(loopsCount, );
m_Delegate = action;
} public override void Dispose()
{
if (this.IsDisposed) return;
this.m_Delegate = null;
this.m_Action = null;
base.Dispose();
} internal void UpdateActionFromAction()
{
if (m_InfiniteLoops)
m_LoopsCount = INFINITE; if (m_Action != null)
m_Delegate = delegate { m_Action.Invoke(); };
} internal void UpdateTimer()
{
if (m_bIsPaused)
return; if (m_Delegate == null || m_Interval < )
{
m_Interval = ;
return;
} if (m_CurrentLoopsCount >= m_LoopsCount && m_LoopsCount != INFINITE)
{
m_ElapsedTime = m_Interval * m_LoopsCount;
m_CurrentCycleElapsedTime = m_Interval;
}
else
{
m_ElapsedTime += (DateTime.Now.Ticks - this.m_TickedTime) / 10000000f;
m_TickedTime = DateTime.Now.Ticks;
m_CurrentCycleElapsedTime = m_ElapsedTime - m_CurrentLoopsCount * m_Interval; if (m_CurrentCycleElapsedTime > m_Interval)
{
m_CurrentCycleElapsedTime -= m_Interval;
m_CurrentLoopsCount++;
m_Delegate.Invoke();
}
}
} /// <summary>
/// Get interval
/// </summary>
public float Interval() { return m_Interval; } /// <summary>
/// Get total loops count (INFINITE (which is uint.MaxValue) if is constantly looping)
/// </summary>
public uint LoopsCount() { return m_LoopsCount; } /// <summary>
/// Get how many loops were completed
/// </summary>
public uint CurrentLoopsCount() { return m_CurrentLoopsCount; } /// <summary>
/// Get how many loops remained to completion
/// </summary>
public uint RemainingLoopsCount() { return m_LoopsCount - m_CurrentLoopsCount; } /// <summary>
/// Get total duration, (INFINITE if it's constantly looping)
/// </summary>
public float Duration() { return (m_LoopsCount == INFINITE) ? INFINITE : (m_LoopsCount * m_Interval); } /// <summary>
/// Get the delegate to execute
/// </summary>
public Action Delegate() { return m_Delegate; } /// <summary>
/// Get total remaining time
/// </summary>
public float RemainingTime() { return (m_LoopsCount == INFINITE && m_Interval > 0f) ? INFINITE : Math.Max(m_LoopsCount * m_Interval - m_ElapsedTime, 0f); } /// <summary>
/// Get total elapsed time
/// </summary>
public float ElapsedTime() { return m_ElapsedTime; } /// <summary>
/// Get elapsed time in current loop
/// </summary>
public float CurrentCycleElapsedTime() { return m_CurrentCycleElapsedTime; } /// <summary>
/// Get remaining time in current loop
/// </summary>
public float CurrentCycleRemainingTime() { return Math.Max(m_Interval - m_CurrentCycleElapsedTime, ); } /// <summary>
/// Checks whether this timer is ok to be removed
/// </summary>
public bool ShouldClear() { return (m_Delegate == null || RemainingTime() == ); } /// <summary>
/// Checks if the timer is paused
/// </summary>
public bool IsPaused() { return m_bIsPaused; } /// <summary>
/// Pause / Inpause timer
/// </summary>
public void SetPaused(bool bPause) { m_bIsPaused = bPause; } /// <summary>
/// Compare frequency (calls per second)
/// </summary>
public static bool operator >(SmartTimer A, SmartTimer B) { return (A == null || B == null) || A.Interval() < B.Interval(); } /// <summary>
/// Compare frequency (calls per second)
/// </summary>
public static bool operator <(SmartTimer A, SmartTimer B) { return (A == null || B == null) || A.Interval() > B.Interval(); } /// <summary>
/// Compare frequency (calls per second)
/// </summary>
public static bool operator >=(SmartTimer A, SmartTimer B) { return (A == null || B == null) || A.Interval() <= B.Interval(); } /// <summary>
/// Compare frequency (calls per second)
/// </summary>
public static bool operator <=(SmartTimer A, SmartTimer B) { return (A == null || B == null) || A.Interval() >= B.Interval(); }
}
}
3.SmartTimerComponent 模块Manager:
using System;
using System.Collections.Generic;
using System.Linq; namespace ETModel
{
[ObjectSystem]
public class SmartTimerComponentAwakeSystem: AwakeSystem<SmartTimerComponent>
{
public override void Awake(SmartTimerComponent self)
{
self.Awake();
}
} [ObjectSystem]
public class SmartTimerComponentUpdateSystem: UpdateSystem<SmartTimerComponent>
{
public override void Update(SmartTimerComponent self)
{
self.Update();
}
} public class SmartTimerComponent: Component
{
// Ensure we only have a single instance of the SmartTimerComponent loaded (singleton pattern).
public static SmartTimerComponent Instance = null;
private IList<SmartTimer> smartTimers = new List<SmartTimer>(); // Whether the game is paused
public bool Paused { get; set; } = false; public void Awake()
{
Instance = this;
} public void Update()
{
if (this.Paused)
return; foreach (SmartTimer smartTimer in this.smartTimers.ToArray())
{
smartTimer.UpdateTimer();
if (smartTimer.ShouldClear() || smartTimer.IsDisposed)
{
this.smartTimers.Remove(smartTimer);
}
}
} public void Add(SmartTimer smartTimer)
{
this.smartTimers.Add(smartTimer);
} public SmartTimer Get(Action action)
{
foreach (SmartTimer smartTimer in this.smartTimers)
{
if (smartTimer.Delegate() == action) return smartTimer;
} return null;
} public void Remove(SmartTimer smartTimer)
{
this.smartTimers.Remove(smartTimer);
smartTimer.Dispose();
} public void Remove(Action action)
{
foreach (SmartTimer smartTimer in this.smartTimers.ToArray())
{
if (smartTimer.Delegate() == action)
{
Remove(smartTimer);
break;
}
}
} public int Count => this.smartTimers.Count; public override void Dispose()
{
if (this.IsDisposed) return;
base.Dispose(); foreach (SmartTimer smartTimer in this.smartTimers)
{
smartTimer.Dispose();
}
this.smartTimers.Clear();
Instance = null;
} /// <summary>
/// Get timer interval. Returns 0 if not found.
/// </summary>
/// <param name="action">Delegate name</param>
public float Interval(Action action) { SmartTimer timer = this.Get(action); return timer?.Interval() ?? 0f; } /// <summary>
/// Get total loops count (INFINITE (which is uint.MaxValue) if is constantly looping)
/// </summary>
/// <param name="action">Delegate name</param>
public uint LoopsCount(Action action) { SmartTimer timer = this.Get(action); return timer?.LoopsCount() ?? ; } /// <summary>
/// Get how many loops were completed
/// </summary>
/// <param name="action">Delegate name</param>
public uint CurrentLoopsCount(Action action) { SmartTimer timer = this.Get(action); return timer?.CurrentLoopsCount() ?? ; } /// <summary>
/// Get how many loops remained to completion
/// </summary>
/// <param name="action">Delegate name</param>
public uint RemainingLoopsCount(Action action) { SmartTimer timer = this.Get(action); return timer?.RemainingLoopsCount() ?? ; } /// <summary>
/// Get total remaining time
/// </summary>
/// <param name="action">Delegate name</param>
public float RemainingTime(Action action) { SmartTimer timer = this.Get(action); return timer?.RemainingTime() ?? -1f; } /// <summary>
/// Get total elapsed time
/// </summary>
/// <param name="action">Delegate name</param>
public float ElapsedTime(Action action) { SmartTimer timer = this.Get(action); return timer?.ElapsedTime() ?? -1f; } /// <summary>
/// Get elapsed time in current loop
/// </summary>
/// <param name="action">Delegate name</param>
public float CurrentCycleElapsedTime(Action action) { SmartTimer timer = this.Get(action); return timer?.CurrentCycleElapsedTime() ?? -1f; } /// <summary>
/// Get remaining time in current loop
/// </summary>
/// <param name="action">Delegate name</param>
public float CurrentCycleRemainingTime(Action action) { SmartTimer timer = this.Get(action); return timer?.CurrentCycleRemainingTime() ?? -1f; } /// <summary>
/// Verifies whether the timer exits
/// </summary>
/// <param name="action">Delegate name</param>
public bool IsTimerActive(Action action) { SmartTimer timer = this.Get(action); return timer != null; } /// <summary>
/// Checks if the timer is paused
/// </summary>
/// <param name="action">Delegate name</param>
public bool IsTimerPaused(Action action) { SmartTimer timer = this.Get(action); return timer?.IsPaused() ?? false; } /// <summary>
/// Pause / Unpause timer
/// </summary>
/// <param name="action">Delegate name</param>
/// <param name="bPause">true - pause, false - unpause</param>
public void SetPaused(Action action, bool bPause) { SmartTimer timer = this.Get(action); if (timer != null) timer.SetPaused(bPause); } /// <summary>
/// Get total duration, (INFINITE if it's constantly looping)
/// </summary>
/// <param name="action">Delegate name</param>
public float Duration(Action action) { SmartTimer timer = this.Get(action); return timer?.Duration() ?? 0f; }
}
}
4.SmartTimerFactory 模块工厂:
using System; namespace ETModel
{
public static class SmartTimerFactory
{
public static SmartTimer Create(float interval, uint loopsCount, Action action)
{
SmartTimer smartTimer = ComponentFactory.Create<SmartTimer, float, uint, Action>(interval, loopsCount, action);
SmartTimerComponent smartTimerComponent = Game.Scene.GetComponent<SmartTimerComponent>();
smartTimerComponent.Add(smartTimer);
return smartTimer;
}
}
}
5.Example:
ET框架之自写模块SmartTimerModule的更多相关文章
- 基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理
最近一直很多事情,博客停下来好久没写了,整理下思路,把最近研究的基于Metronic的Bootstrap开发框架进行经验的总结出来和大家分享下,同时也记录自己对Bootstrap开发的学习研究的点点滴 ...
- 第三百零四节,Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器
Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器 这一节主讲url控制器 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个 ...
- 二 Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器
Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器 这一节主讲url控制器 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个 ...
- 微信公众帐号开发。大家是用框架还是自己写的流程。现在遇到若干问题。请教各路大仙 - V2EX
微信公众帐号开发.大家是用框架还是自己写的流程.现在遇到若干问题.请教各路大仙 - V2EX 微信公众帐号开发.大家是用框架还是自己写的流程.现在遇到若干问题.请教各路大仙
- robot framework 如何自己写模块下的方法或者库
一.写模块(RF能识别的模块) 例如:F:\Python3.4\Lib\site-packages\robot\libraries这个库(包)下面的模块(.py),我们可以看下源码 注意:这种是以方法 ...
- TP3.2框架,实现空模块、空控制器、空操作的页面404替换||同步实现apache报错404页面替换
一,前言 一.1)以下代码是在TP3.0版本之后,URL的默认模式=>PATHINFO的前提下进行的.(通俗点,URL中index.php必须存在且正确) 代码和讲解如下: 1.空模块解决:ht ...
- WhyGL:一套学习OpenGL的框架,及翻写Nehe的OpenGL教程
最近在重学OpenGL,之所以说重学是因为上次接触OpenGL还是在学校里,工作之后就一直在搞D3D,一转眼已经毕业6年了.OpenGL这门手艺早就完全荒废了,现在只能是重学.学习程序最有效的办法是动 ...
- Python高级进阶(二)Python框架之Django写图书管理系统(LMS)
正式写项目准备前的工作 Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使用之前就我 ...
- 3) drf 框架生命周期 请求模块 渲染模块 解析模块 自定义异常模块 响应模块(以及二次封装)
一.DRF框架 1.安装 pip3 install djangorestframework 2.drf框架规矩的封装风格 按功能封装,drf下按不同功能不同文件,使用不同功能导入不同文件 from r ...
随机推荐
- [Python]PyCharm在创建py文件时自动添加头部注释
在Pycharm主界面找到 File ----->> Setting ----->> Editor ----->> File and Code Templates ...
- ubuntu--- tracker/libdeepsort.so 找不到cv报错
一.刚开始解决尝试:因为“删掉lib下的libdeepsort.so报错”,原先以为是 libdeepsort.so 需要拷贝到 /lib路径下的问题,可是因为后来的工程有的好使,又的不好使了.''' ...
- react 中 函数bind 和箭头函数
用bind形式 方便测试,含有this时候最好用bind形 其他情况用箭头函数 含有this的时候也可以用箭头函数
- Mac下安装MySQL8的问题
黑苹果用了一段时间之后,发现很多方面用起来比Windows还舒服些,没什么具体指标,就是纯粹一种感觉. 所以,慢慢将很多程序都迁移过来,在迁移过程中发现的一些有意思的事儿,我都把他们记录下来.如果,不 ...
- jQuery中校验时间格式的正则表达式小结
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)
https://codeforces.com/contest/1304/problem/E #include<bits/stdc++.h> using namespace std; typ ...
- ECMAScript基本语法——⑤运算符 三元运算符
?: 简化ifelse的操作
- Gin_中间件
gin可以构建中间件,但它只对注册过的路由函数起作用 对于分组路由,嵌套使用中间件,可以限定中间件的作用范围 中间件分为全局中间件,单个路由中间件和群组中间件 gin中间件必须是一个 gin.Hand ...
- Pytest学习9-常用插件
pytest-django:为django应用程序编写测试. pytest-twisted:为twisted应用程序编写测试,启动反应堆并处理测试函数的延迟. pytest-cov:覆盖率报告,与分布 ...
- MapReduce异常:java.lang.ClassCastException: interface javax.xml.soap.Text
MapReduce异常:java.lang.ClassCastException: interface javax.xml.soap.Text java.lang.ClassCastException ...