;*****************************************************************************************************
; Progam name : DoPlay.pb
; Version     : 1.00
; Build       : 0001
; PB Version  : 5.62
; Auther      : B.J.Fernhout.
; Copyright   : None
; Date        : 2018
; Discription : Start the init of the game And play the game here.
;             :
;             :
;*****************************************************************************************************

;=====================================================================================================
; Init the game before playing
;=====================================================================================================


Procedure ClearGrids()
  Protected x,y
  
  For x = 0 To 5
    For y = 0 To 12
      Grid(x,y) = 0
      Explode(x,y) = 0
      Colors(x,y) = 0
    Next
  Next

EndProcedure
Procedure NewAwaits()
  
  ;The next awaiting drops
  Awaits(0) = Random(6,1)
  Awaits(1) = Random(6,1)
  Awaits(2) = Random(6,1)
  
EndProcedure
Procedure SetDrops()
  ;The drops above the pit
  Drops(0) = Random(6,1)
  Drops(1) = Random(6,1)
  Drops(2) = Random(6,1)
  
  ;The next awaiting drops
  NewAwaits()
  
EndProcedure
Procedure ChangeMusic()

  StopSound(Snd_Colums)
  PlaySound(Snd_Battle,#PB_Sound_Loop,Set_VolumeMusic)

EndProcedure
Procedure GetReady()
  Protected x.f = 641
  Protected Adding.f = 0.0
  CycleTime_Start = ElapsedMilliseconds()
  Repeat
    ClearScreen(RGB(255,255,255))
    DisplayTransparentSprite(Im_Get_Ready,x,221)
    x - ((MoveGetReady+Adding) * CycleTime_Verschil)
    If x <= 207.0
      adding +0.0002
    EndIf
    FlipBuffers()
    CycleTime_End = ElapsedMilliseconds()
    CycleTime_Verschil = CycleTime_End - CycleTime_Start
    CycleTime_Start = CycleTime_End
  Until x < - 226
  Active_Case = 6
EndProcedure
Procedure DoInit()
  
  ClearGrids()
  SetDrops()
  ChangeMusic()
  GetReady()

EndProcedure

;=====================================================================================================
; Play the game from here
;=====================================================================================================
Procedure GetLocation()
  
  ;Get grid location
  LocY = ((DropY+9)/32)+1
  LocX = (DropX-224)/32
  
EndProcedure
Procedure DisplayGame()
  Protected x
  Protected Darker = Level_Player*20 ; make the screen darker afther every new level
  
  CycleTime_Start = ElapsedMilliseconds()
  If Darker > 255 : darker = 255 : EndIf
  ClearScreen(RGB(255-darker,255-darker,255-darker))
  ;five time a big font As bitmap
  Big_Font("SCORE",420,50)
  Big_Font("LEVEL",420,150)
  Big_Font("STONE",420,250)
  Big_Font("TO GO",420,350)
  Big_Font("NEXT",50,50)
  StartDrawing(ScreenOutput())
  ; set text And background
  FrontColor(RGB(255,255,255))
  ; do the text placing in string mode
  DrawingMode(#PB_2DDrawing_XOr)
  DrawText(450,80,Str(Score_Player))
  DrawText(450,180,Str(Level_Player))
  DrawText(450,280,Str(Stone_Player))
  DrawText(450,380,Str(Togo_Player))
  DrawingMode(#PB_2DDrawing_Transparent)
  
  ; draw the compleet grid overview incl. jewel
  For x = 0 To 5
    For Y = 1 To 12
      DrawImage(ImageID(JewColors(Grid(x,y))),224+(x*32),55+(y*32))
    Next
  Next
  
  ;Above the grid
  DrawImage(ImageID(JewColors(Drops(0))),DropX,DropY)
  DrawImage(ImageID(JewColors(Drops(1))),DropX,DropY+32.0)
  DrawImage(ImageID(JewColors(Drops(2))),DropX,DropY+64.0)
    
  ;Draw the next drops
  DrawImage(ImageID(JewColors(Awaits(0))),188,58)
  DrawImage(ImageID(JewColors(Awaits(1))),188,90)
  DrawImage(ImageID(JewColors(Awaits(2))),188,122)
  StopDrawing()
  
  If ShowUpLevel 
    Big_Font("LEVEL",50,180)
    Big_Font("UP",70,230)
    If Tim_ShowLevel < ElapsedMilliseconds()
      ShowUpLevel = #False
    EndIf
  EndIf

 
  
EndProcedure
Procedure DoDropDown()
  Protected Ready = #False
  
  GetLocation()
  
  ;Check drop line
  Repeat
    If LocY+1 < 13 And Grid(LocX,LocY+1) = 0
      LocY+1
    Else
      ready = #True
    EndIf
  Until Ready
  
  ;Is top line full
  If LocY <=2 
    Active_Case = 7
    ProcedureReturn
  EndIf
  
  ;Set Tet over to grid
  grid(LocX,LocY)   = Drops(2)
  grid(LocX,LocY-1) = Drops(1)
  grid(LocX,LocY-2) = Drops(0)
  
  Drops(0) = Awaits(0)
  Drops(1) = Awaits(1)
  Drops(2) = Awaits(2)
  
  DropDown = #True
  PlaySound(Snd_Drop)
  
  NewAwaits()
  DropY = -9
  DropX = 320

EndProcedure
;Clear explosion field and remove the tile that are exploded
Procedure ClearExplosion()
  Protected x,y
  For x = 0 To 5
    For Y = 0 To 12
      If explode(x,y) > 0
        Grid(x,y) = 0
      EndIf
      Explode(x,y) = 0
    Next
  Next
EndProcedure
Procedure DropAll()
Protected x,y,yt
For x = 0 To 5
   For y = 12 To 0 Step -1
      ; found empty grid pos
      If grid(x,y) = 0
         yt = y
         ;go over the line en pull all down
         While yt <> -1
            If grid(x,yt)<>0
               grid(x,y) = grid(x,yt)
               grid(x,yt) = 0
               y-1
            EndIf
            yt-1
         Wend
         ; done it we can go To mext line
         y = yt
      EndIf
   Next y
 Next x
 
EndProcedure
Procedure ShowExplosions()
  
  ;If there is a line found then explosion is active
  If Explosion_Found = #False
    ProcedureReturn
  EndIf
  
  ;Precheck when marker is low the first explosion sound may played
  If Explosion_On = #False
    PlaySound(Snd_Explosion)
  EndIf  
  
  ;Scan for explosion markers
  Explosion_On = #False
  For x = 0 To 5
    For Y = 0 To 12
      If Explode(x,y) <> 0
        StartDrawing(ScreenOutput())
        DrawImage(ImageID(explosions(ExplodeSequence)),224+(x*32),55+(y*32))
        StopDrawing()
        Explosion_On = #True
        If ElapsedMilliseconds()>WaitExplosion
          ExplodeSequence+1
          WaitExplosion = ElapsedMilliseconds()+50
        EndIf
        If ExplodeSequence >= 10
          ExplodeSequence = 0
          ClearExplosion()
          Grid(x,y) = 0
          DropAll()
        EndIf
      EndIf
    Next
  Next
  
EndProcedure
Procedure MoveDrops()
  
  dropy + (MoveTet*Level_Player)

EndProcedure
Procedure CheckFreePlace()
  
  GetLocation()
  
  If grid(locx,locy) <> 0
    If LocY <= 2
      Active_Case = 7
      ProcedureReturn
    EndIf
    DoDropDown()
  EndIf
  
EndProcedure
Procedure ClearColor()
  Protected x,y
  
  For x = 0 To 5
    For y = 0 To 12
      Colors(x,y) = 0
    Next
  Next
  
EndProcedure
Procedure LevelUp()
  
  If Togo_Player <= 0
    Level_Player + 1
    Togo_Player = 30 + (Level_Player * 10)
    ShowUpLevel = #True
    Tim_ShowLevel = ElapsedMilliseconds() + 5000
  EndIf
  
EndProcedure
Procedure CopyExplosion()
   Protected x,y
  
  For x = 0 To 5
    For y = 0 To 12
      Explode(x,y) = Colors(x,y)
    Next
  Next
  ClearColor()

EndProcedure
Procedure CalculateNumbers()
  Protected x,y
  
   For x = 0 To 5
    For y = 0 To 12
      If Explode(x,y) = 1
        Togo_Player-1
        Score_Player+(Level_Player * Locy)
        Stone_Player+1
        levelup()
        EndIf
    Next
  Next
  
EndProcedure
Procedure ExtendLine(x,y,count)
  Protected x1,y1,xt,yt
  For x1 = -1 To 1
    For y1 = -1 To 1
      xt = x
      yt = y
      ; The center is already searched
      If x1 <> 0 Or y1 <> 0
        While x+x1 >= 0 And x+x1 <= 5 And y+y1 >= 1 And y+y1 <= 12
          If grid(x+x1,y+y1) = grid (x,y)
            count+1
            Colors(x+x1,y+y1) = 1
            x+x1
            y+y1 
          Else
            Break
          EndIf
        Wend
        ;Is there a line
        If count > 2
          ProcedureReturn count
        Else
          count = 1
          ClearColor()
          x = xt
          y = yt
          colors(x,y) = 1
        EndIf
      EndIf
    Next
  Next
  ProcedureReturn -1
  
EndProcedure
Procedure CheckForLine()
  Protected x,y
  Protected Count
  
  ;Do not search if explosion is busy or no new elements is in
  If Explosion_On Or DropDown = #False
    ProcedureReturn
  EndIf
  
  ;Go find a color
  For x = 0 To 5
    For y = 1 To 12
      If grid(x,y) <> 0
        Count = 1
        colors(x,y) = 1
        count =ExtendLine(x,y,count)
        If count = -1
          ClearColor()
        ElseIf count > 2
          CopyExplosion()
          Explosion_Found = #True
          count = -1
        EndIf
      EndIf
    Next
  Next
  
  If Explosion_Found = #False
    DropDown = #False
  Else
    CalculateNumbers()
  EndIf
  
EndProcedure
;Let color roll over
Procedure ShiftColors()
  Protected TempColor
  
  TempColor = drops(0)
  drops(0) = Drops(1)
  drops(1) = drops(2)
  drops(2) = TempColor
   
EndProcedure
;Get keyboard action
Procedure ExecuteKeyboard()
  
  GetLocation()
  
  ;Can the line drop
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space) And Key_Space_Down = #False
    ;Drop the line down
    DoDropDown()
    Key_Space_Down = #True
  EndIf
  
  ;Move line or shift colors
  If KeyDelay < ElapsedMilliseconds()
    If  KeyboardPushed(#PB_Key_Up)
      ShiftColors()
      KeyDelay = ElapsedMilliseconds()+ 100
    EndIf
    If  KeyboardPushed(#PB_Key_Left)
      If LocX -1 >= 0 
        If grid(LocX-1,LocY) = 0
          LocX -1
          DropX = (LocX*32)+224
        EndIf
      EndIf
      KeyDelay = ElapsedMilliseconds()+ 75
    EndIf
    If  KeyboardPushed(#PB_Key_Right)
      If LocX +1 <= 5 
        If grid(LocX+1,LocY) = 0
          LocX + 1
          dropX = (LocX*32)+224
        EndIf
      EndIf
    EndIf
    KeyDelay = ElapsedMilliseconds()+ 75 
  EndIf
  
  ;Set marker for releas keyboard
  If KeyboardReleased(#PB_Key_Space) 
    Key_Space_Down = #False
  EndIf
  
EndProcedure
;Calculate the time needed to make the full round in the game 
Procedure CalculateMoves()

  CycleTime_End = ElapsedMilliseconds()
  CycleTime_Verschil = CycleTime_End - CycleTime_Start
  CycleTime_Start = CycleTime_End

EndProcedure
Procedure ReadyScreen()
  
  FlipBuffers()
  ClearScreen(0)
  
EndProcedure
Procedure DoPlay()
  
  CheckForLine()
  ShowExplosions()
  MoveDrops()
  CheckFreePlace()
  ExecuteKeyboard()
  CalculateMoves()
  ReadyScreen()
  Displaygame()
  ;event = WindowEvent()
  
EndProcedure




; IDE Options = PureBasic 5.62 (Windows - x64)
; CursorPosition = 463
; FirstLine = 51
; Folding = AgAA-
; EnableXP
; CompileSourceDirectory
; EnableCompileCount = 0
; EnableBuildCount = 0
; EnableExeConstant