# use console.error for debug - stdout is busy writing WAD files.
fs = require("fs")
{ WAD, MapEdit } = require('..')

w = new WAD(true)
m = new MapEdit('MAP02')

minX = -1024
maxX = 1024
rangeY = 1024 + 512

buildSlantSquare = (x, y, width, height, startSlant, endSlant, bInvert, sectorData, lineData) ->
    if bInvert
        pos = [
            [
                x
                y - startSlant
            ]
            [
                x + width
                y - endSlant
            ]
            [
                x + width
                y + height + endSlant
            ]
            [
                x
                y + height + startSlant
            ]
        ]

    else
        pos = [
            [
                x
                y + startSlant
            ]
            [
                x + width
                y + endSlant
            ]
            [
                x + width
                y - height - endSlant
            ]
            [
                x
                y - height - startSlant
            ]
        ]

    return m.buildSector(pos, sectorData, lineData)

cosineSlant = (segmentWidth, index, maxIndex) ->
    input = (index / maxIndex - 0.5) * Math.PI
    output = Math.cos(input) * segmentWidth

    return output

buildSlantProgression = (x, y, width, height, slantFunc, resolution, sectorData, lineData) ->
    prevSlant = slantFunc(resolution, 0, width / resolution)

    index = 0
    while index < resolution
        pos = width / resolution * index
        newSlant = slantFunc(width / resolution, index + 1, resolution)
        buildSlantSquare(x + pos, y, width / resolution, height, prevSlant, newSlant, true, sectorData, lineData)
        prevSlant = newSlant
        index++

    return m

buildSlantProgression(0, -rangeY / 2, -minX + maxX, rangeY, cosineSlant, 32)
    .addThing(96, 0, 1, 0)
    .toLumps(w)

fs.writeFileSync("testSlant.wad", w.write())