QTE为Quick Time Event的简写,多见于主机游戏中,通常以在规定时间内根据提示完成(一系列)按键操作的形式出现。就表现上看,其实是过场动画的一种分解触发机制,用于表现看似复杂实则固定的动作或者场景效果,一方面在消除了单纯过场乏味感的同时增强了游戏的互动性,另一方面还降低了表现复杂效果的开发难度。
图1 QTE in GOD OF WAR III
目前MMORPG的一个开发趋势就是融入主机游戏中的一些元素,提升网游在传统游戏性之外的可玩性。本文主要根据自己在项目中开发的QTE系统,介绍一个相对比较简单的原型框架。
一、分析
以一个限时按键的QTE来分析,其中包括的元素有:时间、键、结果。时间与键可看作Event的约束条件,它们共同决定该Event是否达成。而结果可作为一条消息发送给关心者,然后由后者决定与之相关的表现。
图2
鼠标作为PC上重要的外设,当然会被用于QTE,其形式可能会包括:摇晃、按键、滚轮滚动等等。Event是否达成的判断流程,与限时按键大同小异。
有时候Designer可能会设计一系列Event,增加操作难度,这时就需要考虑将多个Event串联起来,只有所有Event达成时,该QTE才能算达成。因此,在图1之上,需要加入另一个判断逻辑。
图3
二、实现
通过分析,不难得出一个典型的QTE系统的核心数据结构:Event链表。此外,为了将结果反馈给QTE请求者,还应该提供一个通用的通知机制。在这个原型框架中,通过加入一个中间层来隔离异构的请求者,实现了回调机制。
图4
QTEFunctor作为处理Event的仿函数基类,给出了各种QTE的处理接口。
Code Snippet
- class QTEFunctor
- {
- public:
- QTE_TYPE m_type;
- protected:
- float _fTimer;
- float _fDuration;
- unsigned int _uValue; ///< condition
- int _iRetValue; ///< returned value
- public:
- virtual QTEFUNCTOR_RETVAL operator() (float) = 0;
- unsigned int GetValue() { return _iRetValue; }
- };
-
- class MouseShakeFunctor : public QTEFunctor
- {
- private:
- CPoint _point;
- public:
- MouseShakeFunctor(const QTECondition&);
- ~MouseShakeFunctor() {}
- QTEFUNCTOR_RETVAL operator() (float);
- };
-
- class KeyDownFunctor : public QTEFunctor
- {
- public:
- KeyDownFunctor(const QTECondition&);
- ~KeyDownFunctor() {}
- QTEFUNCTOR_RETVAL operator() (float);
- };
QTEDelegateBase提供了回调函数的统一接口,为兼容类函数和非类函数,分别派生出了QTETypeDelegate和QTENoTypeDelegate。
Code Snippet
- class QTEDelegateBase
- {
- public:
- virtual void Call(QTEManager::QTE_TYPE , int) = 0;
- virtual ~QTEDelegateBase() {}
- protected:
- QTEDelegateBase() {}
- };
-
- /// non-class method callback
- typedef void (*CallbackFuncPtrType)(QTEManager::QTE_TYPE , int); ///< FAILED: second parameter <=0, OK : >0 (may include additional meanings)
- class QTENoTypeDelegate : public QTEDelegateBase
- {
- public:
- QTENoTypeDelegate(CallbackFuncPtrType func) : _pCallbackFunc(func) {}
- ~QTENoTypeDelegate() {}
- void Call(QTEManager::QTE_TYPE , int) {}
- private:
- CallbackFuncPtrType _pCallbackFunc;
- };
-
- /// class method callback
- template<class C>
- class QTETypeDelegate : public QTEDelegateBase
- {
- public:
- QTETypeDelegate(C* , void (C::*)(QTEManager::QTE_TYPE , int));
- ~QTETypeDelegate() {}
- void Call(QTEManager::QTE_TYPE , int);
- private:
- C* _pObj;
- void (C::*_pCallbackFunc)(QTEManager::QTE_TYPE , int);
- };
-
- template<class C>
- QTETypeDelegate<C>::QTETypeDelegate(C* pObj , void (C::*func)(QTEManager::QTE_TYPE , int))
- : _pObj(pObj) , _pCallbackFunc(func)
- {
-
- }
-
- template<class C>
- void QTETypeDelegate<C>::Call(QTEManager::QTE_TYPE type , int value)
- {
- (_pObj->*_pCallbackFunc)(type , value);
- }
QTEManager中采用了vector容器来保存待处理的多个Event链,并提供申请QTE、取消QTE的接口,以及Event是否达成的逻辑更新。
Code Snippet
- void QTEManager::RequestQTE(QTEDelegateBase* pQTE , std::vector<QTECondition>& condList)
- {
- std::vector<QTECondition>::iterator iter = condList.begin();
- QTELinkNode* pNode = NULL;
- QTELinkNode* pNextNode = NULL;
-
- for(; iter != condList.end(); ++iter)
- {
- if(pNextNode)
- {
- pNextNode->pNext = new QTELinkNode;
- pNextNode = pNextNode->pNext;
- }
- else
- {
- pNode = new QTELinkNode;
- pNextNode = pNode;
- }
-
- switch(iter->type)
- {
- case QT_MOUSE_SHAKE:
- pNextNode->pDelegate = pQTE;
- pNextNode->pFunctor = new MouseShakeFunctor(*iter);
- break;
- case QT_KEY_DOWN:
- pNextNode->pDelegate = pQTE;
- pNextNode->pFunctor = new KeyDownFunctor(*iter);
- break;
- case QT_KEY_PRESS:
- pNextNode->pDelegate = pQTE;
- pNextNode->pFunctor = new KeyPressFunctor(*iter);
- break;
- }
- }
-
- if(pNode)
- _RequestList.push_back(pNode);
- }
-
- void QTEManager::CancelQTE(QTEDelegateBase* pQTE)
- {
- QTEListType::iterator iter = _RequestList.begin();
-
- for(; _RequestList.end() != iter; ++iter)
- {
- if(pQTE == (*iter)->pDelegate)
- {
- (*iter)->pDelegate->Call((*iter)->pFunctor->m_type , -1 , 0); ///< failed
-
- QTELinkNode* pNode = *iter;
- /// delete the rest node
- while(pNode)
- {
- *iter = pNode->pNext;
- delete pNode;
- pNode = *iter;
- }
- _RequestList.erase(iter);
- break;
- }
- }
- }
Code Snippet
- #define STEP_OK_BASE 1000
- #define SUCCESS_BASE 2000
-
- void QTEManager::Update(float dt)
- {
- QTEListType::iterator iter = _RequestList.begin();
- for(; iter != _RequestList.end();)
- {
- QTEFUNCTOR_RETVAL ret = (*iter)->pFunctor->operator ()(dt);
- if(QTEFR_FAILED == ret)
- {
- (*iter)->pDelegate->Call((*iter)->pFunctor->m_type , -1); ///< failed
-
- QTELinkNode* pNode = *iter;
- /// delete the rest event
- while(pNode)
- {
- *iter = pNode->pNext;
- delete pNode;
- pNode = *iter;
- }
- iter = _RequestList.erase(iter);
- }
- else if(QTEFR_OK == ret)
- {
- /// get next event
- if((*iter)->pNext)
- {
- QTELinkNode* pNode = *iter;
- pNode->pDelegate->Call(pNode->pFunctor->m_type ,
- STEP_OK_BASE + pNode->pFunctor->GetValue()); ///< step ok
- *iter = (*iter)->pNext;
- delete pNode;
- ++iter;
- }
- else
- {
- (*iter)->pDelegate->Call((*iter)->pFunctor->m_type ,
- SUCCESS_BASE + (*iter)->pFunctor->GetValue()); ///< finally ok
- delete *iter;
- iter = _RequestList.erase(iter);
- }
- }
- else if(QTEFR_PENDING == ret)
- {
- (*iter)->pDelegate->Call((*iter)->pFunctor->m_type ,
- (*iter)->pFunctor->GetValue()); ///< continue progressing
- ++iter;
- }
- }
- }