• QQ21031394

    工作时间

    周一至周五:9:30-18:30

    周末及节日:根据情况调休

  • 手机版二维码

    随时手机查素材

  • 扫描二维码

    加入官方QQ群

站长推荐
Rubbish 一级会员
  • 未知地域
  • 15发帖数
  • 4主题数
  • 1关注数
  • 1粉丝

[Rubbish][乐色]一个类TimerStart用法的中心计时器

[复制链接]
Rubbish 发表于 2021-12-20 23:55:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题 <
提莫作坊QQ群:提莫作坊www.tbwlm.cn

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 Rubbish 于 2021-12-20 23:57 编辑

没什么好说的
直接上代码吧

  1. library CenterTimer requires RemoveSystem
  2.     globals
  3.         private integer ExpiredTimer = 0
  4.     endglobals
  5.     module Alloc
  6.         private static integer instanceCount = 0
  7.         private thistype recycle
  8.       
  9.         static method allocate takes nothing returns thistype
  10.             local thistype this
  11.       
  12.             if (thistype(0).recycle == 0) then
  13.                 if (instanceCount == JASS_MAX_ARRAY_SIZE) then
  14.                     call BJDebugMsg( "Alloc ERROR: Attempted to allocate too many instances!" )
  15.                     return 0
  16.                 endif
  17.                 set instanceCount = instanceCount + 1
  18.                 set this = instanceCount
  19.             else
  20.                 set this = thistype(0).recycle
  21.                 set thistype(0).recycle = thistype(0).recycle.recycle
  22.             endif
  23.    
  24.             debug set this.recycle = -1
  25.       
  26.             return this
  27.         endmethod
  28.       
  29.         method deallocate takes nothing returns nothing
  30.             if (this.recycle != -1) then
  31.                 call BJDebugMsg( "Alloc ERROR: Attempted to deallocate an invalid instance at [" + I2S(this) + "]!" )
  32.             return
  33.             endif
  34.    
  35.             set this.recycle = thistype(0).recycle
  36.             set thistype(0).recycle = this
  37.         endmethod
  38.     endmodule
  39.    
  40.     module LinkedList
  41.         readonly thistype next
  42.         readonly thistype prev
  43.    
  44.         method init takes nothing returns thistype
  45.             set .next = this
  46.             set .prev = this
  47.    
  48.             return this
  49.         endmethod
  50.    
  51.         method pushBack takes thistype node returns thistype
  52.             set node.prev = prev
  53.             set node.next = this
  54.             set prev.next = node
  55.             set prev = node
  56.    
  57.             return node
  58.         endmethod
  59.    
  60.         method pushFront takes thistype node returns thistype
  61.             set node.prev = this
  62.             set node.next = next
  63.             set next.prev = node
  64.             set next = node
  65.    
  66.             return node
  67.         endmethod
  68.    
  69.         method pop takes nothing returns nothing
  70.             set prev.next = next
  71.             set next.prev = prev
  72.         endmethod
  73.     endmodule  
  74.     private struct Group extends array
  75.         implement LinkedList
  76.         implement Alloc
  77.         TimerSystem timer
  78.         
  79.         method remove takes nothing returns nothing
  80.             call .pop()
  81.             call .deallocate()
  82.         endmethod
  83.         method insert takes TimerSystem m returns thistype
  84.             local thistype node = .pushBack( allocate( ) )
  85.             set node.timer = m
  86.             return node
  87.         endmethod
  88.         
  89.         static method create takes nothing returns thistype
  90.             return thistype(allocate()).init()
  91.         endmethod
  92.     endstruct
  93.     private struct TGroup
  94.         Group group
  95.         integer size
  96.         integer time
  97.         private static constant hashtable ht = InitHashtable( )
  98.         method destroy takes nothing returns nothing
  99.             call RemoveSavedInteger( thistype.ht , <?=StringHash( 'record' )?> , .time )
  100.             call .group.deallocate()
  101.             call .deallocate()
  102.         endmethod
  103.         method insert takes TimerSystem time returns nothing
  104.             set  .size = .size + 1
  105.             call .group.insert( time )
  106.         endmethod
  107.         static method create takes integer time returns thistype
  108.             local thistype this = 0
  109.             if HaveSavedInteger( thistype.ht , <?=StringHash( 'record' )?>  , time ) then
  110.                 set this = LoadInteger( thistype.ht , <?=StringHash( 'record' )?> , time )
  111.             else
  112.                 set this   = thistype.allocate( )
  113.                 set .group = Group.create( )
  114.                 set .size  = 0
  115.                 set .time  = time
  116.                 call SaveInteger(  thistype.ht , <?=StringHash( 'record' )?> , time , this )
  117.             endif
  118.             return this
  119.         endmethod
  120.         static method have takes integer time returns boolean
  121.             return HaveSavedInteger( thistype.ht , <?=StringHash( 'record' )?>  , time )
  122.         endmethod
  123.     endstruct
  124.     struct TimerSystem
  125.         private integer timerout
  126.         private boolean is_loop
  127.         private trigger trig
  128.         private boolean removed
  129.         private boolean paused
  130.         private integer remaing
  131.         private integer cur_index
  132.                 integer userDataI
  133.                 static          integer   cur_frame = 0
  134.                 static          integer   max_frame = 0
  135.         private static constant timer     time      = CreateTimer( )
  136.         private static constant real      times     = 0.01 / 10
  137.         method destroy takes nothing returns nothing
  138.             call DestroyTrigger( .trig )
  139.             set  .timerout  = 0
  140.             set  .is_loop   = false
  141.             set  .trig      = null
  142.             set  .removed   = false
  143.             set  .paused    = false
  144.             set  .cur_index = 0
  145.             set  .remaing   = 0
  146.             set  .userDataI = 0
  147.             call .deallocate( )
  148.         endmethod
  149.         method wake_up takes nothing returns nothing
  150.             local integer time = thistype.cur_frame + this.timerout
  151.             local TGroup  data = 0
  152.             if .removed or .paused then
  153.                 if .removed then
  154.                     call .destroy( )
  155.                 endif
  156.                 return
  157.             endif
  158.             set  ExpiredTimer  = this
  159.             call TriggerEvaluate( this.trig )
  160.             if this.is_loop then
  161.                 set   data       = TGroup.create( time )
  162.                 set   .cur_index = time
  163.                 call  data.insert( this )
  164.             else
  165.                 call .destroy( )
  166.             endif
  167.         endmethod
  168.         method get_remaining takes nothing returns integer
  169.             if .removed then
  170.                 return 0
  171.             elseif .paused then
  172.                 return .remaing
  173.             elseif .cur_index == thistype.cur_frame then
  174.                 if .is_loop then
  175.                     return timerout
  176.                 else
  177.                     return 0
  178.                 endif
  179.             endif
  180.             return .cur_index - thistype.cur_frame
  181.         endmethod
  182.         
  183.         method get_timerout takes nothing returns integer
  184.             return .timerout
  185.         endmethod
  186.         
  187.         method remove takes nothing returns nothing
  188.             if not .removed then
  189.                 set .removed = true
  190.             endif
  191.         endmethod
  192.         method pause takes nothing returns nothing
  193.             if not .paused then
  194.                 set .remaing = .get_remaining( )
  195.                 set .paused  = true
  196.             endif
  197.         endmethod
  198.         method resume takes nothing returns nothing
  199.             local integer time = 0
  200.             local TGroup  data = 0
  201.             if .paused then
  202.                 set  .paused    = true
  203.                 set  time       = thistype.cur_frame + .remaing
  204.                 set  .cur_index = time
  205.                 set  data       = TGroup.create( time )
  206.                 call data.insert( this )
  207.             endif
  208.         endmethod
  209.         
  210.         static method Actions takes nothing returns nothing
  211.             local thistype this  = 0
  212.             local TGroup   data  = 0
  213.             local Group    node  = 0
  214.             set thistype.max_frame = thistype.max_frame + R2I( thistype.times *  1000 )
  215.             loop
  216.                 exitwhen thistype.cur_frame >= thistype.max_frame
  217.                 if TGroup.have( thistype.cur_frame ) then
  218.                     set data = TGroup.create( thistype.cur_frame )
  219.                     if data.size > 0 then
  220.                         set node = data.group.next
  221.                         loop
  222.                             exitwhen node == data.group
  223.                             call node.timer.wake_up( )
  224.                             call node.remove( )
  225.                             set  node = node.next
  226.                         endloop
  227.                     endif
  228.                     call data.destroy( )
  229.                 endif
  230.                 set thistype.cur_frame = thistype.cur_frame + 1
  231.             endloop
  232.         endmethod
  233.         static method create takes integer t , boolean periodic , code handlerFunc returns thistype
  234.             local integer    time   = thistype.cur_frame + t
  235.             local TGroup     data   = TGroup.create( time )   
  236.             local boolexpr   filter = Filter( handlerFunc )
  237.             local thistype this     = thistype.allocate( )
  238.             call data.insert( this )
  239.             set  .trig      = CreateTrigger( )
  240.             set  .timerout  = t
  241.             set  .is_loop   = periodic
  242.             set  .removed   = false
  243.             set  .paused    = false
  244.             set  .cur_index = time
  245.             set  .remaing   = 0
  246.             set  .userDataI = 0
  247.             call TriggerAddCondition( .trig , filter )
  248.             return this
  249.         endmethod
  250.         static method onInit takes nothing returns nothing
  251.             call TimerStart( thistype.time , thistype.times , true , function thistype.Actions )
  252.         endmethod
  253.     endstruct
  254.     //获取到期中心计时器序号
  255.     function EXGetExpiredTimer takes nothing returns TimerSystem
  256.         return ExpiredTimer
  257.     endfunction
  258.     //删除中心计时器
  259.     function EXDestroyTimer takes TimerSystem this returns nothing
  260.         call this.remove( )
  261.     endfunction
  262.     //创建中心计时器
  263.     function EXTimerStart takes real timeout , boolean periodic , code handlerFunc returns integer
  264.         return TimerSystem.create( R2I( timeout * 1000 ) , periodic , handlerFunc )
  265.     endfunction
  266.     //暂停中心计时器
  267.     function EXPauseTimer takes TimerSystem this returns nothing
  268.         call this.pause( )
  269.     endfunction
  270.     //恢复中心计时器
  271.     function EXResumeTimer takes TimerSystem this returns nothing
  272.         call this.pause( )
  273.     endfunction
  274.     //获取剩余时间
  275.     function EXGetTimerRemaining takes TimerSystem this returns real
  276.         return I2R( this.get_remaining( ) ) / 1000
  277.     endfunction
  278.     //获取设置时间
  279.     function EXGetTimerTimerout takes TimerSystem this returns real
  280.         return I2R( this.get_timerout( ) ) / 1000
  281.     endfunction
  282. endlibrary
复制代码
[发帖际遇]: Rubbish 乐于助人,奖励 1 枚 绿钻. 幸运榜 / 衰神榜
楼主热帖
免责条款:本站仅提供学习的平台,所有资料均来自于网络,版权归原创者所有!本站不提供任何保证,并不承担任何法律责任,如果对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。

本网站所收集的部分公开资料来源于互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。本站部分作品是由网友自主投稿和发布、编辑整理上传,对此类作品本站仅提供交流平台,不为其版权负责。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除。

本网站所提供的信息,只供参考之用。本网站不保证信息的准确性、有效性、及时性和完整性。本网站及其雇员一概毋须以任何方式就任何信息传递或传送的失误、不准确或错误,对用户或任何其他人士负任何直接或间接责任。在法律允许的范围内,本网站在此声明,不承担用户或任何人士就使用或未能使用本网站所提供的信息或任何链接所引致的任何直接、间接、附带、从属、特殊、惩罚性或惩戒性的损害赔偿。

强调下版规的规定,如有恶意灌水从重处罚:
=====发表主题规则=======
1.主题内容请一定要附带预览图片,谢谢;
2.搬运来的东西请尽量不要卖钱!;
3.发帖有任何疑问请联系QQ21031394;
=====发表回复规则=======
1.禁止直接复制标题内容的;
2.禁止纯数字或者纯字母的;
3.禁止乱打一通文不对题;
4.禁止屠版和刷分行为;
第一次扣分处理,第二次扣分+警告,警告三次禁言三天,绝不手软,情况严重的直接封号,请大家珍惜自己的账号!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

猜你喜欢
天工维度唯一QQ群
786014245

欢迎进群交流,入群答案:编辑器

  • 官方在线客服

    QQ客服:小雪

    点击交谈

    QQ客服:砂糖

    点击交谈

    团队老大:荀公子

    商务合作
  • 上海市静安区共和新路4718号宏慧新汇园6号楼603室

  • 手机扫码查看手机版

    手机查找资源更方便

  • 扫一扫关注官方微信

    加入官方QQ群

Powered by 天天RPG&DZX3.4 ©2020-2021 Comsenz Inc.提百万设计( 沪ICP备18032615号-1 )营业执照