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

📄 objectfactory.cpp

📁 奇迹世界 部分源代码奇迹世界 部分源代码奇迹世界 部分源代码
💻 CPP
字号:
#include "stdafx.h"
#include ".\objectfactory.h"
#include ".\object.h"
#include ".\Player.h"
#include ".\Item.h"
#include ".\MapObject.h"
#include <Macro.h>
#include "Monster.h"
#include "Summoned.h"
#include "MapNPC.h"


ObjectFactory::ObjectFactory()
{
	m_pPlayerPool		= new CMemoryPoolFactory<Player>;
	m_pMonsterPool		= new CMemoryPoolFactory<Monster>;
	m_pSummonedPool		= new CMemoryPoolFactory<Summoned>;
	m_pItemPool			= new CMemoryPoolFactory<Item>;
	m_pMapObjectPool	= new CMemoryPoolFactory<MapObject>;
	m_pMapNPCPool		= new CMemoryPoolFactory<MapNpc>;
}

ObjectFactory::~ObjectFactory()
{
	SAFE_DELETE( m_pPlayerPool );		
	SAFE_DELETE( m_pMonsterPool );
	SAFE_DELETE( m_pSummonedPool );
	SAFE_DELETE( m_pItemPool );
	SAFE_DELETE( m_pMapObjectPool );
	SAFE_DELETE( m_pMapNPCPool );

	SAFE_DELETE( m_pMonsterKeyGenerator );
	SAFE_DELETE( m_pFieldItemKeyGenerator );
}

VOID ObjectFactory::Init( DWORD dwPlayerPool, DWORD dwMonsterPool, DWORD dwItemPool, DWORD dwMapObjectPool, DWORD dwMapNPCPool )
{
	m_pPlayerPool->Initialize( dwPlayerPool, dwPlayerPool/4+1 );
	m_pMonsterPool->Initialize( dwMonsterPool, dwMonsterPool/4+1 );
	m_pSummonedPool->Initialize( dwPlayerPool, dwPlayerPool/4+1 );
	m_pItemPool->Initialize( dwItemPool, dwItemPool/4+1 );
	m_pMapObjectPool->Initialize( dwMapObjectPool );
	m_pMapNPCPool->Initialize( dwMapNPCPool );

	m_pMonsterKeyGenerator = new util::CObjKeyGenerator( MONSTER_OBJECT_KEY + 1, NPC_OBJECT_KEY );
	m_pFieldItemKeyGenerator = new util::CObjKeyGenerator( ITEM_OBJECT_KEY + 1, MAX_OBJECT_KEY );
}

VOID ObjectFactory::Release()
{
	if(m_pPlayerPool)		m_pPlayerPool->Release();
	if(m_pMonsterPool)		m_pMonsterPool->Release();
	if(m_pSummonedPool)		m_pSummonedPool->Release();
	if(m_pItemPool)			m_pItemPool->Release();
	if(m_pMapObjectPool)	m_pMapObjectPool->Release();
	if(m_pMapNPCPool)		m_pMapNPCPool->Release();
}

Object * ObjectFactory::AllocObject( eOBJECT_TYPE eObjecType )
{
	Object * pNewObject = NULL; 
	DWORD dwKey = 0;
	switch( eObjecType )
	{	
	case PLAYER_OBJECT:
		{
			pNewObject = m_pPlayerPool->Alloc();
			dwKey = 1;
		}
		break;
	case MONSTER_OBJECT:
		{
			pNewObject = m_pMonsterPool->Alloc();
			dwKey = m_pMonsterKeyGenerator->GetKey();
		}
		break;
	case SUMMON_OBJECT:
		{
			pNewObject = m_pSummonedPool->Alloc();
			dwKey = m_pMonsterKeyGenerator->GetKey();
		}
		break;
	case ITEM_OBJECT:
		{
			Item * pItem = m_pItemPool->Alloc();
			//pItem->setFieldItemType( eFIELDITEM_ITEM );
			pItem->SetObjectType( ITEM_OBJECT );
			pNewObject = pItem;
			dwKey = m_pFieldItemKeyGenerator->GetKey();
		}
		break;
	case MONEY_OBJECT:
		{
			Item * pItem = m_pItemPool->Alloc();
			//pItem->setFieldItemType( eFIELDITEM_MONEY );
			pItem->SetObjectType( MONEY_OBJECT );
			pNewObject = pItem;
			dwKey = m_pFieldItemKeyGenerator->GetKey();
		}
		break;
	case MAP_OBJECT:
		{
			pNewObject = m_pMapObjectPool->Alloc();
			dwKey = 1;
		}
		break;
	case MAPNPC_OBJECT:
		{
			pNewObject = m_pMapNPCPool->Alloc();
			dwKey = m_pMonsterKeyGenerator->GetKey();
		}
		break;
	case CHARACTER_OBJECT:
	case NONCHARACTER_OBJECT:
	case OBJECT_OBJECT:
	default:
		{
			ASSERT( !"积己且 荐 绝绰 Object涝聪促." );
		}
	}
	SASSERT( 0 != dwKey, VSTR("虐惯鞭阑 且 荐 绝嚼聪促. 钱 促靖, 鸥涝(%d)", eObjecType) );

	pNewObject->SetObjectKey( dwKey );
	return pNewObject;
}

VOID ObjectFactory::FreeObject( Object * pObject )
{
	switch( pObject->GetObjectType() )
	{	
	case PLAYER_OBJECT:
		{
			//m_pPlayerKeyGenerator->RestoreKey( pObject->GetObjectKey() );
			pObject->Reuse();
			return m_pPlayerPool->Free( (Player *)pObject );
		}
		break;
	case MONSTER_OBJECT:
		{
			m_pMonsterKeyGenerator->RestoreKey( pObject->GetObjectKey() );

			// 馆券窍扁 傈俊 group捞 NULL捞 登绢具 茄促! LeaveGroup捞 龋免登瘤 臼疽阑 啊瓷己捞 怒. 眉农眉农!
			pObject->Reuse();

			Monster* pMonster = dynamic_cast<Monster*>(pObject);
			SASSERT( pMonster != NULL, "[ObjectFactory::FreeObject] pMonster == NULL" );
			SASSERT( pMonster->GetGroup() == NULL, "[ObjectFactory::FreeObject] pMonster->GetGroup() != NULL" );

			return m_pMonsterPool->Free( (Monster*)pObject );
		}
		break;
	case SUMMON_OBJECT:
		{
			m_pMonsterKeyGenerator->RestoreKey( pObject->GetObjectKey() );

			pObject->Reuse();

			Summoned* pSummoned = dynamic_cast<Summoned*>(pObject);
			SASSERT( pSummoned != NULL, "[ObjectFactory::FreeObject] pSummoned == NULL" );

			return m_pSummonedPool->Free( (Summoned*)pObject );
		}
		break;
	case MAPNPC_OBJECT:
		{
			m_pMonsterKeyGenerator->RestoreKey( pObject->GetObjectKey() );

			pObject->Reuse();

			MapNpc* pMapNpc = dynamic_cast<MapNpc*>(pObject);
			SASSERT( pMapNpc != NULL, "[ObjectFactory::FreeObject] pMapNpc == NULL" );

			return m_pMapNPCPool->Free( (MapNpc*)pObject );
		}
		break;
	case MONEY_OBJECT:
	case ITEM_OBJECT:
		{
			m_pFieldItemKeyGenerator->RestoreKey( pObject->GetObjectKey() );

			pObject->Reuse();
			return m_pItemPool->Free( (Item*)pObject );
		}
		break;
	case MAP_OBJECT:
		{
			pObject->Reuse();
			return m_pMapObjectPool->Free( (MapObject*)pObject );
		}
		break;
	case CHARACTER_OBJECT:
	case NONCHARACTER_OBJECT:
	case OBJECT_OBJECT:
	default:
		{
			ASSERT( !"昏力 且 荐 绝绰 Object涝聪促." );
		}
	}
}

VOID ObjectFactory::DisplayPoolInfo()
{
	DISPMSG( "[%4u,%4u][band:%u node:%u] Player\n", m_pPlayerPool->GetPoolBasicSize(), m_pPlayerPool->GetPoolExtendSize(), 
		m_pPlayerPool->GetNumberOfBands(), m_pPlayerPool->GetAvailableNumberOfTypes() );
	DISPMSG( "[%4u,%4u][band:%u node:%u] Monster\n", m_pMonsterPool->GetPoolBasicSize(), m_pMonsterPool->GetPoolExtendSize(), 
		m_pMonsterPool->GetNumberOfBands(), m_pMonsterPool->GetAvailableNumberOfTypes() );
	DISPMSG( "[%4u,%4u][band:%u node:%u] Summoned\n", m_pSummonedPool->GetPoolBasicSize(), m_pSummonedPool->GetPoolExtendSize(), 
		m_pSummonedPool->GetNumberOfBands(), m_pSummonedPool->GetAvailableNumberOfTypes() );
	DISPMSG( "[%4u,%4u][band:%u node:%u] Item\n", m_pItemPool->GetPoolBasicSize(), m_pItemPool->GetPoolExtendSize(), 
		m_pItemPool->GetNumberOfBands(), m_pItemPool->GetAvailableNumberOfTypes() );
	DISPMSG( "[%4u,%4u][band:%u node:%u] MapObject\n", m_pMapObjectPool->GetPoolBasicSize(), m_pMapObjectPool->GetPoolExtendSize(), 
		m_pMapObjectPool->GetNumberOfBands(), m_pMapObjectPool->GetAvailableNumberOfTypes() );
	DISPMSG( "[%4u,%4u][band:%u node:%u] MapNPC\n", m_pMapNPCPool->GetPoolBasicSize(), m_pMapNPCPool->GetPoolExtendSize(), 
		m_pMapNPCPool->GetNumberOfBands(), m_pMapNPCPool->GetAvailableNumberOfTypes() );

}

⌨️ 快捷键说明

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