GamePiece.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework;
  7. namespace Flood_Control
  8. {
  9. class GamePiece
  10. {
  11. public static string[] PieceTypes =
  12. {
  13. "Left,Right",
  14. "Top,Bottom",
  15. "Left,Top",
  16. "Top,Right",
  17. "Right,Bottom",
  18. "Bottom,Left",
  19. "Empty"
  20. };
  21. public const int PieceHeight = 40;
  22. public const int PieceWidth = 40;
  23. public const int MaxPlayablePieceIndex = 5;
  24. public const int EmptyPieceIndex = 6;
  25. private const int textureOffsetX = 1;
  26. private const int textureOffsetY = 1;
  27. private const int texturePaddingX = 1;
  28. private const int texturePaddingY = 1;
  29. private string pieceType = "";
  30. private string pieceSuffix = "";
  31. public string PieceType
  32. {
  33. get { return pieceType; }
  34. }
  35. public string Suffix
  36. {
  37. get { return pieceSuffix; }
  38. }
  39. public GamePiece(string type, string suffix)
  40. {
  41. pieceType = type;
  42. pieceSuffix = suffix;
  43. }
  44. public GamePiece(string type)
  45. {
  46. pieceType = type;
  47. pieceSuffix = "";
  48. }
  49. public void SetPiece(string type, string suffix)
  50. {
  51. pieceType = type;
  52. pieceSuffix = suffix;
  53. }
  54. public void SetPiece(string type)
  55. {
  56. SetPiece(type, "");
  57. }
  58. public void AddSuffix(string suffix)
  59. {
  60. if (!pieceSuffix.Contains(suffix))
  61. pieceSuffix += suffix;
  62. }
  63. public void RemoveSuffix(string suffix)
  64. {
  65. pieceSuffix = pieceSuffix.Replace(suffix, "");
  66. }
  67. public void RotatePiece(bool Clockwise)
  68. {
  69. switch (pieceType)
  70. {
  71. case "Left,Right":
  72. pieceType = "Top,Bottom";
  73. break;
  74. case "Top,Bottom":
  75. pieceType = "Left,Right";
  76. break;
  77. case "Left,Top":
  78. if (Clockwise)
  79. pieceType = "Top,Right";
  80. else
  81. pieceType = "Bottom,Left";
  82. break;
  83. case "Top,Right":
  84. if (Clockwise)
  85. pieceType = "Right,Bottom";
  86. else
  87. pieceType = "Left,Top";
  88. break;
  89. case "Right,Bottom":
  90. if (Clockwise)
  91. pieceType = "Bottom,Left";
  92. else
  93. pieceType = "Top,Right";
  94. break;
  95. case "Bottom,Left":
  96. if (Clockwise)
  97. pieceType = "Left,Top";
  98. else
  99. pieceType = "Right,Bottom";
  100. break;
  101. case "Empty":
  102. break;
  103. }
  104. }
  105. public string[] GetOtherEnds(string startingEnd)
  106. {
  107. List<string> opposites = new List<string>();
  108. foreach (string end in pieceType.Split(','))
  109. {
  110. if (end != startingEnd)
  111. opposites.Add(end);
  112. }
  113. return opposites.ToArray();
  114. }
  115. public bool HasConnector(string direction)
  116. {
  117. return pieceType.Contains(direction);
  118. }
  119. public Rectangle GetSourceRect()
  120. {
  121. int x = textureOffsetX;
  122. int y = textureOffsetY;
  123. if (pieceSuffix.Contains("W"))
  124. x += PieceWidth + texturePaddingX;
  125. y += (Array.IndexOf(PieceTypes, pieceType) *
  126. (PieceHeight + texturePaddingY));
  127. return new Rectangle(x, y, PieceWidth, PieceHeight);
  128. }
  129. }
  130. }