using System; using System.Collections.Generic; using System.Linq; using System.Text; /// /// 时间管理器 /// public class TimeHelper { /// /// 初始时间 /// private DateTime mInitTime = DateTime.MaxValue; /// /// 倍数 /// private float mSpeed = 1; /// /// 总持续毫秒数 /// private double totalMillsec = 0; /// /// 上一次检查时间 /// private DateTime mLastCheckTime = DateTime.MinValue; /// /// 是否暂停 /// private bool mIsPause = false; /// /// 开始计时 /// public void Start() { this.mInitTime = DateTime.MinValue; this.mSpeed = 1.0f; this.totalMillsec = 0; this.mLastCheckTime = DateTime.Now; this.mIsPause = false; } /// /// 获取当前时间倍率 /// /// 倍率值 public float GetSpeed() { return this.mSpeed; } /// /// 设置时间倍率 /// /// 倍率值 public void SetSpeed(float val) { this.mSpeed = val; } /// /// 暂停计时器 /// public void Pause() { this.Update(); this.mIsPause = true; } /// /// 继续计时器 /// public void Continue() { this.mIsPause = false; this.mLastCheckTime = DateTime.Now; } /// /// 时间刷新 /// public void Update() { if (this.mIsPause == true) { return; } TimeSpan span = DateTime.Now - this.mLastCheckTime; this.totalMillsec += span.TotalMilliseconds * this.mSpeed; this.mLastCheckTime = DateTime.Now; } /// /// 获取当前时间 /// /// 时间 public long Now() { return (long)this.totalMillsec; } }