/// <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> /// Initialize a new human player. /// </summary> /// <param name="name">The name of the player.</param> /// <param name="diceHandler">The <see cref="DiceHandler"/> that handles the player's dice.</param> /// <param name="gameType">The type of game the player is participating in.</param> /// <param name="input">The <see cref="InputState"/> to check for touch input.</param> /// <param name="rollButtonTexture">Texture for the roll button.</param> /// <param name="scoreButtonTexture">Texture for the score button.</param> /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> depicting the display.</param> public HumanPlayer(string name, DiceHandler diceHandler, GameTypes gameType, InputState input, Rectangle screenBounds) : base(name, diceHandler) { this.input = input; this.gameType = gameType; this.screenBounds = screenBounds; }
/// <summary> /// Handle the "Roll" button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void roll_Click(object sender, EventArgs e) { DiceHandler.Roll(); if (gameType == GameTypes.Online) { NetworkManager.Instance.ResetTimeout(); } }
/// <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> /// Handle the human player's input. /// </summary> public override void PerformPlayerLogic() { // Enable or disable buttons roll.Enabled = DiceHandler.Rolls != 3 && !DiceHandler.DiceRolling(); score.Enabled = GameStateHandler != null && GameStateHandler.IsScoreSelect; for (int i = 0; i < input.Gestures.Count; i++) { roll.HandleInput(input.Gestures[i]); score.HandleInput(input.Gestures[i]); HandleDiceHandlerInput(input.Gestures[i]); HandleSelectScoreInput(input.Gestures[i]); } HandleShakeInput(); }
/// <summary> /// Initialize a new game handler component. /// </summary> /// <param name="diceHandler">The dice handler to use for managing the players' dice.</param> /// <param name="input">The <see cref="InputState"/> to check for touch input.</param> /// <param name="name">The name of the human player.</param> /// <param name="state">The game state to manage.</param> /// <param name="screenBounds">The screen's bounds.</param> /// <param name="contentManager">Content manager to use when initializing the player display.</param> public GameStateHandler(DiceHandler diceHandler, InputState input, string name, GameState state, Rectangle screenBounds, ContentManager contentManager) { // Initialize members this.diceHandler = diceHandler; this.input = input; this.type = state == null ? GameTypes.Offline : state.GameType; this.screenBounds = screenBounds; this.contentManager = contentManager; if (state == null) { State = new GameState(); LoadNewOfflinePlayers(name); } else { State = state; players = new YachtPlayer[State.Players.Count]; HumanPlayer humanPlayer = new HumanPlayer(State.Players[0].Name, diceHandler, State.GameType, input, screenBounds); humanPlayer.LoadAssets(contentManager); humanPlayer.GameStateHandler = this; players[0] = humanPlayer; if (type == GameTypes.Offline) { InitializeOfflinePlayers(); } else { IsWaitingForPlayer = !State.IsStarted; InitializeOnlinePlayers(); } Initialize(false); } }
/// <summary> /// Check if the phone was shaken and if so roll the dice. /// </summary> private void HandleShakeInput() { // Register for shake detection if (!registeredForShakeDetection) { Accelerometer.ShakeDetected += Accelerometer_ShakeDetected; registeredForShakeDetection = true; } if (shakeDetect) { DiceHandler.Roll(); if (gameType == GameTypes.Online) { NetworkManager.Instance.ResetTimeout(); } shakeDetect = false; } }
/// <summary> /// Initializes the dice handler used to handle the player's dice, and loads resources used for displaying /// it. /// </summary> /// <param name="diceState">State to initialize the dice handler according to, or null to use the default /// initial state.</param> /// <param name="gameType">The game type for which the dice are initialized.</param> private void InitializeDiceHandler(DiceState diceState, GameTypes gameType) { diceHandler = new DiceHandler(ScreenManager.Game.GraphicsDevice, diceState); diceHandler.LoadAssets(ScreenManager.Game.Content); }
/// <summary> /// Initialize a new AI player. /// </summary> /// <param name="name">The player's name.</param> /// <param name="diceHandler">The <see cref="DiceHandler"/> that the player will use.</param> public AIPlayer(string name, DiceHandler diceHandler) : base(name, diceHandler) { }
/// <summary> /// Initialize a new instance. /// </summary> /// <param name="name">The name of the player.</param> /// <param name="diceHandler">The <see cref="DiceHandler"/> that the player will use.</param> public YachtPlayer(string name, DiceHandler diceHandler) { Name = name; DiceHandler = diceHandler; }