www.pudn.com > GameEngine_src.rar > CBaseSprite.cpp


#include "CBaseSprite.h" 
#include "CMap.h" 
#include "BaseUtil.h" 
#include "CPathSeeker.h" 
#include  
 
extern CMap	theMap; 
extern CPathSeeker	thePathSeeker; 
extern PEASYDRAW	g_pEasyDraw; 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
CBaseSprite::CBaseSprite() 
{ 
	m_bSelected		= false; 
	m_Priority		= 0; 
	m_Frame			= 0; 
	m_LastTick		= 0; 
	m_cur_pg		= 0; 
	m_cur_shadow_pg	= 0; 
	m_Priority		= 0; 
} 
 
CBaseSprite::~CBaseSprite() 
{ 
 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CBaseSprite::IsSelected( int x, int y ) 
{ 
	if ( x > m_Rect.left && x < m_Rect.right ) 
	{ 
		if ( y > m_Rect.top && y < m_Rect.bottom ) 
		{ 
			return true; 
		} 
	} 
 
	return false; 
} 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CBaseSprite::IsVisible( const RECT &Rect ) 
{ 
	if ( m_Rect.bottom < Rect.top || 
		 Rect.bottom < m_Rect.top || 
		 m_Rect.right < Rect.left || 
		 Rect.right < m_Rect.left ) 
		 return false; 
	else 
		return true; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CBaseSprite::CalcPriority() 
{ 
	int row = m_BasePoint.y/CELL_HEIGHT; 
	int col = m_BasePoint.x/CELL_WIDTH; 
 
	//m_Priority = ( ( row * theMap.GetCellPitch() + col ) * 12 + (int)m_SpriteType ); 
	m_Priority = ( (int)m_SpriteType >> 2 ) * theMap.GetCellNum() + (row * theMap.GetCellPitch() + col);														 
} 
 
////////////////////////////////////////////////////////////////////// 
//计算两个精灵之间的距离 
////////////////////////////////////////////////////////////////////// 
int CBaseSprite::CalcDistance( const POINT &base, const POINT &dest ) 
{ 
	int x_odds = abs(base.x - dest.x) / CELL_WIDTH; 
	int y_odds = abs(base.y - dest.y) / CELL_HEIGHT; 
 
	if ( x_odds == y_odds ) 
		return x_odds; 
	else 
		return ( x_odds > y_odds ? x_odds : y_odds ); 
} 
 
////////////////////////////////////////////////////////////////////// 
//计算两个精灵之间的方向 
////////////////////////////////////////////////////////////////////// 
int CBaseSprite::CalcDirection( const POINT &base, const POINT &dest ) 
{ 
	int x_odds = dest.x - base.x; 
	int y_odds = dest.y - base.y; 
	int direction = 1; 
 
	if ( x_odds > 0 ) 
	{ 
		if ( y_odds == 0 )			direction = 1; 
		else if ( y_odds > 0 )		direction = 2; 
		else						direction = 8; 
	} 
	else if ( x_odds == 0 ) 
	{ 
		if ( y_odds > 0 )			direction = 3; 
		else						direction = 7; 
	} 
	else 
	{ 
		if ( y_odds == 0 )			direction = 5; 
		else if ( y_odds > 0 )		direction = 4; 
		else						direction = 6; 
	} 
 
	return direction; 
} 
 
////////////////////////////////////////////////////////////////////// 
//计算两个精灵之间的方向 
////////////////////////////////////////////////////////////////////// 
int CBaseSprite::CalcDirection( const CELL &from, const CELL &to ) 
{ 
	int x_odds = to.col - from.col; 
	int y_odds = to.row - from.row; 
	int direction = 1; 
 
	if ( x_odds > 0 ) 
	{ 
		if ( y_odds == 0 )			direction = 1; 
		else if ( y_odds > 0 )		direction = 2; 
		else						direction = 8; 
	} 
	else if ( x_odds == 0 ) 
	{ 
		if ( y_odds > 0 )			direction = 3; 
		else						direction = 7; 
	} 
	else 
	{ 
		if ( y_odds == 0 )			direction = 5; 
		else if ( y_odds > 0 )		direction = 4; 
		else						direction = 6; 
	} 
 
	return direction; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CBaseSprite::GetNextPoint( int direction, POINT &next ) 
{ 
	next.x = m_BasePoint.x; 
	next.y = m_BasePoint.y; 
 
	switch ( direction ) 
	{ 
	case 1: 
		next.x += CELL_WIDTH; 
		break; 
	case 2: 
		next.x += CELL_WIDTH; 
		next.y += CELL_HEIGHT; 
		break; 
	case 3: 
		next.y += CELL_HEIGHT; 
		break; 
	case 4: 
		next.x -= CELL_WIDTH; 
		next.y += CELL_HEIGHT; 
		break; 
	case 5: 
		next.x -= CELL_WIDTH; 
		break; 
	case 6: 
		next.x -= CELL_WIDTH; 
		next.y -= CELL_HEIGHT; 
		break; 
	case 7: 
		next.y -= CELL_HEIGHT; 
		break; 
	case 8: 
		next.x += CELL_WIDTH; 
		next.y -= CELL_HEIGHT; 
		break; 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CBaseSprite::DrawShadow() 
{ 
	if ( m_cur_shadow_pg != NULL ) 
		m_cur_shadow_pg->DrawFrame( m_Frame, m_BasePoint ); 
} 
 
//****************************************CBuilding类定义******************************************** 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
CBuilding::CBuilding() 
{ 
} 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
CBuilding::~CBuilding() 
{ 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CBuilding::Init( CPictureGroup *pPG, CPictureGroup *pSPG, const POINT &base ) 
{ 
	m_cur_pg = pPG; 
	m_cur_shadow_pg = pSPG; 
	m_BasePoint.x = base.x; 
	m_BasePoint.y = base.y; 
	m_SpriteType = ST_N_B; 
	m_Frame = 0; 
 
	m_cur_pg->UpdateStaticSprite( m_Frame, m_LastTick, m_BasePoint, m_RenderPoint, m_Rect ); 
 
	//由于建筑物不活动,所以渲染优先级在初始化确定 
	CalcPriority(); 
	return true; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CBuilding::DrawSprite() 
{ 
#ifdef ALPHA_SHADOW 
	if ( m_cur_shadow_pg != NULL ) 
		m_cur_shadow_pg->DrawShadow( m_Frame, m_BasePoint ); 
#endif  
 
	m_cur_pg->DrawFrame( m_Frame, m_RenderPoint.x, m_RenderPoint.y ); 
} 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CBuilding::UpdateSprite() 
{ 
	//如果只有一帧,则不用更新 
	//if ( m_cur_pg->GetNumFrame() == 1 ) 
		//return true; 
 
	m_cur_pg->UpdateStaticSprite( m_Frame, m_LastTick, m_BasePoint, m_RenderPoint, m_Rect ); 
	return true; 
}