; ESCape
; LD21 entry
; 2011 - Ruben Mueller, Aaron Mueller

EnableExplicit

; Initialize subsystems
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0
  MessageRequester("Error", "Sorry, can't keep up.", 0)
  End
EndIf


XIncludeFile "Structs.pbi"
XIncludeFile "Globals.pbi"
XIncludeFile "Functions.pbi"
XIncludeFile "Draw.pbi"
XIncludeFile "Events.pbi"


LoadMap(#DATA_PATH + "maps/joni.tmx")


; Initialize all the variables and stuff. I think we refactor this
; into a separate file if it gets messy.
GameStates("MAIN_MENU")\DrawFun = @DrawMenu()
GameStates("MAIN_MENU")\HandleEventFun = @HandleEventMenu()
GameStates("RLY_QUIT")\DrawFun = @DrawMenu()
GameStates("RLY_QUIT")\HandleEventFun = @HandleEventMenu()

GameStates("RUNNING_GAME")\DrawFun = @DrawRunningGame()
GameStates("RUNNING_GAME")\HandleEventFun = @HandleEventRunningGame()



; Initialize the menus
Menus("MAIN_MENU")\Title = "ESCape"

Define Item.GameMenuItem
Item.GameMenuItem\Label = "Escape now!"
Item.GameMenuItem\Selected = #True
Item.GameMenuItem\TargetState = "RUNNING_GAME"
AddElement(Menus("MAIN_MENU")\Entries())
menus("MAIN_MENU")\Entries() = Item

Define Item.GameMenuItem
Item.GameMenuItem\Label = "End game"
Item.GameMenuItem\Selected = #False
Item.GameMenuItem\TargetState = "RLY_QUIT"
AddElement(Menus("MAIN_MENU")\Entries())
menus("MAIN_MENU")\Entries() = Item


Menus("RLY_QUIT")\Title = "Really quit?"

Define Item.GameMenuItem
Item.GameMenuItem\Label = "Yes, let me go!"
Item.GameMenuItem\Selected = #True
Item.GameMenuItem\TargetState = "QUIT"
AddElement(Menus("RLY_QUIT")\Entries())
menus("RLY_QUIT")\Entries() = Item

Define Item.GameMenuItem
Item.GameMenuItem\Label = "No way!"
Item.GameMenuItem\Selected = #False
Item.GameMenuItem\TargetState = "MAIN_MENU"
AddElement(Menus("RLY_QUIT")\Entries())
menus("RLY_QUIT")\Entries() = Item


*ActiveMenu.GameMenu = @Menus(CurrentState)



; Start the main loop which will create the window, wait for events and draw things
; on the screen. To escape from this loop, press ESC or close the window.
If OpenWindow(0, 0, 0, Screen\Width, Screen\Height, title, #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, Screen\Width, Screen\Height, 0, 0, 0)
  
  InitGraphics()
  InitTileset()
  
  Repeat
    ; Event loop
    Repeat
      Define Event = WindowEvent()
    Until Event = 0
    
    ; Handle the background music
    If CurrentState = "MAIN_MENU" Or CurrentState = "RLY_QUIT"
      If MenuSoundStarted = 0
        MenuSoundStarted = PlaySound(2, #PB_Sound_MultiChannel|#PB_Sound_Loop)
        SoundVolume(2, 50, MenuSoundStarted)
      EndIf
    Else
      StopSound(2, MenuSoundStarted)
      MenuSoundStarted = 0
    EndIf
    
    ; Handle global events
    ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F)
      CloseScreen()
      If Fullscreen = 0
        OpenScreen(800, 600, 32, Title)
        Fullscreen = 1
      Else
        OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
        Fullscreen = 0
      EndIf
      InitGraphics()
      InitTileset()
    EndIf
    
    ; Debug CurrentState
    
    ; Handle the events for the current state
    GameStates(CurrentState)\HandleEventFun()
    
    ; Draw the stuff on the screen
    FlipBuffers()
    ClearScreen(RGB(0, 0, 0))

    If CurrentState <> "QUIT"
      GameStates(CurrentState)\DrawFun()
    EndIf
    
    Delay(1)
  Until CurrentState = "QUIT"
EndIf

End
