'**********************************************************************
'* Developed in QB64 v1.5
'**********************************************************************


On Error GoTo ErrorHandle
If _FileExists("Error.txt") Then Kill "Error.txt"

'$INCLUDE:'Global.h'
'$INCLUDE:'Main.h'
'$INCLUDE:'Controller.h'
'$INCLUDE:'Leaderclient.bh'




Call InitEngine



Do
    If Settings.Intro = 1 Then Call Intro
    If Settings.MainMenu = 1 Then Call MainMenu

    If ClockTrack <> Val(Mid$(Time$, 7, 2)) And Settings.Ending = 0 Then
        Player1.Score = Player1.Score + 1

        ClockTrack = Val(Mid$(Time$, 7, 2))
    End If


    _Limit 60
    If Settings.ModifyCash <> 0 And Settings.ModifyingCash = 0 Then
        Timer(CashTimer) On
        Settings.ModifyingCash = 1
    ElseIf Settings.ModifyCash = 0 And Settings.ModifyingCash = 1 Then
        Call SaveGame("Modifying Cash")
        Timer(CashTimer) Off
        Settings.ModifyingCash = 0
    End If

    If ActiveMap.OpenCondition > 0 And Player1.Death = 0 Then Call CheckLockdown(0)
    If ActiveMap.Type = 1 Then Call AnimateWater
    If Player1.InRoom = 0 And LinkInRoom = 1 Then Player1.InRoom = 1

    If _KeyDown(27) = -1 Then
        Call JukeBox(-3)
        Call SoundBoard("", 0)
        Settings.MainMenu = 1
    ElseIf Settings.Debug <> 0 And _KeyDown(15104) = -1 Then
        Screen Display&
    ElseIf Settings.Debug <> 0 And _KeyDown(15360) = -1 Then
        Screen Collision&
    ElseIf Settings.Debug <> 0 And _KeyDown(15616) = -1 Then
        Screen Transition&
    ElseIf Settings.Debug <> 0 And _KeyDown(15872) = -1 Then
        Screen Scene&
    ElseIf Settings.Debug <> 0 And _KeyDown(16128) = -1 Then
        Screen Sprites&
    ElseIf Settings.Debug <> 0 And _KeyDown(16384) = -1 Then
        Player1.Health = 0
        Call LinkDeath
    ElseIf Settings.Debug <> 0 And _KeyDown(34304) = -1 Then
        Error 97
    End If

    If Controller.Enabled = 1 Then
        If Strig(Controller.A) = -1 Then ControllerCmd$ = "A"
        If Strig(Controller.B) = -1 Then ControllerCmd$ = "B"
        If Strig(Controller.Up) = -1 Then Call InputHandle("North")
        If Strig(Controller.Down) = -1 Then Call InputHandle("South")
        If Strig(Controller.Left) = -1 Then Call InputHandle("East")
        If Strig(Controller.Right) = -1 Then Call InputHandle("West")
        If Strig(Controller.Start) = -1 Then Call PauseScreen 'ControllerCmd$ = "Start"
        Player1.Animate = 0

        If Stick(1) < 128 - Controller.DeadZone And Player1.CanMove = 1 Then
            ControllerUsed = 1
            Controller.MovePerc = 1 - (Stick(1) / (128 - Controller.DeadZone))
            Call InputHandle("North")
            'END IF
        ElseIf Stick(1) > 128 + Controller.DeadZone And Player1.CanMove = 1 Then
            ControllerUsed = 1
            Controller.MovePerc = 1 - (255 - Stick(1)) / (255 - (128 + Controller.DeadZone))
            Call InputHandle("South")
            'END IF
        ElseIf Stick(0) < 128 - Controller.DeadZone And Player1.CanMove = 1 Then
            ControllerUsed = 1
            Controller.MovePerc = 1 - (Stick(0) / (128 - Controller.DeadZone))
            Call InputHandle("West")
            'END IF
        ElseIf Stick(0) > 128 + Controller.DeadZone And Player1.CanMove = 1 Then
            ControllerUsed = 1
            Controller.MovePerc = 1 - (255 - Stick(0)) / (255 - (128 + Controller.DeadZone))
            Call InputHandle("East")
        End If
    End If

    If ControllerUsed = 0 Then
        If _KeyDown(87) = -1 Or _KeyDown(119) = -1 Then
            If Player1.CanMove = 1 Then Call InputHandle("North")
        ElseIf _KeyDown(83) = -1 Or _KeyDown(115) = -1 Then
            If Player1.CanMove = 1 Then Call InputHandle("South")
        ElseIf _KeyDown(65) = -1 Or _KeyDown(97) = -1 Then
            If Player1.CanMove = 1 Then Call InputHandle("West")
        ElseIf _KeyDown(68) = -1 Or _KeyDown(100) = -1 Then
            If Player1.CanMove = 1 Then Call InputHandle("East")
        ElseIf _KeyDown(9) = -1 And Settings.LockInput < 2 Then
            Call PauseScreen
        Else
            Player1.Animate = 0
        End If
    End If


    If _KeyDown(69) = -1 Or _KeyDown(101) = -1 Or ControllerCmd$ = "A" Then
        If LinkInRoom = 1 And Player1.Sword > 0 Then
            Player1.Animate = 0
            If Settings.LockAttack = 0 Then
                Call InputHandle("Primary")
                Timer(SwordDelayTimer) On
                Settings.LockAttack = 1
            End If
        End If
    ElseIf _KeyDown(81) = -1 Or _KeyDown(113) = -1 Or ControllerCmd$ = "B" Then
        If Player1.SecondaryWeapon <> 0 And LinkInRoom = 1 Then
            If Player1.SecondaryWeapon <> 0 Then
                Call InputHandle("Secondary")
                Timer(SwordDelayTimer) On
                Settings.LockAttack = 1
            End If
        End If
    End If

    If _KeyDown(49) = 0 And _KeyDown(50) = 0 Then Settings.LockAccKeys = 0

    Call DrawSprites
    Call AssembleDisplay
    Call CheckForAudioLoop

    ControllerCmd$ = ""
    ControllerUsed = 0
    Controller.MovePerc = 1
    Framecount = Framecount + 1

    If Settings.DelayLinkHurt > 0 And Settings.PlayerCollisionDetected = 0 Then Settings.DelayLinkHurt = Settings.DelayLinkHurt - 1
    If Player1.Health <= 2 And Player1.Health > 0 And _SndPlaying(SFX(21)) = 0 Then Call SoundBoard("LowHealth", -1)

    Settings.PlayerCollisionDetected = 0
Loop

ErrorHandle:
Call ErrorHandler

Sub LockMenu
    Settings.LockInput = 0
    Timer(MenuTimer) Off
End Sub






'$include:'LeaderClient.bi'
'$include:'Controller.bi'
'$include:'Error.bi'
'$include:'Engine.bi'
'$include:'Enviroment.bi'
'$include:'Global.bi'
'$include:'Math.bi'
'$include:'Sprites.bi'
'$include:'Store.bi'
'$include:'SpawnSprite.bi'
'$include:'UI.bi'
'$include:'Menus.bi'
'$include:'Objectlist.bi'
