event.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. --------------------------------------------------------------------------------
  2. -- Copyright (c) 2015 - 2016 , 蒙占志(topameng) topameng@gmail.com
  3. -- All rights reserved.
  4. -- Use, modification and distribution are subject to the "MIT License"
  5. --------------------------------------------------------------------------------
  6. local setmetatable = setmetatable
  7. local xpcall = xpcall
  8. local pcall = pcall
  9. local assert = assert
  10. local rawget = rawget
  11. local error = error
  12. local print = print
  13. local maxn = table.maxn
  14. local traceback = tolua.traceback
  15. local ilist = ilist
  16. local _xpcall = {}
  17. _xpcall.__call = function(self, ...)
  18. if jit then
  19. if nil == self.obj then
  20. return xpcall(self.func, traceback, ...)
  21. else
  22. return xpcall(self.func, traceback, self.obj, ...)
  23. end
  24. else
  25. local args = {...}
  26. if nil == self.obj then
  27. local func = function() self.func(unpack(args, 1, maxn(args))) end
  28. return xpcall(func, traceback)
  29. else
  30. local func = function() self.func(self.obj, unpack(args, 1, maxn(args))) end
  31. return xpcall(func, traceback)
  32. end
  33. end
  34. end
  35. _xpcall.__eq = function(lhs, rhs)
  36. return lhs.func == rhs.func and lhs.obj == rhs.obj
  37. end
  38. local function xfunctor(func, obj)
  39. return setmetatable({func = func, obj = obj}, _xpcall)
  40. end
  41. local _pcall = {}
  42. _pcall.__call = function(self, ...)
  43. if nil == self.obj then
  44. return pcall(self.func, ...)
  45. else
  46. return pcall(self.func, self.obj, ...)
  47. end
  48. end
  49. _pcall.__eq = function(lhs, rhs)
  50. return lhs.func == rhs.func and lhs.obj == rhs.obj
  51. end
  52. local function functor(func, obj)
  53. return setmetatable({func = func, obj = obj}, _pcall)
  54. end
  55. local _event = {}
  56. _event.__index = _event
  57. --废弃
  58. function _event:Add(func, obj)
  59. assert(func)
  60. if self.keepSafe then
  61. func = xfunctor(func, obj)
  62. else
  63. func = functor(func, obj)
  64. end
  65. if self.lock then
  66. local node = {value = func, _prev = 0, _next = 0, removed = true}
  67. table.insert(self.opList, function() self.list:pushnode(node) end)
  68. return node
  69. else
  70. return self.list:push(func)
  71. end
  72. end
  73. --废弃
  74. function _event:Remove(func, obj)
  75. for i, v in ilist(self.list) do
  76. if v.func == func and v.obj == obj then
  77. if self.lock then
  78. table.insert(self.opList, function() self.list:remove(i) end)
  79. else
  80. self.list:remove(i)
  81. end
  82. break
  83. end
  84. end
  85. end
  86. function _event:CreateListener(func, obj)
  87. if self.keepSafe then
  88. func = xfunctor(func, obj)
  89. else
  90. func = functor(func, obj)
  91. end
  92. return {value = func, _prev = 0, _next = 0, removed = true}
  93. end
  94. function _event:AddListener(handle)
  95. assert(handle)
  96. if self.lock then
  97. table.insert(self.opList, function() self.list:pushnode(handle) end)
  98. else
  99. self.list:pushnode(handle)
  100. end
  101. end
  102. function _event:RemoveListener(handle)
  103. assert(handle)
  104. if self.lock then
  105. table.insert(self.opList, function() self.list:remove(handle) end)
  106. else
  107. self.list:remove(handle)
  108. end
  109. end
  110. function _event:Count()
  111. return self.list.length
  112. end
  113. function _event:Clear()
  114. self.list:clear()
  115. self.opList = {}
  116. self.lock = false
  117. self.keepSafe = false
  118. self.current = nil
  119. end
  120. function _event:Dump()
  121. local count = 0
  122. for _, v in ilist(self.list) do
  123. if v.obj then
  124. print("update function:", v.func, "object name:", v.obj.name)
  125. else
  126. print("update function: ", v.func)
  127. end
  128. count = count + 1
  129. end
  130. print("all function is:", count)
  131. end
  132. _event.__call = function(self, ...)
  133. local _list = self.list
  134. self.lock = true
  135. local ilist = ilist
  136. for i, f in ilist(_list) do
  137. self.current = i
  138. local flag, msg = f(...)
  139. if not flag then
  140. _list:remove(i)
  141. self.lock = false
  142. error(msg)
  143. end
  144. end
  145. local opList = self.opList
  146. self.lock = false
  147. for i, op in ipairs(opList) do
  148. op()
  149. opList[i] = nil
  150. end
  151. end
  152. function event(name, safe)
  153. safe = safe or false
  154. return setmetatable({name = name, keepSafe = safe, lock = false, opList = {}, list = list:new()}, _event)
  155. end
  156. UpdateBeat = event("Update", true)
  157. LateUpdateBeat = event("LateUpdate", true)
  158. FixedUpdateBeat = event("FixedUpdate", true)
  159. CoUpdateBeat = event("CoUpdate") --只在协同使用
  160. local Time = Time
  161. local UpdateBeat = UpdateBeat
  162. local LateUpdateBeat = LateUpdateBeat
  163. local FixedUpdateBeat = FixedUpdateBeat
  164. local CoUpdateBeat = CoUpdateBeat
  165. --逻辑update
  166. function Update(deltaTime, unscaledDeltaTime)
  167. Time:SetDeltaTime(deltaTime, unscaledDeltaTime)
  168. UpdateBeat()
  169. end
  170. function LateUpdate()
  171. LateUpdateBeat()
  172. CoUpdateBeat()
  173. Time:SetFrameCount()
  174. end
  175. --物理update
  176. function FixedUpdate(fixedDeltaTime)
  177. Time:SetFixedDelta(fixedDeltaTime)
  178. FixedUpdateBeat()
  179. end
  180. function PrintEvents()
  181. UpdateBeat:Dump()
  182. FixedUpdateBeat:Dump()
  183. end