// File: Custom.agc
// Created: 20-04-23

type tVanColours
	primary	as integer
	trim		as integer
endtype

function CustomizeVehicle(e ref as tEnvironment)
	obj as integer
	light as integer
	id as integer
	set as integer
	scale as float
	v as tVehicle
	rgb as tRGB
	vanc as tVanColours
	path as tPath

	v = LoadVehicle("test", "test.png", "custom", 500)
	SetupVehicleLighting(v)
	path = FindPath("player")
	v.colour = path.colour
	SetObjectColor(v.chassis, v.colour.r,  v.colour.g,  v.colour.b, 0xff)
	
	e.custom.panel = CreateSprite(LoadImage("customize/customize.png"))
	scale = GetVirtualHeight() / GetSpriteHeight(e.custom.panel)
	SetSpriteSize(e.custom.panel, GetSpriteWidth(e.custom.panel) * scale, GetSpriteHeight(e.custom.panel) * scale)	
	SetSpritePositionByOffset(e.custom.panel, GetScreenBoundsLeft() + (GetScreenBoundsRight() - GetScreenBoundsLeft()) * 0.5, GetVirtualHeight() * 0.5)

	e.custom.set =  LoadObjectAndTexture("customize/set.obj", "customize/set.png", 0, 0, 0, RECEIVE_SHADOW)		
	SetObjectPosition(e.custom.set.id, 50000, 0, 0)
	SetObjectColor(e.custom.set.id, v.colour.r, v.colour.g, v.colour.b, 0xff)

	SetObjectRotation(v.hit, 0, 130, 0)
	
	SetCameraPosition(1, 50000, 200, -400)
	SetCameraLookAt(1, 50000, 0, 200, 0)	

	e.window.x = 1
	Resize(v, e, scale)

	SetupRumble(v, e)

	SetupSliders(v, e, scale)
	
	vanc = SetupVanColours(v, e, scale)
	
	ColorPickerInit(830 * scale, 370 * scale)
	sync()
	
	SetRGB(v.colour.r, v.colour.g, v.colour.b)
	
	repeat
		Resize(v, e, scale)
		RotateObjectLocalY(v.hit, .5)
		PlayTweenChain( e.custom.rumble[0] )

		UpdateAllTweens(GetFrameTime())		
		
		rgb = ColorPicker()
		
		if rgb.r <> v.colour.r or rgb.g <> v.colour.g or rgb.b <> v.colour.b
			SetObjectColor(v.chassis, rgb.r, rgb.g, rgb.b, 0xff)
			SetObjectColor(e.custom.set.id, rgb.r, rgb.g, rgb.b, 0xff)		
			SetSpriteColor(vanc.primary, rgb.r, rgb.g, rgb.b, 0xff)		
		endif
		
		sync()	
	until GetRawKeyPressed(KEY_ESCAPE)
	
	DeleteVehicle(v)
	DeletePicker()
	e.custom.panel = DestroySprite(e.custom.panel)
	DestroyObjectandTexture(e.custom.set)	
	
	repeat
		DestroySprite(e.ids[0])
		e.ids.remove(0)		
	until e.ids.length < 0
	
	DeleteTween(e.custom.rumble[1])
	DeleteTween(e.custom.rumble[2])	
	DeleteTweenChain(e.custom.rumble[0])		
	e.custom.rumble.length = -1
endfunction


function	SetupSliders(v ref as tVehicle, e ref as tEnvironment, scale as float)
	i as integer
	id as integer
	s as tSlider
	p as tPoint2D
	
	id = LoadImage("customize/slider.png")
	
	s.range.min = 1060
	s.range.max = 1320
	
	for i=0 to 3
		s.id = CreateSprite(id)
		e.ids.insert(s.id)
		select i
			case 0
				p = GetPointFromSprite(e.custom.panel, s.range.min * scale, 60 * scale)
				SetSpritePositionByOffset(s.id, p.x, p.y)
			endcase
			case 1
				p = GetPointFromSprite(e.custom.panel, s.range.min * scale, 146 * scale)
				SetSpritePositionByOffset(s.id, p.x, p.y)
			endcase
			case 2
				p = GetPointFromSprite(e.custom.panel, s.range.min * scale, 230 * scale)
				SetSpritePositionByOffset(s.id, p.x, p.y)
			endcase
			case 3
				p = GetPointFromSprite(e.custom.panel, s.range.min * scale, 315 * scale)
				SetSpritePositionByOffset(s.id, p.x, p.y)				
			endcase			
		endselect
		e.custom.sliders.insert(s)
	next	
	
endfunction

function	SetupVanColours(v ref as tVehicle, e ref as tEnvironment, scale as float)
	i		as integer
	p 		as tPoint2D
	name 	as string
	id		as integer
	vanc 	as tVanColours
	
	p = GetPointFromSprite(e.custom.panel, 933 * scale, 373 * scale)
	
	for i=1 to 3
		id = CreateSprite(LoadImage("customize/vanmap000"+str(i)+".png"))
		e.ids.insert(id)
		SetSpriteScale(id, scale, scale)
		SetSpritePosition(id, p.x, p.y)	
		select i	
			case 1
				SetSpriteColor(id, v.colour.r, v.colour.g, v.colour.b, 0xff)
				vanc.primary = id
			endcase
			case 3
				vanc.trim = id				
			endcase
		endselect
	next
endfunction vanc


function Resize(v ref as tVehicle, e ref as tEnvironment, scale as float)
	p as tPoint2D
	d as float
	n as tPoint
	
	if e.window.x = GetWindowWidth() and e.window.y = GetWindowHeight()
		exitfunction
	endif
	
	e.window.x = GetWindowWidth() 
	e.window.y = GetWindowHeight() 	

	SetCameraPosition(1, 50000, 200, -(GetWindowWidth() / 3))
	SetCameraLookAt(1, 50000, 0, 200, 0)	
				
	p = GetPointFromSprite(e.custom.panel, 673 * scale, 500 * scale)
	
	n = Get3DPointFromScreen((GetWindowWidth() / 3), p)	
	SetObjectPos(v.hit, n)
	
	SetObjectPosition(e.custom.set.id, GetObjectX(v.hit), GetObjectY(v.hit), GetObjectZ(v.hit))
	
endfunction

function SetupRumble(v as tVehicle, e ref as tEnvironment)
	
	if e.custom.rumble.length < 0
		e.custom.rumble.insert(CreateTweenChain())
		e.custom.rumble.insert(CreateTweenObject(0.03))
		SetTweenObjectAngleZ(e.custom.rumble[1], -1, 1, TweenBounce())
		AddTweenChainObject(e.custom.rumble[0], e.custom.rumble[1], v.chassis, 0 ) 		
		e.custom.rumble.insert(CreateTweenObject(0.03))
		SetTweenObjectAngleZ(e.custom.rumble[2], 1, -1, TweenBounce())
		AddTweenChainObject( e.custom.rumble[0], e.custom.rumble[2], v.chassis, 0 ) 				
	endif
endfunction

function	Get3DPointFromScreen(depth as float, p as tPoint2D)
	f 		as tPoint
	t 		as tPoint
	w		as tPoint
	dot 		as float
	r		as integer
	id		as integer
	
	f.x = Get3DVectorXFromScreen( p.x, p.y )
	f.y = Get3DVectorYFromScreen( p.x, p.y )
	f.z = Get3DVectorZFromScreen( p.x, p.y )

	// get a vector that points in the direction the camera is looking
	t.x = Get3DVectorXFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
	t.y = Get3DVectorYFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
	t.z = Get3DVectorZFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )

	// normalise Vector in the direction of Vector2
	dot = f.x*t.x + f.y*t.y + f.z*t.z
	
	if ( dot > 0 )
		f.x = f.x / dot
		f.y = f.y / dot
		f.z = f.z / dot
	endif
	
	w.x = f.x * depth + GetCameraX(1)
	w.y = f.y * depth + GetCameraY(1)
	w.z = f.z * depth + GetCameraZ(1)

endfunction w
	