voxFile = "creativemode.vox"     -- This is the default creative mode vox file
materials = {153, 154, 155, 156} -- Material indices in the vox file

sculptures = {}
timer = 0.5


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


--Create a new shape and sculpture parameters
function initSculpture()
	local s = {}
	local pos = Vec(rnd(-5, 5), 0, rnd(-5, 5))
	s.center = Vec(15, 0, 15)
	s.angle = rnd(0, 2*math.pi)
	s.previous = VecCopy(s.center)
	s.age = 0
	s.life = rnd(20, 100)
	s.material = materials[math.floor(rnd(1, #materials+1))]
	--Create new shape on the default, static world body using the palette of voxFile
	s.shape = CreateShape(GetWorldBody(), Transform(pos), voxFile)
	--Set up the shape size by providing min/max coordinates
	ResizeShape(s.shape, -15, 0, -15, 15, s.life, 15)
	return s
end


--Draw voxels into sculpture shape
function drawSculpture(s)
	local size = (1.0-s.age/s.life)*10
	local ang = s.angle + s.age*0.2
	local pos = VecAdd(s.center, Vec(math.cos(ang)*5, s.age, math.sin(ang)*5))

	--Set brush material, which is used for the next call to DrawShapeLine
	SetBrush("sphere", size, s.material)
	--Draw a voxelized line into shape using the previously defined brush
	DrawShapeLine(s.shape, s.previous[1], s.previous[2], s.previous[3], pos[1], pos[2], pos[3])

	s.previous = VecCopy(pos)
	s.age = s.age + 1
end


function tick(dt)
	--Tick down timer and create new sculture when it reaches zero
	timer = timer - dt
	if timer < 0 and #sculptures<32 then
		sculptures[#sculptures+1] = initSculpture()
		timer = 0.5
	end

	--Tick existing scultures unless they are already done
	for i=1, #sculptures do
		local s = sculptures[i]
		if s.age < s.life then
			drawSculpture(s)
		end
	end
end
