@name EGP-Pong
@inputs EGP:wirelink
@outputs Speed
@persist [Screen Pod]:entity
@persist [BallPos BallSpeed]:vector2
@persist [HighScore]:array
@persist [Mode]:string
@persist SavedUse StartTime Time Life Start Restart ScoreTime SubmitScore ScoreUpdate
@trigger none

if (duped()) {
    reset()
}

if (first() | changed(->EGP)) {
    runOnTick(1)
    
    HighScore = array(600,300,120,60,10)
    gShare(1)
    
    Pod = entity():isWeldedTo()
    Screen = EGP:entity()
    EGP:egpBox(1,vec2(256,256),vec2(10,50),vec4(255,255,255,0))
    EGP:egpBox(2,vec2(256,256),vec2(10,50),vec4(255,255,255,0))
    EGP:egpBox(3,vec2(256,256),vec2(10,10),vec4(255,255,255,0))
    EGP:egpBox(4,vec2(0,100),vec2(512,10),vec4(255,255,255,255))
    EGP:egpBox(5,vec2(206,231),vec2(100,50),vec4(75,75,125,255))
    EGP:egpBox(6,vec2(156,375),vec2(200,50),vec4(75,75,125,0))
    EGP:egpText(7,"Start",vec2(256,256),vec4(255,255,255,255))
    EGP:egpTextAlign(7,1,1)
    EGP:egpText(8,"Highscore",vec2(256,400),vec4(225,225,225,0))
    EGP:egpTextAlign(8,1,1)
    EGP:egpText(9,"Credits: 0",vec2(256,474),vec4(200,200,125,125))
    EGP:egpTextAlign(9,1,2)
    EGP:egpText(10,"Pong",vec2(256,-10),vec4(225,225,225,255))
    EGP:egpSetFont(10,"arial",75)
    EGP:egpTextAlign(10,1,0)
    EGP:egpText(11,"Made by Feha",vec2(256,85),vec4(225,225,225,255))
    EGP:egpTextAlign(11,1,1)
    EGP:egpText(12,"Life: ",vec2(0,100),vec4(225,225,225,0))
    EGP:egpSetFont(12,"arial",42)
    EGP:egpTextAlign(12,0,2)
    EGP:egpText(13,"Time: ",vec2(512,100),vec4(225,225,225,0))
    EGP:egpSetFont(13,"arial",42)
    EGP:egpTextAlign(13,2,2)
    EGP:egpText(14,"Game over",vec2(256,150),vec4(225,225,225,0))
    EGP:egpSetFont(14,"arial",42)
    EGP:egpTextAlign(14,1,1)
    
    EGP:egpText(20,"High Score",vec2(256,150),vec4(225,225,225,0))
    EGP:egpSetFont(20,"arial",42)
    EGP:egpTextAlign(20,1,1)
    EGP:egpText(21,"1. ",vec2(256,200),vec4(225,225,225,0))
    EGP:egpTextAlign(21,1,1)
    EGP:egpText(22,"2. ",vec2(256,225),vec4(225,225,225,0))
    EGP:egpTextAlign(22,1,1)
    EGP:egpText(23,"3. ",vec2(256,250),vec4(225,225,225,0))
    EGP:egpTextAlign(23,1,1)
    EGP:egpText(24,"4. ",vec2(256,275),vec4(225,225,225,0))
    EGP:egpTextAlign(24,1,1)
    EGP:egpText(25,"5. ",vec2(256,300),vec4(225,225,225,0))
    EGP:egpTextAlign(25,1,1)
}

#Stuff
User=Pod:driver()

#Interact
if (User:aimEntity() == Screen) {
    BoxSize = Screen:boxSize()
    Pos = floor(shift(vec2(Screen:toLocal(User:aimPos())+BoxSize/2)*(512/BoxSize:x())))
    EGP:egpCircle(0,Pos,vec2(10,10),vec4(0,255,0,255))
    Use = User:keyAttack1()
    Use2 = User:keyAttack2()
}

#Losing
if (SubmitScore) {
    if (ScoreUpdate <= curtime()) {
        Global = glonDecode(gGetStr("Feha-Pong-HighScore"))
        for (I=1,Global:count()) {
            Score1 = Global[I,number]
            Score2 = HighScore[I,number]
            if (Score1 >= Score2) {
                HighScore[I,number]=Score1
            }
        }
        gSetStr("Feha-Pong-HighScore",glonEncode(HighScore))
        
        EGP:egpColor(20,vec4(255,255,255,255))
        for (I=21,25) {
            Score = HighScore[I-20,number]
            EGP:egpColor(I,vec4(255,255,255,255))
            EGP:egpSetText(I,toString(I-20)+". "+Score)
        }
        
        ScoreUpdate = curtime()+10
    }
    
    Inrange = inrange(Pos,vec2(156,375),vec2(156,375)+vec2(200,50))
    if (Inrange) {
        EGP:egpColor(6,vec4(75,150,75,255))
        if (SavedUse != Use & Use) {
            SubmitScore = 0

            for (I=0,5) {
                EGP:egpColor(20+I,vec4(255,255,255,0))
            }
            EGP:egpSetText(8,"Highscore")
        }
    } else {
        EGP:egpColor(6,vec4(75,75,125,255))
    }
} elseif (Start) {
    if (Restart) {
        Restart = 0
        #Set variables
        GameOver = 1
        Life = 3
        StartTime = curtime()
        #Reposition everything
        BallSpeed = vec2(-1,random(-1,1))
        BallPos = vec2(256,256)
        
        EGP:egpColor(1,vec4(255,255,255,255))
        EGP:egpColor(2,vec4(255,255,255,255))
        EGP:egpColor(3,vec4(255,255,255,255))
        EGP:egpColor(4,vec4(255,255,255,255))
        
        EGP:egpColor(12,vec4(255,255,255,255))
        EGP:egpColor(13,vec4(255,255,255,255))
        EGP:egpSetText(12,"Life: 3")
        EGP:egpSetText(13,"Time: 0")
    }
    
    if (Life) {
        Time = curtime()-StartTime
        
        #ScoreBoard
        EGP:egpSetText(12,"Life: "+Life)
        EGP:egpSetText(13,"Time: "+round(Time))
        
        #Your Paddle
        PaddlePos = Pos:setX(42)-vec2(0,25)
        PaddleY = Pos:y()
        EGP:egpPos(1,PaddlePos)
        
        #The ball
        Speed = clamp(0.0065*Time+1.15,1,3)
        BallSpeed = vec2(sign(BallSpeed:x())*Speed,BallSpeed:y())
        BallPos+= BallSpeed
        BallX = BallPos:x()
        BallY = BallPos:y()
        EGP:egpPos(3,BallPos-vec2(5,5))
        
        if (BallX < 0) {
            Life--
            ScoreTime = Time
            #Reposition everything
            BallSpeed = vec2(-1,random(-1,1))
            BallPos = vec2(256,256)
            
            EGP:egpColor(1,vec4(255,255,255,255))
            EGP:egpColor(2,vec4(255,255,255,255))
            EGP:egpColor(3,vec4(255,255,255,255))
            EGP:egpColor(4,vec4(255,255,255,255))
            EGP:egpSetText(8,"Submit Score")
        }
        
        if (BallY < 110 | BallY > 507) {
            BallSpeed*= vec2(1,-1)
        }
        
        if (BallX > 475) {
            BallSpeed*= vec2(-1,1)
        }
        
        if (BallX < 50 & inrange(BallY,PaddleY-25,PaddleY+25)) {
            BallSpeed*= vec2(-1,1)
            Offset = (BallY-PaddleY)/20
            BallSpeed+= vec2(0,Offset)
        }
        
        if (Mode == "1P") {
            EGP:egpPos(2,vec2(475,BallY-25))
        }
    } else {
        EGP:egpColor(1,vec4(255,255,255,0))
        EGP:egpColor(2,vec4(255,255,255,0))
        EGP:egpColor(3,vec4(255,255,255,0))
        EGP:egpColor(5,vec4(75,75,125,255))
        EGP:egpColor(12,vec4(255,255,255,0))
        EGP:egpColor(13,vec4(255,255,255,0))
        EGP:egpColor(14,vec4(255,255,255,255))
        Start = 0
    }
} else {
    EGP:egpColor(7,vec4(255,255,255,255))
    EGP:egpColor(8,vec4(255,255,255,255))
    EGP:egpColor(9,vec4(200,200,125,255))
    EGP:egpSetText(9,"Credits: "+User:inVehicle() )
    
    
    if (inrange(Pos,vec2(206,231),vec2(306,281))) {
        EGP:egpColor(5,vec4(75,150,75,255))        
        if (SavedUse != Use & Use) {
            Start=1
            Restart=1
            Mode = "1P"
            EGP:egpColor(5,vec4(75,150,75,0))
            EGP:egpColor(6,vec4(75,150,75,0))
            EGP:egpColor(7,vec4(255,255,255,0))
            EGP:egpColor(8,vec4(255,255,255,0))
            EGP:egpColor(9,vec4(255,255,255,0))
            EGP:egpColor(14,vec4(255,255,255,0))
        }
    } elseif (!Start) {
        EGP:egpColor(5,vec4(75,75,125,255))
    }
    
    Inrange = inrange(Pos,vec2(156,375),vec2(156,375)+vec2(200,50))
    if (Inrange) {
        EGP:egpColor(6,vec4(75,150,75,255))
        if (SavedUse != Use & Use) {
            SubmitScore = 1
            ScoreUpdate = curtime()
            for (I=1,HighScore:count()) {
                Time = HighScore[I,number]
                if (ScoreTime >= Time) {
                    HighScore:insertNumber(I,ScoreTime)
                    break
                }
            }
            EGP:egpColor(5,vec4(75,150,75,0))
            EGP:egpColor(7,vec4(255,255,255,0))
            EGP:egpColor(9,vec4(255,255,255,0))
            EGP:egpColor(14,vec4(255,255,255,0))
            EGP:egpSetText(8,"Return")
        }
    } elseif (!Start) {
        EGP:egpColor(6,vec4(75,75,125,255))
    }
}


