一、基础
1、Unity的C#脚本含有一个可以调用Update的函数
2、Lua中“面向对象”的基础类
二、状态机代码
FSM
Object:subClass("FSMBase")
function FSMBase:CreateFSM(className)
FSMBase:subClass(className)
_G[className].defaultFSMStateID = nil
_G[className].fsmStates = {}
_G[className].currentFSMState = nil
_G[className].defaultFSMState = nil
end
-- 初始化状态机
function FSMBase:InitFSM()
self.currentFSM = self
self:InitComponent()
self:InitDefaultState()
mono:AddUpdateEventListener(function()
self:Update()
end)
end
-- 初始化组件 //TODO需要子类处理
function FSMBase:InitComponent()
end
--初始化状态机的默认状态 //TODO需要子类处理
function FSMBase:InitDefaultState()
self.defaultFSMState = self.fsmStates[self.defaultFSMStateID]
if self.defaultFSMState == nil then
error("默认状态不存在" .. self.defaultFSMStateID)
end
self.currentFSMState = self.defaultFSMState
self.currentFSMState:StateEnter(self)
end
--需要依赖MONO工具
function FSMBase:Update()
if self.currentFSMState ~= nil then
self.currentFSMState:Reason(self.currentFSM)
self.currentFSMState:StateStay(self.currentFSM)
end
end
--更改当前状态
function FSMBase:ChanegActiveState(stateID)
if self.currentFSMState ~= nil then
self.currentFSMState:StateExit(self.currentFSM)
end
self.currentFSMState = self.fsmStates[stateID]
print(self.currentFSMState.stateID .. "State ID")
self.currentFSMState:StateEnter();
if self.currentFSMState == nil then
error("默认状态不存在" .. stateID)
self.currentFSMState:StateStay(self.currentFSM)
end
end
FSMSate
Object:subClass("FSMStateBase")
function FSMStateBase:CreateFSMState(className)
FSMStateBase:subClass(className)
_G[className].stateID = nil;
_G[className].conditionAndStateMap = {}
_G[className].fsmConditions = {}
end
function FSMStateBase:AddConditioAndState(conditionID, stateID)
if not self.conditionAndStateMap[conditionID] == nil then
error("条件已经存在" .. conditionID)
return
end
self.conditionAndStateMap[conditionID] = stateID
table.insert(self.fsmConditions, _G[conditionID]:new())
end
function FSMStateBase:Reason(fsm)
for index, value in ipairs(self.fsmConditions) do
if value:CheckCondition(fsm) then
stateID = self.conditionAndStateMap[value.conditionID]
fsm:ChanegActiveState(stateID)
end
end
end
function FSMStateBase:StateEnter(fsm)
end
function FSMStateBase:StateStay(fsm)
end
function FSMStateBase:StateExit(fsm)
end
FSMCondition
Object:subClass("FSMConditionBase")
function FSMConditionBase:CreateFSMCondition(className)
FSMConditionBase:subClass(className)
self.conditionID = nil
end
function FSMConditionBase:CheckCondition(fsm)
end
三、使用
1、定义状态机
FSMBase:CreateFSM("TestFSM")
function TestFSM:InitComponent()
--初始化状态条件列表
local state1 = TestState1:new()
local state2 = TestState2:new()
self.fsmStates[state1.stateID] = state1
self.fsmStates[state2.stateID] = state2
state1:AddConditioAndState("TestCondition1", "TestState2")
state2:AddConditioAndState("TestCondition2", "TestState1")
--设置默认状态
self.defaultFSMStateID = "TestState1"
end
2、定义状态
FSMStateBase:CreateFSMState("TestState1")
TestState1.stateID = "TestState1"
function TestState1:StateEnter(fsm)
print("TestState1 In")
end
function TestState1:StateStay()
print("TestState1 Stay")
end
function TestState1:StateExit()
print("TestState1 Out")
end
3、定义条件
FSMConditionBase:CreateFSMCondition("TestCondition1")
TestCondition1.conditionID = "TestCondition1"
function TestCondition1:CheckCondition(fsm)
return true
end
4、使用状态机
留言