@name FallDown
@inputs Scr:wirelink
@persist User:entity Use Using Menu Anim:table Price Background:vector
@persist Stage Floor Winnings Map:table CurPix Speed CurrentID MaxID MinID
@persist LastGen
#include "lib/egp/func"
#include "Arcade/lib/token"
#arr[1,number]=holepos
#arr[2,number]=index1
#arr[3,number]=index2
interval(1000/30)
local Draw = changed(Scr) | changed(Menu) | first()
if (first()) {
    CurrentID=210
    function a(S:string,N) {
        Anim[S,number]=N
    }
    function number g(S:string) {
        return Anim[S,number]
    }
    function bg(R,G,B) {
        Background=vec(R,G,B)
    }
    function setMusic(S:string) {
        for (I=1, 5) {
            Scr:entity():soundPlay(4+I,0,S)
        }
    }
    function addFloor(MinDist,MaxDist) {#add a floor
        local Meh = random(MinDist,MaxDist) #distance from last hole
        #do the math
        local HolePos=Map[LastGen,array][1,number]+Meh
        LastGen++
        local I1=CurrentID+1
        local I2=CurrentID+2
        CurrentID+=2
        if (CurrentID>MaxID) {CurrentID=MaxID}
        Map[LastGen,array]=array(HolePos,I1,I2)
        #draw the damn thing
        #right HolePos-30
        local Right=HolePos-30
        local Left=HolePos+30
        Scr:egpBox(I1,vec2(256-Right,522),vec2(256,20))
    }
    MaxID=290
    MinID=210
}
if (User:keyUse() & User:aimEntity()==Scr:entity() & !Using) {Using=1 Use=1}
if (!User:keyUse()) {Using=0 Use=0}



#menus
if (Draw) {
    Scr:egpClear()
    local Index=egpMaxObjects()
    Scr:egpBox(Index,vec2(256,256),vec2(512,512))
    Scr:egpMaterial(Index,"gui/gradient_down")
}
if (Menu==0) {
    bg(255,255,255)
    Price=10
    findByClass("player")
    User=findClosest(entity():pos())
    if (Draw) {
        Anim["Rot1",number]=0
        Scr:egpText(1,"FallDown",vec2(256,256))
        Scr:egpAlign(1,1,1)
        Scr:egpSize(1,50)
        Scr:egpColor(1,vec(1,1,1))
        Scr:egpCircle(2,vec2(128,256),vec2(20,20))
        Scr:egpColor(2,vec(172,0,0))
        Scr:egpCircle(3,Scr:egpPos(2),Scr:egpSize(2))
        Scr:egpMaterial(3,"gui/gradient_up")
        Scr:egpAlpha(3,128)
        Scr:egpColor(3,vec(0,0,0))
        Scr:egpText(4,""+Price+" Tokens",vec2(256,270))
        Scr:egpSize(4,25)
        Scr:egpColor(4,vec(1,1,1))
        Scr:egpText(5,"Press Anywhere to Begin",vec2(256,320))
        Scr:egpSize(5,30)
        Scr:egpAlign(5,1,1)
    }
    a("Pos1",g("Pos1")+5)
    Scr:egpAngle(3,g("Pos1"))
    
    a("C1",g("C1")+1)
    Scr:egpColor(4,hsv2rgb((360/600)*g("C1"),1,1))
    if (Use) {Menu=1}
} elseif (User:pos():distance(Scr:entity():pos())>150) {Menu=0 User=noentity()}

if (Menu==1) {#Payment
    if (Draw) {
        Scr:egpText(1,"Payment",vec2(256,30))
        Scr:egpColor(1,vec(1))
        Scr:egpAlign(1,1)
        Scr:egpSize(1,30)
        Scr:egpText(2,"It costs "+Price+" tokens to play.",vec2(256,200))
        Scr:egpSize(2,25)
        Scr:egpAlign(2,1,1)
        Scr:egpColor(2,vec(1))
        Scr:egpText(3,"Your tokens: "+User:getVar("Tokens"),vec2(256,226))
        Scr:egpColor(3,vec(1))
        Scr:egpSize(3,25)
        Scr:egpAlign(3,1,1)
        if (User:getVar("Tokens")>=Price) {
            Scr:egpBox(4,vec2(256,256),vec2(100,30))
            Scr:egpColor(4,vec(0,172,0))
            Scr:egpText(5,"Accept",Scr:egpPos(4))
            Scr:egpSize(5,30)
            Scr:egpAlign(5,1,1)
        }
        Scr:egpBox(6,vec2(256,300),vec2(100,30))
        Scr:egpColor(6,vec(172,0,0))
        Scr:egpText(7,"Cancel",Scr:egpPos(6))
        Scr:egpAlign(7,1,1)
        Scr:egpSize(7,30)
    }
    if (pIR(4)) {
        Scr:egpColor(4,vec(0,200,0))
        if (Use) {
            User:addVar("Tokens",-Price)
            Menu=2
        }
    } else {Scr:egpColor(4,vec(0,172,0))}
    if (pIR(6)) {
        Scr:egpColor(6,vec(200,0,0))
        if (Use) {
            Menu=0
        }
    } else {
        Scr:egpColor(6,vec(172,0,0))
    }
}


if (Menu==2) {#intro sequence
    #music/hl2_song26.mp3
    if (Draw) {
        a("Rot",0)
        a("Fadeback",0)
        soundPlay(0,0,"music/hl2_song26.mp3")
        Scr:egpCircle(1,vec2(256,256),vec2(20,20))
        Scr:egpColor(1,vec(172,0,0))
        Scr:egpCircle(2,vec2(0),Scr:egpSize(1))
        Scr:egpAlpha(2,128)
        Scr:egpMaterial(2,"gui/gradient_down")
        Scr:egpColor(2,vec(1))
        Scr:egpParent(2,1)
        Scr:egpText(5,"It was a normal day of being on a background...",vec2(256,60))
        Scr:egpAlign(5,1,1)
        timer("fade1",3000)
        bg(0,255,255)
        a("intro",1)
    }
    if (clk("fade1")) {
        Scr:egpAlpha(5,Scr:egpAlpha(5)-255/10)
        #if (Scr:egpAlpha(5)<0) {Scr:egpAlpha(5,0)}
        if (Scr:egpAlpha(5)<-0) {
            Scr:egpText(5,"EARTHQUAKE!!!",vec2(256,60))
            soundPlay(3,0,"ambient/explosions/explode_5.wav")
            Scr:egpSize(5,60)
            timer("shake",10)
            timer("panic",2000)
        } else {
            timer("fade1",100)
        }
    }
    if (clk("shake")) {
        Scr:egpPos(5,vec2(256,60)+vec2(random(-10,10),random(-10,10)))
        if (g("Fadeback")>0) {
            Scr:egpAlpha(5,0)
    } else {timer("shake",10)}
    }
    if (g("intro")) {
        a("Rot",g("Rot")+1)
        Scr:egpAngle(1,g("Rot"))
    }
    if (clk("panic")) {
        timer("fade",100)
        soundStop(0)
        Scr:entity():soundPlay(1,0,"ambient/explosions/citadel_end_explosion1.wav")
    }
    if (clk("fade") & g("Fadeback")<1) {
        a("Fadeback",g("Fadeback")+0.05)
        local A = g("Fadeback")
        local BG = A * (vec(200,128,0)-vec(0,255,255)) + vec(200,128,0)
        bg(BG:x(),BG:y(),BG:z())
        if (g("Fadeback")>=0.5) {
            local Fade=((g("Fadeback")-0.5)/0.5) * 255
            soundPlay(2,0,"music/hl2_song6.mp3")
            Scr:egpText(200,"Move your crosshair to move!",vec2(256,30))
            Scr:egpAlpha(200,Fade)
            Scr:egpAlign(200,1,0)
            Scr:egpSize(200,30)
            timer("tut2",3000)
            a("canmove",1)
        }
        timer("fade",100)
    }
    if (clk("tut2")) {
        Scr:egpSetText(200,"Don't get crushed by the ceiling!")
        timer("tut3",3000)
    }
    if (clk("tut3")) {
        Scr:egpSetText(200,"Fall, damnit, fall!")
        timer("fadeout",3000)
    }
    if (clk("fadeout")) {
        Scr:egpAlpha(200,Scr:egpAlpha(200)-255/10)
        if (Scr:egpAlpha(200)<0) {
            Scr:egpAlpha(200,0)
            a("on",1)
            Scr:entity():soundPlay(0,0,"music/hl2_song16.mp3")
        } else {timer("fadeout",100)}
    }
    if (g("canmove")) {
        local Move=0
        Goal=Scr:egpCursor(User):x()
        if (Goal<20) {Goal=20}
        if (Goal>512-20) {Goal=512-20}
        local Pos=Scr:egpPos(1):x()
        Move=(Pos-Goal)/10
        Scr:egpPos(1,Scr:egpPos(1)-vec2(Move,0))
    }
    if (g("on")) { #mapgen, falling
        #move everything up
        addFloor(-100,100)
    }
} else {soundPurge()}
Use=0
Scr:egpColor(egpMaxObjects(),Background)
