import Shader from './Shader.js'

/**
* ScaledTriangle
*/
export default class ScaledTriangleShader extends Shader {
  
  constructor (name) {
    super(name || "Scaled Triangles")
    this.verticalSkip = 2
  }

  renderPixel (ctx, pixel, data, palette) {
    
    const blockWidth = data.blockDimension*2

    var triangleWidth = blockWidth

    var starter = triangleWidth/2

    var triangleHeight = triangleWidth

    var startingX = (pixel.x)*starter
    var startingY = pixel.y*triangleHeight

    triangleHeight *= Math.min(1-pixel.brightness, 0.75)
    triangleWidth = triangleHeight

    starter = triangleWidth / 2

    startingY += (blockWidth - starter*2)

    const evenRow = pixel.y % 2 === 0
    const evenCol = pixel.x % 2 === 0

    ctx.beginPath()

    if ((evenRow && evenCol) || (!evenRow && !evenCol) ) {
      ctx.moveTo(startingX, startingY)
      ctx.lineTo(startingX - starter, startingY+triangleHeight)
      ctx.lineTo(startingX + starter, startingY+triangleHeight)
      ctx.lineTo(startingX, startingY)
    } else {
      ctx.moveTo(startingX, startingY + triangleHeight)
      ctx.lineTo(startingX-starter, startingY)
      ctx.lineTo(startingX+starter, startingY)
      ctx.lineTo(startingX, startingY + triangleHeight)
    }

    ctx.closePath()

    this.fillPixelWithPalette(ctx, pixel, palette)

  }
}
@@ -253,16 +253,80 @@
 ) %7B%0A    
+// we have twice as much space as the triangles are interlocking
 %0A    con
@@ -364,335 +364,350 @@
 sion
-*
+ * 
 2%0A%0A    
-var triangleWidth = blockWidth%0A%0A    var starter = triangleWidth/2%0A%0A    var triangleHeight = triangleWidth%0A%0A    var startingX = (pixel.x)*starter%0A    var startingY = pixel.y*triangleHeight%0A%0A    triangleHeight *= Math.min(1-pixel.brightness, 0.75)%0A    triangleWidth = triangleHeight
+// scale down height based on brightness%0A    var triangleDimension = blockWidth * Math.max(Math.min(1-pixel.brightness, 0.75), 0)%0A%0A    // we start the triangle every dimension / 2 because they interlace %0A    var starter = triangleDimension / 2
 %0A%0A    
+var 
 start
-er = triangleWidth / 2%0A
+ingX = pixel.x * data.blockDimension%0A%0A    // center it vertically
 %0A    
+var 
 star
@@ -716,35 +716,67 @@
 ngY 
-+= (blockWidth - starter*2)
+= pixel.y * blockWidth + (blockWidth - triangleDimension)/2
 %0A%0A  
@@ -926,24 +926,114 @@
 venCol) ) %7B%0A
+      %0A      //  Point at the top%0A      //%0A      //       /%5C%0A      //      /__%5C%0A      //%0A%0A
       ctx.mo
@@ -1102,33 +1102,35 @@
 r, startingY
-+
+ + 
 triangle
 Height)%0A    
@@ -1109,38 +1109,41 @@
 tingY + triangle
-Height
+Dimension
 )%0A      ctx.line
@@ -1179,17 +1179,19 @@
 ingY
-+
+ + 
 triangle
 Heig
@@ -1178,38 +1178,41 @@
 tingY + triangle
-Height
+Dimension
 )%0A      ctx.line
@@ -1249,16 +1249,116 @@
  else %7B%0A
+      %0A      //  Point at the bottom%0A      //     ____%0A      //     %5C  /%0A      //      %5C/%0A      //%0A%0A
       ct
@@ -1393,30 +1393,33 @@
 Y + triangle
-Height
+Dimension
 )%0A      ctx.
@@ -1434,17 +1434,19 @@
 tartingX
--
+ - 
 starter,
@@ -1483,17 +1483,19 @@
 tartingX
-+
+ + 
 starter,
@@ -1558,14 +1558,17 @@
 ngle
-Height
+Dimension
 )%0A  
@@ -1644,14 +1644,14 @@
 alette)%0A
-%0A
   %7D%0A
+%0A
 %7D

center the triangle