一、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()
本文详细介绍了 Lua 中如何在构造函数中实现对象的属性单独设置,探讨了 Lua 对象属性修改的机制及其在实际编程中的应用。
1200

被折叠的 条评论
为什么被折叠?



