markVISTa

jsjsjs

Aug 17th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.56 KB | Source Code | 0 0
  1. import { Tabla } from "../tabla.js";
  2.  
  3. export class Piesa {
  4.     constructor(color, row, col, tabla) {
  5.         this.color = color;
  6.         this.row = row;
  7.         this.col = col;
  8.         this.prerow = row;
  9.         this.precol = col;
  10.         this.miscata = false;
  11.         this.lovestePiese = null;
  12.         this.tabla = tabla;
  13.         this.x = this.getX(col);
  14.         this.y = this.getY(row);
  15.     }
  16.  
  17.  
  18.  
  19.     desen(ctx, imagine) {
  20.         if (!imagine) return;
  21.  
  22.         let xPixeli, yPixeli;
  23.  
  24.         if (this.isDragging && this.dragX !== undefined && this.dragY !== undefined) {
  25.             xPixeli = this.dragX;
  26.             yPixeli = this.dragY;
  27.         } else {
  28.             xPixeli = this.col*Tabla.squareSize;
  29.             yPixeli = this.row*Tabla.squareSize;
  30.         }
  31.  
  32.         if (imagine.complete) {
  33.             ctx.drawImage(imagine, xPixeli, yPixeli, Tabla.squareSize, Tabla.squareSize);
  34.         } else {
  35.             imagine.onload = () => {
  36.                 ctx.drawImage(imagine, xPixeli, yPixeli, Tabla.squareSize, Tabla.squareSize);
  37.             };
  38.         }
  39.     }
  40.  
  41.  
  42.     getCol(x) {
  43.         return Math.floor(x / Tabla.squareSize);
  44.     }
  45.  
  46.     getRow(y) {
  47.         return Math.floor(y / Tabla.squareSize);
  48.     }
  49.  
  50.     getX(col) {
  51.         return col*Tabla.squareSize;
  52.     }
  53.  
  54.     getY(row) {
  55.         return row * Tabla.squareSize;
  56.     }
  57.  
  58.  
  59.     getLovesteP(targetCol, targetRow)
  60.     {
  61.         for(const piesa of this.tabla.piese)
  62.         {
  63.             if(piesa.col === targetCol && piesa.row === targetRow && piesa !== this)
  64.                 return piesa;
  65.         }
  66.         return null;
  67.     }
  68.  
  69.  
  70.     patratValid(targetCol, targetRow)
  71.     {
  72.         this.lovestePiese = this.getLovesteP(targetCol, targetRow);
  73.         if(this.lovestePiese === null)
  74.             return true;
  75.         if(this.lovestePiese.color !== this.color)
  76.             return true;
  77.         this.lovestePiese = null;
  78.         return false;
  79.     }
  80.  
  81.     peTabla(targetCol, targetRow)
  82.     {
  83.         if(targetRow >= 0 && targetRow <= 7 && targetCol >=0 && targetCol <= 7)
  84.             return true;
  85.         return false;
  86.     }
  87.  
  88.     acelasiPatrat(targetCol, targetRow) {
  89.         return targetCol === this.precol && targetRow === this.prerow;
  90.     }
  91.  
  92.  
  93.     resetPozitie()
  94.     {
  95.         this.col = this.precol;
  96.         this.row = this.prerow;
  97.         this.x = this.getX(this.col);
  98.         this.y = this.getY(this.row)
  99.     }
  100.  
  101.     updatePozitie() {
  102.         this.x = this.col * Tabla.squareSize;
  103.         this.y = this.row * Tabla.squareSize;
  104.  
  105.         this.precol = this.col;
  106.         this.prerow = this.row;
  107.         this.miscata = true;
  108.  
  109.         this.dragX = undefined;
  110.         this.dragY = undefined;
  111.     }
  112.  
  113.     piesaInFata(targetCol, targetRow) {
  114.         if (this.prerow === targetRow) {
  115.             const start = Math.min(this.precol, targetCol) + 1;
  116.             const end = Math.max(this.precol, targetCol);
  117.  
  118.             for (let col = start; col < end; col++) {
  119.                 for (const piesa of this.tabla.piese) {
  120.                     if (piesa.col === col && piesa.row === targetRow) {
  121.                         this.lovestePiese = piesa;
  122.                         return true;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.  
  128.         if (this.precol === targetCol) {
  129.             const start = Math.min(this.prerow, targetRow) + 1;
  130.             const end = Math.max(this.prerow, targetRow);
  131.  
  132.             for (let row = start; row < end; row++) {
  133.                 for (const piesa of this.tabla.piese) {
  134.                     if (piesa.col === targetCol && piesa.row === row) {
  135.                         this.lovestePiese = piesa;
  136.                         return true;
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.         return false;
  143.     }
  144.  
  145.     piesePeDiagonala(targetCol, targetRow)
  146.     {
  147.         if(targetRow < this.prerow)
  148.         {
  149.             for(let i = this.precol-1; i > targetCol; i--)
  150.             {
  151.                 let dif = Math.abs(i - this.precol);
  152.                 for(const piesa of this.tabla.piese)
  153.                     if(piesa.col === i && piesa.row === this.prerow - dif){
  154.                         this.lovestePiese = piesa;
  155.                         return true;
  156.                     }
  157.             }
  158.  
  159.             for(let i = this.precol+1; i < targetCol; i++)
  160.             {
  161.                 let dif = Math.abs(i - this.precol);
  162.                 for(const piesa of this.tabla.piese)
  163.                     if(piesa.col === i && piesa.row === this.prerow - dif){
  164.                         this.lovestePiese = piesa;
  165.                         return true;
  166.                     }
  167.             }
  168.         }
  169.  
  170.         if(targetRow > this.prerow)
  171.         {
  172.             for(let i = this.precol-1; i > targetCol; i--)
  173.             {
  174.                 let dif = Math.abs(i-this.precol);
  175.                 for(const piesa of this.tabla.piese)
  176.                     if(piesa.col === i && piesa.row === this.prerow + dif){
  177.                         this.lovestePiese = piesa;
  178.                         return true;
  179.                     }
  180.             }
  181.  
  182.             for(let i = this.precol+1; i < targetCol; i++)
  183.             {
  184.                 let dif = Math.abs(i - this.precol);
  185.                 for(const piesa of this.tabla.piese)
  186.                     if(piesa.col === i && piesa.row === this.prerow + dif){
  187.                         this.lovestePiese = piesa;
  188.                         return true;
  189.                     }
  190.             }
  191.         }
  192.  
  193.         return false;
  194.     }
  195.  
  196.     mutariLegale(targetCol, targetRow) {
  197.         return false;
  198.     }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment