예제 #1
0
        static public void Collide(ref Pickup pickup1, ref Pickup pickup2)
        {
            Vector2 tempVelocity1 = pickup1.Velocity;
            Vector2 tempVelocity2 = pickup2.Velocity;

            float magnitude = (pickup1.Velocity.Length() + pickup2.Velocity.Length()) / 2f;

            if (tempVelocity1 == Vector2.Zero)
            {
                tempVelocity2.Normalize();
                tempVelocity1 = tempVelocity2 = Vector2.Multiply(tempVelocity2, magnitude);
            }
            else if (tempVelocity2 == Vector2.Zero)
            {
                tempVelocity1.Normalize();
                tempVelocity2 = tempVelocity1 = Vector2.Multiply(tempVelocity1, magnitude);
            }

            tempVelocity1.Normalize();
            tempVelocity2.Normalize();

            pickup1.Velocity = Vector2.Multiply(tempVelocity2, magnitude);
            pickup2.Velocity = Vector2.Multiply(tempVelocity1, magnitude);
        }
예제 #2
0
        public void Update(GameTime gameTime, Rectangle playArea, ref MainPlayer player)
        {
            double deltaTime = gameTime.ElapsedGameTime.TotalSeconds;

            ComboTimer -= (float)deltaTime;
            if (ComboTimer <= 0)
            {
                ComboCount = 0;
            }

            Vector2 playerCenter = new Vector2(player.Position.X + player.Rectangle.Width / 2, player.Position.Y + player.Rectangle.Height / 2);

            TimeToSpawn -= deltaTime;
            if (TimeToSpawn <= 0 && Enemies.Count < 7)
            {
                float DifficultyModifier = 1f / (((float)EnemiesKilled * Difficulty) + 1f);
                TimeToSpawn = (float)random.Next(875, 4376) * DifficultyModifier / 1000;

                Pickup pickup;
                bool   isColliding = false;

                int spawnAttempts = 0;
                do
                {
                    // Find whether the object will be spawned from above, the right, below, or the left
                    int start = random.Next(0, 2 * Pickup.PickupWidth() + 2 * Pickup.PickupHeight() + 1);
                    int x, y;
                    if (start < Pickup.PickupWidth())
                    {
                        y = -(Pickup.PickupHeight());
                        x = random.Next(-(Pickup.PickupWidth()), playArea.Width);
                    }
                    else if (start < Pickup.PickupWidth() + Pickup.PickupHeight())
                    {
                        x = playArea.Width;
                        y = random.Next(-(Pickup.PickupHeight()), playArea.Height);
                    }
                    else if (start < Pickup.PickupWidth() * 2 + Pickup.PickupHeight())
                    {
                        y = playArea.Height;
                        x = random.Next(-(Pickup.PickupWidth()), playArea.Width);
                    }
                    else
                    {
                        x = -(Pickup.PickupWidth());
                        y = random.Next(-(Pickup.PickupHeight()), playArea.Height);
                    }

                    Vector2 position = new Vector2(x, y);
                    //Vector2 direction = new Vector2((playArea.Width / 2) - (x + Pickup.PickupWidth() / 2), (playArea.Height / 2) - (y + Pickup.PickupHeight() / 2));
                    Vector2 direction = playerCenter - position;
                    pickup = new FollowPickup(position, direction, pikcups_Speed);

                    spawnAttempts++;
                    if (spawnAttempts >= 10)
                    {
                        break;
                    }

                    foreach (Pickup other in Enemies)
                    {
                        isColliding = false;
                        if (pickup.IsColliding(other))
                        {
                            isColliding = true;
                            break;
                        }
                    }
                }while (isColliding);

                Enemies.Add(pickup);
            }

            // Check enemy collisions. This is done the naive O(n^2) way. If given more time, something like
            // an axis-aligned bounding box tree could be implemented.
            int count = 0;

            while (count < Enemies.Count)
            {
                // Delete enemies who are dead and have had their animation finish
                if (Enemies[count].NeedsDeletion())
                {
                    Enemies.RemoveAt(count);
                    continue;
                }

                // Update enemies
                Enemies[count].Update(deltaTime, playerCenter);
                Enemies[count].Update(gameTime);

                // Remove the object if it has left the screen
                if (Enemies[count].IsOutOfBounds(playArea))
                {
                    Enemies.RemoveAt(count);
                    continue;
                }

                // Make enemies somewhat bounce off one another
                for (int i = 0; i < Enemies.Count; i++)
                {
                    if (Enemies[count].IsColliding(Enemies[i]))
                    {
                        Pickup temp1 = Enemies[count];
                        Pickup temp2 = Enemies[i];

                        Pickup.Collide(ref temp1, ref temp2);

                        Enemies[count] = temp1;
                        Enemies[i]     = temp2;
                    }
                }

                // Kill the enemy or the player if the two collide
                if (player.IsColliding(Enemies[count]))
                {
                    if (player.Boosting)
                    {
                        HandleEnemyDestroyed(Enemies[count]);
                        Enemies[count].Kill();
                        EnemiesKilled++;

                        player.BoostMeter += 0.1f;
                        if (player.BoostMeter >= 1.2f)
                        {
                            player.BoostMeter = 1.2f;
                        }

                        player.allsounds[1].Play(volume: 0.3f, pitch: 0f, pan: 0f);
                    }
                    else
                    {
                        player.allsounds[3].Play(volume: 1f, pitch: 0f, pan: 0f);
                        IsGameOver = true;
                    }
                }

                // Kill the enemies if they hit any obstacles
                //for (int i = 0; i < Obstacles.Count; i++)
                //{
                //    if (Enemies[count].IsColliding(Obstacles[i]))
                //    {
                //        Enemies[count].Kill();
                //        break;
                //    }
                //}

                count++;
            }
        }
예제 #3
0
 public bool IsOutOfBounds(Rectangle bounds)
 {
     return(Position.X < -(Pickup.PickupWidth() << 2) || Position.X > bounds.Width + Pickup.PickupWidth() ||
            Position.Y < -(Pickup.PickupHeight() << 2) || Position.Y > bounds.Height + Pickup.PickupHeight());
 }