GreenJuiceTeam/GreenJuiceTeam.Shared/game/Player.cpp
/**
* Reads the input and updates the player position
* @param keyPressing
* @param carBlock 
*/
Reads the input and updates the player position
Reads the input and updates the player position
void Player::update(int keyPressing,std::vector<Enemy*> enemies,std::vector<Block*> blocks){
	int nitro_button=keyPressing & UP;
	int left=keyPressing & LEFT;
	int right=keyPressing & RIGHT;
	int down=keyPressing & DOWN;
	if(nitro_button!=0){ 
		useNitro();
	}
	if(left!=0){ 
		if(speedAxisZ > MIN_CURVE_SPEED){
			if(curve > -(CAR_IMAGE_NUM_SPRITES-1)){
				curve--;
			}
			if(curve<0){
				if(curve == -1){
					speedAxisX = -HORIZONTAL_SPEED/2;
				}
				else{
					speedAxisX = -HORIZONTAL_SPEED;
				}
			}
			skid->play();
		}
	}
	else if(right!=0){ 
		if(speedAxisZ > MIN_CURVE_SPEED){ 
			if(curve < (CAR_IMAGE_NUM_SPRITES-1)){
				curve++;
			}
			if(curve>0){
				if(curve == 1){
					speedAxisX = HORIZONTAL_SPEED/2;
				}
				else{
					speedAxisX = HORIZONTAL_SPEED;
				}
			}
			skid->play();
		}
	}
	else{
		skid->stop();
		if(curve > 0){
			curve--;
		}
		else if(curve<0){
			curve++;
		}
		doBalanceEffect();
	}
	if(!nitro && down!=0){ 
		speedAxisZ = std::max(0,speedAxisZ-BREAK);
	}
	else{ 
		if(!nitro){
			if(curve == 0){ 
				 if the speed is bigger than the maximum
				if(speedAxisZ > MAX_SPEED){
					speedAxisZ -= BREAK;
				}
				else{
					speedAxisZ = std::min(MAX_SPEED, speedAxisZ+ACCEL);
				}
			}
			else{
				if(speedAxisZ > MAX_CURVE_SPEED){
					speedAxisZ -= BREAK;
				}
				else{
					speedAxisZ = std::min(MAX_CURVE_SPEED, speedAxisZ+ACCEL);
				}
			}
		}
		else{ 
			speedAxisZ = std::min(MAX_NITRO_SPEED, speedAxisZ+ACCEL);
		}
	}
	double force = 0;
	if(nitro){
		force = (4 * speedAxisZ) / Player::MAX_CURVE_SPEED;
	}
	else{
		force = (3 * speedAxisZ) / Player::MAX_CURVE_SPEED;
	}
	speedAxisX -= (blocks[Road::CAR_BLOCK]->angle * force);
	checkNitro(); 
	checkCurve(); 
	int futureX = position[X] + speedAxisX;
	int futureZ = position[Z] + speedAxisZ;
	Enemy* collisionEnemy = hasCollisionWithEnemies(futureX,futureZ,enemies);
	if(collisionEnemy != NULL){
		collisionSND->play();
		position[Z] = collisionEnemy->position[Z] - 25;
		position[X] += speedAxisX;
	}
	else{
		bool outOfRoad = isOutOfRoad(futureX,futureZ,blocks);
		if(outOfRoad){
			position[Z] += speedAxisZ;
			if(speedAxisZ > MAX_OUT_OF_ROAD_SPEED){
				speedAxisZ = speedAxisZ - ACCEL - BREAK;
				if(speedAxisZ < MAX_OUT_OF_ROAD_SPEED){
					speedAxisZ = MAX_OUT_OF_ROAD_SPEED;
				}
			}
		}
		else{
			position[Z] += speedAxisZ;
			position[X] += speedAxisX;
		}
	}
	speedAxisX = 0;
}
void Player :: update ( int keyPressing , std :: vector < Enemy * > enemies , std :: vector < Block * > blocks ) { int nitro_button = keyPressing & UP ; int left = keyPressing & LEFT ; int right = keyPressing & RIGHT ; int down = keyPressing & DOWN ; if ( nitro_button != 0 ) { useNitro ( ) ; } if ( left != 0 ) { if ( speedAxisZ > MIN_CURVE_SPEED ) { if ( curve > - ( CAR_IMAGE_NUM_SPRITES - 1 ) ) { curve -- ; } if ( curve < 0 ) { if ( curve == -1 ) { speedAxisX = - HORIZONTAL_SPEED / 2 ; } else { speedAxisX = - HORIZONTAL_SPEED ; } } skid -> play ( ) ; } } else if ( right != 0 ) { if ( speedAxisZ > MIN_CURVE_SPEED ) { if ( curve < ( CAR_IMAGE_NUM_SPRITES - 1 ) ) { curve ++ ; } if ( curve > 0 ) { if ( curve == 1 ) { speedAxisX = HORIZONTAL_SPEED / 2 ; } else { speedAxisX = HORIZONTAL_SPEED ; } } skid -> play ( ) ; } } else { skid -> stop ( ) ; if ( curve > 0 ) { curve -- ; } else if ( curve < 0 ) { curve ++ ; } doBalanceEffect ( ) ; } if ( ! nitro && down != 0 ) { speedAxisZ = std :: max ( 0 , speedAxisZ - BREAK ) ; } else { if ( ! nitro ) { if ( curve == 0 ) { if ( speedAxisZ > MAX_SPEED ) { speedAxisZ -= BREAK ; } else { speedAxisZ = std :: min ( MAX_SPEED , speedAxisZ + ACCEL ) ; } } else { if ( speedAxisZ > MAX_CURVE_SPEED ) { speedAxisZ -= BREAK ; } else { speedAxisZ = std :: min ( MAX_CURVE_SPEED , speedAxisZ + ACCEL ) ; } } } else { speedAxisZ = std :: min ( MAX_NITRO_SPEED , speedAxisZ + ACCEL ) ; } } double force = 0 ; if ( nitro ) { force = ( 4 * speedAxisZ ) / Player :: MAX_CURVE_SPEED ; } else { force = ( 3 * speedAxisZ ) / Player :: MAX_CURVE_SPEED ; } speedAxisX -= ( blocks [ Road :: CAR_BLOCK ] -> angle * force ) ; checkNitro ( ) ; checkCurve ( ) ; int futureX = position [ X ] + speedAxisX ; int futureZ = position [ Z ] + speedAxisZ ; Enemy * collisionEnemy = hasCollisionWithEnemies ( futureX , futureZ , enemies ) ; if ( collisionEnemy != NULL ) { collisionSND -> play ( ) ; position [ Z ] = collisionEnemy -> position [ Z ] - 25 ; position [ X ] += speedAxisX ; } else { bool outOfRoad = isOutOfRoad ( futureX , futureZ , blocks ) ; if ( outOfRoad ) { position [ Z ] += speedAxisZ ; if ( speedAxisZ > MAX_OUT_OF_ROAD_SPEED ) { speedAxisZ = speedAxisZ - ACCEL - BREAK ; if ( speedAxisZ < MAX_OUT_OF_ROAD_SPEED ) { speedAxisZ = MAX_OUT_OF_ROAD_SPEED ; } } } else { position [ Z ] += speedAxisZ ; position [ X ] += speedAxisX ; } } speedAxisX = 0 ; }
Reads the input and updates the player position
Reads the input and updates the player position
//printf("%d\n",keyPressing); //nitro //curve to left //just let a curve be done if the car speed on axis Z is bigger than the minimum //variable to represent witch sprite will be set to represent the curve //if the curve tick is less than zero, it means that the curve started. So, you can move the car on //the X axis. //curve to right //don't let the car do a curve if the speed is less than 5 //if the curve tick is bigger than zero, it means that the curve started. So, you can move the car on //the X axis. //this the player isn't pressing left or right keys, the curve tick should go to zero. //while the player isn't making a curve, //change the Y position to give the balance effect. //break //acceleration effect //if the nitro isn't activate, what some rules to accelerate. //take normal acceleration if not doing a curve,  //break if the speed is bigger than the maximum //set on MAX_SPEED, or accelerate until that value. //if doing a curve, break if the speed is bigger than the maximum //set on MAX_CURVE_SPEED, or accelerate until that value. //if the nitro is activate, accelerate no matter what //give the effect of curve to the car //1. estimate the force that throw the car out of road  //double force = (1.4 * speedAxisZ) / Player.MAX_SPEED; //2. and throw on the opposite direction of the curve //check things relative to nitro (e.g time, speed inc, frames..) //check things relative to the curve effect (e.g frames) //check if the player collides with some enemy //position[Z] = collisionEnemy->position[Z] - Util::CAR_LENGTH; //update the axis positions //check if the next position will be out of road //if out of road, take off the acceleration effect given before //and put the break effect. //update the axis positions //the speed on X axis should be zero on the next update tick