51 lines
1.4 KiB
Lua
51 lines
1.4 KiB
Lua
------------------------------------------------
|
||
--作者:xihan
|
||
--日期:2019-07-04
|
||
--文件:AIManager.lua
|
||
--模块:AIManager
|
||
--描述:场景中有AI的实体的管理器
|
||
------------------------------------------------
|
||
|
||
local AIManager = {
|
||
--当前正在执行的状态机
|
||
AllStateMachine = {},
|
||
--状态机池
|
||
StateMachinePool = {},
|
||
};
|
||
|
||
--绑定FSM
|
||
function AIManager:Bind(owner, configid)
|
||
if self.AllStateMachine[owner] then
|
||
if self.AllStateMachine[owner].ConfigId ~= configid then
|
||
self.AllStateMachine[owner]:SetConfig(configid);
|
||
end
|
||
else
|
||
if #self.StateMachinePool > 0 then
|
||
self.AllStateMachine[owner] = table.remove(self.StateMachinePool,1);
|
||
self.AllStateMachine[owner]:SetOwner(owner);
|
||
self.AllStateMachine[owner]:SetConfig(configid);
|
||
else
|
||
self.AllStateMachine[owner] = StateMachine:New(owner, configid);
|
||
end
|
||
end
|
||
end
|
||
|
||
--解除绑定FSM
|
||
function AIManager:UnBind(owner)
|
||
local _machine = self.AllStateMachine[owner];
|
||
if _machine then
|
||
_machine:Clear();
|
||
_machine.Owner = nil;
|
||
table.insert(self.StateMachinePool, _machine);
|
||
self.AllStateMachine[owner] = nil;
|
||
end
|
||
end
|
||
|
||
--更新
|
||
function AIManager:Update(dt)
|
||
for _,v in pairs(self.AllStateMachine) do
|
||
v:Update(dt);
|
||
end
|
||
end
|
||
|
||
return AIManager; |