TutorialGameBoard.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TutorialGameBoard.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. #endregion
  13. namespace Marblets
  14. {
  15. /// <summary>
  16. /// An override on the gameboard class that allows us to add different modes to the
  17. /// game play This is a very simple example for a tutorial. It just adds new marbles
  18. /// every few seconds.
  19. /// </summary>
  20. class TutorialGameBoard : GameBoard
  21. {
  22. private double nextDropTime;
  23. private const double dropInterval = 5.0;
  24. /// <summary>
  25. /// Create a new gameboard
  26. /// </summary>
  27. /// <param name="game"></param>
  28. public TutorialGameBoard(Game game)
  29. : base(game)
  30. {
  31. }
  32. /// <summary>
  33. /// Updates the game for the tutorial. Calls the normal gameboard update and
  34. /// then checks the clock to see if its time to add some missing marbles
  35. /// </summary>
  36. /// <param name="gameTime"></param>
  37. public override void Update(GameTime gameTime)
  38. {
  39. if(gameTime == null)
  40. {
  41. throw new ArgumentNullException("gameTime");
  42. }
  43. //Ensure base class is updated
  44. base.Update(gameTime);
  45. //If its time to add marbles then add a row
  46. if(gameTime.TotalGameTime.TotalSeconds > nextDropTime)
  47. {
  48. AttemptDrop();
  49. nextDropTime = gameTime.TotalGameTime.TotalSeconds + dropInterval;
  50. }
  51. }
  52. private void AttemptDrop()
  53. {
  54. //Drop a marble into every column that isn't full
  55. bool createdMarble = false;
  56. for(int x = 0; x < Width; x++)
  57. {
  58. for(int y = Height-1; y >=0; y--)
  59. {
  60. if(Marbles[x, y] == null)
  61. {
  62. Marbles[x, y] = new Marble();
  63. Marbles[x, y].Position = BoardToScreen(x, y);
  64. createdMarble = true;
  65. break;
  66. }
  67. }
  68. }
  69. if(createdMarble)
  70. {
  71. Sound.Play(SoundEntry.LandMarbles);
  72. }
  73. }
  74. }
  75. }