你沒有提到你正在使用哪種語言,所以我將要在Lua與中產階級提供面向對象的寫這篇 - https://github.com/kikito/middleclass(免責聲明:我是中產階級的創造者)
另一種選擇會將你的過場動畫分成「動作列表」。如果你已經有一個遊戲循環在對象列表上調用'update'方法,這可能會更好地融合你的代碼。
像這樣:
helloJane = CutScene:new(
WalkAction:new(bob, jane),
LookAction:new(bob, jane),
SayAction:new(bob, "How are you?"),
WaitAction:new(2),
SayAction:new(jane, "Fine")
)
行動將有status
屬性有三個可能的值:'new'
,'running'
,'finished'
。所有的「動作類別」將是Action
的子類,它們將定義start
和stop
方法,並且默認情況下將狀態初始化爲'new'
。這裏也將是一個默認的update
方法引發錯誤
行動
Action = class('Action')
function Action:initialize() self.status = 'new' end
function Action:stop() self.status = 'finished' end
function Action:start() self.status = 'running' end
function Action:update(dt)
error('You must re-define update on the subclasses of Action')
end
子類可以提高在這些方法,並實現update
。例如,這裏的WaitAction
:
WaitAction = class('WaitAction', Action) -- subclass of Action
function WaitAction:start()
Action.start(self) -- invoke the superclass implementation of start
self.startTime = os.getTime() -- or whatever you use to get the time
end
function WaitAction:update(dt)
if os.getTime() - self.startTime >= 2 then
self:stop() -- use the superclass implementation of stop
end
end
唯一缺少的實現部分是過場動畫。過場動畫將主要有三個方面: *動作列表執行 * A參考當前動作,或動作列表 對行動的指數*如下所示的更新方法:
function CutScene:update(dt)
local currentAction = self:getCurrentAction()
if currentAction then
currentAction:update(dt)
if currentAction.status == 'finished' then
self:moveToNextAction()
-- more refinements can be added here, for example detecting the end of actions
end
end
end
有了這個結構,你唯一需要的就是你的遊戲循環在每次循環迭代時調用helloJane:update(dt)
。而且你消除了對協程的需求。
協程,FSM和基於事件的編程的替代方法是CSP(通信順序進程)。查看LuaCSP實現(在協程上): https://github.com/loyso/LuaCSP注意:人們通常缺少的是協調和溝通方面。 – 2013-03-12 10:10:01