/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location (bear sprite size = 46 x 64) int randomLocationX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize); int randomLocationY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize); // generate random velocity float bearRandomSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float randomAngle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); float speedX = bearRandomSpeed * (float)Math.Cos(randomAngle); float speedY = bearRandomSpeed * (float)Math.Sin(randomAngle); Vector2 bearVelocity = new Vector2(speedX, speedY); // create new bear TeddyBear newBear; newBear = new TeddyBear(Content, @"graphics\teddybear", randomLocationX, randomLocationY, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, GetCollisionRectangles())) { newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize); int y = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferHeight - 2 * GameConstants.SpawnBorderSize); // generate random velocity float speed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 velocity = new Vector2( (float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle))); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, null, null); // make sure we don't spawn into a collision List <Rectangle> rects = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, rects)) { x = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize); y = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferHeight - 2 * GameConstants.SpawnBorderSize); newBear.X = x; newBear.Y = y; } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location //Vector2 bearLocation = new Vector2(GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize), // GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize)); int bearLocationX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int bearLocationY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float bearSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float bearAngle = RandomNumberGenerator.NextFloat(2) * (float)Math.PI; // Radians // Using Math.Sin to calculate the 'y' component and Math.Cos for the 'x' component Vector2 bearVelocity = new Vector2((float)Math.Cos(bearAngle) * bearSpeed, (float)Math.Sin(bearAngle) * bearSpeed); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", bearLocationX, bearLocationY, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(0, GameConstants.WindowWidth); newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize); int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize); // generate random velocity float speed = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange) + GameConstants.MinBearSpeed; float angle = RandomNumberGenerator.NextFloat(2.0f * (float)Math.PI); Vector2 velocity = new Vector2(speed * (float)Math.Cos(angle), speed * (float)Math.Sin(angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int randomX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2); int randomY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2); // generate random speed float randomSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); // generate random angle (in radians, as Math.Sin and Math.Cos expects values in radians) float randomAngle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); // generate X, Y vector components based on randomAngle and randomSpeed; float randomVectorX = (float)(Math.Cos(randomAngle) * randomSpeed); float randomVectorY = (float)(Math.Sin(randomAngle) * randomSpeed); // generate random velocity Vector2 randomVector = new Vector2(randomVectorX, randomVectorY); // create new bear TeddyBear newBear = new TeddyBear(Content, @"Graphics\teddybear", randomX, randomY, randomVector, null, null); // make sure we don't spawn into a collision // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int randomLocationX = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowWidth - GameConstants.SpawnBorderSize)); int randomLocationY = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowHeight - GameConstants.SpawnBorderSize)); // generate random velocity float randomSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float randomAngle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 randomVelocity = new Vector2((float)(randomSpeed * Math.Cos(randomAngle)), (float)(randomSpeed * Math.Sin(randomAngle))); // create new bear TeddyBear newBear = new TeddyBear(Content, @"BuildGraphics\teddybear", randomLocationX, randomLocationY, randomVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> otherRectangles = GetCollisionRectangles(); while (CollisionUtils.IsCollisionFree(newBear.DrawRectangle, otherRectangles) == false) { newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowWidth - GameConstants.SpawnBorderSize)); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowHeight - GameConstants.SpawnBorderSize)); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int xBear = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2); int yBear = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2); // generate random velocity float randVel = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange - GameConstants.MinBearSpeed); float randAng = RandomNumberGenerator.NextFloat((float)Math.PI / 2); Vector2 velBear = new Vector2(randVel * (float)Math.Cos(randAng), randVel * (float)Math.Sin(randAng)); // create new bear TeddyBear newBear = new TeddyBear(Content, "graphics/teddybear", xBear, yBear, velBear, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> otherCollisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, otherCollisionRectangles)) { newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int bearRndLocX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int bearRandLoxY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity, angle and Vector for the bear velocity float bearRndSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float bearRndAngle = RandomNumberGenerator.NextFloat((float)Math.PI); //float bearRndAngle = (float)Math.PI/3; Vector2 bearVelocityVector = new Vector2((float)(bearRndSpeed * Math.Cos(Convert.ToDouble(bearRndAngle))), (float)(bearRndSpeed * Math.Sin(Convert.ToDouble(bearRndAngle)))); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", bearRndLocX, bearRandLoxY, bearVelocityVector, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(0, GameConstants.WindowWidth); newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float Rand_Speed = RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); if (Rand_Speed < GameConstants.MIN_BEAR_SPEED) { Rand_Speed += GameConstants.MIN_BEAR_SPEED; } float Rand_Angle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); Vector2 Rand_Velocity = new Vector2(Rand_Speed * (float)Math.Cos(Rand_Angle), Rand_Speed * (float)Math.Sin(Rand_Angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, Rand_Velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, GetCollisionRectangles()) == false) { newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int locationX = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE); int locationY = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float speed = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat((float)(Math.PI * 2)); Vector2 velocity = new Vector2(speed * (float)Math.Cos((double)angle), speed * (float)Math.Sin((double)angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", locationX, locationY, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE); newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int start_x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); int start_y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity //float v_random=GameConstants.MIN_BEAR_SPEED+RandomNumberGenerator.NextFloat(RandomNumberGenerator.NextFloat((GameConstants.BEAR_SPEED_RANGE))); float v_random = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); //float angle_random = (float)Math.PI * RandomNumberGenerator.NextFloat(2); float angle_random = RandomNumberGenerator.NextFloat((float)Math.PI * 2); Vector2 velocity = new Vector2(v_random * (float)Math.Cos(angle_random), v_random * (float)Math.Sin(angle_random)); // create new bear TeddyBear NewBear = new TeddyBear(Content, "teddybear", start_x, start_y, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(NewBear.CollisionRectangle, collisionRectangles)) { NewBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE); NewBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(NewBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int bx = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize); int by = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize); // generate random velocity float vel = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float ang = RandomNumberGenerator.NextFloat((float)Math.PI); Vector2 vec = new Vector2((float)Math.Cos(ang) * vel, -1 * (float)Math.Sin(ang) * vel); // create new bear TeddyBear newbear = new TeddyBear(Content, @"graphics/teddybear", bx, by, vec, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangules = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newbear.DrawRectangle, collisionRectangules)) { newbear.X = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize); newbear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize); } // add new bear to list bears.Add(newbear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float speed = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 velocity = new Vector2( (float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle))); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, velocity, null, null); // make sure we don't spawn into a collision List <Rectangle> CollisionRectangles = new List <Rectangle>(GetCollisionRectangles()); while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, CollisionRectangles) == false) { newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - (2 * GameConstants.SPAWN_BORDER_SIZE)); newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - (2 * GameConstants.SPAWN_BORDER_SIZE)); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2); int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2); // generate random velocity float speed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float angle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); Vector2 velocity = new Vector2((float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle))); // create new bear TeddyBear bear = new TeddyBear(Content, @"graphics/teddybear", x, y, velocity, null, null); // make sure we don't spawn into a collision List <Rectangle> rectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(bear.CollisionRectangle, rectangles)) { y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2); x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2); bear.X = x; bear.Y = y; } // add new bear to list bears.Add(bear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int locationX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth); int locationY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight); // generate random velocity float speed1 = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float speed2 = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); /* ------- Vidi za angle generator -------------- * RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); * // generate random angle float angle = (float)Math.PI * RandomNumberGenerator.NextFloat(360.0f) / 180.0f; * // generate vector velocity Vector2 velocity = new Vector2((float)Math.Cos(angle) * speed, -(float)Math.Sin(angle) * speed); */ Vector2 vector = new Vector2(speed1, speed2); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics/teddybear", locationX, locationY, vector, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionsList = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionsList)) { newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float bearSpeedValue = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); Vector2 bearVelocity = new Vector2(bearSpeedValue * (float)Math.Cos(angle), (float)bearSpeedValue * (float)Math.Sin(angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision //Get list of collision rectangles List <Rectangle> collisionRectangles = GetCollisionRectangles(); //Reset spawn location until collision free location is chosen while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(0, GameConstants.WINDOW_WIDTH); newBear.Y = GetRandomLocation(0, GameConstants.WINDOW_HEIGHT); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (2 * GameConstants.SpawnBorderSize)); int Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - (2 * GameConstants.SpawnBorderSize)); // generate random velocity float angle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); float velocity = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); Vector2 bearVector = new Vector2(velocity * (float)Math.Cos(angle), velocity * (float)Math.Sin(angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", X, Y, bearVector, null, null); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles) == false) { newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (2 * GameConstants.SpawnBorderSize)); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (2 * GameConstants.SpawnBorderSize)); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float BearSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); //random angle float BearAngle = RandomNumberGenerator.NextFloat((float)Math.PI); //new vector2 object Vector2 Velocity = new Vector2((float)Math.Cos(BearAngle) * BearSpeed, ((float)Math.Sin(BearAngle) * BearSpeed)); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, Velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionList = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionList)) { newBear.X = GetRandomLocation(0, GameConstants.WindowWidth); newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth); int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight); // generate random velocity float speed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float randomAngle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); float velocityX = (float)(speed * Math.Cos(randomAngle)); float velocityY = (float)(speed * Math.Sin(randomAngle)); Vector2 velocity = new Vector2(velocityX, velocityY); // create new bear TeddyBear newBear = new TeddyBear(Content, @"Graphics\teddybear", x, y, velocity, null, null); // make sure we don't spawn into a collision // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int bearXLocation = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int bearYLocation = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float bearSpeed = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); if (bearSpeed < GameConstants.MinBearSpeed) { bearSpeed = GameConstants.MinBearSpeed; } float bearAngle = RandomNumberGenerator.NextFloat((float)Math.PI); float bearVelocityX = bearSpeed * (float)Math.Cos(bearAngle); float bearVelocityY = bearSpeed * (float)Math.Sin(bearAngle); Vector2 bearVelocity = new Vector2(bearVelocityX, bearVelocityY); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics/teddybear", bearXLocation, bearYLocation, bearVelocity, null, null); // make sure we don't spawn into a collision // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float XVelocity = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float YVelocity = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float angle = (float)((RandomNumberGenerator.NextFloat(360)) / (2 * Math.PI)); Vector2 bearVelocity = new Vector2((float)(XVelocity * Math.Sin(angle)), (float)(YVelocity * Math.Cos(angle))); // create new bear TeddyBear newBear = new TeddyBear(Content, "Graphics//teddyBear", X, Y, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(0, GameConstants.WindowWidth); newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight); } bears.Add(newBear); // add new bear to list }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location : SpawnBorderSize is subtracted twice, one for left/top side and one for right/bottom side int randomXLocation = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (GameConstants.SpawnBorderSize * 2)); int randomYLocation = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - (GameConstants.SpawnBorderSize * 2)); // generate random velocity //create random speeds float randomSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); //create random angle float randomAngle = 2 * (float)Math.PI * RandomNumberGenerator.NextFloat(1); //create Vector2 Vector2 randomVelocity = new Vector2((float)Math.Cos(randomAngle) * randomSpeed, (float)Math.Sin(randomAngle) * randomSpeed); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", randomXLocation, randomYLocation, randomVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, GetCollisionRectangles()) == false) { //change x and y values newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (GameConstants.SpawnBorderSize * 2)); newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - (GameConstants.SpawnBorderSize * 2)); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Explodes a teddy bear. Sets bear to inactive, adds explosion on teddy bear location. /// </summary> /// <param name="bear">The bear to explode.</param> private void explodeTeddy(TeddyBear bear) { bear.Active = false; Point bearCenter = bear.CollisionRectangle.Center; Explosion explosion = new Explosion(explosionSpriteStrip, bearCenter.X, bearCenter.Y, this.explosion); explosions.Add(explosion); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int bearX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int bearY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float velMagnitude = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); if(velMagnitude < GameConstants.MinBearSpeed) { velMagnitude = GameConstants.MinBearSpeed; } float velAngle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 bearVelocity = new Vector2((float)(velMagnitude * Math.Cos((double)velAngle)), (float)(velMagnitude * Math.Sin((double)velAngle))); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddyBear", bearX, bearY, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List<Rectangle> collisionRectangles = GetCollisionRectangles(); while(CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionRectangles)) { bearX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); bearY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); newBear.X = bearX; newBear.Y = bearY; } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location, note the location might be in border since we use it's center as (x,y) int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float v = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange - GameConstants.MinBearSpeed); float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 velocity = new Vector2((float)(v * Math.Cos(angle)), (float)(v * Math.Sin(angle))); // create new bear TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, GetCollisionRectangles())) { x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, teddyBounce, teddyShot); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int xLoc = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); int yLoc = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float speed = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 velocity = new Vector2(speed * (float)Math.Cos(angle), speed * (float)Math.Sin(angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", xLoc, yLoc, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> rectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, rectangles)) { newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int xBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Width); int yBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Height); //GameConstants.SpawnBorderSize + RandomNumberGenerator.Next(GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2 + 1); // generate random velocity float speedBear = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); if (speedBear < GameConstants.MinBearSpeed) { speedBear = GameConstants.MinBearSpeed; } // a random angle using Math.PI and the RandomNumberGenerator NextFloat method float angleInRadians = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); //360 = 2*Pi float angleInDegree = angleInRadians * (180 / (float)Math.PI); //random speed and angles you generated and the appropriate trigonometry: speed as the length of hypotenuse of a right triangle float xVelocity = speedBear * (float)Math.Cos(angleInRadians); float yVelocity = speedBear * (float)Math.Sin(angleInRadians); Vector2 velocityBear = new Vector2(xVelocity, yVelocity); // create new bear be speed * cos(angle) and the Y component would be speed * sin(angle) TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", xBearRandom, yBearRandom, velocityBear, teddyBounce, teddyShot); // make sure we don't spawn into a collision List <Rectangle> collisions = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisions)) { int newXBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Width); int newYBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Height); newBear.X = newXBearRandom; newBear.Y = newYBearRandom; //alternate way ()instead of 2 above statements: //newBear = new TeddyBear(Content, @"graphics\teddybear", newXBearRandom, newYBearRandom, velocityBear, null, null); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = RandomNumberGenerator.Next(GraphicsDevice.Viewport.TitleSafeArea.Width); int y = RandomNumberGenerator.Next(GraphicsDevice.Viewport.TitleSafeArea.Height); // generate random velocity Vector2 velocity = new Vector2(RandomNumberGenerator.NextFloat(5), RandomNumberGenerator.NextFloat(5)); // create new bear TeddyBear teddy = new TeddyBear(Content, "teddybear", x, y, velocity, teddyShot, teddyShot); // make sure we don't spawn into a collision // add new bear to list bears.Add(teddy); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int locationX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize); int locationY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize); // generate random velocity float speed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float angle = RandomNumberGenerator.NextFloat((float)Math.PI); Vector2 velocity = new Vector2((float)Math.Cos(angle) * speed, (float)Math.Sin(angle) * speed); // create new bear TeddyBear newBear = new TeddyBear(Content, "graphics/teddybear", locationX, locationY, velocity, null, null); // make sure we don't spawn into a collision // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int teddyBearPositionX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize); int teddyBearPositionY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize); // generate random velocity float teddyBearSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange); float angle = RandomNumberGenerator.NextFloat(360) * (float)Math.PI; // create Vector2 float velocityX = System.Convert.ToSingle(teddyBearSpeed * Math.Cos(angle)); float velocityY = System.Convert.ToSingle(teddyBearSpeed * Math.Sin(angle)); Vector2 velocity = new Vector2(velocityX, velocityY); // create new bear TeddyBear newBear = new TeddyBear(Content, "sprites//teddybear", teddyBearPositionX, teddyBearPositionY, velocity, teddyBounce, teddyShot); bears.Add(newBear); // make sure we don't spawn into a collision // add new bear to list }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - (2 * GameConstants.SPAWN_BORDER_SIZE)); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - (2 * GameConstants.SPAWN_BORDER_SIZE)); // generate random velocity float speed = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); Vector2 velocity = new Vector2((speed * (float)Math.Cos(angle)), (speed * (float)Math.Sin(angle))); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List<Rectangle> collisionRectangles = GetCollisionRectangles(); while ((CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionRectangles))==false) { newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE); newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x =GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,GameConstants.WINDOW_WIDTH-2*GameConstants.SPAWN_BORDER_SIZE); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2*GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float bearSpeedValue = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat((float)Math.PI*2); Vector2 bearVelocity = new Vector2(bearSpeedValue * (float)Math.Cos(angle), (float)bearSpeedValue * (float)Math.Sin(angle)); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision //Get list of collision rectangles List<Rectangle> collisionRectangles = GetCollisionRectangles(); //Reset spawn location until collision free location is chosen while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(0, GameConstants.WINDOW_WIDTH); newBear.Y = GetRandomLocation(0, GameConstants.WINDOW_HEIGHT); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int spawnX = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - GameConstants.SPAWN_BORDER_SIZE*2); int spawnY = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - GameConstants.SPAWN_BORDER_SIZE*2); // generate random velocity float speed = RandomNumberGenerator.NextFloat(0.2f)+0.1f; float angle = RandomNumberGenerator.NextFloat((float)Math.PI * 2); Vector2 velocity = new Vector2( speed*(float)Math.Cos(angle), speed*(float)Math.Sin(angle) ); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", spawnX, spawnY, velocity,teddyBounce,teddyShot); // make sure we don't spawn into a collision foreach(Projectile shot in projectiles) { if(shot.CollisionRectangle.Intersects(newBear.CollisionRectangle)) { return; } } foreach(TeddyBear teddy in bears) { if (teddy.CollisionRectangle.Intersects(newBear.CollisionRectangle)) { return; } } if (burger.CollisionRectangle.Intersects(newBear.CollisionRectangle)) { return; } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - (GameConstants.SPAWN_BORDER_SIZE * 2)); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - (GameConstants.SPAWN_BORDER_SIZE * 2)); // generate random velocity float speed = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float angle = RandomNumberGenerator.NextFloat((float)(2*Math.PI)); Vector2 Velocity = new Vector2((float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle))); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, Velocity, null, null); // make sure we don't spawn into a collision // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int xLocation = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); int yLocation = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity float randomBearSpeed = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); float randomAngle = RandomNumberGenerator.NextFloat((float) Math.PI*2); Vector2 bearVelocity = new Vector2(randomBearSpeed * (float)Math.Cos(randomAngle), randomBearSpeed * (float)Math.Sin(randomAngle)); // create new bear TeddyBear newBear = new TeddyBear(Content, "teddybear", xLocation, yLocation, bearVelocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List<Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree (newBear.CollisionRectangle, collisionRectangles)) { newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location //x position of the left border line int leftBorder = GameConstants.SPAWN_BORDER_SIZE; //x position of the right border line int righBorder = GameConstants.WINDOW_WIDTH - GameConstants.SPAWN_BORDER_SIZE; //y position of the upper border line int upperBorder = GameConstants.SPAWN_BORDER_SIZE; //y position of the lower border line int lowerBorder = GameConstants.WINDOW_HEIGHT - GameConstants.SPAWN_BORDER_SIZE; //x and y position of the bear int x = GetRandomLocation(leftBorder, righBorder - leftBorder); int y = GetRandomLocation(upperBorder, lowerBorder - upperBorder); // generate random velocity float speed = RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE) + GameConstants.MIN_BEAR_SPEED; float angle = RandomNumberGenerator.NextFloat((float)(Math.PI * 2)); Vector2 velocity = new Vector2(speed * (float)Math.Cos(angle), speed * (float)Math.Sin(angle)); // create new bear TeddyBear newBear = new TeddyBear(this.Content, "teddyBear", x, y, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision while(!CollisionUtils.IsCollisionFree(newBear.DrawRectangle,GetCollisionRectangles())) { newBear.X = GetRandomLocation(leftBorder, righBorder - leftBorder); newBear.Y = GetRandomLocation(upperBorder, lowerBorder - upperBorder); } // add new bear to list bears.Add(newBear); }
/// <summary> /// Spawns a new teddy bear at a random location /// </summary> private void SpawnBear() { // generate random location int start_x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE); int start_y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); // generate random velocity //float v_random=GameConstants.MIN_BEAR_SPEED+RandomNumberGenerator.NextFloat(RandomNumberGenerator.NextFloat((GameConstants.BEAR_SPEED_RANGE))); float v_random=GameConstants.MIN_BEAR_SPEED+RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE); //float angle_random = (float)Math.PI * RandomNumberGenerator.NextFloat(2); float angle_random = RandomNumberGenerator.NextFloat((float)Math.PI * 2); Vector2 velocity = new Vector2(v_random * (float)Math.Cos(angle_random), v_random * (float)Math.Sin(angle_random)); // create new bear TeddyBear NewBear = new TeddyBear(Content, "teddybear", start_x, start_y, velocity, teddyBounce, teddyShot); // make sure we don't spawn into a collision List<Rectangle> collisionRectangles = GetCollisionRectangles(); while (!CollisionUtils.IsCollisionFree(NewBear.CollisionRectangle, collisionRectangles)) { NewBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE); NewBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE); } // add new bear to list bears.Add(NewBear); }