-- Code for the falling leaf particles
local battlePlayer = require("scripts/battlePlayer")
local battleCamera = require("scripts/battleCamera")

local particlePath = Misc.resolveFile("fallingLeaves.ini")

local splitScreenEmitters = {}
local singleScreenEmitter
local lastCameraX
local lastCameraY

function onCameraDraw(camIdx)
    if battleCamera.isSplitScreen() then
        if splitScreenEmitters[camIdx] == nil then
            splitScreenEmitters[camIdx] = Particles.Emitter(0,0,particlePath,10)
            splitScreenEmitters[camIdx]:attachToCamera(Camera(camIdx),true)
        end

        splitScreenEmitters[camIdx]:draw(-4,true)
    elseif (camIdx == 2) == battleCamera.wrapSeamIsVisible then
        if singleScreenEmitter == nil then
            singleScreenEmitter = Particles.Emitter(0,0,particlePath,10)
            --singleScreenEmitter:setParam("space","local")
        end

        local cameraPlayer = battleCamera.getCamerasPlayers(1)[1]
        local playerData = battlePlayer.getPlayerData(cameraPlayer)

        local effectiveCameraX = camera.x - playerData.seamlessWrapOffsetX
        local effectiveCameraY = camera.y - playerData.seamlessWrapOffsetY

        if lastCameraX ~= nil then
            -- Move particles to mimic the movement of the camera
            for _,particle in ipairs(singleScreenEmitter.particles) do
                particle.x = particle.x - effectiveCameraX + lastCameraX
                particle.y = particle.y - effectiveCameraY + lastCameraY
            end
        end

        lastCameraX = effectiveCameraX
        lastCameraY = effectiveCameraY

        singleScreenEmitter:draw(-4,true,nil,false)
    end
end