-- Debugging printf function. :3
function debug(...)
	print(string.format(...))
end

-- Random "blinking" animation delays
function blink()
	if (obj.duration() <= 9999) then
		return
	elseif (math.floor(obj.life()%(TICKRATE/2)) ~= 0) then
		return
	elseif (math.floor(math.random(4)-1) ~= 0) then
		return
	end
	obj.frame(obj.frame()+1)
end

-- Gravity
function gravity()
	local hit = obj.do_collisions(true)

	-- Gravity
	if (not obj.nogravity()) and (not obj.onfloor()) and (obj.momy() < 3.5*FRACUNIT) then
		if (obj.momy() < -FRACUNIT/6) then
			obj.momy(obj.momy() + math.abs(obj.momy()/10))
			--obj.momy(obj.momy() + math.abs(obj.momy()/16))
		elseif (obj.momy() < 0) then
			obj.momy(0)
		else
			obj.momy(obj.momy() + FRACUNIT/10)
			--obj.momy(obj.momy() + 4*FRACUNIT/TICKRATE)
		end
	end
	obj.nogravity(false)

	-- Friction (Not really a part of gravity, but oh well)
	if (not obj.nofriction()) and (obj.onfloor()) then
		if (obj.momx() > 0) then
			obj.momx(obj.momx()-FRACUNIT/10)
			if (obj.momx() < 0) then
				obj.momx(0)
			end
		elseif (obj.momx() < 0) then
			obj.momx(obj.momx()+FRACUNIT/10)
			if (obj.momx() > 0) then
				obj.momx(0)
			end
		end
	end

	return hit
end
