Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Piece.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import ShapeL from "./Shapes/ShapeL";
  2. import ShapeI from "./Shapes/ShapeI";
  3. import ShapeJ from "./Shapes/ShapeJ";
  4. import ShapeO from "./Shapes/ShapeO";
  5. import ShapeS from "./Shapes/ShapeS";
  6. import ShapeT from "./Shapes/ShapeT";
  7. import ShapeZ from "./Shapes/ShapeZ";
  8. import type {coordinates} from './Shapes/BaseShape';
  9. import type {grid} from './GridManager';
  10. /**
  11. * Class used as an abstraction layer for shapes.
  12. * Use this class to manipulate pieces rather than Shapes directly
  13. *
  14. */
  15. export default class Piece {
  16. #shapes = [
  17. ShapeL,
  18. ShapeI,
  19. ShapeJ,
  20. ShapeO,
  21. ShapeS,
  22. ShapeT,
  23. ShapeZ,
  24. ];
  25. #currentShape: Object;
  26. #colors: Object;
  27. /**
  28. * Initializes this piece's color and shape
  29. *
  30. * @param colors Object containing current theme colors
  31. */
  32. constructor(colors: Object) {
  33. this.#currentShape = this.getRandomShape(colors);
  34. this.#colors = colors;
  35. }
  36. /**
  37. * Gets a random shape object
  38. *
  39. * @param colors Object containing current theme colors
  40. */
  41. getRandomShape(colors: Object) {
  42. return new this.#shapes[Math.floor(Math.random() * 7)](colors);
  43. }
  44. /**
  45. * Removes the piece from the given grid
  46. *
  47. * @param grid The grid to remove the piece from
  48. */
  49. removeFromGrid(grid: grid) {
  50. const coord: Array<coordinates> = this.#currentShape.getCellsCoordinates(true);
  51. for (let i = 0; i < coord.length; i++) {
  52. grid[coord[i].y][coord[i].x] = {
  53. color: this.#colors.tetrisBackground,
  54. isEmpty: true,
  55. key: grid[coord[i].y][coord[i].x].key
  56. };
  57. }
  58. }
  59. /**
  60. * Adds this piece to the given grid
  61. *
  62. * @param grid The grid to add the piece to
  63. * @param isPreview Should we use this piece's current position to determine the cells?
  64. */
  65. toGrid(grid: grid, isPreview: boolean) {
  66. const coord: Array<coordinates> = this.#currentShape.getCellsCoordinates(!isPreview);
  67. for (let i = 0; i < coord.length; i++) {
  68. grid[coord[i].y][coord[i].x] = {
  69. color: this.#currentShape.getColor(),
  70. isEmpty: false,
  71. key: grid[coord[i].y][coord[i].x].key
  72. };
  73. }
  74. }
  75. /**
  76. * Checks if the piece's current position is valid
  77. *
  78. * @param grid The current game grid
  79. * @param width The grid's width
  80. * @param height The grid's height
  81. * @return {boolean} If the position is valid
  82. */
  83. isPositionValid(grid: grid, width: number, height: number) {
  84. let isValid = true;
  85. const coord: Array<coordinates> = this.#currentShape.getCellsCoordinates(true);
  86. for (let i = 0; i < coord.length; i++) {
  87. if (coord[i].x >= width
  88. || coord[i].x < 0
  89. || coord[i].y >= height
  90. || coord[i].y < 0
  91. || !grid[coord[i].y][coord[i].x].isEmpty) {
  92. isValid = false;
  93. break;
  94. }
  95. }
  96. return isValid;
  97. }
  98. /**
  99. * Tries to move the piece by the given offset on the given grid
  100. *
  101. * @param x Position X offset
  102. * @param y Position Y offset
  103. * @param grid The grid to move the piece on
  104. * @param width The grid's width
  105. * @param height The grid's height
  106. * @param freezeCallback Callback to use if the piece should freeze itself
  107. * @return {boolean} True if the move was valid, false otherwise
  108. */
  109. tryMove(x: number, y: number, grid: grid, width: number, height: number, freezeCallback: Function) {
  110. if (x > 1) x = 1; // Prevent moving from more than one tile
  111. if (x < -1) x = -1;
  112. if (y > 1) y = 1;
  113. if (y < -1) y = -1;
  114. if (x !== 0 && y !== 0) y = 0; // Prevent diagonal movement
  115. this.removeFromGrid(grid);
  116. this.#currentShape.move(x, y);
  117. let isValid = this.isPositionValid(grid, width, height);
  118. if (!isValid)
  119. this.#currentShape.move(-x, -y);
  120. let shouldFreeze = !isValid && y !== 0;
  121. this.toGrid(grid, false);
  122. if (shouldFreeze)
  123. freezeCallback();
  124. return isValid;
  125. }
  126. /**
  127. * Tries to rotate the piece
  128. *
  129. * @param grid The grid to rotate the piece on
  130. * @param width The grid's width
  131. * @param height The grid's height
  132. * @return {boolean} True if the rotation was valid, false otherwise
  133. */
  134. tryRotate(grid: grid, width: number, height: number) {
  135. this.removeFromGrid(grid);
  136. this.#currentShape.rotate(true);
  137. if (!this.isPositionValid(grid, width, height)) {
  138. this.#currentShape.rotate(false);
  139. this.toGrid(grid, false);
  140. return false;
  141. }
  142. this.toGrid(grid, false);
  143. return true;
  144. }
  145. /**
  146. * Gets this piece used cells coordinates
  147. *
  148. * @return {Array<coordinates>} An array of coordinates
  149. */
  150. getCoordinates(): Array<coordinates> {
  151. return this.#currentShape.getCellsCoordinates(true);
  152. }
  153. }