import { SpriteWidth, SpriteHeight, SpriteRatioX, SpriteRatioY } from "../Constants.js";
import { Wall } from "../SpriteConstants";

class Room extends Phaser.Group {

  constructor(game, parent, name) {
    super(game, parent, name, false, false, Phaser.Physics.ARCADE);
  }

  createLineByTile(x1, y1, nbTilesX, nbTilesY) {
    this.createLine(x1, y1, nbTilesX * SpriteWidth + x1 , nbTilesY * SpriteHeight + y1);
  }

  createLine(x1, y1, x2, y2) {
    const xBegin = Math.min(x1, x2);
    const xEnd = Math.max(x1, x2);
    const yBegin = Math.min(y1, y2);
    const yEnd = Math.max(y1, y2);

    const xStep = (x2-x1);
    const yStep = (y2-y1);

    if (xStep === 0) {
      //x1 === x2
      return this.createVerticalLine(x1, yBegin, yEnd);
    }

    if (yStep === 0) {
      //y1 === y2
      return this.createHorizontalLine(xBegin, xEnd, y1);
    }

    let x = xBegin;
    let y = yBegin;
    while(x != xEnd || y != yEnd) {
      if(x < xEnd) {
        x += SpriteWidth;
      }
      if(y < yEnd) {
        y += SpriteHeight;
      }
      this.createSprite(x,y);
    }

  }

  createVerticalLine(x, y1, y2) {
    for(let y = y1; y < y2; y += SpriteHeight) {
      this.createSprite(x, y);
    }
  }

  createHorizontalLine(x1, x2, y) {
    for(let x = x1; x < x2; x += SpriteWidth) {
      this.createSprite(x, y);
    }
  }

  createSprite(x,y) {
    let sprite = this.create(x, y, Wall);
    sprite.scale.setTo(SpriteRatioX, SpriteRatioY);
  }

  ////
  //       ####
  //       #  #
  //       #  #
  //       #  #
  // (x,y) ####
  //
  ///
  createSquare(x, y, nbTilesBySide) {
    this.createLineByTile(x, y, nbTilesBySide, 0);
    this.createLineByTile(x, y + (nbTilesBySide - 1) * SpriteHeight, nbTilesBySide, 0);

    this.createLineByTile(x, y + SpriteHeight, 0, nbTilesBySide - 2);
    this.createLineByTile(x + (nbTilesBySide - 1) * SpriteWidth, y + SpriteHeight, 0, nbTilesBySide - 2);
  }
}

export default Room;
@@ -282,48 +282,277 @@
 eate
-LineByTile(x1, y1, nbTilesX, nbTilesY) %7B
+RandomLine(x1, y1, x2, y2, division = 2) %7B%0A%0A  %7D%0A%0A  createLineByTile(x1, y1, nbTilesX, nbTilesY) %7B%0A    if(nbTilesX %3C 0 ) %7B%0A      throw %60createLineByTile $%7BnbTilesX%7D is negative%60;%0A    %7D%0A    if(nbTilesY %3C 0 ) %7B%0A      throw %60createLineByTile $%7BnbTilesY%7D is negative%60;%0A    %7D
 %0A   
@@ -576,24 +576,25 @@
 x1, y1, 
+(
 nbTilesX
  * Sprit
@@ -585,16 +585,19 @@
 nbTilesX
+-1)
  * Sprit
@@ -610,24 +610,25 @@
  + x1 , 
+(
 nbTilesY
  * Sprit
@@ -619,16 +619,19 @@
 nbTilesY
+-1)
  * Sprit
@@ -642,32 +642,59 @@
 ght + y1);%0A  %7D%0A%0A
+  // //Brasenhem algorithm%0A
   createLine(x1,
@@ -685,33 +685,32 @@
   createLine(x1,
- 
 y1, x2, y2) %7B%0A  
@@ -721,22 +721,18 @@
 nst 
+d
 x
-Begin
  = Math.
 min(
@@ -731,18 +731,19 @@
 ath.
-min(x1, x2
+abs(x2 - x1
 );%0A 
@@ -755,68 +755,49 @@
 nst 
-xEnd = Math.max(x1, x2);%0A    const yBegin = Math.min(y1, y2)
+sx = x1 %3C x2 ? SpriteWidth : -SpriteWidth
 ;%0A  
@@ -808,20 +808,18 @@
 nst 
-yEn
 d
+y
  = Math.
 max(
@@ -818,22 +818,22 @@
 ath.
-max(y1, y2
+abs(y2 - y1
 );%0A
-%0A
     
@@ -842,731 +842,379 @@
 nst 
-xStep = (x2-x1);%0A    const yStep = (y2-y1);%0A%0A    if (xStep === 0) %7B%0A      //x1 === x2%0A      return this.createVerticalLine(x1, yBegin, yEnd);%0A    %7D%0A%0A    if (yStep === 0) %7B%0A      //y1 === y2%0A      return this.createHorizontalLine(xBegin, xEnd, y1);%0A    %7D%0A%0A    let x = xBegin;%0A    let y = yBegin;%0A    while(x != xEnd %7C%7C y != yEnd) %7B%0A      if(x %3C xEnd) %7B%0A        x += SpriteWidth;%0A      %7D%0A      if(y %3C yEnd) %7B%0A        y += SpriteHeight;%0A      %7D%0A      this.createSprite(x,y)
+sy = y1 %3C y2 ? SpriteHeight : -SpriteHeight;%0A    var err = (dx %3E dy ? dx : -dy) / 2;%0A%0A    let x = x1;%0A    let y = y1;%0A%0A    while (true) %7B%0A      this.createSprite(x,y);%0A      debugger%0A      if (x %3E= x2 && y %3E= y2) break;%0A      let e2 = err;%0A      if (e2 %3E - dx) %7B%0A        err -= dy;%0A        x += sx
 ;%0A    
-%7D%0A%0A
   %7D%0A
-%0A
   
-createVerticalLine(x, y1, y2) %7B%0A    for(let y = y1; y %3C y2; y += SpriteHeight) %7B%0A      this.createSprite(x, y);%0A    %7D%0A  %7D%0A%0A  createHorizontalLine(x1, x2, y) %7B%0A    for(let x = x1; x %3C x2; x += SpriteWidth) %7B%0A      this.createSprite(x, y);
+    if (e2 %3C dy) %7B%0A        err += dx;%0A        y += sy;%0A      %7D
 %0A   

use an algo to improve tracing