-- NAME: Touch Counter
-- DESC: Simple tap counter demo

-- Initialize game state
function init()
    game.vars.count = 0
    game.vars.last_x = 0
    game.vars.last_y = 0
    print("Counter initialized")
end

-- Update game logic based on input
function update(event)
    -- Check if touch/button pressed
    if event.type == INPUT.TOUCH_DOWN or event.type == INPUT.BUTTON_0 or event.type == INPUT.BUTTON_1 then
        game.vars.count = game.vars.count + 1
        game.vars.last_x = event.x
        game.vars.last_y = event.y
        print("Count: " .. game.vars.count)
        return true  -- Request redraw
    end
    
    return false  -- No redraw needed
end

-- Draw the game
function draw()
    -- Clear screen
    renderer.clear(true)
    
    -- Draw title
    renderer.text_scaled(20, 20, "Touch Counter", true, 2)
    
    -- Draw count (centered)
    local count_text = "Count: " .. tostring(game.vars.count)
    renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 10, count_text, true, 2)
    
    -- Draw last touch position
    if game.vars.count > 0 then
        local pos_text = "Last: (" .. tostring(game.vars.last_x) .. ", " .. tostring(game.vars.last_y) .. ")"
        renderer.text_scaled(20, game.height() - 30, pos_text, true, 2)
        
        -- Draw marker at last touch (convert to integers)
        renderer.circle(math.floor(game.vars.last_x + 0.5), math.floor(game.vars.last_y + 0.5), 5, true, false)
    end
    
    -- Draw instructions
    renderer.text_scaled(20, 50, "Tap screen to increment", true, 2)
end
