www.pudn.com > Example2D.rar > CChrPlayer.cpp


/* 
* ============================================================================ 
*  Name     : CChrPlayer from CChrPlayer.h 
*  Part of  : 2DExample 
*  Created  : 01/01/2005 by Forum Nokia 
*  Version  : 1.0 
*  Copyright: Nokia Corporation 
* ============================================================================ 
*/ 
 
// INCLUDE FILES 
#include "CChrPlayer.h" 
#include "CSprite.h" 
#include "MSystem.h" 
#include "CTileMap.h" 
 
const TInt KMaxSpeed = 16500; 
 
 
 
CChrPlayer* CChrPlayer::NewL( CSprite* aSprite, MSystem* aSystem, CTileMap* aMap ) 
	{ 
	CChrPlayer* self = new( ELeave )CChrPlayer( aSprite, aSystem, aMap ); 
	CleanupStack::PushL( self ); 
	self->ConstructL(); 
	CleanupStack::Pop( self ); 
	return self; 
	}; 
 
 
 
CChrPlayer::~CChrPlayer() 
	{ 
 
	} 
 
 
 
CChrPlayer::CChrPlayer( CSprite* aSprite, MSystem* aSystem, CTileMap* aMap ) 
	: iSprite( aSprite ) 
	, iSystem( aSystem ) 
	, iTileMap( aMap ) 
	{ 
 
	} 
 
 
 
void CChrPlayer::ConstructL() 
	{ 
 
	} 
 
 
 
void CChrPlayer::ActivateL() 
	{ 
 
	} 
 
 
 
void CChrPlayer::Deactivate() 
	{ 
 
	} 
 
 
 
void CChrPlayer::Draw( CBitmap& aTarget, const TPoint& aCamera ) 
	{ 
	if( iSpeed.iX > 0 ) iXDir = 1; 
	if( iSpeed.iX < 0 ) iXDir = 0; 
 
	TInt spr = ( iPosition.iX >> 19 ) & 3; 
	spr += iXDir * 4; 
	 
	iSprite->Draw( aTarget, spr, aCamera ); 
	} 
 
 
 
void CChrPlayer::Move() 
	{ 
	TBool move = EFalse; 
 
	if( !iPlayerDead ) 
		{ 
		if( iSystem->KeyState( EStdKeyLeftArrow ) || iSystem->KeyState( EStdKeyDevice6 ) ) 
			{ 
			if( ( iPosition.iX >> 16 ) <= 0 ) // Beginning of tilemap 
				{ 
				iSpeed.iX -= iSpeed.iX; 
				} 
			else 
				{ 
				iSpeed.iX -= 256; 
				move = ETrue; 
				} 
			} 
		if( iSystem->KeyState( EStdKeyRightArrow )  || iSystem->KeyState( EStdKeyDevice7 ) ) 
			{ 
			if( ( iPosition.iX >> 16 ) > 253*8 ) // End of Tilemap 
				{ 
				iSpeed.iX -= iSpeed.iX; 
				} 
			else 
				{ 
				iSpeed.iX += 256; 
				move = ETrue; 
				} 
			} 
 
		if( ! move ) 
			{ 
			if( iSpeed.iX > 0 )  
				{ 
				iSpeed.iX -= 32; 
				if( iSpeed.iX < 0 ) 
					{ 
					iSpeed.iX = 0; 
					} 
				} 
 
			if( iSpeed.iX < 0 )  
				{ 
				iSpeed.iX += 32; 
				if( iSpeed.iX > 0 ) 
					{ 
					iSpeed.iX = 0; 
					} 
				} 
			} 
		 
		if( iSpeed.iX > KMaxSpeed ) iSpeed.iX = KMaxSpeed; 
		if( iSpeed.iX < -KMaxSpeed ) iSpeed.iX = -KMaxSpeed; 
		if( iSpeed.iY > KMaxSpeed ) iSpeed.iY = KMaxSpeed; 
		 
		TInt x = iPosition.iX >> 16; 
		TInt y = iPosition.iY >> 16; 
 
		TInt tile1 = iTileMap->Tile( TPoint( x+5, y+23 ), 0 ); 
		TInt tile2 = iTileMap->Tile( TPoint( x+20, y+23 ), 0 ); 
 
		if( tile1 == 1 && tile2 == 1 ) 
			{ 
			iSpeed.iY += 450; 
			} 
		else 
			{ 
			iSpeed.iY = 0; 
			if( iSystem->KeyState( EStdKeyUpArrow )  || iSystem->KeyState( EStdKeyDevice8 ) ) 
				{ 
				iSpeed.iY = -KMaxSpeed * 4; 
				} 
			} 
		 
		tile1 = iTileMap->Tile( TPoint( x+4, y ), 0 ); 
		tile2 = iTileMap->Tile( TPoint( x+19, y ), 0 ); 
		if( tile1 != 1 || tile2 != 1 ) 
			{ 
			if( iSpeed.iY < 0 ) 
				{ 
				iSpeed.iY = 0; 
				} 
			} 
 
		tile1 = iTileMap->Tile( TPoint( x+3, y+20 ), 0 ); 
		tile2 = iTileMap->Tile( TPoint( x+21, y+20 ), 0 ); 
 
		if( tile1 != 1 ) 
			{ 
			if( iSpeed.iX < 0 ) 
				{ 
				iSpeed.iX = 0; 
				} 
			} 
 
		if( tile2 != 1 ) 
			{ 
			if( iSpeed.iX > 0 ) 
				{ 
				iSpeed.iX = 0; 
				} 
			} 
 
		iPosition += iSpeed; 
		} 
	else 
		{ // Player is dead, make last jump 
		iSpeed.iY += 256; 
		iPosition += iSpeed; 
		} 
 
	iSprite->SetPosition( TPoint( iPosition.iX >> 16, iPosition.iY >> 16 ) ); 
	} 
 
 
 
TPoint CChrPlayer::Position() 
	{ 
	return TPoint( iPosition.iX >> 16, iPosition.iY >> 16 ); 
	} 
 
 
 
void CChrPlayer::SetPosition( const TPoint& aPosition ) 
	{ 
	iPosition.iX = aPosition.iX << 16; 
	iPosition.iY = aPosition.iY << 16; 
	} 
 
 
 
void CChrPlayer::Die() 
	{ 
	iPlayerDead = ETrue; 
 
	iSpeed.iX = 0; 
	iSpeed.iY = -KMaxSpeed * 3; // Make Player Jump 
	} 
 
 
 
void CChrPlayer::SetType( const TChType& aChType ) 
	{ 
	iChType = aChType; 
	} 
 
 
 
CSprite* CChrPlayer::Sprite() 
	{ 
	return iSprite; 
	} 
 
 
 
TChType CChrPlayer::Type() 
	{ 
	return iChType; 
	} 
	 
// End of file