MoveList.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MoveList.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 System.Collections.Generic;
  12. using System.Linq;
  13. #endregion
  14. namespace InputSequenceSample
  15. {
  16. /// <summary>
  17. /// Represents a set of available moves for matching. This internal storage of this
  18. /// class is optimized for efficient match searches.
  19. /// </summary>
  20. class MoveList
  21. {
  22. private Move[] moves;
  23. public MoveList(IEnumerable<Move> moves)
  24. {
  25. // Store the list of moves in order of decreasing sequence length.
  26. // This greatly simplifies the logic of the DetectMove method.
  27. this.moves = moves.OrderByDescending(m => m.Sequence.Length).ToArray();
  28. }
  29. /// <summary>
  30. /// Finds the longest Move which matches the given input, if any.
  31. /// </summary>
  32. public Move DetectMove(InputManager input)
  33. {
  34. // Perform a linear search for a move which matches the input. This relies
  35. // on the moves array being in order of decreasing sequence length.
  36. foreach (Move move in moves)
  37. {
  38. if (input.Matches(move))
  39. {
  40. return move;
  41. }
  42. }
  43. return null;
  44. }
  45. public int LongestMoveLength
  46. {
  47. get
  48. {
  49. // Since they are in decreasing order,
  50. // the first move is the longest.
  51. return moves[0].Sequence.Length;
  52. }
  53. }
  54. }
  55. }