赞 | 54 |
VIP | 0 |
好人卡 | 3 |
积分 | 46 |
经验 | 31992 |
最后登录 | 2024-11-7 |
在线时间 | 1206 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 4598
- 在线时间
- 1206 小时
- 注册时间
- 2016-4-7
- 帖子
- 982
|
- class Timer
- @@list = []
- def self.update
- @@list.each{|o| o.update if o != nil } if @@list != []
- end
- attr_reader :status
- TimerEvent = Struct.new(:start_time, :time, :block)
- def initialize
- @@list.push(self)
- @afters = []
- @everys = []
- @status = :run
- @stops_time = 0
- end
- def start
- return if @status == :run
- @stops_time += Time.now - @stop_time
- @status = :run
- end
- def stop
- return if @status == :stop
- @stop_time = Time.now
- @status = :stop
- end
- def after(time, &block)
- @afters.push object = TimerEvent.new(Time.now, time, block)
- object
- end
- def every(time, &block)
- @everys.push object = TimerEvent.new(Time.now, time, block)
- object
- end
- def delete_every(object)
- @everys.each do |o|
- @everys.delete(o) if o == object
- end
- end
- def delete_after(object)
- @afters.each do |o|
- @afters.delete(o) if o == object
- end
- end
- def dispose
- @@list.delete(self)
- @afters.clear
- @everys.clear
- end
- def update_afters
- return if @afters == []
- @afters.each do |o|
- if Time.now - o.start_time - @stops_time >= o.time
- o.block.call
- @afters.delete(o)
- end
- end
- end
- def update_everys
- return if @everys == []
- @everys.each do |o|
- if Time.now - o.start_time - @stops_time >= o.time
- o.block.call
- o.start_time = Time.now
- @stops_time = 0
- end
- end
- end
- def update
- update_afters
- update_everys
- end
- end
复制代码
在game loop里调用Timer.update
然后你可以通过事件代码创建各种计时器
使用方法可以参考这里 https://github.com/molingyu/rgui ... /core_plus.md#timer |
评分
-
查看全部评分
|