public override void Update(GameTime gameTime) { if (curGame.IsActive) { // 获取鼠标状态 lastMouseState = currentMouseState; currentMouseState = Mouse.GetState(); bool isMouseInUp = false, isMouseInDown = false; if (currentMouseState.X >= buttonRectangle.Left && currentMouseState.Y >= buttonRectangle.Top && currentMouseState.X <= buttonRectangle.Right && currentMouseState.Y <= buttonRectangle.Bottom) { if (currentMouseState.Y <= buttonRectangle.CenterY) { isMouseInUp = true; } else { isMouseInDown = true; } } if (isMouseInUp || isMouseInDown) { if (type == SettingType.Start) { buttonTexture = buttonStartRollTexture; } else if (type == SettingType.Pause) { if (isMouseInUp) { buttonTexture = buttonResumeRollTexture; } else if (isMouseInDown) { buttonTexture = buttonRestartRollTexture; } } // 如果单击鼠标左键 if ((currentMouseState.LeftButton == ButtonState.Released) && (lastMouseState.LeftButton == ButtonState.Pressed)) { if (type == SettingType.Start || isMouseInDown) { startButtonClicked = true; } else { resumeButtonClicked = true; } } } // 当按下 Enter 键时 lastKeyboardState = currentKeyboardState; currentKeyboardState = Keyboard.GetState(Keys.Enter); if (currentKeyboardState.IsKeyUp(Keys.Enter) && lastKeyboardState.IsKeyDown(Keys.Enter)) { if (type == SettingType.Start) { startButtonClicked = true; } else { resumeButtonClicked = true; } } if (buttonTexture == null) { if (type == SettingType.Start) { buttonTexture = buttonStartNormalTexture; } else if (type == SettingType.Pause) { buttonTexture = buttonResumeNormalTexture; } } } // base.Update(gameTime); }
public override void Update(GameTime gameTime) { if (settingBoardState == SettingBoardState.Showing) { settingBoardState = SettingBoardState.Show; } else if (settingBoardState == SettingBoardState.Show) { if (startButton.StartButtonClicked) { for (int i = 0; i < 2; i++) { PlayerSettings.Player[i].Type = PlayerType[i]; if (PlayerType[i] == PlayerTypes.AI) { PlayerSettings.Player[i].AIIndex = AIIndex[i]; } } settingBoardState = SettingBoardState.HidingForNewGame; } else if (startButton.ResumeButtonClicked) { settingBoardState = SettingBoardState.HidingForResume; } else { // 绘制选择状态 foreach (Piece piece in showPieces) { piece.CurrentDisplay = DisplayState.Normal; } foreach (Piece piece in playerTypePieces) { piece.CurrentDisplay = DisplayState.Normal; } foreach (int i in chooseIndex) { playerTypePieces[i].CurrentDisplay = DisplayState.CanMove; } for (int i = 0; i < AICount; i++) { if (chooseIndex[0] == 1) { aiTypePieces[0, i].PieceState = PieceState.Black; } else { aiTypePieces[0, i].PieceState = PieceState.Empty; } if (chooseIndex[1] == 2) { aiTypePieces[1, i].PieceState = PieceState.White; } else { aiTypePieces[1, i].PieceState = PieceState.Empty; } } if (chooseIndex[0] == 1) { aiTypePieces[0, AIIndex[0]].CurrentDisplay = DisplayState.WillReverse; } if (chooseIndex[1] == 2) { aiTypePieces[1, AIIndex[1]].CurrentDisplay = DisplayState.WillReverse; } if (curGame.IsActive) { // 获取鼠标状态 lastMouseState = currentMouseState; currentMouseState = Mouse.GetState(); ReversiPiecePosition tempPosition = Board.IsMouseInPiece(currentMouseState); // 如果移动鼠标 if (tempPosition != null) { // 如果划过的是黑白子 if (tempPosition.Y == 2) { if (tempPosition.X == 3) { showPieces[0].CurrentDisplay = DisplayState.WillMove; } else if (tempPosition.X == 4) { showPieces[1].CurrentDisplay = DisplayState.WillMove; } } // 如果划过的是玩家类型选择 else if (tempPosition.Y == 3) { int index = -1; if (tempPosition.X == 1) { index = 0; } else if (tempPosition.X == 2) { index = 1; } else if (tempPosition.X == 5) { index = 2; } else if (tempPosition.X == 6) { index = 3; } if (index >= 0) { playerTypePieces[index].CurrentDisplay = DisplayState.WillMove; } } // 如果划过的是AI类型 if ((tempPosition.X == 0 || tempPosition.X == 7) && (tempPosition.Y >= 3 && tempPosition.Y <= 2 + AICount)) { if (chooseIndex[0] == 1 && tempPosition.X == 0) { int index = tempPosition.Y - 3; aiTypePieces[0, index].CurrentDisplay = DisplayState.WillMove; } if (chooseIndex[1] == 2 && tempPosition.X == 7) { int index = tempPosition.Y - 3; aiTypePieces[1, index].CurrentDisplay = DisplayState.WillMove; } } // 如果按下鼠标左键 if ((currentMouseState.LeftButton == ButtonState.Pressed) && (lastMouseState.LeftButton == ButtonState.Released)) { // 如果点击的是黑白子 if (tempPosition.Y == 2) { if (tempPosition.X == 3) { chooseIndex[0] = 0; PlayerType[0] = PlayerTypes.Human; chooseIndex[1] = 2; PlayerType[1] = PlayerTypes.AI; } else if (tempPosition.X == 4) { chooseIndex[0] = 1; PlayerType[0] = PlayerTypes.AI; chooseIndex[1] = 3; PlayerType[1] = PlayerTypes.Human; } } // 如果点击的是玩家类型选择 else if (tempPosition.Y == 3) { if (tempPosition.X == 1) { chooseIndex[0] = 0; PlayerType[0] = PlayerTypes.Human; } if (tempPosition.X == 2) { chooseIndex[0] = 1; PlayerType[0] = PlayerTypes.AI; } if (tempPosition.X == 5) { chooseIndex[1] = 2; PlayerType[1] = PlayerTypes.AI; } if (tempPosition.X == 6) { chooseIndex[1] = 3; PlayerType[1] = PlayerTypes.Human; } } // 如果点击的是AI类型选择 if (tempPosition.X == 0 || tempPosition.X == 7) { if (chooseIndex[0] == 1 && tempPosition.X == 0) { AIIndex[0] = tempPosition.Y - 3; } if (chooseIndex[1] == 2 && tempPosition.X == 7) { AIIndex[1] = tempPosition.Y - 3; } } } } } foreach (Piece piece in showPieces) { piece.Update(gameTime); } foreach (Piece piece in playerTypePieces) { piece.Update(gameTime); } foreach (Piece piece in aiTypePieces) { piece.Update(gameTime); } startButton.Update(gameTime); } // 获取键盘状态 lastKeyboardState = currentKeyboardState; currentKeyboardState = Keyboard.GetState(Keys.Escape); // 当按下 Escape 键时 if (currentKeyboardState.IsKeyUp(Keys.Escape) && lastKeyboardState.IsKeyDown(Keys.Escape)) { if (settingType == SettingType.Pause) { settingBoardState = SettingBoardState.HidingForResume; } } } else if (settingBoardState == SettingBoardState.HidingForNewGame) { // TODO: 隐藏动画 settingBoardState = SettingBoardState.HideForNewGame; } else if (settingBoardState == SettingBoardState.HidingForResume) { // TODO: 隐藏动画 settingBoardState = SettingBoardState.HideForResume; } else if (settingBoardState == SettingBoardState.HideForNewGame) { curGame.State = GameState.StartLoad; } else if (settingBoardState == SettingBoardState.HideForResume) { curGame.State = GameState.InGame; } // base.Update(gameTime); }
/// <summary> /// 允许游戏允许逻辑例如更新地图, 检查碰撞, 收集输入, 或者播放音效. /// </summary> /// <param name="gameTime">提供一个游戏时间快照.</param> public override void Update(GameTime gameTime) { // 获取键盘状态 lastKeyboardState = currentKeyboardState; currentKeyboardState = Keyboard.GetState(Keys.F, Keys.Escape); // 当按下 F 键时 if (currentKeyboardState.IsKeyDown(Keys.F) && lastKeyboardState.IsKeyUp(Keys.F)) { GameSettings.ShowFPS = !(GameSettings.ShowFPS); } // 开始状态循环 if (State == GameState.Menu) { playerSettings.Update(gameTime); } else if (State == GameState.StartLoad) { loading.Initialize(); loading.Update(gameTime); board.Initialize(); State = GameState.Loading; } else if (State == GameState.Loading) { loading.Update(gameTime); } else if (State == GameState.CheckLoaded) { if (board.IsInitializing) { loading.Initialize(); loading.Update(gameTime); State = GameState.Loading; } else { board.Update(gameTime); State = GameState.InGame; } } else if (State == GameState.InGame) { // 当按下 Escape 键时 if (currentKeyboardState.IsKeyUp(Keys.Escape) && lastKeyboardState.IsKeyDown(Keys.Escape)) { if (board.CurrentBoardState == BoardState.GameOver) { State = GameState.Menu; playerSettings.Show(SettingType.Start); } else { State = GameState.Pause; playerSettings.Show(SettingType.Pause); } playerSettings.Update(gameTime); } board.Update(gameTime); } else if (State == GameState.Pause) { playerSettings.Update(gameTime); } else if (State == GameState.Over) { } if (GameSettings.ShowFPS) { fpsCounter.Update(gameTime); } base.Update(gameTime); }
/// <summary> /// Reads the keyboard state from <see cref="GameHost.GetKeyboardState"/>. /// </summary> /// <param name="elapsedSeconds">Fractional seconds passed since Update was called.</param> public void Update(System.TimeSpan elapsedSeconds) { KeysPressedInternal.Clear(); KeysReleasedInternal.Clear(); // Cycle all the keys down known if any are up currently, remove them from KeysDownInternal _state = GameHost.Instance.GetKeyboardState(); bool shiftPressed = _state.IsKeyDown(Keys.LeftShift) || _state.IsKeyDown(Keys.RightShift); Keys[] unmappedVirtualKeys = _state.GetPressedKeys(); for (int i = 0; i < KeysDownInternal.Count;) { if (_state.IsKeyUp(UnmappedVirtualKeysDown[i])) { KeysReleasedInternal.Add(KeysDownInternal[i]); RemoveKeyDownAt(i); } else { i++; } } // KeysDownInternal now contains all the keys that are currently down except potentially newly pressed keys // however the shift state may have changed so if there was an 'a' previously and the shift key was // just pressed, then the 'a' needs to get replaced with 'A'. // For all new keys down, if we don't know them, add them to pressed, add them to down. foreach (Keys unmappedVirtualKey in unmappedVirtualKeys) { bool firstPressed = false; AsciiKey key = new AsciiKey(); AsciiKey keyOppositeShift = new AsciiKey(); int activeKeyIndex = -1; // These keys will be mapped since Fill does the mapping automatically key.Fill(unmappedVirtualKey, shiftPressed, _state); keyOppositeShift.Fill(unmappedVirtualKey, !shiftPressed, _state); if (KeysDownInternal.Contains(key)) { activeKeyIndex = KeysDownInternal.FindIndex(k => k == key); AsciiKey thisKey = KeysDownInternal[activeKeyIndex]; thisKey.TimeHeld += (float)elapsedSeconds.TotalSeconds; KeysDownInternal[activeKeyIndex] = thisKey; } else if (KeysDownInternal.Contains(keyOppositeShift)) { activeKeyIndex = KeysDownInternal.FindIndex(k => k == keyOppositeShift); AsciiKey thisKey = KeysDownInternal[activeKeyIndex]; thisKey.Character = key.Character; thisKey.TimeHeld += (float)elapsedSeconds.TotalSeconds; KeysDownInternal[activeKeyIndex] = thisKey; } else { // The physical key (independent of shift) for this character was not being pressed before // so add it in. firstPressed = true; activeKeyIndex = KeysDownInternal.Count; AddKeyDown(key, unmappedVirtualKey); } AsciiKey activeKey = KeysDownInternal[activeKeyIndex]; if (firstPressed) { KeysPressedInternal.Add(activeKey); } else if (activeKey.PostInitialDelay == false && activeKey.TimeHeld >= InitialRepeatDelay) { activeKey.PostInitialDelay = true; activeKey.TimeHeld = 0f; KeysPressedInternal.Add(activeKey); KeysDownInternal[activeKeyIndex] = activeKey; } else if (activeKey.PostInitialDelay && activeKey.TimeHeld >= RepeatDelay) { activeKey.TimeHeld = 0f; KeysPressedInternal.Add(activeKey); KeysDownInternal[activeKeyIndex] = activeKey; } } }
public override void Update(GameTime gameTime) { if (isMyTurn && !isMovingPiece && curGame.IsActive) { // 获取键盘状态 lastKeyboardState = currentKeyboardState; currentKeyboardState = Keyboard.GetState(Keys.LeftControl, Keys.RightControl, Keys.Z); // 当按下 Ctrl+Z 时 if ((currentKeyboardState.IsKeyDown(Keys.LeftControl) || currentKeyboardState.IsKeyDown(Keys.RightControl)) && currentKeyboardState.IsKeyDown(Keys.Z)) { isCtrlZPressed = true; } else if (currentKeyboardState.IsKeyUp(Keys.Z)) { if (isCtrlZPressed) { isCtrlZPressed = false; if (GameSettings.IsRegretEnabled) { RegretMovePiece(); } } } foreach (ReversiPiecePosition rpp in reversiGame.GetEnabledPositions()) { pieces[rpp.X, rpp.Y].CurrentDisplay = DisplayState.CanMove; } lastMouseState = currentMouseState; currentMouseState = Mouse.GetState(); ReversiPiecePosition tempPosition = Board.IsMouseInPiece(currentMouseState); // 如果移动鼠标 if (tempPosition != null && reversiGame.IspieceEnabled(tempPosition)) { pieces[tempPosition.X, tempPosition.Y].CurrentDisplay = DisplayState.WillMove; foreach (ReversiPiecePosition rpp in reversiGame.GetReversePositions(tempPosition)) { pieces[rpp.X, rpp.Y].CurrentDisplay = DisplayState.WillReverse; } } // 如果单击鼠标左键 if ((currentMouseState.LeftButton == ButtonState.Released) && (lastMouseState.LeftButton == ButtonState.Pressed)) { if (tempPosition != null && reversiGame.IspieceEnabled(tempPosition)) { isMovePieceCompleted = false; /*pieces[tempPosition.X, tempPosition.Y].PieceState = (PieceState)myPieceColor; * foreach (ReversiPiecePosition rpp in reversiGame.GetReversePositions(tempPosition)) * pieces[rpp.X, rpp.Y].PieceState = (PieceState)myPieceColor;*/ MovePiece(tempPosition); } } } if (isMovePieceCompleted && isMyTurn) { if (!reversiGame.IsGameOver && reversiGame.CurrentPiece == reversiGame.LastPiece) { // TODO: 添加无子可下的显示 } isMyTurn = false; isMovingPiece = false; } base.Update(gameTime); }