源战役客户端
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

306 righe
9.2 KiB

4 settimane fa
  1. cc = cc or {}
  2. --Cat_Todo : 本文件暂时没用,所以没测试过的
  3. local DeepCopy = function ( object )
  4. local lookup_table = {}
  5. local function _copy(object)
  6. if type(object) ~= "table" then
  7. return object
  8. elseif lookup_table[object] then
  9. return lookup_table[object]
  10. end
  11. local new_table = {}
  12. lookup_table[object] = new_table
  13. for index, value in pairs(object) do
  14. new_table[_copy(index)] = _copy(value)
  15. end
  16. return setmetatable(new_table, getmetatable(object))
  17. end
  18. return _copy(object)
  19. end
  20. cc.PointArray = cc.PointArray or BaseClass()
  21. function cc.PointArray:__init( points )
  22. self._controlPoints = points
  23. end
  24. function cc.PointArray:clone()
  25. return DeepCopy(self)
  26. end
  27. function cc.PointArray:getControlPoints()
  28. return _controlPoints;
  29. end
  30. function cc.PointArray:setControlPoints(controlPoints)
  31. self._controlPoints = controlPoints
  32. end
  33. function cc.PointArray:addControlPoint(controlPoint)
  34. table.insert( self._controlPoints, controlPoint )
  35. end
  36. function cc.PointArray:insertControlPoint(controlPoint, index)
  37. -- self._controlPoints[index] = controlPoint
  38. table.insert( self._controlPoints, index, controlPoint )
  39. end
  40. function cc.PointArray:getControlPointAtIndex(index)
  41. index = math.floor( index )
  42. local count = 0
  43. if self._controlPoints then
  44. count = #self._controlPoints
  45. end
  46. if index > count then
  47. index = count
  48. end
  49. if index <= 0 then
  50. index = 1
  51. end
  52. -- index = math.min(count, index)
  53. -- index = index>=1 and index or 1
  54. -- print("Cat:ActionCatmullRom.lua [34][start] self._controlPoints", self._controlPoints,index)
  55. -- PrintTable(self._controlPoints)
  56. -- print("Cat:ActionCatmullRom.lua [34][end]")
  57. return self._controlPoints[index]
  58. end
  59. function cc.PointArray:replaceControlPoint(controlPoint, index)
  60. self._controlPoints[index] = controlPoint
  61. end
  62. function cc.PointArray:removeControlPointAtIndex(index)
  63. if self._controlPoints[index] then
  64. table.remove( self._controlPoints, index )
  65. end
  66. end
  67. function cc.PointArray:count()
  68. return #self._controlPoints
  69. end
  70. function cc.PointArray:reverse()
  71. local newArray = {}
  72. for i=#self._controlPoints,1,-1 do
  73. table.insert( newArray, self._controlPoints[i])
  74. end
  75. local config = cc.PointArray.New()
  76. config:setControlPoints(newArray);
  77. return config;
  78. end
  79. -- function cc.PointArray:reverseInline()
  80. -- {
  81. -- size_t l = _controlPoints->size();
  82. -- Vec2 *p1 = nullptr;
  83. -- Vec2 *p2 = nullptr;
  84. -- float x, y;
  85. -- for (size_t i = 0; i < l/2; ++i)
  86. -- {
  87. -- p1 = _controlPoints->at(i);
  88. -- p2 = _controlPoints->at(l-i-1);
  89. -- x = p1->x;
  90. -- y = p1->y;
  91. -- p1->x = p2->x;
  92. -- p1->y = p2->y;
  93. -- p2->x = x;
  94. -- p2->y = y;
  95. -- }
  96. -- }
  97. -- CatmullRom Spline formula:
  98. function cc.CardinalSplineAt(p0, p1, p2, p3, tension, t)
  99. local t2 = t * t
  100. local t3 = t2 * t
  101. --Formula: s(-ttt + 2tt - t)P1 + s(-ttt + tt)P2 + (2ttt - 3tt + 1)P2 + s(ttt - 2tt + t)P3 + (-2ttt + 3tt)P3 + s(ttt - tt)P4
  102. local s = (1 - tension) / 2
  103. local b1 = s * ((-t3 + (2 * t2)) - t); -- s(-t3 + 2 t2 - t)P1
  104. local b2 = s * (-t3 + t2) + (2 * t3 - 3 * t2 + 1); -- s(-t3 + t2)P2 + (2 t3 - 3 t2 + 1)P2
  105. local b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); -- s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3
  106. local b4 = s * (t3 - t2); -- s(t3 - t2)P4
  107. local x = (p0.x*b1 + p1.x*b2 + p2.x*b3 + p3.x*b4);
  108. local y = (p0.y*b1 + p1.y*b2 + p2.y*b3 + p3.y*b4);
  109. return {x=x, y=y}
  110. end
  111. --points是控制点列表,tension是松紧程度。tension==1时,样条线是分段直线。tension<1向外松弛弯曲,tension>1向内缩紧弯曲。By动作是以当前坐标为新坐标原点
  112. cc.CardinalSplineTo = cc.CardinalSplineTo or BaseClass(cc.ActionInterval)
  113. function cc.CardinalSplineTo:__init(duration, points, tension)
  114. self:initWithDuration(duration, points, tension)
  115. end
  116. function cc.CardinalSplineTo:initWithDuration(duration, points, tension)
  117. self._deltaT = 0.0
  118. -- self._tension = 0.0
  119. -- CCASSERT(points->count() > 0, "Invalid configuration. It must at least have one control point");
  120. cc.ActionInterval.initWithDuration(self, duration)
  121. self._points = cc.PointArray.New(points)
  122. self._tension = tension
  123. end
  124. function cc.CardinalSplineTo:startWithTarget(target)
  125. cc.ActionInterval.startWithTarget(self, target);
  126. -- _deltaT = (float) 1 / _points->count();
  127. self._deltaT = 1 / (self._points:count() - 1);
  128. self._previousPosition = {}
  129. self._previousPosition.x, self._previousPosition.y, self._previousPosition.z = cc.Wrapper.GetLocalPosition(target)
  130. self._accumulatedDiff = {x=0, y=0}
  131. end
  132. function cc.CardinalSplineTo:clone()
  133. local a = cc.CardinalSplineTo.New()
  134. a:initWithDuration(self._duration, self._points:clone(), self._tension)
  135. return a
  136. end
  137. function cc.CardinalSplineTo:update(time)
  138. local p;
  139. local lt;
  140. -- eg.
  141. -- p..p..p..p..p..p..p
  142. -- 1..2..3..4..5..6..7
  143. -- want p to be 1, 2, 3, 4, 5, 6
  144. if (time == 1) then
  145. p = self._points:count()-1
  146. lt = 1;
  147. else
  148. p = math.floor(time / self._deltaT)
  149. lt = (time - self._deltaT * p) / self._deltaT
  150. end
  151. -- print("Cat:ActionCatmullRom.lua [150] time,p,lt:", time,p,lt)
  152. -- Interpolate
  153. local addition = 0
  154. local pp0 = DeepCopy(self._points:getControlPointAtIndex(p+0+addition))
  155. local pp1 = DeepCopy(self._points:getControlPointAtIndex(p+1+addition))
  156. local pp2 = DeepCopy(self._points:getControlPointAtIndex(p+2+addition))
  157. local pp3 = DeepCopy(self._points:getControlPointAtIndex(p+3+addition))
  158. print("Cat:ActionCatmullRom [166] pp0, pp1, pp2, pp3: ", pp0.x, pp0.y, pp1.x, pp1.y, pp2.x, pp2.y, pp3.x, pp3.y, p)
  159. pp0.y = 720-pp0.y
  160. pp1.y = 720-pp1.y
  161. pp2.y = 720-pp2.y
  162. pp3.y = 720-pp3.y
  163. local newPos = cc.CardinalSplineAt(pp0, pp1, pp2, pp3, self._tension, lt)
  164. newPos.y = 720-newPos.y
  165. -- print("Cat:ActionCatmullRom.lua [157] newPos.x,newPos.y:", newPos.x,newPos.y, lt)
  166. -- #if CC_ENABLE_STACKABLE_ACTIONS
  167. -- -- Support for stacked actions
  168. -- Node *node = _target;
  169. -- local diff = node->getPosition() - _previousPosition;
  170. -- if( diff.x !=0 || diff.y != 0 )
  171. -- _accumulatedDiff = _accumulatedDiff + diff;
  172. -- newPos = newPos + _accumulatedDiff;
  173. -- end
  174. -- #endif
  175. self:updatePosition(newPos)
  176. end
  177. function cc.CardinalSplineTo:updatePosition(newPos)
  178. if self._target.SetVectorValue then
  179. self._target:SetVectorValue(WidgetProperty.Position, newPos.x, newPos.y)
  180. elseif self._target.setPosition then
  181. self._target:setPosition(newPos.x, newPos.y)
  182. end
  183. self._previousPosition = newPos
  184. end
  185. function cc.CardinalSplineTo:reverse()
  186. local pReverse = self._points:reverse()
  187. return cc.CardinalSplineTo.New(self._duration, pReverse, self._tension)
  188. end
  189. -- function cc.CardinalSplineBy:create(duration, points, tension)
  190. -- CardinalSplineBy *ret = new (std:nothrow) CardinalSplineBy();
  191. -- if (ret)
  192. -- if (ret->initWithDuration(duration, points, tension))
  193. -- ret->autorelease();
  194. -- else
  195. -- CC_SAFE_RELEASE_NULL(ret);
  196. -- end
  197. -- end
  198. -- return ret;
  199. -- end
  200. -- CardinalSplineBy:CardinalSplineBy() : _startPosition(0,0)
  201. -- end
  202. -- function cc.CardinalSplineBy:updatePosition(cocos2d:Vec2 &newPos)
  203. -- Vec2 p = newPos + _startPosition;
  204. -- _target->setPosition(p);
  205. -- _previousPosition = p;
  206. -- end
  207. -- CardinalSplineBy* CardinalSplineBy:reverse() const
  208. -- PointArray *copyConfig = _points->clone();
  209. -- //
  210. -- // convert "absolutes" to "diffs"
  211. -- //
  212. -- Vec2 p = copyConfig->getControlPointAtIndex(0);
  213. -- for (ssize_t i = 1; i < copyConfig->count(); ++i)
  214. -- Vec2 current = copyConfig->getControlPointAtIndex(i);
  215. -- Vec2 diff = current - p;
  216. -- copyConfig->replaceControlPoint(diff, i);
  217. -- p = current;
  218. -- end
  219. -- // convert to "diffs" to "reverse absolute"
  220. -- PointArray *pReverse = copyConfig->reverse();
  221. -- // 1st element (which should be 0,0) should be here too
  222. -- p = pReverse->getControlPointAtIndex(pReverse->count()-1);
  223. -- pReverse->removeControlPointAtIndex(pReverse->count()-1);
  224. -- p = -p;
  225. -- pReverse->insertControlPoint(p, 0);
  226. -- for (ssize_t i = 1; i < pReverse->count(); ++i)
  227. -- Vec2 current = pReverse->getControlPointAtIndex(i);
  228. -- current = -current;
  229. -- Vec2 abs = current + p;
  230. -- pReverse->replaceControlPoint(abs, i);
  231. -- p = abs;
  232. -- end
  233. -- return CardinalSplineBy:create(_duration, pReverse, _tension);
  234. -- end
  235. -- function cc.CardinalSplineBy:startWithTarget(cocos2d:Node *target)
  236. -- CardinalSplineTo:startWithTarget(target);
  237. -- _startPosition = target->getPosition();
  238. -- end
  239. -- CardinalSplineBy* CardinalSplineBy:clone() const
  240. -- // no copy constructor
  241. -- auto a = new (std:nothrow) CardinalSplineBy();
  242. -- a->initWithDuration(this->_duration, this->_points->clone(), this->_tension);
  243. -- a->autorelease();
  244. -- return a;
  245. -- end