lua 基础方法封装

function DY.SplitString(str,split)
local _tem = {}
if str and split then
local i = 1
while(true) do 
local x = string.find(str,split)
if x then 
_tem[i] = string.sub(str,1,x - 1)
local _d = tonumber(_tem[i])
if _d then 
_tem[i] = _d
end 
str = string.sub(str,x + 1)
i = i + 1
else 
_tem[i] = str
local _d = tonumber(_tem[i])
if _d then 
_tem[i] = _d
end 
break
end 
end
else 
_tem = {str} 
end 
return _tem
end


function DY.getATableLength(t)
local len = 0
if t then 
for k,v in pairs(t) do 
len = len + 1
end 
end 
return len
end


function DY.insertTableNotRepeat( _table,val )
local _key = true
if not next(_table) then 
table.insert(_table,val)
return
end
for k,v in pairs(_table) do
if v == val then
_key = false
return
end
end
if _key == true  then
table.insert(_table,val)
end
end


function DY.convertRadiansToAngle(radians) ----弧度到角度
return 180 * (radians / math.pi)
end


function DY.convertAngleToRadians( angel ) ---角度到弧度
return math.pi * (angel / 180)
end


function DY.convertRadianToPositive( radian )-----把负角转成正角
if radian < 0 then 
return math.pi * 2 + radian
else 
return radian
end 
end


function DY.convertRadianToNegative( radian ) -----把大角转成小角,大于180的为负角,
if radian > math.pi then 
return radian - math.pi * 2
else 
return radian
end 
end


function DY.getRadianVector(angle,isRadian) -----获取一个给定角度的角度向量
if angle then
local radian = angle
if not isRadian then 
radian = DY.convertAngleToRadians(angle)
end 
local v = cc.p(1,math.tan(radian))
return cc.pNormalize(v)
end 
return cc.p(0,0)
end


function DY.getARate(rate) -----获取一个概率,如果在这个概率内返回true(rate:0.1,10%的几率返回true)
rate = rate * 100
local data = math.random(100)
if data <= rate then
return true
else 
return false
end 

end

深拷贝

function DY.cloneTable(data)

local _outTable = {}
local function _copy(data)
if type(data) ~= "table" then
return data
end
local _newTable = {}
_outTable[data] = _newTable
for k,v in pairs(data) do
_newTable[_copy(k)] = _copy(v)
end
return setmetatable(_newTable , getmetatable(data))
end 
return _copy(data)
end

function DY.alphaDarkSprite( node ) -----node置灰
    local shaderNameStroke = "Shader_Stroke_Dark"
    local pProgram = cc.GLProgramCache:getInstance():getGLProgram(shaderNameStroke)
    if not pProgram then   
        local vertDefaultSource = "\n"..
            "attribute vec4 a_position; \n" ..
            "attribute vec2 a_texCoord; \n" ..
            "attribute vec4 a_color; \n"..                                                    
            "#ifdef GL_ES  \n"..
            "varying lowp vec4 v_fragmentColor;\n"..
            "varying mediump vec2 v_texCoord;\n"..
            "#else                      \n" ..
            "varying vec4 v_fragmentColor; \n" ..
            "varying vec2 v_texCoord;  \n"..
            "#endif    \n"..
            "void main() \n"..
            "{\n" ..
            "gl_Position = CC_PMatrix * a_position; \n"..
            "v_fragmentColor = a_color;\n"..
            "v_texCoord = a_texCoord;\n"..
            "}"
        local fragGray = "#ifdef GL_ES \n" ..
            "precision mediump float; \n" ..
            "#endif \n" ..
            "varying vec4 v_fragmentColor; \n" ..
            "varying vec2 v_texCoord; \n" ..
            "void main(void) \n" ..
            "{ \n" ..
            "vec4 c = texture2D(CC_Texture0, v_texCoord); \n" ..
            "gl_FragColor.xyz = vec3(0.4*c.r + 0.4*c.g +0.4*c.b); \n"..
            "gl_FragColor.w = c.w; \n"..
            "}"
   pProgram = cc.GLProgram:createWithByteArrays(vertDefaultSource,fragGray)
        pProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION,cc.VERTEX_ATTRIB_POSITION)
        pProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_COLOR,cc.VERTEX_ATTRIB_COLOR)
        pProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD,cc.VERTEX_ATTRIB_FLAG_TEX_COORDS)
        pProgram:link()
        pProgram:updateUniforms()     
        cc.GLProgramCache:getInstance():addGLProgram(pProgram, shaderNameStroke)
    end
    node:setGLProgram(pProgram)
end


function DY.alphaLightSprite( node )   -----还原成原色
local shaderNameStroke = "Shader_Stroke_Light"
    local pProgram = cc.GLProgramCache:getInstance():getGLProgram(shaderNameStroke)   
    if not pProgram then 
        local vertDefaultSource = "\n"..
            "attribute vec4 a_position; \n" ..
            "attribute vec2 a_texCoord; \n" ..
            "attribute vec4 a_color; \n"..                                                    
            "#ifdef GL_ES  \n"..
            "varying lowp vec4 v_fragmentColor;\n"..
            "varying mediump vec2 v_texCoord;\n"..
            "#else                      \n" ..
            "varying vec4 v_fragmentColor; \n" ..
            "varying vec2 v_texCoord;  \n"..
            "#endif    \n"..
            "void main() \n"..
            "{\n" ..
            "gl_Position = CC_PMatrix * a_position; \n"..
            "v_fragmentColor = a_color;\n"..
            "v_texCoord = a_texCoord;\n"..
            "}"
        local fragmentSource = "#ifdef GL_ES \n" ..
            "precision mediump float; \n" ..
            "#endif \n" ..
            "varying vec4 v_fragmentColor; \n" ..
            "varying vec2 v_texCoord; \n" ..
            "void main(void) \n" ..
            "{ \n" ..
            "vec4 c = texture2D(CC_Texture0, v_texCoord); \n" ..
            "gl_FragColor = c; \n"..
            "}"
        pProgram = cc.GLProgram:createWithByteArrays(vertDefaultSource, fragmentSource)
        pProgram:link()
        pProgram:updateUniforms()     
        cc.GLProgramCache:getInstance():addGLProgram(pProgram, shaderNameStroke);
    end
   
    node:setGLProgram(pProgram)

end

--字符串时间转换成时间戳
function DY:string2time( timeString )
    local Y = string.sub(timeString , 1, 4)  
    local m = string.sub(timeString , 5, 6)  
    local d = string.sub(timeString , 7, 8)
    local H = string.sub(timeString , 9, 10)
    local M = string.sub(timeString , 11, 12)
    local S = string.sub(timeString , 13, 14)
    return os.time({year=Y, month=m, day=d, hour=H,min=M,sec=S})
end
--时间戳转换成时间
function DY:time2string( time )
 local t = time
 local time = os.date("%Y",t) .."/"
 time = time.. os.date("%m",t) .."/"
 time = time.. os.date("%d",t) .."/"
 time = time.. os.date("%H",t) ..":"
 time = time.. os.date("%M",t) ..":"
 time = time.. os.date("%S",t) 
   return time 

end

 -- 一个震动耗时4个duration左,复位,右,复位
 -- 同时左右和上下震动
 local times = math.floor(time / (duration * 4))
 local moveLeft = cc.MoveBy:create(duration, cc.p(-offset, 0))
 local moveLReset = cc.MoveBy:create(duration, cc.p(offset, 0))
 local moveRight = cc.MoveBy:create(duration, cc.p(offset, 0))
 local moveRReset = cc.MoveBy:create(duration, cc.p(-offset, 0))
 local horSeq = cc.Sequence:create(moveLeft, moveLReset, moveRight, moveRReset)
 local moveUp = cc.MoveBy:create(duration, cc.p(0, offset))
 local moveUReset = cc.MoveBy:create(duration, cc.p(0, -offset))
 local moveDown = cc.MoveBy:create(duration, cc.p(0, -offset))
 local moveDReset = cc.MoveBy:create(duration, cc.p(0, offset))
 local verSeq = cc.Sequence:create(moveUp, moveUReset, moveDown, moveDReset)
 node:runAction(cc.Sequence:create(cc.Repeat:create(cc.Spawn:create(horSeq, verSeq), times), cc.CallFunc:create(function()
     node:setPosition(originPos)
 end)))

end

裁剪

 function DY:getMaskClipSprite(spritePath, maskPath,Sp)
    local textureSprite = cc.Sprite:create(spritePath)  
    local textureSize = textureSprite:getContentSize()


    local maskSprite = cc.Sprite:create(maskPath)  
    local maskSize = maskSprite:getContentSize()


    local renderTexture = cc.RenderTexture:create(maskSize.width,maskSize.height)  
    maskSprite:setPosition(cc.p(maskSize.width/2,maskSize.height/2))  
    textureSprite:setPosition(cc.p(textureSize.width/2,textureSize.height/2))  
    maskSprite:setBlendFunc (GL_ONE, GL_ZERO) --
    textureSprite:setBlendFunc(GL_DST_ALPHA, GL_ZERO) --
     


    renderTexture:begin()  
    maskSprite:visit()  
    textureSprite:visit()  
    renderTexture:endToLua()  
  
    local retSprite = cc.Sprite:createWithTexture(renderTexture:getSprite():getTexture())  
    retSprite:setFlippedY(true)
    return retSprite  

end

- @class function
-- @description 文本描边颜色outline
-- @param ttfLabel 要被设置描边的文本控件
-- @param color cc.c4b颜色
-- @param size int像素Size
-- @return
-- end --
local function setTTFLabelStroke(ttfLabel, color, size)
    if not ttfLabel then
        return
    end


    color = color or cc.c4b(27, 27, 27, 255)
    size = size or 1


    ttfLabel:enableOutline(color, size)

end

- start --
--------------------------------
-- @class function
-- @description 文本阴影
-- @param ttfLabel 要被设置阴影的文本控件
-- @param color cc.c4b颜色
-- @param offset Size偏移量cc.size(2, -2)
-- @return
-- end --
local function setTTFLabelShadow(ttfLabel, color, offset)
    if not ttfLabel then
        return
    end


    ttfLabel:enableShadow(color, offset, 0)

end

-- start --
--------------------------------
-- @class function
-- @description 用当前时间反置设置随机数种子
-- @return
-- end --
local function setRandomSeed()
    math.randomseed(tostring(os.time()):reverse():sub(1, 6))
end

猜你喜欢

转载自blog.csdn.net/xuefujin/article/details/80924891