源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1082 lines
32 KiB

преди 4 седмици
  1. cc = cc or {}
  2. local math_fmod = math.fmod
  3. local math_min = math.min
  4. local math_max = math.max
  5. local math_pow = math.pow
  6. cc.ExtraAction = cc.ExtraAction or BaseClass(cc.FiniteTimeAction)
  7. function cc.ExtraAction:__init()
  8. end
  9. function cc.ExtraAction:clone()
  10. return cc.ExtraAction.New()
  11. end
  12. function cc.ExtraAction:reverse()
  13. return cc.ExtraAction.New()
  14. end
  15. function cc.ExtraAction:update(time)
  16. end
  17. function cc.ExtraAction:step(dt)
  18. end
  19. cc.ActionInterval = cc.ActionInterval or BaseClass(cc.FiniteTimeAction)
  20. cc.ActionInterval.FLT_EPSILON = 1.192092896e-07
  21. function cc.ActionInterval:__init()
  22. self._classType = "ActionInterval"
  23. end
  24. function cc.ActionInterval:getElapsed()
  25. return self._elapsed
  26. end
  27. function cc.ActionInterval:setAmplitudeRate(amp)
  28. --Subclass should implement this method!
  29. end
  30. function cc.ActionInterval:getAmplitudeRate()
  31. --Subclass should implement this method!
  32. return 0
  33. end
  34. function cc.ActionInterval:isDone()
  35. return self._elapsed >= self._duration
  36. end
  37. function cc.ActionInterval:sendUpdateEventToScript(dt, actionObject)
  38. return false;
  39. end
  40. function cc.ActionInterval:step(dt)
  41. if self._firstTick then
  42. self._firstTick = false
  43. self._elapsed = 0
  44. else
  45. self._elapsed = self._elapsed + dt
  46. end
  47. local updateDt = math_max(0,math_min(1,self._elapsed / math_max(self._duration,cc.ActionInterval.FLT_EPSILON)))
  48. self:update(updateDt)
  49. end
  50. function cc.ActionInterval:startWithTarget(target)
  51. cc.FiniteTimeAction.startWithTarget(self, target);
  52. self._elapsed = 0
  53. self._firstTick = true
  54. end
  55. function cc.ActionInterval:reverse()
  56. print("Cat_Error:ActionInterval.lua [ActionInterval:reverse] should not exec this method!")
  57. return nil
  58. end
  59. function cc.ActionInterval:clone()
  60. print("Cat_Error:ActionInterval.lua [ActionInterval:clone] should not exec this method!")
  61. return nil
  62. end
  63. function cc.ActionInterval:initWithDuration(d)
  64. self._duration = d
  65. if self._duration == 0 then
  66. self._duration = cc.ActionInterval.FLT_EPSILON
  67. end
  68. self._elapsed = 0
  69. self._firstTick = true
  70. return true
  71. end
  72. cc.MoveTypeMap = cc.MoveTypeMap or {
  73. ["AnchoredPos"] = {GetFunc=cc.Wrapper.GetAnchoredPosition, SetFunc=cc.Wrapper.SetAnchoredPosition},
  74. ["LocalPos"] = {GetFunc=cc.Wrapper.GetLocalPosition, SetFunc=cc.Wrapper.SetLocalPosition},
  75. ["Pos"] = {GetFunc=cc.Wrapper.GetPosition, SetFunc=cc.Wrapper.SetPosition},
  76. }
  77. --MoveBy start
  78. cc.MoveBy = cc.MoveBy or BaseClass(cc.ActionInterval)
  79. --别用.New了,用create前缀的接口
  80. function cc.MoveBy:__init(duration, delta_x, delta_y, delta_z, previous_x, previous_y, previous_z )
  81. self:initWithMoveType(duration, delta_x, delta_y, delta_z, "LocalPos", previous_x, previous_y, previous_z)
  82. end
  83. --移动节点的anchoredPosition
  84. function cc.MoveBy.createAnchoredType( duration, x, y, previous_x, previous_y )
  85. local action = cc.MoveBy.New()
  86. action:initWithMoveType(duration, x, y, 0, "AnchoredPos", previous_x, previous_y)
  87. return action
  88. end
  89. --移动节点的position绝对坐标
  90. function cc.MoveBy.createAbsType( duration, x, y, z, previous_x, previous_y, previous_z )
  91. local action = cc.MoveBy.New()
  92. action:initWithMoveType(duration, x, y, z, "Pos", previous_x, previous_y, previous_z)
  93. return action
  94. end
  95. --移动节点的localPosition
  96. function cc.MoveBy.createLocalType( duration, x, y, z, previous_x, previous_y, previous_z )
  97. local action = cc.MoveBy.New()
  98. action:initWithMoveType(duration, x, y, z, "LocalPos", previous_x, previous_y, previous_z)
  99. return action
  100. end
  101. function cc.MoveBy:initWithMoveType(duration, delta_x, delta_y, delta_z, move_type, previous_x, previous_y, previous_z)
  102. cc.ActionInterval.initWithDuration(self, duration)
  103. self.move_type = move_type
  104. self._positionDeltaX = delta_x or 0
  105. self._positionDeltaY = delta_y or 0
  106. self._positionDeltaZ = delta_z or 0
  107. self._previousPositionX, self._previousPositionY, self._previousPositionZ = previous_x, previous_y, previous_z
  108. end
  109. function cc.MoveBy:clone()
  110. local action = cc.MoveBy.New()
  111. action:initWithMoveType(self._duration, self._positionDeltaX, self._positionDeltaY, self._positionDeltaZ, self.move_type)
  112. return action
  113. end
  114. function cc.MoveBy:reverse()
  115. local action = cc.MoveBy.New()
  116. action:initWithMoveType(self._duration, self._positionDeltaX and -self._positionDeltaX, self._positionDeltaY and -self._positionDeltaY, self._positionDeltaZ and -self._positionDeltaZ, self.move_type)
  117. return action
  118. end
  119. function cc.MoveBy:startWithTarget(target)
  120. cc.ActionInterval.startWithTarget(self, target)
  121. if not self._previousPositionX or not self._previousPositionY then
  122. self._previousPositionX, self._previousPositionY, self._previousPositionZ = cc.MoveTypeMap[self.move_type].GetFunc(self._target)
  123. end
  124. self._previousPositionZ = self._previousPositionZ or 0--anchored pos没有Z轴
  125. self._startPositionX, self._startPositionY, self._startPositionZ = self._previousPositionX, self._previousPositionY, self._previousPositionZ
  126. end
  127. function cc.MoveBy:update(t)
  128. if self._target then
  129. if self._positionDeltaX and self._positionDeltaX ~= 0 then
  130. self._previousPositionX = self._startPositionX + (self._positionDeltaX * t)
  131. end
  132. if self._positionDeltaY and self._positionDeltaY ~= 0 then
  133. self._previousPositionY = self._startPositionY + (self._positionDeltaY * t)
  134. end
  135. if self._positionDeltaZ and self._positionDeltaZ ~= 0 then
  136. self._previousPositionZ = self._startPositionZ + (self._positionDeltaZ * t)
  137. end
  138. cc.MoveTypeMap[self.move_type].SetFunc(self._target, self._previousPositionX, self._previousPositionY, self._previousPositionZ)
  139. end
  140. end
  141. --MoveBy end
  142. --MoveTo start
  143. cc.MoveTo = cc.MoveTo or BaseClass(cc.MoveBy)
  144. --尽量别用.New了,用create前缀的接口
  145. function cc.MoveTo:__init(duration, x, y, z, previous_x, previous_y, previous_z)
  146. self:initWithMoveType(duration, x, y, z, "LocalPos", previous_x, previous_y, previous_z)
  147. end
  148. --移动节点的anchoredPosition
  149. function cc.MoveTo.createAnchoredType( duration, x, y, previous_x, previous_y )
  150. local action = cc.MoveTo.New()
  151. action:initWithMoveType(duration, x, y, 0, "AnchoredPos", previous_x, previous_y)
  152. return action
  153. end
  154. --移动节点的position绝对坐标
  155. function cc.MoveTo.createAbsType( duration, x, y, z, previous_x, previous_y, previous_z )
  156. local action = cc.MoveTo.New()
  157. action:initWithMoveType(duration, x, y, z, "Pos", previous_x, previous_y, previous_z)
  158. return action
  159. end
  160. --移动节点的localPosition
  161. function cc.MoveTo.createLocalType( duration, x, y, z, previous_x, previous_y, previous_z )
  162. local action = cc.MoveTo.New()
  163. action:initWithMoveType(duration, x, y, z, "LocalPos", previous_x, previous_y, previous_z)
  164. return action
  165. end
  166. function cc.MoveTo:initWithMoveType(duration, x, y, z, move_type, previous_x, previous_y, previous_z)
  167. cc.ActionInterval.initWithDuration(self, duration)
  168. self.move_type = move_type
  169. self._endPositionX = x or 0
  170. self._endPositionY = y or 0
  171. self._endPositionZ = z or 0
  172. self._previousPositionX, self._previousPositionY, self._previousPositionZ = previous_x, previous_y, previous_z
  173. end
  174. function cc.MoveTo:clone()
  175. local action = cc.MoveTo.New()
  176. action:initWithMoveType(self._duration, self._endPositionX, self._endPositionY, self._endPositionZ, "LocalPos")
  177. return action
  178. end
  179. function cc.MoveTo:startWithTarget(target)
  180. cc.MoveBy.startWithTarget(self, target)
  181. local oldX, oldY, oldZ = self._previousPositionX, self._previousPositionY, self._previousPositionZ
  182. if not oldX or not oldY then
  183. oldX, oldY, oldZ = cc.MoveTypeMap[self.move_type].GetFunc(self._target)
  184. end
  185. self._positionDeltaX = self._endPositionX - oldX
  186. self._positionDeltaY = self._endPositionY - oldY
  187. self._positionDeltaZ = self._endPositionZ - (oldZ or 0)
  188. end
  189. function cc.MoveTo:reverse()
  190. print("reverse() not supported in MoveTo")
  191. return nil
  192. end
  193. --MoveTo end
  194. --Sequence start
  195. cc.Sequence = cc.Sequence or BaseClass(cc.ActionInterval)
  196. function cc.Sequence:__init(...)
  197. self._actions = {}
  198. self:initWithTable({...})
  199. end
  200. function cc.Sequence.createWithTwoActions(actionOne, actionTwo)
  201. local sequence = cc.Sequence.New()
  202. sequence:initWithTwoActions(actionOne, actionTwo);
  203. return sequence;
  204. end
  205. function cc.Sequence.createWithTable( action_tb )
  206. local action = cc.Sequence.New()
  207. action:initWithTable(action_tb)
  208. return action
  209. end
  210. function cc.Sequence:initWithTable(actions)
  211. local count = #actions
  212. if (count == 0) then
  213. --进入这里也是正常的
  214. return
  215. end
  216. if (count == 1) then
  217. return self:initWithTwoActions(actions[1], cc.ExtraAction.New());
  218. end
  219. local prev = actions[1]
  220. for i=2,#actions-1 do
  221. prev = cc.Sequence.createWithTwoActions(prev, actions[i])
  222. end
  223. self:initWithTwoActions(prev, actions[count]);
  224. end
  225. function cc.Sequence:initWithTwoActions(actionOne, actionTwo)
  226. local d = actionOne:getDuration() + actionTwo:getDuration()
  227. cc.ActionInterval.initWithDuration(self, d)
  228. self._actions[0] = actionOne
  229. self._actions[1] = actionTwo
  230. return true
  231. end
  232. function cc.Sequence:clone()
  233. local a = cc.Sequence.New()
  234. a:initWithTwoActions(self._actions[0]:clone(), self._actions[1]:clone() )
  235. return a
  236. end
  237. function cc.Sequence:startWithTarget(target)
  238. cc.ActionInterval.startWithTarget(self, target)
  239. self._split = self._actions[0]:getDuration() / self._duration
  240. self._last = -1
  241. end
  242. function cc.Sequence:stop()
  243. -- Issue #1305
  244. if ( self._last ~= - 1) then
  245. self._actions[self._last]:stop()
  246. end
  247. cc.ActionInterval.stop(self)
  248. end
  249. function cc.Sequence:update(t)
  250. local found = 0
  251. local new_t = 0.0
  252. if( t < self._split ) then
  253. found = 0
  254. if( self._split ~= 0 ) then
  255. new_t = t / self._split
  256. else
  257. new_t = 1
  258. end
  259. else
  260. found = 1;
  261. if ( self._split == 1 ) then
  262. new_t = 1;
  263. else
  264. new_t = (t-self._split) / (1 - self._split );
  265. end
  266. end
  267. if ( found==1 ) then
  268. if( self._last == -1 ) then
  269. -- action[0] was skipped, execute it.
  270. self._actions[0]:startWithTarget(self._target);
  271. self._actions[0]:update(1.0)
  272. self._actions[0]:stop()
  273. elseif( self._last == 0 ) then
  274. -- switching to action 1. stop action 0.
  275. self._actions[0]:update(1.0)
  276. self._actions[0]:stop()
  277. end
  278. elseif (found==0 and self._last==1 ) then
  279. self._actions[1]:update(0);
  280. self._actions[1]:stop();
  281. end
  282. -- Last action found and it is done.
  283. if( found == self._last and self._actions[found]:isDone() ) then
  284. return
  285. end
  286. -- Last action found and it is done
  287. if( found ~= self._last ) then
  288. self._actions[found]:startWithTarget(self._target);
  289. end
  290. self._actions[found]:update(new_t);
  291. self._last = found;
  292. end
  293. function cc.Sequence:reverse()
  294. return cc.Sequence.createWithTwoActions(self._actions[1]:reverse(), self._actions[0]:reverse())
  295. end
  296. --Sequence end
  297. --ScaleTo start
  298. cc.ScaleTo = cc.ScaleTo or BaseClass(cc.ActionInterval)
  299. function cc.ScaleTo:__init(duration, sx, sy, sz)
  300. self:initWithDuration(duration, sx, sy, sz);
  301. end
  302. function cc.ScaleTo:initWithDuration(duration, sx, sy, sz)
  303. cc.ActionInterval.initWithDuration(self, duration)
  304. self._endScaleX = sx or 1
  305. self._endScaleY = sy or 1
  306. self._endScaleZ = sz or 1
  307. end
  308. function cc.ScaleTo:clone()
  309. return ScaleTo.New(self._duration, self._endScaleX, self._endScaleY, self._endScaleZ)
  310. end
  311. function cc.ScaleTo:reverse()
  312. print("reverse() not supported in ScaleTo")
  313. return nil
  314. end
  315. function cc.ScaleTo:startWithTarget(target)
  316. cc.ActionInterval.startWithTarget(self, target);
  317. self._startScaleX, self._startScaleY, self._startScaleZ = self._target.localScale.x,self._target.localScale.y,self._target.localScale.z
  318. self._deltaX = self._endScaleX - self._startScaleX;
  319. self._deltaY = self._endScaleY - self._startScaleY;
  320. self._deltaZ = self._endScaleZ - self._startScaleZ;
  321. end
  322. function cc.ScaleTo:update(time)
  323. if self._target then
  324. self._target.localScale = Vector3(self._startScaleX + self._deltaX * time, self._startScaleY + self._deltaY * time, self._startScaleZ + self._deltaZ * time)
  325. end
  326. end
  327. --ScaleTo end
  328. --Fade start
  329. cc.FadeTo = cc.FadeTo or BaseClass(cc.ActionInterval)
  330. function cc.FadeTo:__init(duration, opacity, isAllChildren)
  331. self:initWithDuration(duration, opacity, isAllChildren)
  332. end
  333. function cc.FadeTo:initWithDuration(duration, opacity, isAllChildren)
  334. cc.ActionInterval.initWithDuration(self, duration)
  335. self._toOpacity = opacity
  336. self._isAllChildren = isAllChildren
  337. end
  338. function cc.FadeTo:clone()
  339. return FadeTo.New(self._duration, self._toOpacity, self._isAllChildren)
  340. end
  341. function cc.FadeTo:reverse()
  342. print("reverse() not supported in FadeTo");
  343. return nil;
  344. end
  345. function cc.FadeTo:startWithTarget(target)
  346. cc.ActionInterval.startWithTarget(self, target)
  347. self._fromOpacity = self._fromOpacity or cc.Wrapper.GetAlpha(self._target)
  348. end
  349. function cc.FadeTo:setCurOpacity( alpha )
  350. self._fromOpacity = alpha
  351. end
  352. -- function cc.FadeTo:initChildrenList( )
  353. -- if not self.childrenList then
  354. -- self.childrenList = self._target:GetComponentsInChildren(typeof(UnityEngine.RectTransform))
  355. -- end
  356. -- end
  357. function cc.FadeTo:update(time)
  358. if self._target then
  359. local newOpacity = (self._fromOpacity + (self._toOpacity - self._fromOpacity) * time)
  360. if self._isAllChildren then
  361. cc.Wrapper.SetChildrenAlpha(self._target, newOpacity, self)
  362. -- self:initChildrenList()
  363. -- if not self.childrenList then return end
  364. -- for i=0,self.childrenList.Length-1 do
  365. -- local child = self.childrenList[i]
  366. -- cc.Wrapper.SetAlpha(child, newOpacity)
  367. -- end
  368. else
  369. cc.Wrapper.SetAlpha(self._target, newOpacity)
  370. end
  371. end
  372. end
  373. cc.FadeIn = cc.FadeIn or BaseClass(cc.FadeTo)
  374. function cc.FadeIn:__init(d)
  375. self:initWithDuration(d,1.0);
  376. end
  377. function cc.FadeIn:clone()
  378. return cc.FadeIn.New(self._duration)
  379. end
  380. function cc.FadeIn:reverse()
  381. return cc.FadeOut.New(self._duration)
  382. end
  383. function cc.FadeIn:startWithTarget(target)
  384. cc.ActionInterval.startWithTarget(self, target)
  385. self._toOpacity = 1.0
  386. self._fromOpacity = cc.Wrapper.GetAlpha(self._target)
  387. end
  388. cc.FadeOut = cc.FadeOut or BaseClass(cc.FadeTo)
  389. function cc.FadeOut:__init(d)
  390. self:initWithDuration(d,0.0)
  391. end
  392. function cc.FadeOut:clone()
  393. return cc.FadeOut.New(self._duration,0.0);
  394. end
  395. function cc.FadeOut:startWithTarget(target)
  396. cc.ActionInterval.startWithTarget(self, target);
  397. self._toOpacity = 0.0
  398. self._fromOpacity = cc.Wrapper.GetAlpha(self._target)
  399. end
  400. function cc.FadeOut:reverse()
  401. return cc.FadeIn.New(self._duration)
  402. end
  403. --Fade end
  404. --Rotate start
  405. --huangcong可以用了rotate_type旋转轴
  406. --[rotate_type == 1 z轴]
  407. --[rotate_type == 2 x轴]
  408. --[rotate_type == 3 y轴]
  409. cc.RotateTo = cc.RotateTo or BaseClass(cc.ActionInterval)
  410. function cc.RotateTo:__init(duration, dstAngle, rotate_type)--0.1, targetRotate
  411. self._dstAngle = 0
  412. self._startAngle = 0
  413. self._diffAngle = 0
  414. self.rotate_type = rotate_type or 1
  415. self:initWithDuration(duration, dstAngle)
  416. end
  417. function cc.RotateTo:initWithDuration(duration, dstAngle)
  418. cc.ActionInterval.initWithDuration(self, duration)
  419. self._dstAngle = dstAngle
  420. end
  421. function cc.RotateTo:clone()
  422. return cc.RotateTo.New(self._duration, self._dstAngle)
  423. end
  424. function cc.RotateTo:calculateAngles(startAngle, diffAngle, dstAngle)
  425. if (startAngle > 0) then
  426. startAngle = math_fmod(startAngle, 360.0)
  427. else
  428. startAngle = math_fmod(startAngle, -360.0)
  429. end
  430. diffAngle = dstAngle - startAngle
  431. if (diffAngle > 180) then
  432. diffAngle = diffAngle - 360
  433. end
  434. if (diffAngle < -180) then
  435. diffAngle = diffAngle + 360
  436. end
  437. return startAngle, diffAngle
  438. end
  439. function cc.RotateTo:startWithTarget(target)
  440. cc.ActionInterval.startWithTarget(self, target)
  441. self._startAngle = target.eulerAngles
  442. if self.rotate_type == 1 then
  443. self._startAngle_num, self._diffAngle = self:calculateAngles(self._startAngle.z, self._diffAngle, self._dstAngle)
  444. elseif self.rotate_type == 2 then
  445. self._startAngle_num, self._diffAngle = self:calculateAngles(self._startAngle.x, self._diffAngle, self._dstAngle)
  446. elseif self.rotate_type == 3 then
  447. self._startAngle_num, self._diffAngle = self:calculateAngles(self._startAngle.y, self._diffAngle, self._dstAngle)
  448. end
  449. end
  450. function cc.RotateTo:update(time)
  451. if (self._target) then
  452. local newRotation = self._startAngle_num + self._diffAngle * time
  453. if self.rotate_type == 1 then--Z
  454. self._target.localRotation = Quaternion.Euler(0,0,newRotation)
  455. elseif self.rotate_type == 2 then--X
  456. self._target.localRotation = Quaternion.Euler(newRotation,0,0)
  457. elseif self.rotate_type == 3 then--Y
  458. self._target.localRotation = Quaternion.Euler(0,newRotation,0)
  459. end
  460. end
  461. end
  462. function cc.RotateTo:reverse()
  463. print("RotateTo doesn't support the 'reverse' method")
  464. return nil
  465. end
  466. --huangcong可以用了rotate_type旋转轴
  467. --[rotate_type == 1 z轴]
  468. --[rotate_type == 2 x轴]
  469. --[rotate_type == 3 y轴]
  470. cc.RotateBy = cc.RotateBy or BaseClass(cc.ActionInterval)
  471. function cc.RotateBy:__init(duration, deltaAngle, rotate_type)
  472. self._deltaAngle = 0
  473. self._startAngle = 0
  474. self:initWithDuration(duration, deltaAngle)
  475. self.rotate_type = rotate_type or 1
  476. end
  477. function cc.RotateBy:initWithDuration(duration, deltaAngle)
  478. cc.ActionInterval.initWithDuration(self, duration)
  479. self._deltaAngle = deltaAngle
  480. end
  481. function cc.RotateBy:clone()
  482. return cc.RotateBy.New(self._duration, self._deltaAngle)
  483. end
  484. function cc.RotateBy:startWithTarget(target)
  485. cc.ActionInterval.startWithTarget(self, target)
  486. self._startAngle = target.eulerAngles
  487. end
  488. function cc.RotateBy:update(time)
  489. if (self._target) then
  490. if self.rotate_type == 1 then--Z轴
  491. self._startAngle_num = self._startAngle.z
  492. local newRotation = self._startAngle_num + self._deltaAngle * time
  493. self._target.localRotation = Quaternion.Euler(0,0,newRotation)
  494. elseif self.rotate_type == 2 then--X轴
  495. self._startAngle_num = self._startAngle.x
  496. local newRotation = self._startAngle_num + self._deltaAngle * time
  497. self._target.localRotation = Quaternion.Euler(newRotation,0,0)
  498. elseif self.rotate_type == 3 then--Y轴
  499. self._startAngle_num = self._startAngle.y
  500. local newRotation = self._startAngle_num + self._deltaAngle * time
  501. self._target.localRotation = Quaternion.Euler(0,newRotation,0)
  502. end
  503. end
  504. end
  505. function cc.RotateBy:reverse()
  506. return cc.RotateBy.New(self._duration, -self._deltaAngle, self.rotate_type)
  507. end
  508. -- Rotate end
  509. --Repeat start
  510. cc.Repeat = cc.Repeat or BaseClass(cc.ActionInterval)
  511. function cc.Repeat:__init(action, times)
  512. self:initWithAction(action, times)
  513. end
  514. function cc.Repeat:initWithAction(action, times)
  515. local d = action:getDuration() * times
  516. cc.ActionInterval.initWithDuration(self, d)
  517. self._times = times;
  518. self._innerAction = action;
  519. self._actionInstant = action._classType and action._classType == "ActionInstant" or false
  520. self._total = 0;
  521. end
  522. function cc.Repeat:clone()
  523. -- no copy constructor
  524. return cc.Repeat.New(self._innerAction:clone(), self._times)
  525. end
  526. function cc.Repeat:startWithTarget(target)
  527. self._total = 0
  528. self._nextDt = self._innerAction:getDuration()/self._duration
  529. cc.ActionInterval.startWithTarget(self, target)
  530. self._innerAction:startWithTarget(target)
  531. end
  532. function cc.Repeat:stop()
  533. self._innerAction:stop()
  534. cc.ActionInterval.stop(self)
  535. end
  536. -- issue #80. Instead of hooking step:, hook update: since it can be called by any
  537. -- container action like Repeat, Sequence, Ease, etc..
  538. function cc.Repeat:update(dt)
  539. if (dt >= self._nextDt) then
  540. while (dt >= self._nextDt and self._total < self._times) do
  541. self._innerAction:update(1.0)
  542. self._total = self._total + 1
  543. self._innerAction:stop();
  544. self._innerAction:startWithTarget(self._target)
  545. self._nextDt = self._innerAction:getDuration()/self._duration * (self._total+1)
  546. end
  547. -- fix for issue #1288, incorrect end value of repeat
  548. if((dt - 1.0) < cc.ActionInterval.FLT_EPSILON and (dt - 1.0) > -cc.ActionInterval.FLT_EPSILON and self._total < self._times) then
  549. self._innerAction:update(1.0);
  550. self._total = self._total + 1
  551. end
  552. -- don't set an instant action back or update it, it has no use because it has no duration
  553. if (not self._actionInstant) then
  554. if (self._total == self._times) then
  555. -- minggo: inner action update is invoked above, don't have to invoke it here
  556. -- self._innerAction:update(1);
  557. self._innerAction:stop()
  558. else
  559. -- issue #390 prevent jerk, use right update
  560. self._innerAction:update(dt - (self._nextDt - self._innerAction:getDuration()/self._duration))
  561. end
  562. end
  563. else
  564. self._innerAction:update(math_fmod(dt * self._times,1.0))
  565. end
  566. end
  567. function cc.Repeat:isDone()
  568. return self._total == self._times
  569. end
  570. function cc.Repeat:reverse()
  571. return cc.Repeat.New(self._innerAction:reverse(), self._times)
  572. end
  573. cc.RepeatForever = cc.RepeatForever or BaseClass(cc.ActionInterval)
  574. function cc.RepeatForever:__init(action)
  575. self:initWithAction(action)
  576. end
  577. function cc.RepeatForever:initWithAction(action)
  578. self._innerAction = action
  579. end
  580. function cc.RepeatForever:clone()
  581. return cc.RepeatForever.New(self._innerAction:clone())
  582. end
  583. function cc.RepeatForever:startWithTarget(target)
  584. cc.ActionInterval.startWithTarget(self, target)
  585. self._innerAction:startWithTarget(target)
  586. end
  587. function cc.RepeatForever:step(dt)
  588. self._innerAction:step(dt);
  589. if (self._innerAction:isDone()) then
  590. local diff = self._innerAction:getElapsed() - self._innerAction:getDuration()
  591. if (diff > self._innerAction:getDuration()) then
  592. diff = math_fmod(diff, self._innerAction:getDuration())
  593. end
  594. self._innerAction:startWithTarget(self._target)
  595. -- to prevent jerk. issue #390, 1247
  596. self._innerAction:step(0.0);
  597. self._innerAction:step(diff);
  598. end
  599. end
  600. function cc.RepeatForever:isDone()
  601. return false
  602. end
  603. function cc.RepeatForever:reverse()
  604. return cc.RepeatForever.New(self._innerAction:reverse())
  605. end
  606. --Repeat end
  607. --Spawn start
  608. cc.Spawn = cc.Spawn or BaseClass(cc.ActionInterval)
  609. function cc.Spawn:__init(...)
  610. self._actions = {}
  611. self:initWithTable({...})
  612. end
  613. function cc.Spawn.createWithTwoActions(actionOne, actionTwo)
  614. local Spawn = cc.Spawn.New()
  615. Spawn:initWithTwoActions(actionOne, actionTwo);
  616. return Spawn
  617. end
  618. function cc.Spawn:initWithTable(actions)
  619. local count = #actions
  620. if (count == 0) then
  621. --进入这里也是正常的
  622. return
  623. end
  624. if (count == 1) then
  625. return initWithTwoActions(actions[1], cc.ExtraAction.New());
  626. end
  627. local prev = actions[1]
  628. for i=2,#actions-1 do
  629. prev = cc.Spawn.createWithTwoActions(prev, actions[i])
  630. end
  631. self:initWithTwoActions(prev, actions[count]);
  632. end
  633. function cc.Spawn:initWithTwoActions(actionOne, actionTwo)
  634. local d1 = actionOne:getDuration()
  635. local d2 = actionTwo:getDuration()
  636. local d = math_max(d1 , d2)
  637. cc.ActionInterval.initWithDuration(self, d)
  638. self._one = actionOne
  639. self._two = actionTwo
  640. if (d1 > d2) then
  641. self._two = cc.Sequence.New(actionTwo, cc.DelayTime.New(d1 - d2));
  642. elseif (d1 < d2) then
  643. self._one = cc.Sequence.New(actionOne, cc.DelayTime.New(d2 - d1));
  644. end
  645. end
  646. function cc.Spawn:clone()
  647. local a = cc.Spawn.New()
  648. a:initWithTwoActions(self._one:clone(), self._two:clone() )
  649. return a
  650. end
  651. function cc.Spawn:startWithTarget(target)
  652. cc.ActionInterval.startWithTarget(self, target)
  653. self._one:startWithTarget(target)
  654. self._two:startWithTarget(target)
  655. end
  656. function cc.Spawn:stop()
  657. self._one:stop()
  658. self._two:stop()
  659. cc.ActionInterval.stop(self)
  660. end
  661. function cc.Spawn:update(time)
  662. if (self._one) then
  663. self._one:update(time)
  664. end
  665. if (self._two) then
  666. self._two:update(time)
  667. end
  668. end
  669. function cc.Spawn:reverse()
  670. return cc.Spawn.New(self._one:reverse(), self._two:reverse())
  671. end
  672. --Spawn end
  673. --DelayTime start
  674. cc.DelayTime = cc.DelayTime or BaseClass(cc.ActionInterval)
  675. function cc.DelayTime:__init(d)
  676. self:initWithDuration(d);
  677. end
  678. function cc.DelayTime:clone()
  679. return cc.DelayTime.New(self._duration)
  680. end
  681. function cc.DelayTime:update(time)
  682. --什么都不干
  683. end
  684. function cc.DelayTime:reverse()
  685. return cc.DelayTime.New(self._duration)
  686. end
  687. --DelayTime end
  688. --SizeBy start
  689. cc.SizeBy = cc.SizeBy or BaseClass(cc.ActionInterval)
  690. function cc.SizeBy:__init(duration, delta_w, delta_h)
  691. self:initWithDuration(duration, delta_w, delta_h)
  692. end
  693. function cc.SizeBy:clone()
  694. return cc.SizeBy.New(self._duration, self._SizeDeltaW, self._SizeDeltaH)
  695. end
  696. function cc.SizeBy:reverse()
  697. return cc.SizeBy.New(self._duration, -self._SizeDeltaW, -self._SizeDeltaH)
  698. end
  699. function cc.SizeBy:startWithTarget(target)
  700. cc.ActionInterval.startWithTarget(self, target)
  701. self._previousSizeWidht,self._previousSizeHeight = GetSizeDeltaXY(self._target)
  702. self._startSizeX,self._startSizeY = self._previousSizeWidht,self._previousSizeHeight
  703. end
  704. function cc.SizeBy:update(t)
  705. if self._target then
  706. local currentW,currentH = GetSizeDeltaXY(self._target)
  707. local diffX = currentW - self._previousSizeWidht
  708. local diffY = currentH - self._previousSizeHeight
  709. local newSizeW = self._startSizeX + (self._SizeDeltaW * t)
  710. local newSizeH = self._startSizeY + (self._SizeDeltaH * t)
  711. -- self._target:SetVectorValue(WidgetProperty.Size,newSizeW,newSizeH)
  712. SetSizeDelta(self._target, newSizeW, newSizeH)
  713. self._previousSizeWidht = newSizeW
  714. self._previousSizeHeight = newSizeH
  715. end
  716. end
  717. function cc.SizeBy:initWithDuration(duration, delta_w, delta_h)
  718. cc.ActionInterval.initWithDuration(self,duration)
  719. self._SizeDeltaW = delta_w
  720. self._SizeDeltaH = delta_h
  721. end
  722. --SizeBy end
  723. --SizeTo start
  724. cc.SizeTo = cc.SizeTo or BaseClass(cc.SizeBy)
  725. function cc.SizeTo:__init(duration, w, h)
  726. self:initWithSize(duration, w, h)
  727. end
  728. function cc.SizeTo:initWithSize(duration, w, h)
  729. cc.ActionInterval.initWithDuration(self, duration)
  730. self._endSizeW = w
  731. self._endSizeH = h
  732. end
  733. function cc.SizeTo:clone()
  734. return cc.SizeTo.New(self._duration, self._endSizeW, self._endSizeH)
  735. end
  736. function cc.SizeTo:startWithTarget(target)
  737. cc.SizeBy.startWithTarget(self, target)
  738. local oldW = self._target.sizeDelta.x
  739. local oldH = self._target.sizeDelta.y
  740. self._SizeDeltaW = self._endSizeW - oldW
  741. self._SizeDeltaH = self._endSizeH - oldH
  742. end
  743. function cc.SizeTo:reverse()
  744. print("reverse() not supported in SizeTo")
  745. return nil
  746. end
  747. --SizeTo end
  748. -- Bezier cubic formula:
  749. -- ((1 - t) + t)3 = 1
  750. -- Expands to ...
  751. -- (1 - t)3 + 3t(1-t)2 + 3t2(1 - t) + t3 = 1
  752. function cc.bezierat( a, b, c, d, t )
  753. return (math_pow(1-t,3) * a +
  754. 3*t*(math_pow(1-t,2))*b +
  755. 3*math_pow(t,2)*(1-t)*c +
  756. math_pow(t,3)*d )
  757. end
  758. -- BezierBy start
  759. cc.BezierBy = cc.BezierBy or BaseClass(cc.ActionInterval)
  760. --t为动作时间,c为控制点信息,比如 {end_pos={x=0,y=0},control_1={x=1,y=1},control_2={x=2,y=2}}
  761. function cc.BezierBy:__init(t, c)
  762. self:initWithDuration(t, c)
  763. end
  764. function cc.BezierBy:initWithDuration(t, c)
  765. cc.ActionInterval.initWithDuration(self, t)
  766. self._config = c
  767. end
  768. function cc.BezierBy:startWithTarget(target)
  769. cc.ActionInterval.startWithTarget(self, target)
  770. local x, y = cc.Wrapper.GetLocalPosition(self._target)
  771. self._startPosition = {x=x, y = y}
  772. self._previousPosition = {x=x, y = y}
  773. end
  774. function cc.BezierBy:clone()
  775. return cc.BezierBy.New(self._duration, self._config)
  776. end
  777. function cc.BezierBy:update(time)
  778. if (self._target) then
  779. local xa = 0;
  780. local xb = self._config.control_1.x;
  781. local xc = self._config.control_2.x;
  782. local xd = self._config.end_pos.x;
  783. local ya = 0;
  784. local yb = self._config.control_1.y;
  785. local yc = self._config.control_2.y;
  786. local yd = self._config.end_pos.y;
  787. local x = cc.bezierat(xa, xb, xc, xd, time);
  788. local y = cc.bezierat(ya, yb, yc, yd, time);
  789. -- #if CC_ENABLE_STACKABLE_ACTIONS
  790. -- Vec2 currentPos = _target->getPosition();
  791. -- Vec2 diff = currentPos - _previousPosition;
  792. -- _startPosition = _startPosition + diff;
  793. -- Vec2 newPos = _startPosition + Vec2(x,y);
  794. -- _target->setPosition(newPos);
  795. -- _previousPosition = newPos;
  796. -- #else
  797. cc.Wrapper.SetLocalPosition(self._target, self._startPosition.x+x, self._startPosition.y+y)
  798. -- #endif // !CC_ENABLE_STACKABLE_ACTIONS
  799. end
  800. end
  801. function cc.BezierBy:reverse()
  802. local r = {}
  803. r.end_pos = {x=-self._config.end_pos.x, y=-self._config.end_pos.y}
  804. r.control_1 = {x=0, y=0}
  805. r.control_1.x = self._config.control_2.x - self._config.end_pos.x
  806. r.control_1.y = self._config.control_2.y - self._config.end_pos.y
  807. r.control_2 = {x=0, y=0}
  808. r.control_2.x = self._config.control_1.x - self._config.end_pos.x
  809. r.control_2.y = self._config.control_1.y - self._config.end_pos.y
  810. return cc.BezierBy.New(self._duration, r)
  811. end
  812. -- BezierTo start
  813. cc.BezierTo = cc.BezierTo or BaseClass(cc.BezierBy)
  814. function cc.BezierTo:__init(t, c)
  815. self:initWithDuration(t, c)
  816. end
  817. function cc.BezierTo:initWithDuration(t, c)
  818. cc.ActionInterval.initWithDuration(self, t)
  819. self._toConfig = c
  820. end
  821. function cc.BezierTo:clone()
  822. return cc.BezierTo.New(self._duration, self._toConfig)
  823. end
  824. function cc.BezierTo:startWithTarget(target)
  825. cc.BezierBy.startWithTarget(self, target)
  826. self._config = {}
  827. self._config.control_1 = {x=0,y=0}
  828. self._config.control_1.x = self._toConfig.control_1.x - self._startPosition.x
  829. self._config.control_1.y = self._toConfig.control_1.y - self._startPosition.y
  830. self._config.control_2 = {x=0,y=0}
  831. self._config.control_2.x = self._toConfig.control_2.x - self._startPosition.x
  832. self._config.control_2.y = self._toConfig.control_2.y - self._startPosition.y
  833. self._config.end_pos = {x=0,y=0}
  834. self._config.end_pos.x = self._toConfig.end_pos.x - self._startPosition.x
  835. self._config.end_pos.y = self._toConfig.end_pos.y - self._startPosition.y
  836. end
  837. function cc.BezierTo:reverse()
  838. return nil
  839. end
  840. --DelayTime end
  841. --cc.Animation
  842. cc.Animation = cc.Animation or BaseClass(cc.ActionInterval)
  843. function cc.Animation:__init(data,duration)
  844. self:initWithAnimationData(data,duration)
  845. end
  846. function cc.Animation:initWithAnimationData( data,duration)
  847. self.total_duration = 0
  848. self.TotalDelayUnits = 0
  849. self.cur_frame = 1
  850. for i,v in ipairs(data) do
  851. self.TotalDelayUnits = self.TotalDelayUnits + v.unit
  852. end
  853. self.total_duration = self.TotalDelayUnits * duration
  854. self.splitTime = {}
  855. local accumUnitsOfTime = 0
  856. local newUnitOfTimeValue = self.total_duration / self.TotalDelayUnits
  857. for i,v in ipairs(data) do
  858. local value = (accumUnitsOfTime * newUnitOfTimeValue) / self.total_duration
  859. accumUnitsOfTime = accumUnitsOfTime + v.unit
  860. table.insert(self.splitTime,value)
  861. end
  862. cc.ActionInterval.initWithDuration(self, self.total_duration)
  863. self.data = data
  864. end
  865. function cc.Animation:startWithTarget(target)
  866. cc.ActionInterval.startWithTarget(self, target)
  867. self.img = self._target:GetComponent("Image")
  868. self.cur_frame = 1
  869. end
  870. function cc.Animation:update(t)
  871. if self._target then
  872. for i= self.cur_frame,#self.data do
  873. if self.splitTime[i] <= t then
  874. local index = i
  875. if self.data[index].not_auto_size then
  876. lua_resM:setImageSprite(self, self.img,self.data[index].ab, self.data[index].res,false)
  877. else
  878. lua_resM:setImageSprite(self, self.img,self.data[index].ab, self.data[index].res,true)
  879. end
  880. if self.data[index] and self.data[index].callback then
  881. local callback = self.data[index].callback
  882. callback()
  883. end
  884. self.cur_frame = i + 1
  885. else
  886. break
  887. end
  888. end
  889. end
  890. end
  891. function cc.Animation:reverse()
  892. return nil
  893. end