################
# > HoloDrawBlocks: Draw using blocks and a grid.
# > How to use: Left click to place a block. Right click to remove the last block.
################

@name HoloDrawBlocks
@persist Aim:vector Pos:vector I Fire Remove Stack NoDouble

interval(10)

Inc = 12
Aim = owner():aimPos()
Pos = (round(Aim/Inc)*Inc+vec(0,0,Inc/2))
Fire = owner():keyAttack1()
Remove = owner():keyAttack2()
Colour = vec(0,0,255)

if (first()) {
    I=1
    holoCreate(0)
    holoScale(0, vec(Inc/12,Inc/12,Inc/12))
    holoColor(0, Colour)
    holoAlpha(0, 150)
}

holoPos(0, Pos)

if (changed(Fire)&Fire) {
    holoCreate(I)
    holoScale(I, vec(Inc/12,Inc/12,Inc/12))
    holoColor(I, Colour)
    holoPos(I, Pos)
    for (Stack=1, I-1) {
        if (holoEntity(Stack):pos()==Pos) {
            holoPos(I, Pos+vec(0,0,Inc))
        }
    }
    I++
}

if (changed(Remove)&Remove) {
    for (Stack=1, I-1) {
        if (holoEntity(Stack):pos()==Pos) {
            holoDelete(Stack)
            I--
        }
    }
}

if (changed(Pos)&Fire) {
    holoCreate(I)
    holoScale(I, vec(Inc/12,Inc/12,Inc/12))
    holoColor(I, Colour)
    holoPos(I, Pos)
    for (Stack=1, I-1) {
        if (holoEntity(Stack):pos()==Pos) {
            holoPos(I, Pos+vec(0,0,Inc))
        }
    }
    I++
}

for (NoDouble=1, I-1) {
    if (holoEntity(NoDouble):pos()==holoEntity(I):pos()) {
        holoDelete(NoDouble)
    }
}
