// Undo private void btn_Click(object sender, EventArgs e) { engine.UndoLastMove(); UpdateGUI(); }
/// <summary> /// Updates the user input, handles all the keypresses, mouse movements etc. /// </summary> /// <param name="gameTime">Elapsed time</param> private void UpdateInput(GameTime gameTime) { newKeyboardState = Keyboard.GetState(); if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.gameState = GameState.MENU; } #region Rotation if (Keyboard.GetState().IsKeyDown(Keys.Right)) { rotY += 0.1f; cam.DoYRotation(rotY); } if (Keyboard.GetState().IsKeyDown(Keys.Left)) { rotY += 0.1f; cam.DoYRotation(rotY * -1); } if (Keyboard.GetState().IsKeyDown(Keys.Home) || Keyboard.GetState().IsKeyDown(Keys.OemMinus)) { rotX -= 0.5f; cam.DoXRotation(rotX); } if (Keyboard.GetState().IsKeyDown(Keys.End) || Keyboard.GetState().IsKeyDown(Keys.OemPlus)) { rotX += 0.5f; cam.DoXRotation(rotX); } // Reset rotation if (!Keyboard.GetState().IsKeyDown(Keys.Left) && !Keyboard.GetState().IsKeyDown(Keys.Right)) { rotY = 0.0f; } if (!Keyboard.GetState().IsKeyDown(Keys.Down) && !Keyboard.GetState().IsKeyDown(Keys.Up)) { rotX = 0.0f; } if (!Keyboard.GetState().IsKeyDown(Keys.Home) && !Keyboard.GetState().IsKeyDown(Keys.End)) { rotX = 0.0f; } // Rest rotX & rotY if (rotY >= 1.0f) { rotY = 0.9f; } if (rotX >= 1.0f) { rotX = 0.9f; } #endregion #region Zoom if (Keyboard.GetState().IsKeyDown(Keys.PageUp) || Keyboard.GetState().IsKeyDown(Keys.Up)) { cam.DoZoom(-0.01f); } if (Keyboard.GetState().IsKeyDown(Keys.PageDown) || Keyboard.GetState().IsKeyDown(Keys.Down)) { cam.DoZoom(0.01f); } if (Mouse.GetState().ScrollWheelValue != oldMouseState.ScrollWheelValue) { float difference = (Mouse.GetState().ScrollWheelValue - oldMouseState.ScrollWheelValue); cam.DoZoom(difference / 1000); } #endregion #region FixedCameraPositions if (Keyboard.GetState().IsKeyDown(Keys.T) && !tPressed) { cam.SetFixedTop(); tPressed = true; } if ((Keyboard.GetState().IsKeyDown(Keys.R) || Mouse.GetState().MiddleButton == ButtonState.Pressed) && !rPresssed) { rPresssed = true; cam.DoYRotation(180f); } // Set view player 1 & 2 with the number keys if (Keyboard.GetState().IsKeyDown(Keys.D1)) { cam.SetFixedViewPlayerOne(); } if (Keyboard.GetState().IsKeyDown(Keys.D2)) { cam.SetFixedViewPlayerTwo(); } // Restore pressed states if (Keyboard.GetState().IsKeyUp(Keys.T)) { tPressed = false; } if (Keyboard.GetState().IsKeyUp(Keys.R) && Mouse.GetState().MiddleButton == ButtonState.Released) { rPresssed = false; } #endregion if (newKeyboardState.IsKeyUp(Keys.F11) && oldKeyboardState.IsKeyDown(Keys.F11)) { graphics.ToggleFullScreen(); Window_ClientSizeChanged(null, null); } if (Keyboard.GetState().IsKeyUp(Keys.Space)) { spacePressed = false; } if (Keyboard.GetState().IsKeyDown(Keys.F1)) { if (!f1Pressed) { ShowBoxes = (ShowBoxes) ? false : true; f1Pressed = true; } } if (Keyboard.GetState().IsKeyUp(Keys.F1)) { f1Pressed = false; } if (Keyboard.GetState().IsKeyDown(Keys.OemOpenBrackets)) { radioLeftPressed = true; } if (Keyboard.GetState().IsKeyDown(Keys.OemCloseBrackets)) { radioRightPressed = true; } if (radioLeftPressed) { if (Keyboard.GetState().IsKeyUp(Keys.OemOpenBrackets)) { radioLeftPressed = false; Sound.Instance().goLeft(); } } if (radioRightPressed) { if (Keyboard.GetState().IsKeyUp(Keys.OemCloseBrackets)) { radioRightPressed = false; Sound.Instance().goRight(); } } #region Handle Mouse //Disable moving while computer is calculating move if (!computerIsThinking) { oldMouseState = Mouse.GetState(); if (oldMouseState.LeftButton == ButtonState.Pressed) { this.leftMouseDown = true; } if (this.leftMouseDown && oldMouseState.LeftButton == ButtonState.Released) { this.leftMouseDown = false; Vector2 mousePosition = new Vector2(oldMouseState.X, oldMouseState.Y); List <KeyValuePair <float?, KeyValuePair <int, Type> > > results = new List <KeyValuePair <float?, KeyValuePair <int, Type> > >(); foreach (var tile in TileComponents) { tile.Value.IsSelected = false; Vector3 nearPlane = new Vector3(mousePosition.X, mousePosition.Y, 0); Vector3 farPlane = new Vector3(mousePosition.X, mousePosition.Y, 1); nearPlane = GraphicsDevice.Viewport.Unproject(nearPlane, cam.Projection, cam.View, tile.Value.TileMatrix); farPlane = GraphicsDevice.Viewport.Unproject(farPlane, cam.Projection, cam.View, tile.Value.TileMatrix); Vector3 direction = farPlane - nearPlane; direction.Normalize(); Ray ray = new Ray(nearPlane, direction); float?result = ray.Intersects((BoundingBox)tile.Value.TileModel.Tag); if (result.HasValue) { results.Add(new KeyValuePair <float?, KeyValuePair <int, Type> >(result, new KeyValuePair <int, Type>(tile.Key, typeof(Tile)))); } } Dictionary <float, int> moveableClick = new Dictionary <float, int>(); Dictionary <float, int> StartingClick = new Dictionary <float, int>(); if (this.StartingPieces.Count == 0) { foreach (var piece in PieceComponents) { piece.Value.IsSelected = false; Vector3 nearPlane = new Vector3(mousePosition.X, mousePosition.Y, 0); Vector3 farPlane = new Vector3(mousePosition.X, mousePosition.Y, 1); nearPlane = GraphicsDevice.Viewport.Unproject(nearPlane, cam.Projection, cam.View, piece.Value.world); farPlane = GraphicsDevice.Viewport.Unproject(farPlane, cam.Projection, cam.View, piece.Value.world); Vector3 direction = farPlane - nearPlane; direction.Normalize(); Ray ray = new Ray(nearPlane, direction); float?result = ray.Intersects((BoundingBox)piece.Value.PieceModel.Tag); if (result.HasValue) { results.Add(new KeyValuePair <float?, KeyValuePair <int, Type> >(result, new KeyValuePair <int, Type>(piece.Key, typeof(Piece)))); } } for (int i = 0; i < moveToList.Count; i++) { Vector3 nearPlane = new Vector3(mousePosition.X, mousePosition.Y, 0); Vector3 farPlane = new Vector3(mousePosition.X, mousePosition.Y, 1); nearPlane = GraphicsDevice.Viewport.Unproject(nearPlane, cam.Projection, cam.View, moveToList[i].TileMatrix); farPlane = GraphicsDevice.Viewport.Unproject(farPlane, cam.Projection, cam.View, moveToList[i].TileMatrix); Vector3 direction = farPlane - nearPlane; direction.Normalize(); Ray ray = new Ray(nearPlane, direction); float?result = ray.Intersects((BoundingBox)moveToList[i].TileModel.Tag); if (result.HasValue) { moveableClick.Add(result.Value, i); } } } else { for (int i = 0; i < this.StartingPieces.Count; i++) { Vector3 nearPlane = new Vector3(mousePosition.X, mousePosition.Y, 0); Vector3 farPlane = new Vector3(mousePosition.X, mousePosition.Y, 1); nearPlane = GraphicsDevice.Viewport.Unproject(nearPlane, cam.Projection, cam.View, StartingPieces[i].OnTopofTile.TileMatrix); farPlane = GraphicsDevice.Viewport.Unproject(farPlane, cam.Projection, cam.View, StartingPieces[i].OnTopofTile.TileMatrix); Vector3 direction = farPlane - nearPlane; direction.Normalize(); Ray ray = new Ray(nearPlane, direction); float?result = ray.Intersects((BoundingBox)StartingPieces[i].PieceModel.Tag); if (result.HasValue) { StartingClick.Add(result.Value, i); } } } float shortestDistance = float.PositiveInfinity; int index = 0; int j = 0; foreach (var result in results) { if (result.Key < shortestDistance) { index = j; } j++; } if (StartingClick.Count > 0) { if (index != 0) { if (results[index].Key < StartingClick.First().Key) { didLastMove = true; DoMove(StartingClick.First().Value, -1, -1); results.Clear(); moveableClick.Clear(); } } else { DoMove(StartingClick.First().Value, -1, -1); } } if (moveableClick.Count > 0) { if (index != 0) { if (results[index].Key < moveableClick.First().Key) { DoMove(-1, -1, moveableClick.First().Value); results.Clear(); } } else { DoMove(-1, -1, moveableClick.First().Value); } } if (results.Count > 0) { if (results[index].Value.Value == typeof(Tile)) { this.DoMove(-1, results[index].Value.Key, -1); } else { this.DoMove(results[index].Value.Key, -1, -1); } } } if (oldMouseState.RightButton == ButtonState.Pressed) { if ((gameTime.TotalGameTime.TotalMilliseconds - this.undoTimer) < 1000 && !moveUndone && engine.GetGameState() == KaroEngine.GameState.PLAYING) { //undo int[] move = engine.UndoLastMove(); Point positionTo = new Point(move[0] % Game1.BOARDWIDTH, move[0] / Game1.BOARDWIDTH); Point positionFrom = new Point(move[1] % Game1.BOARDWIDTH, move[1] / Game1.BOARDWIDTH); Point tileFrom = new Point(move[2] % Game1.BOARDWIDTH, move[2] / Game1.BOARDWIDTH); if (tileFrom.X != -1) { UndoTileMove(tileFrom, positionTo, positionFrom); } else { this.ShowMove(positionFrom, positionTo, tileFrom); } moveUndone = true; } else { this.ClearSelectedItems(); } } } #endregion oldKeyboardState = newKeyboardState; }