local bolts
local sparks

local module = createModule()
module.params.indexNG = -1
module.params.shockwaveSize = 1.0

local function boltInit()
    local part = createSpritePart(bolts)

    part.emitterIndex = getTombIndex(module.params.indexNG)

    part.lifeSpan = 150
    part.fadeIn = 30
    part.fadeOut = 30

    part.spriteIndex = 14

    part.sizeStart = randomInt(120, 160)
    part.sizeEnd = part.sizeStart * 0.4

    part.colStart.g = 255
	part.colStart.b = 255
	part.colEnd.b = 255

    part.pos.x = randomFloat(-256, 256)
    part.pos.y = randomFloat(-128, 128)
    part.pos.z = randomFloat(-256, 256)
	
	part.vel.x = randomFloat(-30, 30)
	part.vel.z = randomFloat(-30, 30)
	part.vel.y = randomFloat(-140, -110)
	part.accel.y = 6
end

local function sparkInit(parent)
	local part = createSpritePart(sparks)
	
	part.lifeSpan = 20
	part.fadeIn = 5
	part.fadeOut = 15
	
	part.sizeStart = randomInt(30, 70)
	part.sizeEnd = part.sizeStart
	
	part.colStart = parent.colCust
	part.colEnd = parent.colCust
	
	local size = parent.sizeCust * 0.5
	part.pos.x = parent.pos.x + randomFloat(-size, size)
	part.pos.y = parent.pos.y + randomFloat(-size, size)
	part.pos.z = parent.pos.z + randomFloat(-size, size)
	
	part.vel = parent.vel * 0.5
end
	
local function boltUpdate(part)
    if randomInt(1, 2) == 1 then
		sparkInit(part)
	end

	partCollideWalls(part, 1)
	if partCollideFloors(part, 0.8, part.sizeCust * 0.5, false) then
		if part.t < 0.8 and abs(part.vel.y) > 45 then
			local size = module.params.shockwaveSize
			local speed = clamp(abs(part.vel.y), 45, 130) * module.params.shockwaveSize
			local flags = SHOCKWAVE_DEADLY + SHOCKWAVE_EXTRA_DAMAGE
			triggerShockwave(part.pos.x, part.pos.y, part.pos.z, 16, 80, speed * 0.67, 15, part.colCust.r, part.colCust.g, part.colCust.b, 0, flags)
		end
	end
end

local timer = 0

local function spawnBolts()
	if module.params.indexNG < 0 then
		return
	end
	
	local item = getItemInfo(getTombIndex(module.params.indexNG))
	if item.triggered then
		if timer == 0 then
			timer = randomInt(10, 30)
			boltInit()
		else
			timer = timer - 1
		end
	end
end

bolts = createGroup(spawnBolts, boltUpdate)
sparks = createGroup(nil, nil)
sparks.drawMode = DRAW_LINE