lua的一些程序片段

    在游戏过程中,很多时候会用一些基础的函数,我把一些自己常用的lua函数拿出来跟大家分享,相信很多函数在大家的游戏开发过程中,也是经常用到的。如果你有好的函数片段,欢迎分享哈。

luaBase = {}

------------------------------------------------------------------------------------------
-- 获取两个数相除结果的整数部分
-- getInt(1.2) ==> 1
function luaBase.getInt(a)
	return (math.modf(a))
end

------------------------------------------------------------------------------------------
-- 对两数相除结果四舍五入
-- getRounding(1.5) ==> 2
-- getRounding(1.4) ==> 1
function luaBase.getRounding(a)
	local r1, r2 = math.modf(a, 1)
	r2 = r2 >= 0.5 and 1 or 0
	return r1 + r2
end

------------------------------------------------------------------------------------------
-- 返回两个数相除结果的整数值和求余值
-- getDivMod(76, 5) ==> 15, 1
function luaBase.getDivMod(data, division)
	if division == 0 then
		return nil
	end
	local a = luaBase.getInt(data / division)
	local b = data % division
	return a, b
end

------------------------------------------------------------------------------------------
-- 获取参数列表的第n个参数
--[[
function fun()
	return 10, 20, 30, 40, 50
end
getAt(3, fun()) ==> 30
]]--
function luaBase.getAt(index, ...)
	return arg[index]
end

------------------------------------------------------------------------------------------
-- 按参数要求分解一个数据
--[[
data = abbcccdddd
a1, a2, a3, a4 = analysisData(data, 1, 2, 3, 4)
a1, a2, a3, a4 ==> a, bb, ccc, dddd

data = aaabcc
a1, a2, a3, a4 = analysisData(data, 3, 1, 2)
a1, a2, a3, a4 ==> aaa, b, cc
]]--
function luaBase.analysisData(data, ...)
	local tblRet = {}
	local strData = tostring(data)

	for i = 1, table.getn(arg) do
		table.insert(tblRet, string.sub(strData, 1, arg[i]))
		if string.len(strData) ~= arg[i] then
			strData = string.sub(strData, arg[i] + 1, string.len(strData))
		end
	end

	return unpack(tblRet)
end

------------------------------------------------------------------------------------------
-- 取a的二进制位index的值
-- index 从右到左,从0开始
-- getBit(17, 4) ==> 1
-- 17 = 10001
function luaBase.getBit(a, index)
	local b = luaBase.getInt(a / math.pow(2, index))
	return b % 2
end

------------------------------------------------------------------------------------------
-- 设置a的二进制位index的值
-- index 从右到左,从0开始
-- setBit(17, 2) ==> 10101 = 21
-- setBit(17, 0) ==> 10001 = 17
-- 17 = 10001
function luaBase.setBit(a, index)
	local b = luaBase.getInt(a / math.pow(2, index))
	if b % 2 == 1 then
		return a
	else
		return a + math.pow(2, index)
	end
end

------------------------------------------------------------------------------------------
-- 清除a的二进制位index的值
-- index 从右到左,从0开始
-- clearBit(17, 2) ==> 10001 = 17
-- clearBit(17, 0) ==> 10000 = 16
-- 17 = 10001
function luaBase.clearBit(a, index)
	local b = luaBase.getInt(a / math.pow(2, index))
	if b % 2 == 0 then
		return a
	else
		return a - math.pow(2, index)
	end
end


 

啊,原来CSDN不支持lua基础类型高亮,好吧,大家将就下吧。拷贝到SciTE就容易看了。

来解释下为什么要加入luaBase = {}。

1、在游戏开发过程中,lua文件实在太多了,100个左右,很多时候,函数名可能跟别的文件的函数名相同了,这时候,调用的函数时看谁先被加载,这样很容易就出现了BUG,所以加入了luaBase ={}。

2、首先这个就解决了函数重名的问题,其次,这个luaBase的名字是跟保存的文件名一样的,这样做是为了方便找到一个函数的原型,在别的文件中,很容易就知道这个函数的来源是哪个文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值