FightUnitManager.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. FightUnitManager = {}
  2. local this = FightUnitManager
  3. local removeObjList = BattleList.New()
  4. local unitArray = {[FightUnitType.UnitSupport] = {},
  5. [FightUnitType.UnitAircraftCarrier] = {},
  6. [FightUnitType.UnitAdjutant] = {}}
  7. local unitPool = BattleObjectPool.New(function ()
  8. return FightUnitLogic.New()
  9. end)
  10. function this.Init()
  11. this.Clear()
  12. end
  13. function FightUnitManager.AddUnit(unitData)
  14. local unit = unitPool:Get()
  15. unit:Init(unitData)
  16. unitArray[tonumber(unitData.type)][tonumber(unitData.camp)] = unit
  17. BattleLogic.Event:DispatchEvent(BattleEventName.AddUnit, unit)
  18. end
  19. function FightUnitManager.GetUnit(type, camp)
  20. return unitArray[type][camp]
  21. end
  22. local _RoundBeginFrame = 2
  23. local _RoundEndFrame = 2
  24. local _RoundBeginCallBack = nil
  25. local _RoundEndCallBack = nil
  26. function FightUnitManager.UpdateRoundBegin()
  27. if _RoundBeginFrame == 2 then
  28. return
  29. end
  30. _RoundBeginFrame = _RoundBeginFrame + 1
  31. if _RoundBeginFrame == 2 then
  32. FightUnitManager.ProcessRoundBegin()
  33. end
  34. end
  35. function FightUnitManager.UpdateRoundEnd()
  36. if _RoundEndFrame == 2 then
  37. return
  38. end
  39. _RoundEndFrame = _RoundEndFrame + 1
  40. if _RoundEndFrame == 2 then
  41. FightUnitManager.ProcessRoundEnd()
  42. end
  43. end
  44. function FightUnitManager.ProcessRoundBeginNextFrame(func)
  45. _RoundBeginFrame = 0
  46. _RoundBeginCallBack = func
  47. end
  48. function FightUnitManager.ProcessRoundEndNextFrame(func)
  49. _RoundEndFrame = 0
  50. _RoundEndCallBack = func
  51. end
  52. function FightUnitManager.ProcessRoundBegin()
  53. --> 支援 等
  54. local processQueue = nil
  55. if RoleManager.GetFastCamp() == 0 then --< 最快我方
  56. processQueue = {{FightUnitType.UnitAircraftCarrier, 0}, {FightUnitType.UnitSupport, 0}, {FightUnitType.UnitAircraftCarrier, 1}, {FightUnitType.UnitSupport, 1}}
  57. else
  58. processQueue = {{FightUnitType.UnitAircraftCarrier, 1}, {FightUnitType.UnitSupport, 1}, {FightUnitType.UnitAircraftCarrier, 0}, {FightUnitType.UnitSupport, 0}}
  59. end
  60. if processQueue == nil then
  61. LogError("### processQueue nil")
  62. end
  63. local index = 1
  64. local next = nil
  65. local processFunc = nil
  66. next = function()
  67. if index == #processQueue then
  68. if _RoundBeginCallBack then
  69. _RoundBeginCallBack()
  70. else
  71. LogError("### ProcessRoundBegin callback error")
  72. end
  73. -- func()
  74. else
  75. index = index + 1
  76. processFunc()
  77. end
  78. end
  79. processFunc = function()
  80. local unit = FightUnitManager.GetUnit(processQueue[index][1], processQueue[index][2])
  81. if not unit then
  82. next()
  83. return
  84. end
  85. -- local release = unit.skill[8].release
  86. -- local cd = unit.skill[8].cd
  87. -- local CurRound, MaxRound = BattleLogic.GetCurRound()
  88. -- if CurRound == release or (CurRound - release) % (cd + 1) == 0 then
  89. -- if unit then
  90. local haveSkill = unit:CastSkill(function()
  91. next()
  92. end)
  93. if not haveSkill then
  94. next()
  95. end
  96. -- else
  97. -- next()
  98. -- end
  99. -- else
  100. -- next()
  101. -- end
  102. end
  103. processFunc()
  104. end
  105. function FightUnitManager.ProcessRoundEnd()
  106. --> 支援 等
  107. local processQueue = nil
  108. if RoleManager.GetFastCamp() == 0 then --< 最快我方
  109. processQueue = {{FightUnitType.UnitAdjutant, 0}, {FightUnitType.UnitAdjutant, 1}}
  110. else
  111. processQueue = {{FightUnitType.UnitAdjutant, 1}, {FightUnitType.UnitAdjutant, 0}}
  112. end
  113. if processQueue == nil then
  114. LogError("### processQueue nil")
  115. end
  116. local index = 1
  117. local next = nil
  118. local processFunc = nil
  119. next = function()
  120. if index == #processQueue then
  121. if _RoundEndCallBack then
  122. _RoundEndCallBack()
  123. else
  124. LogError("### ProcessRoundBegin callback error")
  125. end
  126. -- func()
  127. else
  128. index = index + 1
  129. processFunc()
  130. end
  131. end
  132. processFunc = function()
  133. local unit = FightUnitManager.GetUnit(processQueue[index][1], processQueue[index][2])
  134. if not unit then
  135. next()
  136. return
  137. end
  138. -- local release = unit.skill[8].release
  139. -- local cd = unit.skill[8].cd
  140. -- local CurRound, MaxRound = BattleLogic.GetCurRound()
  141. -- if CurRound == release or (CurRound - release) % (cd + 1) == 0 then
  142. -- if unit then
  143. local haveSkill = unit:CastSkill(function()
  144. next()
  145. end)
  146. if not haveSkill then
  147. next()
  148. end
  149. -- else
  150. -- next()
  151. -- end
  152. -- else
  153. -- next()
  154. -- end
  155. end
  156. processFunc()
  157. end
  158. function this.Clear()
  159. _RoundBeginFrame = 2
  160. _RoundEndFrame = 2
  161. _RoundBeginCallBack = nil
  162. _RoundEndCallBack = nil
  163. for _, unitsByType in pairs(unitArray) do
  164. for _, unit in pairs(unitsByType) do
  165. unit:Dispose()
  166. removeObjList:Add(unit)
  167. end
  168. end
  169. unitArray = {[FightUnitType.UnitSupport] = {},
  170. [FightUnitType.UnitAircraftCarrier] = {},
  171. [FightUnitType.UnitAdjutant] = {}}
  172. while removeObjList.size > 0 do
  173. unitPool:Put(removeObjList.buffer[removeObjList.size])
  174. removeObjList:Remove(removeObjList.size)
  175. end
  176. end
  177. return this