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

📄 hero.cpp

📁 一个symbian上成熟的小游戏源码
💻 CPP
字号:
#include "Hero.h"

namespace mygame
{


CHero::CHero()
{
}
CHero::~CHero()
{
}

CHero* CHero::NewLC()
{
	CHero* self = new (ELeave)CHero();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CHero* CHero::NewL()
{
	CHero* self=CHero::NewLC();
	CleanupStack::Pop(); 
	return self;
}

void CHero::ConstructL()
{
	isCollide = 0;
	iStep = 4;

	iSeqLeft = new( ELeave) TInt[3];
	iSeqRight = new( ELeave) TInt[3];
	iSeqUp = new( ELeave) TInt[3];
	iSeqDown = new( ELeave) TInt[3];
	for ( TInt n = 1; n <= 3; n++ )
		iSeqDown[n-1] = n;
	for ( TInt n = 4; n <= 6; n++ )
		iSeqUp[n-4] = n;
	for ( TInt n = 7; n <= 9; n++ )
		iSeqRight[n-7] = n;
	for ( TInt n = 10; n <= 12; n++ )
		iSeqLeft[n-10] = n;
}

/*
TInt CHero::AccessKeyDown( TInt aKeyCode, CTiledLayer* aLayer, CCamera* aCamera )
{
	TInt r, c;
	isCollide = CollideWith( aLayer, aKeyCode, r, c );
	if ( isCollide <= 0  )
		OnAccessKeyDown( aKeyCode, aCamera );
	else
	{
		//  遇到障碍物自己绕行  上下键
		if ( aKeyCode == EStdKeyDownArrow || aKeyCode == EStdKeyUpArrow )
		{
			TInt dR = FindRLShortCut( aLayer, r, c,aKeyCode );
			OnAccessKeyDown( dR, aCamera );
		}

		//  左右键
		if ( aKeyCode == EStdKeyLeftArrow || aKeyCode == EStdKeyRightArrow )
		{
			TInt dR = FindUDShortCut( aLayer, r, c, aKeyCode );
			OnAccessKeyDown( dR, aCamera );
		}
	}
	return isCollide;
}

TInt CHero::CollideWith( CTiledLayer* aLayer, TInt aKeyCode, TInt& aR, TInt& aC )
{
	TRect  rect0 = CheckRect();
	aR = aC = -1;
	TInt  rows = aLayer->SceneRows( );
	TInt  cols = aLayer->SceneCols( );
	for ( TInt r = 0; r < rows; r++  )
	{
		for ( TInt c = 0; c < cols; c++ )
		{
			TBool collideChack = ETrue;
			TRect  rect = aLayer->RectOfCell( r + 1, c + 1 );
			TInt rTop = rect.iTl.iY;
			TInt rLeft = rect.iTl.iX;
			TInt rBootom = rect.iBr.iY;
			TInt rRight = rect.iBr.iX;
			switch  ( aKeyCode )
			{
			case EStdKeyLeftArrow:
				if ( rTop >= iY + iCellH || rBootom<= iY + iCellH / 2 || rLeft >= iX + iCellW )
					collideChack = EFalse;
				break;
			case EStdKeyRightArrow:
				if ( rTop >= iY + iCellH || rBootom <= iY + iCellH / 2 || rRight <= iX )
					collideChack = EFalse;
				break;
			case EStdKeyUpArrow:
				if ( rBootom >= iY + iCellH  || rRight <= iX  || rLeft >= iX + iCellW )
					collideChack = EFalse;
				break;
			case EStdKeyDownArrow:
				if ( rBootom<= iY + iCellH / 2 || rRight <= iX  || rLeft >= iX + iCellW )
					collideChack = EFalse;
				break;
			default:
				break;
			}

			TInt index = aLayer->CellIndex( r, c );
			if ( index > 0 && collideChack  )
			{	
				if ( BeCollideWith( rect0, rect ) )
				{
					aR = r;
					aC = c;
					return index;
				}
			}
		}
	}
	return -1;
}

void CHero::Move( TInt aX, TInt aY, TBool aStop )
{
	//	计算图片起始帧和位置
	if ( aY > 0 )
	    SetSequence( iSeqDown, 3 );
	else if ( aY < 0 )
		SetSequence( iSeqUp, 3 );

	if ( aX > 0 )
		SetSequence( iSeqRight, 3 );
	else if ( aX < 0 )
		SetSequence( iSeqLeft, 3 );

	// 根据照相机的状态确定人物是否可以移动
	// 开始时人物处于屏幕中间,当照相机移不动时,人物根据方向可以移动到屏幕的
	// 上下左右边;当照相机可移动时,人物的移动目标位置在屏幕中央;
	TPoint tmpPos = TPoint( iX, iY );
	tmpPos += TPoint( aX, aY );
	if ( aStop )																// 照相机不能移动
	{
		if ( aX != 0 )
		{
			if ( tmpPos.iX >= 0 && ( tmpPos.iX+ CellW() ) <= 240 )
			{
				iX += aX;
				return;
			}
		}

		if ( aY != 0 )
		{
			if ( tmpPos.iY >= 0 && ( tmpPos.iY+ CellH() ) <= 320 )
			{
				iY += aY; 
				return;
			}
		}
	}
	else																	    // 照相机可以移动
	{
		if ( aX < 0 )														//  向左行走
		{
			if ( tmpPos.iX >= 120 )								//  如果人物在屏幕的右半部分
				iX += aX;			
		} else if ( aX > 0 )											//  向右行走
		{
			if ( tmpPos.iX <= 120 )
				iX += aX;
		}

 		if ( aY < 0 )														// 向上行走
 		{
 			if ( tmpPos.iY >= 160 )
 				iY += aY;			
 		} else if ( aY > 0 )											// 向下行走
 		{
 			if ( tmpPos.iY <= 160 )
 				iY += aY;
 		}
	}
}

TInt CHero::FindRLShortCut( CTiledLayer* aLayer, TInt aR, TInt aC, TInt aKeyCode )
{
	TInt stepCount1 = 0;						// 向左行走路线步长
	TInt stepCount2 = 0;						// 向右行走路线步长
	TInt aR1, aR2;
	if ( aKeyCode == EStdKeyDownArrow )
	{
		aR1 = aR - 1;
		aR2 = aR - 2;
	}
	else
	{
		aR1 = aR + 1;
		aR2 = aR + 2;
	}

	// 向左查询
	for ( TInt c = aC; c >= 0; c-- )
	{
		TInt index = aLayer->CellIndex( aR, c );
		TInt index0 = aLayer->CellIndex( aR1, c );
		TInt index1 = aLayer->CellIndex( aR2, c );
		if ( index0 > 0 || index1 >0 )
		{
			stepCount1 = -1;
			break;
		}

		if ( c == 0 && index > 0 )		//  判断边界单元格
		{
			stepCount1 = -1;
			break;
		}

		if ( index <= 0 )
			break;
		stepCount1++;
	}

	// 向右查询
	for ( TInt c = aC; c < aLayer->LayerCols(); c++ )
	{
		TInt index = aLayer->CellIndex( aR, c );
		TInt index0 = aLayer->CellIndex( aR1, c);
		TInt index1 = aLayer->CellIndex( aR2, c);
		if ( index0 > 0 || index1 >0 )
		{
			stepCount2 = -1;
			break;
		}

		if ( c == aLayer->LayerCols() - 1 && index > 0 )  		//  判断边界单元格
		{
			stepCount2 = -1;
			break;
		}

		if ( index <= 0 )
			break;
		stepCount2++;
	}

	// 确定方向
	if ( stepCount1 == -1 && stepCount2 != -1 )
		return EStdKeyRightArrow;
	if ( stepCount2 == -1 && stepCount1 != -1 )
		return EStdKeyLeftArrow;

	if ( stepCount1 != -1 && stepCount2 != -1 )
	{
		if ( stepCount1 >= stepCount2	)
			return EStdKeyRightArrow;
		else
			return EStdKeyLeftArrow;
	}
	return -1;
}

TInt CHero::FindUDShortCut( CTiledLayer* aLayer, TInt aR, TInt aC, TInt aKeyCode )
{
	TInt stepCount1 = 0;					// 向上行走路线步长
	TInt stepCount2 = 0;					// 向下行走路线步长
	TInt aC1, aC2;
	if ( aKeyCode == EStdKeyRightArrow )
	{
		aC1 = aC - 1;
		aC2 = aC - 2;
	}
	else
	{
		aC1 = aC + 1;
		aC2 = aC + 2;
	}

	// 向上查询
	for ( TInt r = aR; r >= 0; r-- )
	{
		TInt index = aLayer->CellIndex( r, aC );
		TInt index0 = aLayer->CellIndex( r, aC1 );
		TInt index1 = aLayer->CellIndex( r, aC2 );
		if ( index0 > 0 || index1 >0 )
		{
			stepCount1 = -1;
			break;
		}

		if ( r == 0 && index > 0 )		//  判断边界单元格
		{
			stepCount1 = -1;
			break;
		}

		if ( index <= 0 )
			break;
		stepCount1++;
	}

	// 向下查询
	for ( TInt r = aR; r < aLayer->LayerRows(); r++ )
	{
		TInt index = aLayer->CellIndex( r, aC  );
		TInt index0 = aLayer->CellIndex( r, aC1 );
		TInt index1 = aLayer->CellIndex( r, aC2 );
		if ( index0 > 0 || index1 >0 )
		{
			stepCount2 = -1;
			break;
		}

		if ( r == aLayer->LayerRows( ) -1  && index > 0 )
		{
			stepCount2 = -1;
			break;
		}
		if ( index <= 0  )
			break;

		stepCount2++;
	}

	// 确定方向
	if ( stepCount1 == -1 && stepCount2 != -1 )
		return EStdKeyDownArrow;
	if ( stepCount2 == -1 && stepCount1 != -1 )
		return EStdKeyUpArrow;
	if ( stepCount1 != -1 && stepCount2 != -1 )
	{
		if ( stepCount1 >= stepCount2	)
			return EStdKeyDownArrow;
		else
			return EStdKeyUpArrow;
	}
	return -1;
}

void CHero::OnAccessKeyDown( TInt aKeyCode, CCamera* aCamera )
{
	switch ( aKeyCode )
	{
	case EStdKeyLeftArrow:											//  向左移动
		aCamera->Move( -iStep, 0 );
		Move( -iStep, 0, aCamera->iStop );
		break;
	case EStdKeyRightArrow:											//  向右移动
		aCamera->Move( iStep, 0 );
		Move( iStep, 0, aCamera->iStop );
		break;
	case EStdKeyUpArrow:												// 向上
		aCamera->Move( 0, -iStep );
		Move( 0, -iStep, aCamera->iStop );
		break;
	case EStdKeyDownArrow:										// 向下
		aCamera->Move( 0, iStep );
		Move( 0, iStep, aCamera->iStop );
		break;
	default:
		break;
	}
	NextFrame();
}

TRect CHero::CheckRect()
{
	return TRect( iX, iY + iCellH / 2, iX + iCellW, iY + iCellH );
}

void CHero::GetPosition( TInt& aX, TInt& aY )
{
	aX = iX;
	aY = iY;
}
*/


}

⌨️ 快捷键说明

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