lua

本文详细介绍了 Lua 中如何在构造函数中实现对象的属性单独设置,探讨了 Lua 对象属性修改的机制及其在实际编程中的应用。

一、lua 对象修改类属性
属性单独在构造函数中实现

local AI={}
AI.NodeType={SELECT=1,SEQUENCE=2,CONDITION=3,ACTION=4}
AI.NodeList={}

function AI.RootOneRound()
	print('Have done one round!')
	return true
end

function AI.Condition_Node_Patrol_Condition()
	print("Condition_Node_Patrol_Condition")
	return true
end

function AI.Condition_Node_Patrol()
	print("Condition_Node_Patrol")
	return true
end

local node={NodeType=AI.NodeType.CONDITION,childNum=0,Function=nil,Children=nil}

function node:new()
	local newnode={}
	setmetatable(newnode,self)
	self.__index=self
	self.NodeType=nil
	self.Children={}
	self.childNum=0
	self.Function=nil
	return newnode
end
function node:ctor(nodetype,childrennum,actFuc,...)
	self.NodeType=nodetype
	self.Children={...}
	self.childNum=childrennum
	self.Function=actFuc
end

function node:exact()
	print('exact')
		
	if self.NodeType==AI.NodeType.SELECT then
		for i=1,self.childNum do
			local result=self.Children[i]:exact()
			if result then
				return true
			end
		end
		return false
		
	elseif self.NodeType==AI.NodeType.SEQUENCE then
		for i=1,self.childNum do
			dump(self.Children[i])

			local result=self.Children[i]:exact()
			if not result then
				return false
			end
			
			
		end
		return true
	elseif self.NodeType==AI.NodeType.CONDITION then
		if self.Function~=nil then
			local result=self.Function()	
			return result
		end
		
		
	elseif self.NodeType==AI.NodeType.ACTION then
		if self.Function~=nil then
			local result=self.Function()	
			return result
		end
		
	else
		return false
	end
	return false
end


local patrolCN=node:new()
patrolCN:ctor(AI.NodeType.CONDITION,0,AI.Condition_Node_Patrol_Condition)

local patrolAN=node:new()
patrolAN:ctor(AI.NodeType.ACTION,0,AI.Condition_Node_Patrol)

local patrolSQN=node:new()
patrolSQN:ctor(AI.NodeType.SEQUENCE,2,nil,patrolCN,patrolAN)

local root=node:new()
root:ctor(AI.NodeType.SELECT,1,AI.RootOneRound,patrolSQN)

patrolCN:exact()
patrolAN:exact()
--patrolSQN:exact()
--root:exact()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值