入手cocos2dx3.9笔记 2

前端之家收集整理的这篇文章主要介绍了入手cocos2dx3.9笔记 2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<span style="font-family: Arial,Helvetica,sans-serif; background-color: rgb(255,255,255);">今天成功研究出一套我自己感觉很nice的 lua 类机制,小巧而强大, 类的继承关系 以浅拷贝所有成员字段来实现、对象实例以原表方式与类相关联,这就贴上代码:</span>


-- @author:
-- 	Carl Zhong
-- @desc:
-- 	lua 面向对象解决方案,类机制实现
-- 	类实现不会用到 Metatable



--class id generator
local s_nClassID = 0
local function _genClassID()
	s_nClassID = s_nClassID + 1
	return s_nClassID
end

--Metatable for subClass
local s_clsmt = {}
s_clsmt.__newindex = function(t,key,value)
	assert(type(value) == 'function')
	rawset(t,value)
end

s_clsmt.__index 	= function(t,k) assert(false) end
s_clsmt.__add 		= function(t1,t2) assert(false) end
s_clsmt.__sub 		= function(t1,t2) assert(false) end
s_clsmt.__mul 		= function(t1,t2) assert(false) end
s_clsmt.__div 		= function(t1,t2) assert(false) end
s_clsmt.__mod 		= function(t1,t2) assert(false) end
s_clsmt.__pow 		= function(t1,t2) assert(false) end
s_clsmt.__eq 		= function(t1,t2) assert(false) end
s_clsmt.__lt 		= function(t1,t2) assert(false) end
s_clsmt.__le 		= function(t1,t2) assert(false) end
s_clsmt.__concat 	= function(t1,t2) assert(false) end
s_clsmt.__call 		= function(t,...) assert(false) end
s_clsmt.__unm 		= function(t) assert(false) end
s_clsmt.__len 		= function(t) assert(false) end
--s_clsmt.__gc 	= function(t)end __gc is for userdata


function isclass(cls)
	return getMetatable(cls) == s_clsmt
end

function isobject(obj)
	local mt = getMetatable(obj)
	return mt and isclass(mt.__index) or false
end

function isinstance(obj,cls)
	assert(isobject(obj) and isclass(cls))
	return obj:IsInstance(cls)
end



local _clsInfo = {} 		--key:clsName,value:clsTemplate
							--key:clsID,value:clsTemplate
							--key:clsTemplate,value:{clsID,clsName}
-- @desc:
-- 	产生一个类模板
-- @param className:
-- 	类名,必须为字符串
-- @param baseClass:
-- 	基类模板,if baseClass == nil then 返回的类模板无基类
-- @return:
-- 	返回新建类的模板
function CreateClass(className,baseClass)
	local clsID = _genClassID()
	className = className or 'CLS_' .. clsID
	assert(type(className) == 'string' and not _clsInfo[className])

	local subClass
	if baseClass then
		assert(isclass(baseClass))
		subClass = table.copy(baseClass)--浅拷贝
	else
		subClass = {}
	end

	--read only
	-- subClass.__clsIsCls = true
	subClass.__clsID 	= clsID
	subClass.__clsName 	= className
	subClass.__clsBase 	= baseClass

	--class has Metatable
	setMetatable(subClass,s_clsmt)

	_clsInfo[className] = subClass
	_clsInfo[clsID] 	= subClass
	_clsInfo[subClass]	= {clsName = className,clsID = clsID}
	
	--default functions:

	-- @desc:
	--	class method
	-- 	return the object of the class,the returned object has no Metatable
	function subClass:New(...)
		local ret = {}
		setMetatable(ret,{__index = self}) --obj 函数引用 class
		ret.__objCls = self

		--auto init
		ret:__init__(...)
		return ret
	end

	-- @desc:
	-- 	Called when obj is constructed
	function subClass:__init__()
		assert(false,'not implemented')
	end
	
	-- @desc:
	-- 	check wether obj / cls is a kind of class
	function subClass:IsInstance(class)
		assert(isclass(class))
		
		local curCls = self.__objCls or self
		while curCls do
			if curCls.__clsID == class.__clsID then return true end
			curCls = curCls.__clsBase
		end
	end

	-- @desc:
	-- 	return id of cls or obj
	function subClass:GetClsID()
		return self.__clsID
	end

	-- @desc:
	-- 	return name of cls or obj
	function subClass:GetClsName()
		return self.__clsName
	end

	function subClass:GetBaseCls()
		return self.__clsBase
	end

	
	return subClass
end

function GetClassInfo(id)
	return _clsInfo[id]
end

干吧爹、晚上继续搞别的。

原文链接:https://www.f2er.com/cocos2dx/340680.html

猜你在找的Cocos2d-x相关文章