local savX = 0
local savY = 0
local zarX = 0
local zarY = 0

local savBounceY = 0

function onCreatePost()
    makeAnimatedLuaSprite("middle", "bgs/zardyMaze/middle", 0, 0)
    addAnimationByPrefix("middle", "idle", "middle", 12, true)
    scaleObject("middle", 0.5, 0.5)
    screenCenter("middle")
    setObjectCamera("middle", "hud")
    playAnim("middle", "idle", true)
    addLuaSprite("middle")

    setProperty('middle.x', getProperty('middle.x') - 50)

    setProperty('middle.alpha', 0)

    savX = 150


    -- SAV STUFF
    makeAnimatedLuaSprite("savBorderBack", "bgs/zardyMaze/savborder", 0 + savX - 1000, 0)
    addAnimationByPrefix("savBorderBack", "idle", "red", 24, true)
    scaleObject("savBorderBack", 0.5, 0.5)
    screenCenter("savBorderBack", "y")
    setObjectCamera("savBorderBack", "hud")
    playAnim("savBorderBack", "idle", true)
    addLuaSprite("savBorderBack")

    makeAnimatedLuaSprite("savSprite", "characters/sav", 45 + savX - 1000, 0)
    addAnimationByPrefix("savSprite", "idle", "idle", 24, true)
    addAnimationByPrefix("savSprite", "miss", "miss", 24, false)
    addAnimationByPrefix("savSprite", "pose", "pose", 24, false)
    scaleObject("savSprite", 0.5, 0.5)
    screenCenter("savSprite", "y")
    setObjectCamera("savSprite", "hud")
    addLuaSprite("savSprite")

    makeAnimatedLuaSprite("savBorderStroke", "bgs/zardyMaze/savborder", 0 + savX - 1000, 0)
    addAnimationByPrefix("savBorderStroke", "idle", "border", 24, true)
    scaleObject("savBorderStroke", 0.5, 0.5)
    screenCenter("savBorderStroke", "y")
    setObjectCamera("savBorderStroke", "hud")
    addLuaSprite("savBorderStroke")
  
    savBounceY = getProperty("savSprite.y")


    zardyX = 650


    -- ZARDY STUFF
    makeAnimatedLuaSprite("zarBorderBack", "bgs/zardyMaze/zardyborder", 0 + zardyX + 1000, 0)
    addAnimationByPrefix("zarBorderBack", "idle", "red", 24, true)
    scaleObject("zarBorderBack", 0.5, 0.5)
    screenCenter("zarBorderBack", "y")
    setObjectCamera("zarBorderBack", "hud")
    playAnim("zarBorderBack", "idle", true)
    addLuaSprite("zarBorderBack")

    makeAnimatedLuaSprite("zardySprite", "characters/zardy", 45 + zardyX + 1000, 0)
    addAnimationByPrefix("zardySprite", "idle", "idle", 24, true)
    addAnimationByPrefix("zardySprite", "pose", "pose", 24, false)
    scaleObject("zardySprite", 0.5, 0.5)
    screenCenter("zardySprite", "y")
    setObjectCamera("zardySprite", "hud")
    addLuaSprite("zardySprite")

    makeAnimatedLuaSprite("zarBorderStroke", "bgs/zardyMaze/zardyborder", zardyX + 1000, 0)
    addAnimationByPrefix("zarBorderStroke", "idle", "border", 24, true)
    scaleObject("zarBorderStroke", 0.5, 0.5)
    screenCenter("zarBorderStroke", "y")
    setObjectCamera("zarBorderStroke", "hud")
    addLuaSprite("zarBorderStroke")

    zarBounceY = getProperty("zardySprite.y")
end


function tweenIn()

    savX = 150
    doTweenX('savBorderBackTween','savBorderBack',savX,0.7, 'cubeOut')
    doTweenX('savSpriteTween','savSprite',savX + 45,0.7, 'cubeOut')
    doTweenX('savBorderStrokeTween','savBorderStroke',savX,0.7, 'cubeOut')

    zardyX = 650
    doTweenX('zarBorderBackTween','zarBorderBack',zardyX,0.7, 'cubeOut')
    doTweenX('zardySpriteTween','zardySprite',zardyX + 45,0.7, 'cubeOut')
    doTweenX('zarBorderStrokeTween','zarBorderStroke',zardyX,0.7, 'cubeOut')

end

function tweenOut()

    doTweenAlpha('middle','middle', 0, 0.4)

    savX = 150 - 1000
    doTweenX('savBorderBackTween2','savBorderBack',savX,0.7 * 2, 'cubeIn')
    doTweenX('savSpriteTween','savSprite',savX + 45,0.7 * 2, 'cubeIn')
    doTweenX('savBorderStrokeTween','savBorderStroke',savX,0.7 * 2, 'cubeIn')

    zardyX = 650 + 1000
    doTweenX('zarBorderBackTween','zarBorderBack',zardyX,0.7 * 2, 'cubeIn')
    doTweenX('zardySpriteTween','zardySprite',zardyX + 45,0.7 * 2, 'cubeIn')
    doTweenX('zarBorderStrokeTween','zarBorderStroke',zardyX,0.7 * 2, 'cubeIn')
end


function onTweenCompleted(tag, vars)
    if tag == 'savBorderBackTween' then
        doTweenAlpha('middle','middle', 1, 0.3)
    end
end

function onEvent(event, value1, value2, strumTime)
    if event == '' and value1 == 'portrait' then
        tweenIn()
    end

    if event == '' and value1 == 'portraitGo' then
        tweenOut()
    end
end


function opponentNoteHit(index, noteData, noteType, isSustain)
    playAnim("zardySprite", "pose", true, false)
    setProperty("zardySprite.y", zarBounceY + 10)
    doTweenY("zardyBounce", "zardySprite", zarBounceY, 0.5, "quintout")
end

function goodNoteHit(index, noteData, noteType, isSustain)
    playAnim("savSprite", "pose", true, false)
    setProperty("savSprite.y", savBounceY + 10)
    doTweenY("savBounce", "savSprite", savBounceY, 0.5, "quintout")
end

function noteMiss(index, noteData, noteType, isSustain)
    playAnim("savSprite", "miss", true, false)
end


local BFOldCurAnim = ""
local BFNewCurAnim = ""

local dadOldCurAnim = ""
local dadNewCurAnim = ""

local function returnBFCurAnim()
    BFOldCurAnim = getProperty('boyfriend.animation.curAnim.name')
    if BFOldCurAnim ~= BFNewCurAnim then
        return BFOldCurAnim
    end
    BFNewCurAnim = BFOldCurAnim
end

local function returnDadCurAnim()
    dadOldCurAnim = getProperty('dad.animation.curAnim.name')
    if dadOldCurAnim ~= dadNewCurAnim then
        return dadOldCurAnim
    end
    dadNewCurAnim = dadOldCurAnim
end

function onUpdatePost(elapsed)
    if returnBFCurAnim() == "idle" then
        playAnim("savSprite", "idle", true, false)
    end
    if returnDadCurAnim() == "idle" then
        playAnim("zardySprite", "idle", true, false)
    end
end

function onBeatHit()
    if curBeat == 301 then
        if not middlescroll then
            noteTweenX("playerNoteXMiddle1", 4, 420, 0.85, "sineinout")
            noteTweenX("playerNoteXMiddle2", 5, 530, 0.85, "sineinout")
            noteTweenX("playerNoteXMiddle3", 6, 640, 0.85, "sineinout")
            noteTweenX("playerNoteXMiddle4", 7, 750, 0.85, "sineinout")
        end
        makeLuaSprite("blackout", nil, 0, 0)
        setProperty("blackout.alpha", 0)
        makeGraphic("blackout", 1280, 720, "000000")
        setObjectCamera("blackout", "hud")
        doTweenAlpha("FadeIn", "blackout", 0.5, 1, "sineinout")
        setObjectOrder("blackout", 0)
        addLuaSprite("blackout")
    end

    if returnBFCurAnim() == "idle" then
        setProperty("savSprite.y", savBounceY + 10)
        doTweenY("savBounce", "savSprite", savBounceY, 0.5, "quintout")
    end
    if returnDadCurAnim() == "idle" then
        setProperty("zardySprite.y", savBounceY + 10)
        doTweenY("zardyBounce", "zardySprite", savBounceY, 0.5, "quintout")
    end
end