-- defining module parameters

local module = createModule()

module.params.indexNG = 3

-- defining ripples

local ripple

-- ripple particles are spawned in reference to parent particle
local function ripples(parent)

	local part = createMeshPart(ripple)

	part.pos.x = parent.pos.x
	part.pos.y = parent.pos.y + 100
	part.pos.z = parent.pos.z

	part.lifeSpan = 30
	
	part.object = 512
	part.meshIndex = 0
	
        part.tint.r = 40
        part.tint.g = 40 --randomInt(100, 128)
        part.tint.b = 40 --randomInt(100, 128)

	--local scale = randomInt(1, 2)
	part.scale.x = 1.0
	part.scale.y = 1.0
	part.scale.z = 1.0

	
end

-- ripple particle update
local function rippleUpdate(part)

	local t = part.lifeCounter / part.lifeSpan
	if t > 1 then t = 1 end

	local scale = 1.0 + (1 - t) * 1.0
	part.scale.x = scale
	part.scale.y = scale
	part.scale.z = scale

	local tint = 50 - (1 - t) * 30
	part.tint.r = tint
        part.tint.g = tint
        part.tint.b = tint
end

-- create ripple group
ripple = createGroup(nil, rippleUpdate)

-- defining drop group and init functions

local drops

local function dropInit()
    local part = createSpritePart(drops) -- create a particle of the "drop" group

    part.emitterIndex = getTombIndex(module.params.indexNG) -- get Tomb index of moveable

    part.lifeSpan = 30
	
    part.sizeStart = randomInt(20, 128)
    part.sizeEnd = part.sizeStart
    part.sizeCust = part.sizeStart

    part.colStart.r = 128
    part.colStart.g = randomInt(105, 128)
    part.colStart.b = randomInt(105, 128)
    part.colEnd = part.colStart

    -- circular spawning formation
    local ang = degToRad(randomInt(0, 359)) -- get random angle between 0 and 359 degrees, convert to radians
    local r = randomFloat(50, 500) -- circle radius

    local cx = r * cos(ang)
    local cy = r * sin(ang)

    part.pos.x = cx
    part.pos.y = -256
    part.pos.z = cy
	
    -- downward velocity depends on size of drops (bigger is faster)
    part.vel.y = part.sizeStart * 0.50
    --part.vel.y = -5
    --part.accel.y = 4

    part.spriteIndex = 19 -- "drop" sprite texture
    part.fadeIn = 10
    part.fadeOut = 10
   
end

-- drop group init function
local function dropSpawn()
	dropInit()
end

-- drop particle update
local function dropUpdate(part)
	--if part.lifeCounter >= 1 then
		if part.pos.y >= 1664 and part.pos.y - part.vel.y < 1664 then
		--if partCollideFloors(part, 0.0, part.sizeStart, true) then
		ripples(part) -- spawn a ripple particle
		partKill(part)
		end
    --end
end

drops = createGroup(dropSpawn, dropUpdate)