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

📄 cblockfollowing.cpp

📁 俄罗斯方块3D 程序+源码俄罗斯方块3D(程序+源码
💻 CPP
字号:
#include "CBlockFollowing.h"
#include "CBlockPool.h"
#include "CGameState.h"
#include "normal.h"


extern CGameState		theGameState;
extern CBlockPool		theBlockPool;

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
CBlockFollowing::CBlockFollowing()
{
	m_Pos.row = 0;
	m_Pos.col = 0;
	m_BlockArray = NULL;
	m_NewPosition = NULL;
	m_iNumBlock = 0;
}

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
CBlockFollowing::~CBlockFollowing()
{

}

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
bool CBlockFollowing::Init()
{
	bool re = true;
	re &= m_BlockArray1.SetSize( 32 );
	re &= m_BlockArray2.SetSize( 32 );
	m_BlockArray = m_BlockArray1.GetPointer();
	m_NewPosition= m_BlockArray2.GetPointer();
	m_iNumBlock = 0;
	m_Pos.row = 0;
	m_Pos.col = 0;
	return re;
}

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
void CBlockFollowing::Free()
{
	m_BlockArray1.Free();
	m_BlockArray2.Free();

	m_Pos.row = 0;
	m_Pos.col = 0;
	m_BlockArray = NULL;
	m_NewPosition = NULL;
	m_iNumBlock = 0;
}

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
void CBlockFollowing::OnCreateBlock()
{
	m_BlockArray1.SetLength( 0 );
	m_BlockArray2.SetLength( 0 );
	m_BlockArray = m_BlockArray1.GetPointer();
	m_NewPosition= m_BlockArray2.GetPointer();
	m_iNumBlock = 0;
}

/////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////
void CBlockFollowing::AddBlocks( CBlock &block )
{
	m_BlockArray1.Add( block );
	m_BlockArray2.Add( block );
	m_BlockArray = m_BlockArray1.GetPointer();
	m_NewPosition= m_BlockArray2.GetPointer();
	++m_iNumBlock;
}

/////////////////////////////////////////////////////////////
//交换原来状态与新状态数组的指针
/////////////////////////////////////////////////////////////
void CBlockFollowing::Transform()
{
	Swap( m_BlockArray, m_NewPosition );
}

/////////////////////////////////////////////////////////////
//判断新位置是否为空
/////////////////////////////////////////////////////////////
bool CBlockFollowing::IsMobile()
{
	bool re = true;
	for ( int i = 0; i < m_iNumBlock; ++i )
	{
		re &= theBlockPool.IsAvailable( m_NewPosition[i].GetPosition() );
	}

	return re;
}


/////////////////////////////////////////////////////////////
//方块下落
/////////////////////////////////////////////////////////////
void CBlockFollowing::Drop()
{
	for ( int i = 0; i < m_iNumBlock; ++i )
	{
		BlockDrop( m_NewPosition[i], m_BlockArray[i] );			//获取下落的新位置
	}

	if ( IsMobile() )						//检查是否下落到底部
	{
		Transform();
		--m_Pos.row;
	}
	else
	{
		Stop();
	}
}

/////////////////////////////////////////////////////////////
//方块转动
/////////////////////////////////////////////////////////////
void CBlockFollowing::Rotate()
{
	for ( int i = 0; i < m_iNumBlock; ++i )
	{
		BlockRotation( m_NewPosition[i], m_Pos, m_BlockArray[i] );	//获取转动后的新位置
	}

	if ( IsMobile() )						//检查新位置是否可占用
		Transform();
}

/////////////////////////////////////////////////////////////
//方块转动
//参数:IsLeft 为true往左移,为false往右移
/////////////////////////////////////////////////////////////
void CBlockFollowing::Move( bool IsLeft )
{
	int istep = ( IsLeft ? -1 : 1 );								//-1往左移,1往右移

	for ( int i = 0; i < m_iNumBlock; ++i )
	{
		BlockMovement( m_NewPosition[i], m_BlockArray[i], istep );
	}

	if ( IsMobile() )
	{
		Transform();
		m_Pos.col += istep;
	}
}

/////////////////////////////////////////////////////////////
//方块定位在方块池
/////////////////////////////////////////////////////////////
void CBlockFollowing::Stop()
{
	for ( int i = 0; i < m_iNumBlock; ++i )
	{
		theBlockPool.SetCell( m_BlockArray[i].GetPosition(), m_BlockArray[i].GetMaterialIndex() );
		theBlockPool.UpdateHighestLine( m_BlockArray[i].GetRow() );
	}

	if ( m_iNumBlock > 0 )
		theGameState.SetGameState( CHECK_FULL_LINE );			//设置游戏状态
	OnCreateBlock();
}

⌨️ 快捷键说明

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