axmol/scripting/lua/script/Cocos2d.lua

201 lines
4.4 KiB
Lua

cc = cc or {}
cc.DIRECTOR_PROJECTION_2D = 0
cc.DIRECTOR_PROJECTION_3D = 1
--Point
function cc.p(_x,_y)
if nil == _y then
return { x = _x.x, y = _x.y }
else
return { x = _x, y = _y }
end
end
function cc.pAdd(pt1,pt2)
return {x = pt1.x + pt2.x , y = pt1.y + pt2.y }
end
function cc.pSub(pt1,pt2)
return {x = pt1.x - pt2.x , y = pt1.y - pt2.y }
end
function cc.pMul(pt1,factor)
return { x = pt1.x * factor , y = pt1.y * factor }
end
function cc.pForAngle(a)
return { x = math.cos(a), y = math.sin(a) }
end
function cc.pGetLength(pt)
return math.sqrt( pt.x * pt.x + pt.y * pt.y )
end
function cc.pNormalize(pt)
local length = cc.getPointLength(pt)
if 0 == length then
return { x = 1.0,y = 0.0 }
end
return { x = pt.x / length, y = pt.y / length }
end
function cc.pCross(self,other)
return self.x * other.y - self.y * other.x
end
function cc.pDot(self,other)
return self.x * other.x - self.y * other.y
end
function cc.pToAngleSelf(self)
return math.atan2(self.y, self.x)
end
function cc.pToAngle(self,other)
local a2 = cc.pNormalize(self)
local b2 = cc.pNormalize(other)
local angle = math.atan2(cc.pCross(a2, b2), cc.pDot(a2, b2) )
if angle < 1.192092896e-7 then
return 0.0
end
return angle
end
function cc.pGetDistance(startP,endP)
return cc.pGetLength(cc.pSub(startP,endP))
end
function cc.pIsLineIntersect(A, B, C, D, s, t)
if ((A.x == B.x) and (A.y == B.y)) or ((C.x == D.x) and (C.y == D.y))then
return false, s, t
end
local BAx = B.x - A.x
local BAy = B.y - A.y
local DCx = D.x - C.x
local DCy = D.y - C.y
local ACx = A.x - C.x
local ACy = A.y - C.y
local denom = DCy * BAx - DCx * BAy
s = DCx * ACy - DCy * ACx
t = BAx * ACy - BAy * ACx
if (denom == 0) then
if (s == 0 or t == 0) then
return true, s , t
end
return false, s, t
end
s = s / denom
t = t / denom
return true,s,t
end
--Size
function cc.size( _width,_height )
return { width = _width, height = _height }
end
--Rect
function cc.rect(_x,_y,_width,_height)
return { x = _x, y = _y, width = _width, height = _height }
end
function cc.rectEqualToRect(rect1,rect2)
if ((rect1.x >= rect2.x) or (rect1.y >= rect2.y) or
( rect1.x + rect1.width <= rect2.x + rect2.width) or
( rect1.y + rect1.height <= rect2.y + rect2.height)) then
return false
end
return true
end
function cc.rectGetMaxX(rect)
return rect.x + rect.width
end
function cc.rectGetMidX(rect)
return rect.x + rect.width / 2.0
end
function cc.rectGetMinX(rect)
return rect.x
end
function cc.rectGetMaxY(rect)
return rect.y + rect.height
end
function cc.rectGetMidY(rect)
return rect.y + rect.height / 2.0
end
function cc.rectGetMinY(rect)
return rect.y
end
function cc.rectContainsPoint( rect, point )
local ret = false
if (point.x >= rect.x) and (point.x <= rect.x + rect.width) and
(point.y >= rect.y) and (point.y <= rect.y + rect.height) then
ret = true
end
return ret
end
function cc.rectIntersectsRect( rect1, rect2 )
local intersect = not ( rect1.x > rect2.x + rect2.width or
rect1.x + rect1.width < rect2.x or
rect1.y > rect2.y + rect2.height or
rect1.y + rect1.height < rect2.y )
return intersect
end
function cc.rectUnion( rect1, rect2 )
local rect = cc.rect(0, 0, 0, 0)
rect.x = math.min(rect1.x, rect2.x)
rect.y = math.min(rect1.y, rect2.y)
rect.width = math.max(rect1.x + rect1.width, rect2.x + rect2.width) - rect.x
rect.height = math.max(rect1.y + rect1.height, rect2.y + rect2.height) - rect.y
return rect
end
function cc.rectIntersection( rect1, rect2 )
local intersection = cc.rect(
math.max(rect1.x, rect2.x),
math.max(rect1.y, rect2.y),
0, 0)
intersection.width = math.min(rect1.x + rect1.width, rect2.x + rect2.width) - intersection.x
intersection.height = math.min(rect1.y + rect1.height, rect2.y + rect2.height) - intersection.y
return intersection
end
--Color3B
function cc.c3b( _r,_g,_b )
return { r = _r, g = _g, b = _b }
end
--Color4B
function cc.c4b( _r,_g,_b,_a )
return { r = _r, g = _g, b = _b, a = _a }
end
--Color4F
function cc.c4f( _r,_g,_b,_a )
return { r = _r, g = _g, b = _b, a = _a }
end