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.

467 line
11 KiB

  1. cc = cc or {}
  2. TimeSpan = cc.TimeSpanLog:getInstance();
  3. cc.DIRECTOR_PROJECTION_2D = 0
  4. cc.DIRECTOR_PROJECTION_3D = 1
  5. function cc.clampf(value, min_inclusive, max_inclusive)
  6. -- body
  7. local temp = 0
  8. if min_inclusive > max_inclusive then
  9. temp = min_inclusive
  10. min_inclusive = max_inclusive
  11. max_inclusive = temp
  12. end
  13. if value < min_inclusive then
  14. return min_inclusive
  15. elseif value < max_inclusive then
  16. return value
  17. else
  18. return max_inclusive
  19. end
  20. end
  21. --vec4
  22. function cc.vec4(_x,_y,_z,_w)
  23. return { x = _x, y = _y, z= _z, w= _w }
  24. end
  25. --vec3
  26. function cc.vec3(_x,_y,_z)
  27. return { x = _x, y = _y, z= _z }
  28. end
  29. function cc.vec3Add(v1, v2)
  30. return {x = v1.x + v2.x , y = v1.y + v2.y, z = v1.z + v2.z }
  31. end
  32. function cc.vec3Sub(pt1,pt2)
  33. return {x = pt1.x - pt2.x , y = pt1.y - pt2.y, z = pt1.z - pt2.z }
  34. end
  35. function cc.vec3Mul(pt1,factor)
  36. return { x = pt1.x * factor , y = pt1.y * factor , z = pt1.z * factor }
  37. end
  38. function cc.vec3GetLength(pt)
  39. return math.sqrt( pt.x * pt.x + pt.y * pt.y + pt.z * pt.z )
  40. end
  41. function cc.vec3GetDistance(startP,endP)
  42. return cc.vec3GetLength(cc.vec3Sub(startP,endP))
  43. end
  44. function cc.vec3Normalize(pt)
  45. local length = cc.vec3GetLength(pt)
  46. if 0 == length then
  47. return { x = 1.0,y = 0.0 ,z = 0.0 }
  48. end
  49. return { x = pt.x / length, y = pt.y / length, z = pt.z / length }
  50. end
  51. --Point
  52. function cc.p(_x,_y)
  53. if nil == _y then
  54. return { x = _x.x, y = _x.y }
  55. else
  56. return { x = _x, y = _y }
  57. end
  58. end
  59. function cc.pAdd(pt1,pt2)
  60. return {x = pt1.x + pt2.x , y = pt1.y + pt2.y }
  61. end
  62. -- 判读两个点是否相同
  63. function cc.pEqual(pt1, pt2)
  64. return pt1.x == pt2.x and pt1.y == pt2.y;
  65. end
  66. function cc.pSub(pt1,pt2)
  67. return {x = pt1.x - pt2.x , y = pt1.y - pt2.y }
  68. end
  69. function cc.pMul(pt1,factor)
  70. return { x = pt1.x * factor , y = pt1.y * factor }
  71. end
  72. function cc.pMidpoint(pt1,pt2)
  73. return { x = (pt1.x + pt2.x) / 2.0 , y = ( pt1.y + pt2.y) / 2.0 }
  74. end
  75. function cc.pForAngle(a)
  76. return { x = math.cos(a), y = math.sin(a) }
  77. end
  78. function cc.pGetLength(pt)
  79. return math.sqrt( pt.x * pt.x + pt.y * pt.y )
  80. end
  81. function cc.pNormalize(pt)
  82. local length = cc.pGetLength(pt)
  83. if 0 == length then
  84. return { x = 1.0,y = 0.0 }
  85. end
  86. return { x = pt.x / length, y = pt.y / length }
  87. end
  88. function cc.pCross(self,other)
  89. return self.x * other.y - self.y * other.x
  90. end
  91. function cc.pDot(self,other)
  92. return self.x * other.x + self.y * other.y
  93. end
  94. function cc.pToAngleSelf(self)
  95. return math.atan2(self.y, self.x)
  96. end
  97. function cc.pGetAngle(self,other)
  98. local a2 = cc.pNormalize(self)
  99. local b2 = cc.pNormalize(other)
  100. local angle = math.atan2(cc.pCross(a2, b2), cc.pDot(a2, b2) )
  101. if math.abs(angle) < 1.192092896e-7 then
  102. return 0.0
  103. end
  104. return angle
  105. end
  106. --[[ @def CC_DEGREES_TO_RADIANS
  107. converts degrees to radians
  108. --]]
  109. function cc.degreeToRadian(__ANGLE__) return (__ANGLE__ * 0.01745329252) end
  110. --[[ @def CC_RADIANS_TO_DEGREES
  111. converts radians to degrees
  112. --]]
  113. function cc.radianToDegree(__ANGLE__) return (__ANGLE__ * 57.29577951) end
  114. --[[返回2个2D向量的角度
  115. @param a 开始向量
  116. @param b 结束向量
  117. @return 返回a需要转动多少角度就和b重合,逆时针为正,单位角度
  118. --]]
  119. function cc.pGetDirectionRadian(a , b)
  120. local A = cc.pNormalize(a);
  121. local B = cc.pNormalize(b);
  122. local degA = math.acos(A.x);
  123. local degB = math.acos(B.x);
  124. if A.y < 0 then
  125. degA = -degA;
  126. end
  127. if B.y < 0 then
  128. degB = -degB;
  129. end
  130. return cc.radianToDegree(degB - degA);
  131. end
  132. --[[ 返回两个点之间需要指向目标的朝向旋转角度
  133. @param a 开始的点
  134. @param b 结束的点
  135. @return 返回a需要转动多少角度就会指向b,逆时针为正,单位角度
  136. --]]
  137. function cc.pGetPositionRadian(a , b)
  138. return -cc.pGetDirectionRadian(cc.p(0,1) , cc.pSub(b , a));
  139. end
  140. function cc.pGetDistance(startP,endP)
  141. return cc.pGetLength(cc.pSub(startP,endP))
  142. end
  143. function cc.pIsLineIntersect(A, B, C, D, s, t)
  144. if ((A.x == B.x) and (A.y == B.y)) or ((C.x == D.x) and (C.y == D.y))then
  145. return false, s, t
  146. end
  147. local BAx = B.x - A.x
  148. local BAy = B.y - A.y
  149. local DCx = D.x - C.x
  150. local DCy = D.y - C.y
  151. local ACx = A.x - C.x
  152. local ACy = A.y - C.y
  153. local denom = DCy * BAx - DCx * BAy
  154. s = DCx * ACy - DCy * ACx
  155. t = BAx * ACy - BAy * ACx
  156. if (denom == 0) then
  157. if (s == 0 or t == 0) then
  158. return true, s , t
  159. end
  160. return false, s, t
  161. end
  162. s = s / denom
  163. t = t / denom
  164. return true,s,t
  165. end
  166. function cc.pPerp(pt)
  167. return { x = -pt.y, y = pt.x }
  168. end
  169. function cc.RPerp(pt)
  170. return { x = pt.y, y = -pt.x }
  171. end
  172. function cc.pProject(pt1, pt2)
  173. return { x = pt2.x * (cc.pDot(pt1,pt2) / cc.pDot(pt2,pt2)) , y = pt2.y * (cc.pDot(pt1,pt2) / cc.pDot(pt2,pt2)) }
  174. end
  175. function cc.pRotate(pt1, pt2)
  176. return { x = pt1.x * pt2.x - pt1.y * pt2.y, y = pt1.x * pt2.y + pt1.y * pt2.x }
  177. end
  178. function cc.pUnrotate(pt1, pt2)
  179. return { x = pt1.x * pt2.x + pt1.y * pt2.y, pt1.y * pt2.x - pt1.x * pt2.y }
  180. end
  181. --Calculates the square length of pt
  182. function cc.pLengthSQ(pt)
  183. return cc.pDot(pt,pt)
  184. end
  185. --Calculates the square distance between pt1 and pt2
  186. function cc.pDistanceSQ(pt1,pt2)
  187. return cc.pLengthSQ(cc.pSub(pt1,pt2))
  188. end
  189. function cc.pGetClampPoint(pt1,pt2,pt3)
  190. return { x = cc.clampf(pt1.x, pt2.x, pt3.x), y = cc.clampf(pt1.y, pt2.y, pt3.y) }
  191. end
  192. function cc.pFromSize(sz)
  193. return { x = sz.width, y = sz.height }
  194. end
  195. function cc.pLerp(pt1,pt2,alpha)
  196. return cc.pAdd(cc.pMul(pt1, 1.0 - alpha), cc.pMul(pt2,alpha) )
  197. end
  198. function cc.pFuzzyEqual(pt1,pt2,variance)
  199. if (pt1.x - variance <= pt2.x) and (pt2.x <= pt1.x + variance) and (pt1.y - variance <= pt2.y) and (pt2.y <= pt1.y + variance) then
  200. return true
  201. else
  202. return false
  203. end
  204. end
  205. function cc.pRotateByAngle(pt1, pt2, angle)
  206. return cc.pAdd(pt2, cc.pRotate( cc.pSub(pt1, pt2),cc.pForAngle(angle)))
  207. end
  208. function cc.pIsSegmentIntersect(pt1,pt2,pt3,pt4)
  209. local s,t,ret = 0,0,false
  210. ret,s,t =cc.pIsLineIntersect(pt1, pt2, pt3, pt4,s,t)
  211. if ret and s >= 0.0 and s <= 1.0 and t >= 0.0 and t <= 0.0 then
  212. return true;
  213. end
  214. return false
  215. end
  216. function cc.pGetIntersectPoint(pt1,pt2,pt3,pt4)
  217. local s,t, ret = 0,0,false
  218. ret,s,t = cc.pIsLineIntersect(pt1,pt2,pt3,pt4,s,t)
  219. if ret then
  220. return cc.p(pt1.x + s * (pt2.x - pt1.x), pt1.y + s * (pt2.y - pt1.y))
  221. else
  222. return cc.p(0,0)
  223. end
  224. end
  225. --Size
  226. function cc.size( _width,_height )
  227. return { width = _width, height = _height }
  228. end
  229. --Rect
  230. function cc.rect(_x,_y,_width,_height)
  231. return { x = _x, y = _y, width = _width, height = _height }
  232. end
  233. function cc.rectEqualToRect(rect1,rect2)
  234. if ((rect1.x >= rect2.x) or (rect1.y >= rect2.y) or
  235. ( rect1.x + rect1.width <= rect2.x + rect2.width) or
  236. ( rect1.y + rect1.height <= rect2.y + rect2.height)) then
  237. return false
  238. end
  239. return true
  240. end
  241. function cc.rectGetMaxX(rect)
  242. return rect.x + rect.width
  243. end
  244. function cc.rectGetMidX(rect)
  245. return rect.x + rect.width / 2.0
  246. end
  247. function cc.rectGetMinX(rect)
  248. return rect.x
  249. end
  250. function cc.rectGetMaxY(rect)
  251. return rect.y + rect.height
  252. end
  253. function cc.rectGetMidY(rect)
  254. return rect.y + rect.height / 2.0
  255. end
  256. function cc.rectGetMinY(rect)
  257. return rect.y
  258. end
  259. function cc.rectContainsPoint( rect, point )
  260. local ret = false
  261. if (point.x >= rect.x) and (point.x <= rect.x + rect.width) and
  262. (point.y >= rect.y) and (point.y <= rect.y + rect.height) then
  263. ret = true
  264. end
  265. return ret
  266. end
  267. function cc.rectIntersectsRect( rect1, rect2 )
  268. local intersect = not ( rect1.x > rect2.x + rect2.width or
  269. rect1.x + rect1.width < rect2.x or
  270. rect1.y > rect2.y + rect2.height or
  271. rect1.y + rect1.height < rect2.y )
  272. return intersect
  273. end
  274. function cc.rectUnion( rect1, rect2 )
  275. local rect = cc.rect(0, 0, 0, 0)
  276. rect.x = math.min(rect1.x, rect2.x)
  277. rect.y = math.min(rect1.y, rect2.y)
  278. rect.width = math.max(rect1.x + rect1.width, rect2.x + rect2.width) - rect.x
  279. rect.height = math.max(rect1.y + rect1.height, rect2.y + rect2.height) - rect.y
  280. return rect
  281. end
  282. function cc.rectIntersection( rect1, rect2 )
  283. local intersection = cc.rect(
  284. math.max(rect1.x, rect2.x),
  285. math.max(rect1.y, rect2.y),
  286. 0, 0)
  287. intersection.width = math.min(rect1.x + rect1.width, rect2.x + rect2.width) - intersection.x
  288. intersection.height = math.min(rect1.y + rect1.height, rect2.y + rect2.height) - intersection.y
  289. return intersection
  290. end
  291. --Color3B
  292. function cc.c3b( _r,_g,_b )
  293. return { r = _r, g = _g, b = _b }
  294. end
  295. --Color4B
  296. function cc.c4b( _r,_g,_b,_a )
  297. return { r = _r, g = _g, b = _b, a = _a }
  298. end
  299. --Color4F
  300. function cc.c4f( _r,_g,_b,_a )
  301. return { r = _r, g = _g, b = _b, a = _a }
  302. end
  303. --Vertex2F
  304. function cc.vertex2F(_x,_y)
  305. return { x = _x, y = _y }
  306. end
  307. --Vertex3F
  308. function cc.Vertex3F(_x,_y,_z)
  309. return { x = _x, y = _y, z = _z }
  310. end
  311. --Tex2F
  312. function cc.tex2F(_u,_v)
  313. return { u = _u, v = _v }
  314. end
  315. --PointSprite
  316. function cc.PointSprite(_pos,_color,_size)
  317. return { pos = _pos, color = _color, size = _size }
  318. end
  319. --Quad2
  320. function cc.Quad2(_tl,_tr,_bl,_br)
  321. return { tl = _tl, tr = _tr, bl = _bl, br = _br }
  322. end
  323. --Quad3
  324. function cc.Quad3(_tl, _tr, _bl, _br)
  325. return { tl = _tl, tr = _tr, bl = _bl, br = _br }
  326. end
  327. --V2F_C4B_T2F
  328. function cc.V2F_C4B_T2F(_vertices, _colors, _texCoords)
  329. return { vertices = _vertices, colors = _colors, texCoords = _texCoords }
  330. end
  331. --V2F_C4F_T2F
  332. function cc.V2F_C4F_T2F(_vertices, _colors, _texCoords)
  333. return { vertices = _vertices, colors = _colors, texCoords = _texCoords }
  334. end
  335. --V3F_C4B_T2F
  336. function cc.V3F_C4B_T2F(_vertices, _colors, _texCoords)
  337. return { vertices = _vertices, colors = _colors, texCoords = _texCoords }
  338. end
  339. --V2F_C4B_T2F_Quad
  340. function cc.V2F_C4B_T2F_Quad(_bl, _br, _tl, _tr)
  341. return { bl = _bl, br = _br, tl = _tl, tr = _tr }
  342. end
  343. --V3F_C4B_T2F_Quad
  344. function cc.V3F_C4B_T2F_Quad(_tl, _bl, _tr, _br)
  345. return { tl = _tl, bl = _bl, tr = _tr, br = _br }
  346. end
  347. --V2F_C4F_T2F_Quad
  348. function cc.V2F_C4F_T2F_Quad(_bl, _br, _tl, _tr)
  349. return { bl = _bl, br = _br, tl = _tl, tr = _tr }
  350. end
  351. --T2F_Quad
  352. function cc.T2F_Quad(_bl, _br, _tl, _tr)
  353. return { bl = _bl, br = _br, tl = _tl, tr = _tr }
  354. end
  355. --AnimationFrameData
  356. function cc.AnimationFrameData( _texCoords, _delay, _size)
  357. return { texCoords = _texCoords, delay = _delay, size = _size }
  358. end
  359. --PhysicsMaterial
  360. function cc.PhysicsMaterial(_density, _restitution, _friction)
  361. return { density = _density, restitution = _restitution, friction = _friction }
  362. end
  363. local ConfigType =
  364. {
  365. NONE = 0,
  366. COCOSTUDIO = 1,
  367. }
  368. function __onParseConfig(configType,jasonStr)
  369. if configType == ConfigType.COCOSTUDIO then
  370. ccs.TriggerMng.getInstance():parse(jasonStr)
  371. end
  372. end