so after finishing my pong game I set off to start writing my pacman clone. It's been quite grueling, without any info on how to implement some of these features. However, I've managed to do it till now.
The player should only be able to change directions when they are close to the origin of the tile they currently inhabit, but because of how my update function works, the player can get still change the movement of the player.
It's kind of hard to explain, so I will let the code talk for me.
Hopefully you guys can help me. I haven't read how anyone else has done this, so I've been guessing as to how to make everything work correctly.
//===========================================================
// IN MAIN
//===========================================================
void HandleEvents( void )
{
if( g_nCurFrame - g_nHandleEventsFrame <= 0 )
return;
//events
if( SDL_PollEvent( &g_Event) )
{
//check if the user wants to quite
if( g_Event.type == SDL_QUIT || g_Event.key.keysym.sym == SDLK_ESCAPE )
{
g_bQuit = true;
}
switch( g_Event.key.keysym.sym )
{
case SDLK_UP:
g_pPlayer->SetDirection( UP );
break;
case SDLK_DOWN:
g_pPlayer->SetDirection( DOWN );
break;
case SDLK_RIGHT:
g_pPlayer->SetDirection( RIGHT );
break;
case SDLK_LEFT:
g_pPlayer->SetDirection( LEFT );
break;
case SDLK_SPACE:
std::cout << "hit";
break;
}
}
g_nHandleEventsFrame = g_nCurFrame+2;
}
//==================================================
// THE PLAYER UPDATE FUNCTION CALLED EVERY FRAME
//==================================================
//========================================================
// Purpose: constructors, they're so cool
//========================================================
void Player::Update( void )
{
int NextTilex = 0;
int NextTiley = 0;
//find the direction the pacman is traveling in
switch( m_nDirection )
{
case UP:
--NextTiley;
break;
case DOWN:
++NextTiley;
break;
case LEFT:
--NextTilex;
break;
case RIGHT:
++NextTilex;
break;
}
//check to see if the player is moving 90 degrees from their previous direction
if( (m_nOldDirection == UP || m_nOldDirection == DOWN) && (m_nDirection == LEFT || m_nDirection == RIGHT) )
{
//check to see if the player is near the origin of the tile they are currently in
Vector2 vecOrigin = GetTileOrigin(GetTilex(), GetTiley());
//check to see if the player is close enough to the origin to constitute them moving
if( abs(vecOrigin.x - m_vecOrigin.x) <= 4 && abs(vecOrigin.y - m_vecOrigin.y) <= 4 )
{
//set the player to the origin position
SetPos( vecOrigin-Vector2(16,16) );
}
//if the player isn't near the origin, set the direction to the previous one
else
m_nDirection = m_nOldDirection;
}
//check to see if the next tile is collidable
if( GetTileFlag( GetTilex()+NextTilex, GetTiley()+NextTiley ) & NOTWALKABLE )
{
m_nDirection = NONE;
}
//handle input
switch( m_nDirection )
{
case UP:
{
//update the animation
m_pSprPlayer->Animate( 8, 4, true );
SetPosy( GetPos().y - m_fSpeed );
break;
}
case DOWN:
{
//update the animation
m_pSprPlayer->Animate( 12, 4, true );
SetPosy( GetPos().y + m_fSpeed );
break;
}
case LEFT:
{
//update the animation
m_pSprPlayer->Animate( 0, 4, true );
SetPosx( GetPos().x - m_fSpeed );
break;
}
case RIGHT:
{
//update the animation
m_pSprPlayer->Animate( 4, 4, true );
SetPosx( GetPos().x + m_fSpeed );
break;
}
case NONE:
{
//update the animation
m_pSprPlayer->SetCurrentFrame( 0 );
break;
}
}
//set the sprite position
m_pSprPlayer->SetPos( m_vecPos );
}
This post has been edited by mastersmith98: 29 January 2009 - 07:33 PM

New Topic/Question
Reply




MultiQuote






|