⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doctrine.cpp

📁 一个模拟动作源码
💻 CPP
字号:
#include "stdafx.h"
#include "Doctrine.h"
#include "StateObject.h"
#include "StateMap.h"
#include "TypeDefine.h"


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

CDoctrine::CDoctrine()
{ 
	m_pCurrentState = NULL;
	m_pInitialState = NULL;
	
	m_pStateMap = new CStateMap;
	m_bIsStarted = FALSE;
}

CDoctrine::~CDoctrine(void)
{
	if (m_pStateMap)
	{
		delete m_pStateMap;
	}
}

void CDoctrine::Clear()
{
	if ( !m_pStateMap )
	{
		CDoctrineLog::Instance().WriteLog(LOG_WARNING, "CDoctrine", "AddActionHandlerFcn", "m_pStateMap");
		return;
	}

	CDoctrineLog::Instance().WriteLog(LOG_TRACE, "CDoctrine", "Clear", "");
	m_pStateMap->RemoveAll();
	SetCurrentState(NULL);
}

void CDoctrine::Start()
{
	if( m_pInitialState )
	{
		CDoctrineLog::Instance().WriteLog(LOG_TRACE, "CDoctrine", "Start", "");
		m_pCurrentState = m_pInitialState;
		m_pCurrentState->OnEnter();
		m_bIsStarted = TRUE;
	}
	else
	{
		CDoctrineLog::Instance().WriteLog(LOG_CRITICALERROR, "CDoctrine", "Start", "No initial state defined");
	}
}

void CDoctrine::DispatchMsg(EnumMsgType eumMsgType, MSG_PTR* pMsg)
{
	if( m_pCurrentState && eumMsgType != MsgType_Unknown )
	{
		m_pCurrentState->OnMsg(eumMsgType, pMsg);
	}

	// TODO : test what happens if doctrine ended
}

void CDoctrine::AddActionHandlerFcn(const char* szHandlerName, void* pfcnHandler)
{
	if ( "" == szHandlerName )
	{
		CDoctrineLog::Instance().WriteLog(LOG_WARNING, "CDoctrine", "AddActionHandlerFcn", "Empty Action handler name");
		return;
	}
	if ( !pfcnHandler )
	{
		CDoctrineLog::Instance().WriteLog(LOG_WARNING, "CDoctrine", "AddActionHandlerFcn", "Null Action handler function pointer");
		return;
	}
	if ( !m_pStateMap )
	{
		CDoctrineLog::Instance().WriteLog(LOG_WARNING, "CDoctrine", "AddActionHandlerFcn", "m_pStateMap");
		return;
	}

	CStateObject* pStateObject = m_pStateMap->GetStart();
	while (pStateObject != NULL)
	{
		pStateObject->AddActionHandlerFcn(szHandlerName, pfcnHandler);
		pStateObject = m_pStateMap->GetNext();
	}
}

void CDoctrine::AddPredicateHandlerFcn(const char* szHandlerName, void* pfcnHandler)
{
	if ( "" == szHandlerName )
	{
		CDoctrineLog::Instance().WriteLog(LOG_WARNING, "CDoctrine", "AddPredicateHandlerFcn", "Empty Predicate handler name");
		return;
	}
	if ( !pfcnHandler )
	{
		CDoctrineLog::Instance().WriteLog(LOG_WARNING, "CDoctrine", "AddPredicateHandlerFcn", "Null Predicate handler function pointer");
		return;
	}
	if ( !m_pStateMap )
	{
		CDoctrineLog::Instance().WriteLog(LOG_CRITICALERROR, "CDoctrine", "AddPredicateHandlerFcn", "m_pStateMap");
		return;
	}

	CStateObject* pStateObject = m_pStateMap->GetStart();
	while (pStateObject != NULL)
	{
		pStateObject->AddPredicateHandlerFcn(szHandlerName, pfcnHandler);
		pStateObject = m_pStateMap->GetNext();
	}
}

void CDoctrine::SetLogHandlerFcn(ENTITY_PTR* pEntity, void* pfcnLogHandler)
{
	CDoctrineLog::Instance().SetLogHandlerFcn(pEntity, pfcnLogHandler);
}

void CDoctrine::SetCurrentState(CStateObject* pStateObject)
{
	if( m_pCurrentState )
	{
		m_pCurrentState->OnLeave();
	}

	m_pCurrentState = pStateObject;

	if( m_pCurrentState )
	{
		CDoctrineLog::Instance().WriteLog(LOG_TRACE, "CDoctrine", "SetCurrentState", m_pCurrentState->GetName());
		m_pCurrentState->OnEnter();
	}
	else
	{
		CDoctrineLog::Instance().WriteLog(LOG_TRACE, "CDoctrine", "SetCurrentState", "Null");
	}
}

bool CDoctrine::IsEnded()
{
	if ( m_pCurrentState )
	{
		return m_pCurrentState->IsFinalState(); // TODO : maybe need to set m_bIsStarted
	}

	return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -