/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // get location of the mouse x = Mouse.GetState().X; y = Mouse.GetState().Y; // Allows the game to exit if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); // Allows the game to reset if (Keyboard.GetState().IsKeyDown(Keys.R)) { readyToPlay = false; win = false; this.gameTableus = new List<Tableu>(); ; this.gameFoundations = new List<Foundation>(); ; this.playerScore = 0; this.Initialize(); } // Allows the game to reset if (Keyboard.GetState().IsKeyDown(Keys.H)) { help = true; } else { help = false; } Boolean draggedToTableu = false; Boolean draggedToFoundation = false; //Right clicking will check if selected card can fit on a tableu, and if so, will move to legal tableu /* if (!dragging && (Mouse.GetState().RightButton == ButtonState.Pressed)) * */ // If not dragging and mouse left button is pressed, check if mouse is within card bounds. if (!dragging && (Mouse.GetState().LeftButton == ButtonState.Pressed)) { tempTableu = null; temp = null; // Find original tableu for (int i = 0; i < gameTableus.Count; i++) { if(gameTableus.ElementAt(i).contains(new Vector2(x,y))) { tempTableu = gameTableus.ElementAt(i); } } // If tableu is not empty then get the top card if (tempTableu != null) temp = tempTableu.getTopCard(); // If card exist under mouse pointer pick up the card if (temp!=null && tempTableu != null) { dragging = true; } } // Stop dragging if button is released. if (dragging && Mouse.GetState().LeftButton == ButtonState.Released) { dragging = false; // Moving card to Tableus for (int i = 0; i < gameTableus.Count; i++) { if (gameTableus.ElementAt(i).contains(new Vector2(x, y))) { // check if move is valid // topCard is not null (tableu is not empty) // dragged card is 1 less than topCard if (gameTableus.ElementAt(i).getTopCard() != null && gameTableus.ElementAt(i).getTopCard().is1LessThan(temp)) { // moved to a new Tableu draggedToTableu = true; tempTableu.removeCard(temp); gameTableus.ElementAt(i).addCardToTableu(temp); } } } // Moving card to Foundation for (int i = 0; i < gameFoundations.Count; i++) { if (gameFoundations.ElementAt(i).contains(new Vector2(x, y))) { // If the foundation is empty and the card an Ace, allow it to be place on an empty foundation. if (gameFoundations.ElementAt(i).isEmpty() && temp.isAce()) { // moved to a new Foundation gameFoundations.ElementAt(i).addCardToFoundation(temp); tempTableu.removeCard(temp); draggedToFoundation = true; playerScore += 10; } // else if the suit matched the foundation and it is one more than previous, allow it to be placed. else if (gameFoundations.ElementAt(i).getTopCard().suitMatches(temp) && gameFoundations.ElementAt(i).getTopCard().is1MoreThan(temp)) { // moved to a new Foundation gameFoundations.ElementAt(i).addCardToFoundation(temp); tempTableu.removeCard(temp); draggedToFoundation = true; playerScore += 10; } } } // Return card to original location. if (draggedToTableu == false && draggedToFoundation == false) { tempTableu.addCardToTableu(temp); tempTableu.removeCard(temp); } } DragCard(); // remove all empty tableus RemoveEmptyTableus(); int count = 0; //Check for Win for (int i = 0; i < gameFoundations.Count; i++) { if (gameFoundations.ElementAt(i).isFull()) { count++; } } if (count == 4) win = true; // Debug: Where is the mouse, sprite, and vector at. //try // { // System.Diagnostics.Debug.Print("Update: Mouse at: " + x + ", " + y + ". Card at: " + temp.getSprite().Bounds + ". Vector at: " + temp.getVector()); // } // catch (Exception) // { // System.Diagnostics.Debug.Print("Update: Mouse at: " + x + ", " + y + ". Card is Null"); // } base.Update(gameTime); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // get location of the mouse x = Mouse.GetState().X; y = Mouse.GetState().Y; // Allows the game to exit if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // Allows the game to reset if (Keyboard.GetState().IsKeyDown(Keys.R)) { readyToPlay = false; win = false; this.gameTableus = new List <Tableu>();; this.gameFoundations = new List <Foundation>();; this.playerScore = 0; this.Initialize(); } // Allows the game to reset if (Keyboard.GetState().IsKeyDown(Keys.H)) { help = true; } else { help = false; } Boolean draggedToTableu = false; Boolean draggedToFoundation = false; //Right clicking will check if selected card can fit on a tableu, and if so, will move to legal tableu /* if (!dragging && (Mouse.GetState().RightButton == ButtonState.Pressed)) * * */ // If not dragging and mouse left button is pressed, check if mouse is within card bounds. if (!dragging && (Mouse.GetState().LeftButton == ButtonState.Pressed)) { tempTableu = null; temp = null; // Find original tableu for (int i = 0; i < gameTableus.Count; i++) { if (gameTableus.ElementAt(i).contains(new Vector2(x, y))) { tempTableu = gameTableus.ElementAt(i); } } // If tableu is not empty then get the top card if (tempTableu != null) { temp = tempTableu.getTopCard(); } // If card exist under mouse pointer pick up the card if (temp != null && tempTableu != null) { dragging = true; } } // Stop dragging if button is released. if (dragging && Mouse.GetState().LeftButton == ButtonState.Released) { dragging = false; // Moving card to Tableus for (int i = 0; i < gameTableus.Count; i++) { if (gameTableus.ElementAt(i).contains(new Vector2(x, y))) { // check if move is valid // topCard is not null (tableu is not empty) // dragged card is 1 less than topCard if (gameTableus.ElementAt(i).getTopCard() != null && gameTableus.ElementAt(i).getTopCard().is1LessThan(temp)) { // moved to a new Tableu draggedToTableu = true; tempTableu.removeCard(temp); gameTableus.ElementAt(i).addCardToTableu(temp); } } } // Moving card to Foundation for (int i = 0; i < gameFoundations.Count; i++) { if (gameFoundations.ElementAt(i).contains(new Vector2(x, y))) { // If the foundation is empty and the card an Ace, allow it to be place on an empty foundation. if (gameFoundations.ElementAt(i).isEmpty() && temp.isAce()) { // moved to a new Foundation gameFoundations.ElementAt(i).addCardToFoundation(temp); tempTableu.removeCard(temp); draggedToFoundation = true; playerScore += 10; } // else if the suit matched the foundation and it is one more than previous, allow it to be placed. else if (gameFoundations.ElementAt(i).getTopCard().suitMatches(temp) && gameFoundations.ElementAt(i).getTopCard().is1MoreThan(temp)) { // moved to a new Foundation gameFoundations.ElementAt(i).addCardToFoundation(temp); tempTableu.removeCard(temp); draggedToFoundation = true; playerScore += 10; } } } // Return card to original location. if (draggedToTableu == false && draggedToFoundation == false) { tempTableu.addCardToTableu(temp); tempTableu.removeCard(temp); } } DragCard(); // remove all empty tableus RemoveEmptyTableus(); int count = 0; //Check for Win for (int i = 0; i < gameFoundations.Count; i++) { if (gameFoundations.ElementAt(i).isFull()) { count++; } } if (count == 4) { win = true; } // Debug: Where is the mouse, sprite, and vector at. //try // { // System.Diagnostics.Debug.Print("Update: Mouse at: " + x + ", " + y + ". Card at: " + temp.getSprite().Bounds + ". Vector at: " + temp.getVector()); // } // catch (Exception) // { // System.Diagnostics.Debug.Print("Update: Mouse at: " + x + ", " + y + ". Card is Null"); // } base.Update(gameTime); }