local path = "beanbg/"

function onCreate()
    makeLuaSprite('bg', 'beanbg/bgl', -850, -430)
  	scaleObject("bg", 1.6, 1.6)
	addLuaSprite('bg', false);

	addBGDoki("syor", "Syor BG")
	setObjectPosition("syorbg", 20, 250)
   
    addBGDoki("monker", "Monker BG")
	setObjectPosition("monkerbg", -300, 300)

    addBGDoki("yur", "Yur BG")
    setObjectPosition("yurbg", 1070, 250)

    addBGDoki("natki", "Natki BG")
    setObjectPosition("natkibg", 1340, 330)
end

bgDokis = {}
speedMod = 1

function onBeatHit()
    if curBeat % (getProperty('gfSpeed') * speedMod) == 0 then  
        if #bgDokis > 0 then
            for i = 1, #bgDokis do
                objectPlayAnimation(bgDokis[i], "idle", true)
            end
        end
    end
end

function setObjectPosition(tag, x, y)
	setProperty(tag..'.x', x)
	setProperty(tag..'.y', y)
end

function addBGDoki(char, animName)
    makeAnimatedLuaSprite(char.."bg", path..char.."_bg", 0, 0)
    addAnimationByPrefix(char.."bg", "idle", animName, 24, false)
    addOffset(char.."bg", "idle", getProperty(char.."bg.offset.x"), getProperty(char.."bg.offset.y"))
    setScrollFactor(char.."bg", 1, 0.9)
    scaleObject(char.."bg", 0.95, 0.95)
    addLuaSprite(char.."bg", false)

    table.insert(bgDokis, char.."bg")
end

curDad = ""
curBF = ""

-- runs a check whenever a character changes to see if any of the dokis are currently singing. if they are, their respective background sprite will be invisible.

function onUpdate()
    if (curDad ~= getProperty("dad.curCharacter")) or (curBF ~= getProperty("boyfriend.curCharacter")) then
        curDad = getProperty("dad.curCharacter")
        curBF = getProperty("boyfriend.curCharacter")

        if #bgDokis > 0 then
            for i = 1, #bgDokis do
                setProperty(bgDokis[i]..".alpha", 1)
                local charName = string.sub(bgDokis[i], 0, #bgDokis[i] - 2)

                if string.match(getProperty("dad.curCharacter"), charName) or string.match(getProperty("boyfriend.curCharacter"), charName) then
                    setProperty(bgDokis[i]..'.alpha', 0)
                end
            end
        end        
    end
end