/// <summary> /// Constructs a car with the given x velocity /// </summary> /// <param name="direction">the car direction</param> /// <param name="sprite">the sprite for the car</param> /// <param name="x">the x location of the center of the projectile</param> /// <param name="y">the y location of the center of the projectile</param> /// <param name="xVelocity">the x velocity for the projectile</param> public Car(GameConstants.Direction direction, Texture2D sprite, int x, int y, float xVelocity) { this.carDirection = direction; this.sprite = sprite; this.x_Velocity = xVelocity; this.drawRectangle = new Rectangle(x - this.sprite.Width / 2, y - this.sprite.Height / 2, this.sprite.Width, this.sprite.Height); }
//This function generates new car without overlappings private void SpawnCar() { //this is here to avoid overlapping of two consecutive cars this.prevCar = this.currentCar; Car tempCar; int randCar = my_rand.Next(GameConstants.CARS_NUMBER); //set picture for car GameConstants.Direction tempDirection = this.getDirection(my_rand.Next(2)); //set direction for car int linePosition = 0; // init line in apropriate direction int temp_x = 0; // init position int randVelocity = 0; // init random velocity //new car generation while (this.currentCar == this.prevCar) { if (tempDirection == GameConstants.Direction.Right) { temp_x = -100; int tempLine = my_rand.Next(3); if (tempLine == GameConstants.SLOW_LINE) { linePosition = GameConstants.LINE3_POSITION; currentCar = 3; randVelocity = GameConstants.SLOW_CAR_SPEED; } else if (tempLine == GameConstants.MEDIAN_LINE) { linePosition = GameConstants.LINE2_POSITION; currentCar = 4; randVelocity = GameConstants.MEDIAN_CAR_SPEED; } else { linePosition = GameConstants.LINE1_POSITION; currentCar = 5; randVelocity = GameConstants.FAST_CAR_SPEED; } } else // if direction of the car is to the left { temp_x = GameConstants.WINDOW_WIDTH + 100; int tempLine = my_rand.Next(3); if (tempLine == GameConstants.SLOW_LINE) { linePosition = GameConstants.LINE4_POSITION; currentCar = 0; randVelocity = GameConstants.SLOW_CAR_SPEED; } else if (tempLine == GameConstants.MEDIAN_LINE) { linePosition = GameConstants.LINE5_POSITION; currentCar = 1; randVelocity = GameConstants.MEDIAN_CAR_SPEED; } else { linePosition = GameConstants.LINE6_POSITION; currentCar = 2; randVelocity = GameConstants.FAST_CAR_SPEED; } } } //add this generated car if (tempDirection == GameConstants.Direction.Right) { tempCar = new Car(tempDirection, this.rightCarPictures[randCar], temp_x, linePosition, randVelocity); } else //if direction of the car is to the left { tempCar = new Car(tempDirection, this.leftCarPictures[randCar], temp_x, linePosition, randVelocity); } this.cars.Add(tempCar); }