BeatTheStreet/src/player.cpp
//Void function used to move the sprites position, direction is passed in as a parameter.
Void function used to move the sprites position, direction is passed in as a parameter.
Void function used to move the sprites position direction is passed in as a parameter .
void Player::movePlayer(char direction)
{
	float fDistanceToLeft, fDistanceToRight;
	fDistanceToLeft =   -25 + sPlayer.getPosition().x;
	fDistanceToRight =  775 - sPlayer.getPosition().x;
	bool validMoveUp, validMoveDown, validMoveLeft, validMoveRight;
	if (sPlayer.getPosition().x < 75) validMoveLeft = false;
	else validMoveLeft = true;
	if (sPlayer.getPosition().x > 725) validMoveRight = false;
	else validMoveRight = true;
	if (sPlayer.getPosition().y < 105) validMoveUp = false;
	else validMoveUp = true;
	if (sPlayer.getPosition().y > 755) validMoveDown = false;
	else validMoveDown = true;
	if (playerfadeinclock.getElapsedTime().asSeconds() > 1)
	{
		if (direction == 'u' && validMoveUp == true)
		{
			sPlayer.setTexture(pTextureBack);
			sPlayer.move(0, -50);
		}
		else if (direction == 'd' && validMoveDown == true)
		{
			sPlayer.setTexture(pTextureFront);
			sPlayer.move(0, 50);
		}
		else if (direction == 'l' && validMoveLeft == true)
		{
			sPlayer.setTexture(pTextureLeft);
			sPlayer.move(-50, 0);
		}
		else if (direction == 'r' && validMoveRight == true)
		{
			sPlayer.setTexture(pTextureRight);
			sPlayer.move(50, 0);
		}
		else if (direction == 'l' && validMoveLeft == false)
		{
			sPlayer.setTexture(pTextureLeft);
			sPlayer.move(-fDistanceToLeft, 0);
		}
		else if (direction == 'r' && validMoveRight == false)
		{
			sPlayer.setTexture(pTextureRight);
			sPlayer.move(fDistanceToRight, 0);
		}
	}
}
void Player :: movePlayer ( char direction ) { float fDistanceToLeft , fDistanceToRight ; fDistanceToLeft = -25 + sPlayer . getPosition ( ) . x ; fDistanceToRight = 775 - sPlayer . getPosition ( ) . x ; bool validMoveUp , validMoveDown , validMoveLeft , validMoveRight ; if ( sPlayer . getPosition ( ) . x < 75 ) validMoveLeft = false ; else validMoveLeft = true ; if ( sPlayer . getPosition ( ) . x > 725 ) validMoveRight = false ; else validMoveRight = true ; if ( sPlayer . getPosition ( ) . y < 105 ) validMoveUp = false ; else validMoveUp = true ; if ( sPlayer . getPosition ( ) . y > 755 ) validMoveDown = false ; else validMoveDown = true ; if ( playerfadeinclock . getElapsedTime ( ) . asSeconds ( ) > 1 ) { if ( direction == ' ' && validMoveUp == true ) { sPlayer . setTexture ( pTextureBack ) ; sPlayer . move ( 0 , -50 ) ; } else if ( direction == ' ' && validMoveDown == true ) { sPlayer . setTexture ( pTextureFront ) ; sPlayer . move ( 0 , 50 ) ; } else if ( direction == ' ' && validMoveLeft == true ) { sPlayer . setTexture ( pTextureLeft ) ; sPlayer . move ( -50 , 0 ) ; } else if ( direction == ' ' && validMoveRight == true ) { sPlayer . setTexture ( pTextureRight ) ; sPlayer . move ( 50 , 0 ) ; } else if ( direction == ' ' && validMoveLeft == false ) { sPlayer . setTexture ( pTextureLeft ) ; sPlayer . move ( - fDistanceToLeft , 0 ) ; } else if ( direction == ' ' && validMoveRight == false ) { sPlayer . setTexture ( pTextureRight ) ; sPlayer . move ( fDistanceToRight , 0 ) ; } } }
Void function used to move the sprites position, direction is passed in as a parameter.
Void function used to move the sprites position direction is passed in as a parameter .
//Initialize variables //fDistanceToLeft and fDistanceToRight are the distances between the sprite and the x axis window bounds. They are used to make sure the player doesn't move off the screen. //Initialize boolean variables. //Boolean variables are used to indicate if there is at least 50px to the side of the sprite to move into. //If there is space for the player to move into the bool is set to true. //Only allows player movement once the sprite has finished fading in. //Depending on the direction passed in, and if there is a valid move, the player is moved and the texture is changed //If there isn't a valid move (so less than 50px), the player gets moved into the available space, using the distance to left variables