vehiclewheel = {}
setmetatable(vehiclewheel,{__index = entity})

function vehiclewheel.new (name,space,x,y,z,rad,width,angle,matid,mass) 
	local self = entity.new("vehiclewheel",x,y,z)
	setmetatable(self,{__index=vehiclewheel})
	self.defaultspace = space
	self.name = name
	self.l3d = self:addl3d("vehicles/"..name.."_wheel.f3d")
	self.geom,self.cyl = self:addgeom(dgeomtransform.new(),matid,dgeomcylinder.new(rad,width))
	self.cyl:rotrad(0,math.pi*0.5,0)
	self:makebody()
	self.actor:rotrad(0,0,angle)
	self.body:masscylinder(mass,1,rad,width)
	return self
end

vehicle = {}
setmetatable(vehicle,{__index = entity})

function vehicle.new (name,x,y,z, sizex,sizey,sizez, axis,matid,mass) 
	local self = entity.new("vehicle",x,y,z)
	setmetatable(self,{__index=vehicle})
	self.name = name
	self:makesubspace()
	self.l3d = self:addl3d("vehicles/"..name..".f3d",0,0,sizez*1.5)
	self.geom = self:addgeom(dgeomtransform.new(),matid,dgeombox.new(sizex,sizey,sizez),0,0,sizez*4)
	self.subspace:collidetest(false)
	self:makebody()
	self.body:massbox(mass or 0.01,sizex,sizey,sizez)
	--self.body:masstranslate(0,0,-sizez*2)
	self.wheels = {}
	self.steerwheels = {}
	self.body:autodisable(false)
	self.nosteer = {}
	z = z + sizez*1.5
	for i,v in ipairs(axis) do
		if (not v.steer) then self.nosteer[i] = true end
		self:addwheel(x-v.axiswidth*0.5,y+v.pos,z+v.height , v.rad,v.wheelwidth,0,v.steer or 0.01,v.matid or 0,v.f1max,v.f2max,v.mass or 0.01)
		self:addwheel(x+v.axiswidth*0.5,y+v.pos,z+v.height , v.rad,v.wheelwidth,math.pi,v.steer or 0.01,v.matid or 0,v.f1max,v.f2max,v.mass or 0.01)
	end
	
	return self
end

function vehicle.think (self)
	--self.body:addforce(0,10,0,0,0,0)
	local fx,fy,fz =(Keyboard.isKeyDown(Keyboard.keycode.LEFT) and -1 or 0) + (Keyboard.isKeyDown(Keyboard.keycode.RIGHT) and 1 or 0),
		 			(Keyboard.isKeyDown(Keyboard.keycode.UP)	and 1 or 0) + (Keyboard.isKeyDown(Keyboard.keycode.DOWN) and -1 or 0),
		 			0
	for i,v in pairs(self.jointlist) do 
		--v:fmax2(10)
		local vv = v:anglerate2()
		v:velocity2(fy*0.2+vv*0.5)
		local a = v:angle1()
		local vv = v:anglerate1()
		v:velocity(fx*0.01-a*0.01)
	end
end

function vehicle.addwheel (self,x,y,z,rad,width,angle,steer,matid,f1max,f2max,mass)
	local wheel = vehiclewheel.new(self.name,self.subspace,x,y,z,rad,width,angle,matid,mass)
	local hinge = self:addjoint(djointhinge2.new())
	hinge:attach(self.body,wheel.body)
	hinge:axis1(0,0,1)
	hinge:axis2(1,0,0)
	hinge:anchor(x,y,z)
	hinge:fmax(f1max or 2)
	hinge:fmax2(f2max or 0.5)
	hinge:lostop(-steer)
	hinge:histop(steer)
	
--	hinge:cfm(0.0009)
--	hinge:stoperp(0.4)
--	hinge:stopcfm(0.0000001)
	hinge:suspensionerp(0.04)
	hinge:suspensioncfm(0.7)
	--hinge:velocity(0.01)
	table.insert(self.wheels,wheel)
end

dworld.surfacecombination(0,5,4)
dworld.surfacemu(4,dworld.dinfinity())
dworld.surfacebitsofterp(4,0.8)
--dworld.surfacebitslip1(4,true)
--dworld.surfaceslip1(4,1)

dworld.surfacecombination(0,6,5)
dworld.surfacemu(5,10)

function cartest ()

	particlesys.load("chimney.prt"):clearparticles()
	particlesys.load("chimneyhappy.prt"):clearparticles()
	particlesys.load("laternenfall.prt"):clearparticles()
	
	
	vehiclelevel = levelmap.load("test.lvl.lua")
	local player = vehicle.new("pickup1",0,-25,50, 20,65,6,
		{{axiswidth=19.6, pos=-20, height = -7,rad=5,wheelwidth=1.8,steer = 0.0,matid=5, f1max=0,f2max=0.5, mass=0.001},
		 {axiswidth=19.6, pos= 20, height = -7,rad=5,wheelwidth=1.8,steer=0.7,matid=5,f1max=1,f2max=0.0001, mass=0.001}},
		 6,0.007)
	player:addgeom(dgeomtransform.new(),6,dgeombox.new(26,40,5),0,0,10)
	space:add(vehiclelevel.space)
	space:add(entity.space)
	l3dcamera.default():frontplane(4)
	
	local cam = followcamera.new(
		function (a,b) 
			local x,y,z = player.body:local2world(0,-180,300)
			local a,b,c = player.body:pos()
			return x,y,c+150
		end,{cam,player},
		player.body.local2world,{player.body,0,0,0},0.1,0,0)
	
	
	local t = luxinia.time()
	local lift = 0
	Keyboard.setKeyBinding(Keyboard.keycode.LCTRL,function () 
		lift = t + 300
	end)
		
	function xmas () 
		local cnt = 0
		while (t<luxinia.time() and cnt<10) do
			if (lift>t) then
				player.body:addforce(0,0,10,1,1,20,true)  
				local x,y,z = player.body:angvel()
				player.body:angvel(x*0.5,y*0.5,z*0.5)	
				x,y,z = player.body:velocity()
				player.body:velocity(x*0.8,y*0.8,z*0.8)
			end
			entity.thinkall()
	
			dworld.collidetest(space)
			dworld.makecontacts()	
			dworld.quickstep(0.5)
	
			t = t + 3
			cnt = cnt +1
		end
		if (cnt==10) then t = luxinia.time() end
	end
	
	Timer.set("xmas",xmas)
end

function cartestend ()
	particlesys.load("chimney.prt"):deletall()
	particlesys.load("chimneyhappy.prt"):deletall()
	particlesys.load("laternenfall.prt"):deletall()
	
	Keyboard.removeKeyBinding(Keyboard.keycode.LCTRL)
	Timer.remove("xmas")
	vehiclelevel:cleanup()
	entity.removeall()
	vehiclelevel = nil
	initmenubackground ()
	menu:activate()
end