/// <summary> /// Move dice that are tapped. /// </summary> /// <param name="sample">Input gesture performed.</param> private void HandleDiceHandlerInput(GestureSample sample) { if (DiceHandler.Rolls < 3) { Dice[] rollingDice = DiceHandler.GetRollingDice(); Dice[] holdingDice = DiceHandler.GetHoldingDice(); if (sample.GestureType == GestureType.Tap) { // Create the touch rectangle Rectangle touchRect = new Rectangle((int)sample.Position.X - 5, (int)sample.Position.Y - 5, 10, 10); for (int i = 0; i < DiceHandler.DiceAmount; i++) { // Check for intersection between the touch rectangle and any of the dice if ((rollingDice != null && rollingDice[i] != null && !rollingDice[i].IsRolling && rollingDice[i].Intersects(touchRect)) || (holdingDice != null && holdingDice[i] != null && holdingDice[i].Intersects(touchRect))) { DiceHandler.MoveDice(i); if (DiceHandler.GetHoldingDice() == null) { GameStateHandler.SelectScore(null); } } } } } }
/// <summary> /// Draw the score currently selected by the player. /// </summary> /// <param name="spriteBatch">The <see cref="SpriteBatch"/> to use when drawing.</param> private void DrawSelectedScore(SpriteBatch spriteBatch) { if (GameStateHandler != null && GameStateHandler.IsScoreSelect) { Dice[] holdingDice = DiceHandler.GetHoldingDice(); if (holdingDice != null) { // Calculate the score and the position byte selectedScore = GameStateHandler.CombinationScore(GameStateHandler.SelectedScore.Value, holdingDice); string text = GameStateHandler.ScoreTypesNames[(int)GameStateHandler.SelectedScore.Value - 1].ToUpper(); Vector2 position = new Vector2(score.Position.X, roll.Position.Y); position.Y += roll.Texture.Height + 10; Vector2 measure = YachtGame.Font.MeasureString(text); position.X += score.Texture.Bounds.Center.X - measure.X / 2; spriteBatch.DrawString(YachtGame.Font, text, position, Color.White); text = selectedScore.ToString(); position.Y += measure.Y; measure = YachtGame.Font.MeasureString(text); position.X = score.Position.X; position.X += score.Texture.Bounds.Center.X - measure.X / 2; spriteBatch.DrawString(YachtGame.Font, text, position, Color.White); } } }
/// <summary> /// Handle the "Score" button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void score_Click(object sender, EventArgs e) { if (GameStateHandler != null && GameStateHandler.IsScoreSelect) { GameStateHandler.FinishTurn(); AudioManager.PlaySoundRandom("Pencil", 3); DiceHandler.Reset(GameStateHandler.IsGameOver); } }
/// <summary> /// Performs the AI player's game logic, based on its current state. /// </summary> public override void PerformPlayerLogic() { // State machine for handling the AI player behavior. switch (State) { case AIState.Roll: // Roll the dice DiceHandler.Roll(); State = AIState.Rolling; break; case AIState.Rolling: // Wait for the dice to stop rolling if (!DiceHandler.DiceRolling()) { State = AIState.ChooseDice; } break; case AIState.ChooseDice: // Hold one of the dice DiceHandler.MoveDice(random.Next(0, 5)); // Randomly move on to selecting the score, or hold another die if (DiceHandler.GetHoldingDice() != null && random.Next(0, 5) == 1) { State = AIState.SelectScore; } break; case AIState.SelectScore: // Select an unused score line if (GameStateHandler.SelectScore((YachtCombination)random.Next(1, 13))) { State = AIState.WriteScore; } break; case AIState.WriteScore: // If a valid score was selected then write the score if (GameStateHandler != null && GameStateHandler.IsScoreSelect) { GameStateHandler.FinishTurn(); DiceHandler.Reset(GameStateHandler.IsGameOver); State = AIState.Roll; } break; default: break; } }
/// <summary> /// Highlight the score card line that was tapped. /// </summary> /// <param name="sample">Input gesture performed.</param> private void HandleSelectScoreInput(GestureSample sample) { if (sample.GestureType == GestureType.Tap) { // Create the touch rectangle Rectangle touchRect = new Rectangle((int)sample.Position.X - 5, (int)sample.Position.Y - 5, 10, 10); for (int i = 0; i < 12; i++) { if (GameStateHandler.IntersectLine(touchRect, i)) { GameStateHandler.SelectScore((YachtCombination)(i + 1)); } } } }
/// <summary> /// Initializes the game state handler used to manage the game state, and loads resources used for displaying /// it. /// </summary> /// <param name="state">State to initialize the game handler according to, or null to use the default /// initial state.</param> private void InitializeGameStateHandler(GameState state) { gameStateHandler = new GameStateHandler(diceHandler, ScreenManager.input, name, state, ScreenManager.Game.GraphicsDevice.Viewport.Bounds, ScreenManager.Game.Content); }