源战役客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

192 строки
5.7 KiB

4 недель назад
  1. cc = cc or {}
  2. cc.CC_INVALID_INDEX = -1
  3. cc.ActionManager = cc.ActionManager or {}
  4. function cc.ActionManager:init()
  5. cc.ActionManager.Instance = self
  6. self._targets = {}
  7. setmetatable(self._targets, {__mode = "k"})
  8. self._currentTarget = nil
  9. self._currentTargetSalvaged = false
  10. Runner.Instance:AddRunObj(self, 1)
  11. end
  12. function cc.ActionManager:getInstance()
  13. if cc.ActionManager.Instance == nil then
  14. cc.ActionManager.Instance = self
  15. self:init()
  16. end
  17. return cc.ActionManager.Instance
  18. end
  19. function cc.ActionManager:addAction(action, target, paused)
  20. if action == nil then
  21. print("Cat_Error:ActionManager.lua [addAction] action is nil", debug.traceback())
  22. return
  23. end
  24. assert(target ~= false, "你传进来的transform是false 是不是没加载完啊")
  25. if target == nil then
  26. print("Cat_Error:ActionManager.lua [addAction] target is nil", debug.traceback())
  27. return
  28. end
  29. local element = self._targets[target]
  30. if not element then
  31. element = {actions = {} ,target = target, actionIndex = 0, currentActionSalvaged = false, paused=paused}
  32. self._targets[target] = element
  33. end
  34. if self:isArrayContainsObject(element.actions, action) then
  35. print("Cat_Error:ActionManager.lua [addAction] action already be added!")
  36. end
  37. table.insert(element.actions, action)
  38. action:startWithTarget(target);
  39. end
  40. function cc.ActionManager:removeAction(action)
  41. if action == nil then return end
  42. local target = action:getOriginalTarget()
  43. local element = self._targets[target]
  44. if element then
  45. local i = self:arrayGetIndexOfObject(element.actions, action)
  46. if i ~= cc.CC_INVALID_INDEX then
  47. self:removeActionAtIndex(i, element);
  48. end
  49. else
  50. print("Cat:ActionManager.lua [removeAction] Target not found")
  51. end
  52. end
  53. function cc.ActionManager:isTargetInAction(target)
  54. return self:getNumberOfRunningActionsInTarget(target)>0
  55. end
  56. function cc.ActionManager:getNumberOfRunningActionsInTarget(target)
  57. local element = self._targets[target]
  58. if element then
  59. return element.actions and #(element.actions) or 0
  60. end
  61. return 0
  62. end
  63. function cc.ActionManager:Update(total_time, dt)
  64. -- if G_DEBUG_STOP_ACTION then
  65. -- return
  66. -- end
  67. -- print('Cat:ActionManager.lua[86] start update', next(self._targets))
  68. for k,v in pairs(self._targets) do
  69. --unity组件释放了的话会变成null
  70. if tostring(k) == "null" then
  71. self._targets[k] = nil
  72. else
  73. self._currentTarget = v
  74. self._currentTargetSalvaged = false
  75. if not self._currentTarget.paused then
  76. self._currentTarget.actionIndex = 1
  77. while self._currentTarget.actionIndex <= #(self._currentTarget.actions) do
  78. self._currentTarget.currentAction = self._currentTarget.actions[self._currentTarget.actionIndex]
  79. repeat
  80. if self._currentTarget.currentAction == nil then
  81. self._currentTarget.actionIndex = self._currentTarget.actionIndex + 1
  82. break
  83. end
  84. self._currentTarget.currentActionSalvaged = false
  85. self._currentTarget.currentAction:step(dt)
  86. if self._currentTarget.currentActionSalvaged then
  87. self._currentTarget.currentAction = nil
  88. elseif self._currentTarget.currentAction:isDone() then
  89. self._currentTarget.currentAction:stop()
  90. local action = self._currentTarget.currentAction
  91. self._currentTarget.currentAction = nil
  92. self:removeAction(action)
  93. end
  94. self._currentTarget.currentAction = nil
  95. self._currentTarget.actionIndex = self._currentTarget.actionIndex + 1
  96. until true
  97. end
  98. end
  99. -- only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  100. if self._currentTargetSalvaged and #(self._currentTarget.actions) == 0 then
  101. self._targets[k] = nil
  102. end
  103. end
  104. end
  105. self._currentTarget = nil
  106. end
  107. function cc.ActionManager:arrayRemoveObjectAtIndex(actions, index,releaseObj)
  108. if releaseObj and actions[index] then
  109. actions[index] = nil
  110. end
  111. table.remove(actions,index)
  112. end
  113. function cc.ActionManager:isArrayContainsObject(actions, action)
  114. for i,v in ipairs(actions) do
  115. if action == v then
  116. return true
  117. end
  118. end
  119. return false
  120. end
  121. function cc.ActionManager:removeActionAtIndex(index, element)
  122. local action = element.actions[index]
  123. if action == element.currentAction and not element.currentActionSalvaged then
  124. element.currentActionSalvaged = true;
  125. end
  126. self:arrayRemoveObjectAtIndex(element.actions, index, true)
  127. if element.actionIndex > index then
  128. element.actionIndex = element.actionIndex - 1
  129. end
  130. if #(element.actions) == 0 then
  131. if self._currentTarget == element then
  132. self._currentTargetSalvaged = true
  133. end
  134. end
  135. end
  136. function cc.ActionManager:arrayGetIndexOfObject(actions, action)
  137. for i,v in ipairs(actions) do
  138. if action == v then
  139. return i
  140. end
  141. end
  142. return cc.CC_INVALID_INDEX
  143. end
  144. function cc.ActionManager:removeAllActions()
  145. for k,v in pairs(self._targets) do
  146. self:removeAllActionsFromTarget(v.target)
  147. end
  148. self._targets = {}
  149. setmetatable(self._targets, {__mode = "k"})
  150. end
  151. function cc.ActionManager:removeAllActionsFromTarget(target)
  152. if target == nil then
  153. return
  154. end
  155. local element = self._targets[target]
  156. if element then
  157. if self:isArrayContainsObject(element.actions, element.currentAction) and (not element.currentActionSalvaged) then
  158. element.currentActionSalvaged = true
  159. end
  160. element.actions = {}
  161. if self._currentTarget == element then
  162. self._currentTargetSalvaged = true
  163. else
  164. self._targets[target] = nil
  165. end
  166. end
  167. end