--This script will run on all levels when mod is active.
--Modding documentation: http://teardowngame.com/modding
--API reference: http://teardowngame.com/modding/api.html



function initTempest()
	
	circles = {}

	defaultType = 1
	
	sound = LoadLoop("snd/windLoop.ogg")
	
	defaultDist = 25
	
	circles[#circles + 1] = initCircle(defaultType, defaultDist, 20000, 35, 10, 5)
end

--function tick(dt)
	
--end


function updateTempest(dt)
	local playerPos = GetPlayerCameraTransform().pos
	playerPos[2] = playerPos[2] + 5
	PlayLoop(sound, playerPos, 0.2)
	for i=1, #circles do
		playCircle(circles[i], dt)
	end
end


--function draw(dt)
	
--end

--------------------------------------------------------------------------------

function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

function clearConsole()
	for i=1, 20 do
		DebugPrint("")
	end
end

--Return a random vector of desired length
function randVec(length)
	local v = VecNormalize(Vec(math.random(-100,100), math.random(-100,100), math.random(-100,100)))
	return VecScale(v, length)	
end

--Helper to return a random number in range mi to ma
function randFloat(mi, ma)
	return math.random(1000)/1000*(ma-mi) + mi
end

function rand(minv, maxv)
	minv = minv or nil
	maxv = maxv or nil
	if minv == nil then
		return math.random()
	end
	if maxv == nil then
		return math.random(minv)
	end
	return math.random(minv, maxv)
end

--------------------------------------------------------------------------------

function initCircle(weatherType, distFromPlayer, density, windSpeed, windForce, radius, floorLevel)
	
	floorLevel = floorLevel or false

	local c = {
		weather = weatherType,
		dist = distFromPlayer,
		density = density,
		speed = windSpeed,
		force = windForce,
		floorLevel = floorLevel,
		flash = {
			value = 0,
			default = 1,
			intensity = 1,
			pos = Vec(0, 0, 0)
		},
		radius = radius
	}
	
	return deepcopy(c)
end

function playCircle(circle, dt)
	
	dust(circle, dt)
	flash(circle, dt)
	
end

function initColorByWeather(weather, blackProb)
	-- 1: sandstorm
	-- 2: blizzard
	-- 3: urban
	
	blackProb = blackProb or 0
	
	local color = {
		r = 0,
		g = 0,
		b = 0,
		a = 0.9
	}
	
	if rand(100) / 100 < blackProb then
		return color
	end
	
	if weather == 1 then
		color.r = 0.25
		color.g = 0.14
		color.b = 0.22
	end
	
	return deepcopy(color)
end

function dust(circle, dt)
	local radius = circle.radius
	local life = 0.35
	local count = circle.density * dt
	local vel = circle.speed
	local drag = 0.3
	local gravity = 0
	
	local color = initColorByWeather(circle.weather, 0.1)
	local red = color.r
	local green = color.g
	local blue = color.b
	local alpha = color.a

	local playerPos = GetPlayerCameraTransform().pos
	if circle.floorLevel then
		playerPos = GetPlayerTransform().pos
	end
	local dir = Vec(0, 0, -1)
	
	--Set up the particle state
	ParticleReset()
	ParticleType("plain")
	ParticleRadius(radius)
	ParticleDrag(drag)
	ParticleTile(0)
	ParticleCollide(0) -- ignore collisions
	
	--Emit particles
	for i=1, count do
		
		if rand(10) == 1 then
			ParticleColor(red, green, blue, 0, 0, 0)
		else
			ParticleColor(red, green, blue)
		end
		
		local offset = randVec(circle.dist)
		offset[2] = offset[2] / 2 -- reduce vertical offset
		if circle.floorLevel then
			offset[2] = offset[2] / 10 - 1.2
		end
		local pos = VecAdd(playerPos, offset)
		--Randomize velocity slightly
		local v = 0
		
		SpawnParticle(pos, VecScale(dir, vel), life)
	end
end

function flash(circle, dt)
	circle.flash.value = circle.flash.value - dt
	circle.flash.intensity = circle.flash.intensity * 0.97
	if circle.flash.value <= 0 then
		circle.flash.value = randFloat(0.5, circle.flash.default)
		local playerPos = GetPlayerCameraTransform().pos
		local offset = randVec(circle.dist)
		circle.flash.intensity = 1
		circle.flash.pos = VecAdd(playerPos, offset)
	end
	
	local color = {
		r = 0.3,
		g = 0.8,
		b = 0.2
	}
	PointLight(circle.flash.pos, color.r, color.g, color.b, circle.flash.intensity)
end










