/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { KeyboardState keyboardState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Allows the game to exit if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y); // Move forward/backward if (keyboardState.IsKeyDown(Keys.Up)) { m_camera.MoveForward(-0.1f); } else if (keyboardState.IsKeyDown(Keys.Down)) { m_camera.MoveForward(0.1f); } // Strafe left/right if (keyboardState.IsKeyDown(Keys.A)) { m_camera.Starfe(0.1f, true); } else if (keyboardState.IsKeyDown(Keys.D)) { m_camera.Starfe(-0.1f, true); } // Strafe left/right if (keyboardState.IsKeyDown(Keys.Z)) { m_camera.Starfe(-5f, false); } else if (keyboardState.IsKeyDown(Keys.C)) { m_camera.Starfe(5f, false); } // Levitate up/down if (keyboardState.IsKeyDown(Keys.W)) { m_camera.Levitate(-0.1f); } else if (keyboardState.IsKeyDown(Keys.S)) { m_camera.Levitate(0.1f); } m_camera.UpdateMouseRay(mousePos, graphics.GraphicsDevice.Viewport); if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) { List <Vox> toCheckDistanceOn = new List <Vox>(); float distance; Nullable <float> tempDistance = null; Vox tempVox = new Vox(); for (int i = 0; i <= voxels.Count - 1; i++) { if (voxels[i].CheckRayIntersection(m_camera.MouseRay) != null) { toCheckDistanceOn.Add(voxels[i]); } } for (int i = 0; i <= toCheckDistanceOn.Count - 1; i++) { float tempX = toCheckDistanceOn[i].Position.X - m_camera.position.X; float tempY = toCheckDistanceOn[i].Position.Y - m_camera.position.Y; float tempZ = toCheckDistanceOn[i].Position.Z - m_camera.position.Z; distance = (float)Math.Sqrt(Math.Abs(tempX * tempX + tempY * tempY + tempZ * tempZ)); if (distance < tempDistance) { tempDistance = distance; tempVox = toCheckDistanceOn[i]; } if (tempDistance == null) { tempDistance = distance; tempVox = toCheckDistanceOn[i]; } } if (toCheckDistanceOn.Count > 0) { voxels.Remove(tempVox); } } if (mouseState.RightButton == ButtonState.Pressed && prevMouseState.RightButton == ButtonState.Released) { float distance; Nullable <float> tempDistance = null; Nullable <Vector3> tempDist = new Nullable <Vector3>(); Nullable <Vector3> rayDistances = new Nullable <Vector3>(); List <Nullable <Vector3> > toCheckDistanceOn = new List <Nullable <Vector3> >(); for (int i = 0; i <= voxels.Count - 1; i++) { rayDistances = voxels[i].CheckRayIntersection(m_camera.MouseRay); if (rayDistances != null) { toCheckDistanceOn.Add(rayDistances); } } for (int i = 0; i <= toCheckDistanceOn.Count - 1; i++) { float tempX = toCheckDistanceOn[i].Value.X - m_camera.position.X; float tempY = toCheckDistanceOn[i].Value.Y - m_camera.position.Y; float tempZ = toCheckDistanceOn[i].Value.Z - m_camera.position.Z; distance = (float)Math.Sqrt(Math.Abs(tempX * tempX + tempY * tempY + tempZ * tempZ)); if (distance < tempDistance) { tempDistance = distance; tempDist = toCheckDistanceOn[i]; } if (tempDistance == null) { tempDistance = distance; tempDist = toCheckDistanceOn[i]; } } List <Vox> VOXtoCheckDistanceOn = new List <Vox>(); float VOXdistance; Nullable <float> VOXtempDistance = null; Vox tempVox = new Vox(); for (int i = 0; i <= voxels.Count - 1; i++) { if (voxels[i].CheckRayIntersection(m_camera.MouseRay) != null) { VOXtoCheckDistanceOn.Add(voxels[i]); } } for (int i = 0; i <= VOXtoCheckDistanceOn.Count - 1; i++) { float tempX = VOXtoCheckDistanceOn[i].Position.X - m_camera.position.X; float tempY = VOXtoCheckDistanceOn[i].Position.Y - m_camera.position.Y; float tempZ = VOXtoCheckDistanceOn[i].Position.Z - m_camera.position.Z; VOXdistance = (float)Math.Sqrt(Math.Abs(tempX * tempX + tempY * tempY + tempZ * tempZ)); if (VOXdistance < VOXtempDistance) { VOXtempDistance = VOXdistance; tempVox = VOXtoCheckDistanceOn[i]; } if (VOXtempDistance == null) { VOXtempDistance = VOXdistance; tempVox = VOXtoCheckDistanceOn[i]; } } Vector3 valueOfCube = new Vector3(); if (tempDist != null) { if (tempDist.Value.X - tempVox.Position.X == 0.5) { valueOfCube = new Vector3(1, 0, 0); } if (tempDist.Value.Y - tempVox.Position.Y == 0.5) { valueOfCube = new Vector3(0, 1, 0); } if (tempDist.Value.Z - tempVox.Position.Z == 0.5) { valueOfCube = new Vector3(0, 0, 1); } if (tempDist.Value.X - tempVox.Position.X == -0.5) { valueOfCube = new Vector3(-1, 0, 0); } if (tempDist.Value.Y - tempVox.Position.Y == -0.5) { valueOfCube = new Vector3(0, -1, 0); } if (tempDist.Value.Z - tempVox.Position.Z == -0.5) { valueOfCube = new Vector3(0, 0, -1); } if (toCheckDistanceOn.Count > 0) { voxels.Add(new Vox(new Vector3(tempVox.Position.X + valueOfCube.X, tempVox.Position.Y + valueOfCube.Y, tempVox.Position.Z + valueOfCube.Z), 1, Color.Black, GraphicsDevice, view, projection, fx)); } } } // TODO: Add your update logic here for (int i = 0; i < voxels.Count; i++) { voxels[i].Update(gameTime); } m_camera.Update(); prevMouseState = mouseState; prevKeyboardState = keyboardState; base.Update(gameTime); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { KeyboardState keyboardState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Allows the game to exit if (Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y); // Move forward/backward if (keyboardState.IsKeyDown(Keys.Up)) m_camera.MoveForward(-0.1f); else if (keyboardState.IsKeyDown(Keys.Down)) m_camera.MoveForward(0.1f); // Strafe left/right if (keyboardState.IsKeyDown(Keys.A)) m_camera.Starfe(0.1f, true); else if (keyboardState.IsKeyDown(Keys.D)) m_camera.Starfe(-0.1f, true); // Strafe left/right if (keyboardState.IsKeyDown(Keys.Z)) m_camera.Starfe(-5f, false); else if (keyboardState.IsKeyDown(Keys.C)) m_camera.Starfe(5f, false); // Levitate up/down if (keyboardState.IsKeyDown(Keys.W)) m_camera.Levitate(-0.1f); else if (keyboardState.IsKeyDown(Keys.S)) m_camera.Levitate(0.1f); m_camera.UpdateMouseRay(mousePos, graphics.GraphicsDevice.Viewport); if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) { List<Vox> toCheckDistanceOn = new List<Vox>(); float distance; Nullable<float> tempDistance = null; Vox tempVox = new Vox(); for (int i = 0; i <= voxels.Count - 1; i++) { if (voxels[i].CheckRayIntersection(m_camera.MouseRay) != null) toCheckDistanceOn.Add(voxels[i]); } for (int i = 0; i <= toCheckDistanceOn.Count - 1; i++) { float tempX = toCheckDistanceOn[i].Position.X - m_camera.position.X; float tempY = toCheckDistanceOn[i].Position.Y - m_camera.position.Y; float tempZ = toCheckDistanceOn[i].Position.Z - m_camera.position.Z; distance = (float)Math.Sqrt(Math.Abs(tempX * tempX + tempY * tempY + tempZ * tempZ)); if (distance < tempDistance) { tempDistance = distance; tempVox = toCheckDistanceOn[i]; } if (tempDistance == null) { tempDistance = distance; tempVox = toCheckDistanceOn[i]; } } if (toCheckDistanceOn.Count > 0) voxels.Remove(tempVox); } if (mouseState.RightButton == ButtonState.Pressed && prevMouseState.RightButton == ButtonState.Released) { float distance; Nullable<float> tempDistance = null; Nullable<Vector3> tempDist = new Nullable<Vector3>(); Nullable<Vector3> rayDistances = new Nullable<Vector3>(); List<Nullable<Vector3>> toCheckDistanceOn = new List<Nullable<Vector3>>(); for (int i = 0; i <= voxels.Count - 1; i++) { rayDistances = voxels[i].CheckRayIntersection(m_camera.MouseRay); if (rayDistances != null) toCheckDistanceOn.Add(rayDistances); } for (int i = 0; i <= toCheckDistanceOn.Count - 1; i++) { float tempX = toCheckDistanceOn[i].Value.X - m_camera.position.X; float tempY = toCheckDistanceOn[i].Value.Y - m_camera.position.Y; float tempZ = toCheckDistanceOn[i].Value.Z - m_camera.position.Z; distance = (float)Math.Sqrt(Math.Abs(tempX * tempX + tempY * tempY + tempZ * tempZ)); if (distance < tempDistance) { tempDistance = distance; tempDist = toCheckDistanceOn[i]; } if (tempDistance == null) { tempDistance = distance; tempDist = toCheckDistanceOn[i]; } } List<Vox> VOXtoCheckDistanceOn = new List<Vox>(); float VOXdistance; Nullable<float> VOXtempDistance = null; Vox tempVox = new Vox(); for (int i = 0; i <= voxels.Count - 1; i++) { if (voxels[i].CheckRayIntersection(m_camera.MouseRay) != null) VOXtoCheckDistanceOn.Add(voxels[i]); } for (int i = 0; i <= VOXtoCheckDistanceOn.Count - 1; i++) { float tempX = VOXtoCheckDistanceOn[i].Position.X - m_camera.position.X; float tempY = VOXtoCheckDistanceOn[i].Position.Y - m_camera.position.Y; float tempZ = VOXtoCheckDistanceOn[i].Position.Z - m_camera.position.Z; VOXdistance = (float)Math.Sqrt(Math.Abs(tempX * tempX + tempY * tempY + tempZ * tempZ)); if (VOXdistance < VOXtempDistance) { VOXtempDistance = VOXdistance; tempVox = VOXtoCheckDistanceOn[i]; } if (VOXtempDistance == null) { VOXtempDistance = VOXdistance; tempVox = VOXtoCheckDistanceOn[i]; } } Vector3 valueOfCube = new Vector3(); if (tempDist != null) { if (tempDist.Value.X - tempVox.Position.X == 0.5) valueOfCube = new Vector3(1, 0, 0); if (tempDist.Value.Y - tempVox.Position.Y == 0.5) valueOfCube = new Vector3(0, 1, 0); if (tempDist.Value.Z - tempVox.Position.Z == 0.5) valueOfCube = new Vector3(0, 0, 1); if (tempDist.Value.X - tempVox.Position.X == -0.5) valueOfCube = new Vector3(-1, 0, 0); if (tempDist.Value.Y - tempVox.Position.Y == -0.5) valueOfCube = new Vector3(0, -1, 0); if (tempDist.Value.Z - tempVox.Position.Z == -0.5) valueOfCube = new Vector3(0, 0, -1); if (toCheckDistanceOn.Count > 0) voxels.Add(new Vox(new Vector3(tempVox.Position.X + valueOfCube.X, tempVox.Position.Y + valueOfCube.Y, tempVox.Position.Z + valueOfCube.Z), 1, Color.Black, GraphicsDevice, view, projection, fx)); } } // TODO: Add your update logic here for (int i = 0; i < voxels.Count; i++) { voxels[i].Update(gameTime); } m_camera.Update(); prevMouseState = mouseState; prevKeyboardState = keyboardState; base.Update(gameTime); }