--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 23 / yaSnake
--
--------------------------------------------------- 
-- yaSnake (yet another) snake clone
--
-- written by umbiomar for fbacreator
--
-- The game was written in a couple of days for learning purpose
-- to discover fba power.
-- Feel free to enhance, rewrite, enrich without my permission ;D 
-- umbio@yahoo.com
----------------------------------------------------------------- 

--included script
include("resources.fba")
include("game.fba")
include("gui.fba")

-- some defines

SCREEN_ORIG_X   = 7     -- x origin of drawing 
SCREEN_ORIG_Y   = 7     -- y origin of drawing

UP      = 1             -- defines for direction of movement
RIGHT   = 2
DOWN    = 3    
LEFT    = 4

STATE_HIGHSCORES = 0
STATE_PLAYING    = 1
STATE_PAUSED     = 2
STATE_INSERTNAME = 3

CURRENT_STATE = STATE_HIGHSCORES

loadHiscores()
        
--------------------------------------------------------
-- game loop
function onmainloop()
        
        clear(255,255,255)
        
        drawBorder()
        
        if CURRENT_STATE == STATE_HIGHSCORES then

                doHighscores()

        elseif CURRENT_STATE == STATE_PLAYING then
        
                doGame()
                
        elseif CURRENT_STATE == STATE_INSERTNAME then
                doHighscores()
                blending(128)
                fillrect(0,0,screenwidth(),screenheight(),0,0,0)
                blending(255)
                doInsertName()
        end
        
        handleInput()
end

----------------------------------------------------------
-- handle player input
function handleInput()
        
        if CURRENT_STATE == STATE_PLAYING then
        
                if numkeypress( keyup() ) > 0  then              -- if up key
                        turn(UP)                   
                elseif numkeypress( keydown() ) > 0 then         -- if down key
                        turn(DOWN)
                elseif numkeypress( keyleft() ) > 0 then         -- if left key
                        turn(LEFT)
                elseif numkeypress( keyright() ) > 0 then        -- if right key
                        turn(RIGHT)
                end
        end 
        
end

----------------------------------------------------------
-- stylus events 
function onstylusdown(x,y)

        -- quit the game
        if (x>190 and x<225 and y>10 and y<20) then
                playsound(SNDHEAVYCLICK)
                quit()
        end
        -- during hiscore screen a tap start the game
        if CURRENT_STATE == STATE_HIGHSCORES then
                playsound(SNDHEAVYCLICK)
                startGame()
        end
        -- during name insert tap the keys
        if CURRENT_STATE == STATE_INSERTNAME then
                playsound(SNDHEAVYCLICK)
                keyHit( mousex(), mousey() ) 
        end
end 

----------------------------------------------------------
function onquit()
        -- nothing to do
end
