-------------------------------------------------------------------------------
--  Script for mini-game "cards"
-------------------------------------------------------------------------------
require "common"

local c_Skins = { {"oldhouse", "mg_cards_house"}, {"shop", "mg_cards_shop"} }

-- initialization
local nTier         = 1
local c_nRows       = 6
local c_nCols       = 8
local c_WrongCardsShowTime = 1.0

local nSkin = tonumber( get( "skin", "value" ) )
local tSkin = c_Skins[nSkin]
set( "background", "skinpack", tSkin[1] )
set( "card_backside",  "package", tSkin[2] )
set( "card_frontside", "package", tSkin[2] )
set ( "Hint", "enabled", "false")
local c_nPictures   = 24
set ( "FoundCaption", "visible", false )
set ( "FoundAmount", "visible", false )
call( "camera", "render" )
local objCardTemplate = obj( "cardtemplate" )
local objPlayArea     = obj( "playarea" )
local objCards        = obj( "cards" )
delObjects( children( objCards ) )
set ( "TutorialSwitch", "activeidx", "8" )
wait( 0.5 )

local areaX  = tonumber( get( objPlayArea, "left"   ) )
local areaY  = tonumber( get( objPlayArea, "top"    ) )
local areaW  = tonumber( get( objPlayArea, "width"  ) )
local areaH  = tonumber( get( objPlayArea, "height" ) )
local cardW  = tonumber( get( objCardTemplate, "width" ) )
local cardH  = tonumber( get( objCardTemplate, "height" ) )

local nCards     = c_nCols*c_nRows
local nCurCard   = 0
local nCurCardID = 0
local nFlipped   = 0
local bFinished  = false
set( "bcardsgame", "value", "true" )
       
--  smoothly dissolves two given cards
function dissolveCards( cardA, cardB )
    local dt = 0.01
    local allt = 0.3
    local sprA1 = obj( "card_frontside", cardA )
    local sprB1 = obj( "card_frontside", cardB )
    local sprA2 = obj( "card_backside", cardA )
    local sprB2 = obj( "card_backside", cardB )
    for t = 0, allt, dt do
        local transp = (allt - t)/allt 
        set( sprA1, "transparency", transp )
        set( sprA2, "transparency", transp )
        set( sprB1, "transparency", transp )
        set( sprB2, "transparency", transp )
        wait( dt )
    end
    set( cardA, "visible", false )
    set( cardB, "visible", false )
end

--  when card is clicked
function OnCardPress( objCard )
    local id = tonumber( get( objCard, "tag" ) )
    local objFrontSide = obj( "card_frontside", objCard )
    local frameID = tonumber( get( objFrontSide, "startframe" ) )   

    if id == nCurCard then
        do return end
    end
    set( objPlayArea, "enabled", false )
    local objAnmFlip = obj( "flip_card", objCard )
    set( objAnmFlip, "section", "forward" )
    local anmDuration = get( objAnmFlip, "duration" )
    call( objAnmFlip, "play" )
    call( "sndcards", "play" )
    
    if nCurCardID == 0 then
        nCurCardID = frameID
        nCurCard   = id
        set( objPlayArea, "enabled", true )
        wait( anmDuration )
    else
        wait( anmDuration )
        set( objPlayArea, "enabled", true )
        -- retrieve first card
        local objFirstCard = children( objCards )[nCurCard]
        local bWrongCard = (frameID ~= nCurCardID)
        nCurCardID = 0
        nCurCard = 0
        if bWrongCard then
            wait( c_WrongCardsShowTime )
            -- wrong card, flip both of them 
            local objFirstAnmFlip = obj( "flip_card", objFirstCard )
            set( objAnmFlip, "section", "backward" )
            set( objFirstAnmFlip, "section", "backward" )
            call( objAnmFlip, "play" )
            call( "sndcards", "play" )
            call( objFirstAnmFlip, "play" )
            wait( anmDuration )
        else
            -- correct card
            call( "card_right", "play" )
            dissolveCards( objCard, objFirstCard )
            nFlipped = nFlipped + 2
            if nFlipped == nCards then
                bFinished = true
            end
        end
    end
    
end

--  create card id's
local tFrames = {}
local curCardType = 1
for frame = 1, nCards, 2 do
    tFrames[frame] = curCardType
    tFrames[frame + 1] = curCardType
    curCardType = curCardType + 1
    if (curCardType > c_nPictures) then curCardType = 1 end
end
shuffle( tFrames )

--  create cards
for y = 1, c_nRows do
    for x = 1, c_nCols do
        set( objPlayArea, "enabled", false )
        local objCard = create( objCardTemplate, "card" .. x .. y, objCards )
        local cardX = (x - 0.5)*areaW/c_nCols  
        local cardY = (y - 0.5)*areaH/c_nRows
        local objModelInst = obj( "model_inst", objCard )
        set( objCard, "visible", true )
        --  find card's pivot locator, set its position in screen space, convert to world space and set this position to the card's model instance
        local objLocator   = obj( "locator", objCard )
        local locX = cardX + areaX
        local locY = cardY + areaY
        set( objLocator, "position", locX .. "," .. locY .. ",0.5" )
        set( objLocator, "screenspace", "false" )
        set( objModelInst, "position", get( objLocator, "position" ) )
        set( objCard, "left", cardX - cardW/2 )
        set( objCard, "top",  cardY - cardH/2 )
        local objFrontSide = obj( "card_frontside", objCard )
        local idx = x + (y - 1)*c_nCols
        set( objFrontSide, "startframe", tFrames[idx] ) 
        set( objCard, "tag", idx )
        connect( objCard, "press", "OnCardPress" )
    end
end
nCurCard   = 1
for y = 1, c_nRows do
    for x = 1, c_nCols do
        wait ( 0.2 )
        local objFirstCard = children( objCards )[nCurCard]
        local objFirstAnmFlip = obj( "flip_card", objFirstCard )
        set( objAnmFlip, "section", "forward" )
        set( objFirstAnmFlip, "section", "forward" )
        call( objAnmFlip, "play" )
        call( objFirstAnmFlip, "play" )
        wait( anmDuration ) 
        nCurCard = nCurCard + 1
    end
end
nCurCard   = 1
wait ( 5 )
for y = 1, c_nRows do
    for x = 1, c_nCols do
        wait ( 0.2 )
        local objFirstCard = children( objCards )[nCurCard]
        local objFirstAnmFlip = obj( "flip_card", objFirstCard )
        set( objAnmFlip, "section", "backward" )
        set( objFirstAnmFlip, "section", "backward" )
        call( objAnmFlip, "play" )
        call( objFirstAnmFlip, "play" )
        wait( anmDuration ) 
        nCurCard = nCurCard + 1
    end
end

set( objPlayArea, "enabled", true )

--  mini-game loop
while not bFinished do
    wait( 0.5 )
end

--  finish game
set ( "FoundCaption", "visible", true )
set ( "FoundAmount", "visible", true )
set ( "Hint", "enabled", "true")
set( "bcardsgame", "value", "false" )
set( "bcardsgamecomp", "value", "true" )
set( "bcardsgameagain", "value", "true" )
call( obj( "mg_cards" ), "close" )
call( "OnWin", "play" )

