public bool CheckForCollision(BoundingSphere bulletBoundingSphere, Debris[] barriers)
 {
     for (int curBarrier = 0; curBarrier < barriers.Length; curBarrier++)
     {
         if (bulletBoundingSphere.Intersects(
             barriers[curBarrier].BoundingSphere) && !barriers[curBarrier].Destroyed)
         {
             barriers[curBarrier].Destroyed = true;
             return true;
         }
     }
     return false;
 }
        private bool ValidateMovement(Vector3 futurePosition,
            Debris[] barriers)
        {
            BoundingSphere futureBoundingSphere = BoundingSphere;
            futureBoundingSphere.Center.X = futurePosition.X;
            futureBoundingSphere.Center.Y = futurePosition.Y;
            futureBoundingSphere.Center.Z = futurePosition.Z;

            //Don't allow off-terrain driving
            if ((Math.Abs(futurePosition.X) > MaxRange) ||
                (Math.Abs(futurePosition.Z) > MaxRange))
                return false;
            if (futurePosition.Y <= 0 || futurePosition.Y >= 150)
                return false;
            //Don't allow driving through a barrier
            if (CheckForDebrisCollision(futureBoundingSphere, barriers))
                return false;

            return true;
        }
 private bool CheckForDebrisCollision(
     BoundingSphere vehicleBoundingSphere, Debris[] barriers)
 {
     for (int curBarrier = 0; curBarrier < barriers.Length; curBarrier++)
     {
         if (vehicleBoundingSphere.Intersects(
             barriers[curBarrier].BoundingSphere) && !barriers[curBarrier].Destroyed)
             return true;
     }
     return false;
 }
        public void Update(GamePadState gamepadState,
            KeyboardState keyboardState, Debris[] barriers)
        {
            Vector3 futurePosition = Position;
            float xTurnAmount = 0;
            float yTurnAmount = 0;

            if (Mouse.GetState().X != 290 || Mouse.GetState().Y != 240)

            {
                xTurnAmount = 290 - Mouse.GetState().X;

                yTurnAmount = Mouse.GetState().Y - 240;

                Mouse.SetPosition(290, 240);
            }

            ForwardDirection += xTurnAmount * GameConstants.TurnSpeed;
            Matrix orientationMatrix = Matrix.CreateRotationY(ForwardDirection);

            if ((AimDirection + (yTurnAmount * GameConstants.TurnSpeed) < 1.5 &&
                (AimDirection + (yTurnAmount * GameConstants.TurnSpeed) > -1.5)))
            {
                AimDirection += yTurnAmount * GameConstants.TurnSpeed;
            }

            Vector3 movement = Vector3.Zero;

            if (keyboardState.IsKeyDown(Keys.W))
            {
                movement.Z = 1;
            }
            else if (keyboardState.IsKeyDown(Keys.S))
            {
                movement.Z = -1;
            }

            if (keyboardState.IsKeyDown(Keys.A))
            {
                movement.X = 1;
            }
            else if (keyboardState.IsKeyDown(Keys.D))
            {
                movement.X = -1;
            }

            if (keyboardState.IsKeyDown(Keys.LeftShift))
            {
                movement.Y = -1;
            }
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                movement.Y = 1;
            }

            Vector3 speed = Vector3.Transform(movement, orientationMatrix);

            speed *= GameConstants.Velocity;

            futurePosition = Position + speed;

            if (ValidateMovement(futurePosition, barriers))
            {
                Position = futurePosition;

                BoundingSphere updatedSphere;
                updatedSphere = BoundingSphere;

                updatedSphere.Center.X = Position.X;
                updatedSphere.Center.Y = Position.Y;
                updatedSphere.Center.Z = Position.Z;
                BoundingSphere = new BoundingSphere(updatedSphere.Center,
                    updatedSphere.Radius);
            }
        }
        private void InitializeGameField()
        {
            //Initialize barriers
            barriers = new Debris[GameConstants.NumBarriers];
            int randomBarrier = random.Next(3);
            string barrierName = null;

            for (int index = 0; index < GameConstants.NumBarriers; index++)
            {
                switch (randomBarrier)
                {
                    case 0:
                        barrierName = "Models/cube10uR";
                        break;
                    case 1:
                        barrierName = "Models/cylinder10uR";
                        break;
                    case 2:
                        barrierName = "Models/pyramid10uR";
                        break;
                }
                barriers[index] = new Debris();
                barriers[index].LoadContent(Content, barrierName);
                randomBarrier = random.Next(3);
            }
            PlaceFuelCellsAndDebris();
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            startScreen = Content.Load<Texture2D>("Images/Startup");
            instructionScreen = Content.Load<Texture2D>("Images/Instructions");
            controlsScreen = Content.Load<Texture2D>("Images/Controls");

            healthbar1 = Content.Load<Texture2D>("Images/healthbar");
            healthbar2 = Content.Load<Texture2D>("Images/healthbar2");

            crosshair = Content.Load<Texture2D>("Images/crosshair");

            ground.Model = Content.Load<Model>("Models/ground");
            boundingSphere.Model = Content.Load<Model>("Models/sphere1uR");

            spriteBatch = new SpriteBatch(GraphicsDevice);
            statsFont = Content.Load<SpriteFont>("Fonts/StatsFont");

            //Initialize fuel cells
            fuelCells = new FuelCell[GameConstants.NumFuelCells];
            for (int index = 0; index < fuelCells.Length; index++)
            {
                fuelCells[index] = new FuelCell();
                fuelCells[index].LoadContent(Content, "Models/fuelcell");
            }

            //Initialize barriers
            barriers = new Debris[GameConstants.NumBarriers];
            int randomBarrier = random.Next(3);
            string barrierName = null;

            for (int index = 0; index < barriers.Length; index++)
            {

                switch (randomBarrier)
                {
                    case 0:
                        barrierName = "Models/cube10uR";
                        break;
                    case 1:
                        barrierName = "Models/cylinder10uR";
                        break;
                    case 2:
                        barrierName = "Models/pyramid10uR";
                        break;
                }
                barriers[index] = new Debris();
                barriers[index].LoadContent(Content, barrierName);
                randomBarrier = random.Next(3);
            }
            PlaceFuelCellsAndDebris();

            //Initialize fuel carrier
            fuelCarrier = new Ship();
            fuelCarrier.LoadContent(Content, "Models/fuelcarrier");

            //int min = GameConstants.MinDistance;
            //int max = GameConstants.MaxDistance;

            //Load sound effects
            laserSound = Content.Load<SoundEffect>("Sounds/laserSound");
            laserSound2 = Content.Load<SoundEffect>("Sounds/laserSound2");
            refuel = Content.Load<SoundEffect>("Sounds/powerUp");
            explosion = Content.Load<SoundEffect>("Sounds/explosion");
        }
        public void Update(Debris[] barriers)
        {
            distance = distance * 1.2f;
            Vector3 direction = Vector3.Transform(new Vector3(0, 0, distance), Matrix.CreateRotationX(AimDirection));
            direction = Vector3.Transform(direction, Matrix.CreateRotationY(ForwardDirection));

            Position = direction + startPosition;

            timer++;

            BoundingSphere updatedSphere;
            updatedSphere = BoundingSphere;

            updatedSphere.Center.X = Position.X;
            updatedSphere.Center.Y = Position.Y;
            updatedSphere.Center.Z = Position.Z;
            BoundingSphere = new BoundingSphere(updatedSphere.Center,
                updatedSphere.Radius);
        }