globals = {
-- 	camera = { x = 0, y=2.2, z=8 },
 	camera = { x = 0.0, y=-20.8, z=48.1 },
     fov = 25,
	target = { x = 4, y=0.8, z=0 },
	light0 = {   	direction = {x=0, y=0.0, z=1 }, 
				color = {x=1, y=1, z=1}, 
			},
	ambient = { x = 0, y = 0, z = 0 },
     raymarch_steps = 30,
	raymarch_shadow_steps = 4,
 	up = { x = 0, y=1, z=0 },
	sky = 0,
	shadow_scale = 1,
	isPaused = 0
	
}

output = {
	resolution = { width = 160, height=200},
	-- Allow for dithering (2)	
	dither = 0,
	-- set dither strength 30
	ditherStrength= {x=30, y=2, z=2},
	
	-- Enable c64 output
	output_type = 1, -- c64 image type
     aspect = 1,
     -- Set character set
	c64_imageType = 0, -- 0 = character set (4 colors, compressable), 1 = multicolor image 
	c64_multicolor = 1,
	-- Define the 4 colors (black, purple, white, blue)
--	index_colors = { 0,4,1,6 }
--	c64_colors = { 0,5,14,6 }
	index_colors = { 0,1,2,6 }
}


MaterialBlue = {
   color = { x=0, y=0, z=100 },
	reflection = 0.0,
	shininess = 50,
	shininess_intensity = 0 
}

MaterialRed = {
   color = { x=100, y=0, z=0 },
	reflection = 0.0,
	shininess = 50,
	shininess_intensity = 0 
}

MaterialWhite = {
   color = { x=100, y=100, z=100 },
	reflection = 0.0,
	shininess = 50,
	shininess_intensity = 0 
}

MaterialGreen = {
   color = { x=0, y=1, z=0 },
	reflection = 0.0,
	shininess = 50,
	shininess_intensity = 0 
}



MaterialBlack = {
   color = { x=0, y=0.0, z=0 },
	reflection = 0.5,
	shininess = 50,
	shininess_intensity = 0 
}


function getX(tx) 
	
	tx2  =(tx-2) * 0.5
--	return -tx*tx +tx*3+5  + tx2*tx2*tx2*0.2
--	return -tx*10
--	return math.cos(tx*0.3)

	return tx*1-12 - tx2*tx2*tx2 +5;

--	return math.cos(tx*0.9-0.5)*rad*1.2 +(tx*0)  -- last
end

function getY(ty) 
	ty2=ty-5.20

	return ty2*10;
end

function getDeriv(td)
	d = 0.2
	dx = (getX(td+d) - getX(td-d))/(2*d) 
	dy = (getY(td+d) - getY(td-d))/(2*d)

	
	l = math.sqrt(dx*dx + dy*dy)
	if (l>0.01)  then
		dx = dx / l
		dy = dy / l
	end
	
--	return {DX = dx, DY = dy} 

end


-- Init is called on start  
 

N = 6
distance = 10
frame = 0
--noFrames = 24
noFrames = 4
char = 0
curSave = 1

hasSaved = 0

text = "PRX"

charname = "char"
time = 0
function Init()

	N=30
	sx=0.45
	sy =0.3
	for jj=0,N-1 do
		tt = -jj/N*3.9 - time*0.07+7.2 +0.2
		xx = getX(tt)
		yy = getY(tt)
		getDeriv(tt);		
		zz = 0
	
		spread = 0.7
	--	sy = 2*(-cos(jj/5)*1+1)*0.2

--		spread = 1
		for i=0,6 do 
			s = 2.75 * spread
			x = xx + dy*i*(s) -dy*0
			y = yy - dx*i*(s) +dx*0
						
			mat = "MaterialBlue"
			kk = i+jj
			if (kk%2==0) then 
				mat = "MaterialWhite" 
			end
			name = "l1"..tostring(jj).."_"..tostring(i)
			z = -10
			AddObject("box" ,name,"",mat,x,y,z, sx,sy,0.0,0,0);
			t = sin(xp)/3.14169*360
--			SetRotation(name,0,0,math.atan2(dx,dy)/2/3.14159*360);
		
			


		end	
	end

	UpdateScene()

end



function UpdateScene()

  	t = ((frame)/noFrames) 

--	globals.camera.x = t*2
--	globals.target.x = t*2
	

	j=t*360

end

pos = 0x0400

function Update()
	
	if (hasSaved == 0) then 
		for y=0,24 do
			for x=0,39 do
				AddC64LineToData(x,y*8,1,8)
			end
		end

	end

	frame=frame+1



	if (frame<noFrames) then 
		SaveRawData("data/ccycle_tst_".. tostring(frame) ..".bin");
		Save2DInfo("data/ccycle_tst_2d".. tostring(frame) ..".bin",
pos,4,"data/cycle_tst_unroll".. tostring(frame)..".ras");
		pos = pos + 0x4000
		Message("SAVED "..tostring(frame))
	end	
	
	UpdateScene()
	time=time-0.8
	ClearAllObjects();
	Init();
	
	


end
