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.

Tetromino.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. export default class Tetromino {
  2. static types = {
  3. 'I': 0,
  4. 'O': 1,
  5. 'T': 2,
  6. 'S': 3,
  7. 'Z': 4,
  8. 'J': 5,
  9. 'L': 6,
  10. };
  11. static shapes = {
  12. 0: [
  13. [1, 1, 1, 1]
  14. ],
  15. 1: [
  16. [1, 1],
  17. [1, 1]
  18. ],
  19. 2: [
  20. [0, 1, 0],
  21. [1, 1, 1],
  22. ],
  23. 3: [
  24. [0, 1, 1],
  25. [1, 1, 0],
  26. ],
  27. 4: [
  28. [1, 1, 0],
  29. [0, 1, 1],
  30. ],
  31. 5: [
  32. [1, 0, 0],
  33. [1, 1, 1],
  34. ],
  35. 6: [
  36. [0, 0, 1],
  37. [1, 1, 1],
  38. ],
  39. };
  40. static colors = {
  41. 0: '#00f8ff',
  42. 1: '#ffe200',
  43. 2: '#b817ff',
  44. 3: '#0cff34',
  45. 4: '#ff000b',
  46. 5: '#1000ff',
  47. 6: '#ff9400',
  48. }
  49. currentType: Tetromino.types;
  50. position: Object;
  51. constructor(type: Tetromino.types) {
  52. this.currentType = type;
  53. this.position = {x: 0, y: 0};
  54. }
  55. getColor() {
  56. return Tetromino.colors[this.currentType];
  57. }
  58. getCellsCoordinates() {
  59. let coordinates = [];
  60. for (let row = 0; row < Tetromino.shapes[this.currentType].length; row++) {
  61. for (let col = 0; col < Tetromino.shapes[this.currentType][row].length; col++) {
  62. if (Tetromino.shapes[this.currentType][row][col] === 1)
  63. coordinates.push({x: this.position.x + col, y: this.position.y + row});
  64. }
  65. }
  66. return coordinates;
  67. }
  68. move(x: number, y: number) {
  69. this.position.x += x;
  70. this.position.y += y;
  71. }
  72. }