// File: pickups.agc
// Created: 20-05-05

#constant PICKUP_BOOST		1
#constant PICKUP_SPIN_ONE	2
#constant PICKUP_RESET		3
#constant PICKUP_SPIN_ALL	4

type tPickup
	id			as integer
	typ			as integer
endtype

type tTween
	chain		as integer
	id			as integer
	tweens		as integer[]
endtype

global gPickups	as integer[]

function ProcessPickups(vehicle ref as tVehicle, cell ref as tCell, env ref as tEnvironment)
	i as integer
	d as float
	x as integer
	
	if vehicle.pickup <> 0
		exitfunction
	endif
	
	if cell.pickups.length < 0 
		exitfunction
	endif

	d =  (GetObjectDepth(vehicle.hit) * 0.5) + (GetObjectWidth(env.pickup.id) * 0.5)
	
	for i=0 to cell.pickups.length
		if DistanceXZ(GetObjectPos(vehicle.hit), GetObjectPos(cell.pickups[i].id)) < d
			SetObjectVisible(cell.pickups[i].id, 0)
			SetObjectCastShadow(cell.pickups[i].id, 0)			
			vehicle.pickup = cell.pickups[i].typ
			if vehicle.npc = FALSE
				SetSpriteFrame(env.pickups, vehicle.pickup)
				SetSpriteVisible(env.pickups, 1)
			endif
			exit
		endif
	next
	
endfunction

function LoadPickups(env ref as tEnvironment)
	i 		as integer
	node	as tNode
	dist	as tPoint
	fnode	as tNode
	tnode	as tNode
	look	as integer
	path 	as tPath
	g		as tPoint
	cell	as tCell
	pickup	as tPickup
	t		as tTween
	
#constant LOOK_RIGHT_LEFT		1
#constant LOOK_BACK_FORWARD		2
	
	path = FindPathByName("pickups")
	
	if path.name = "bad"
		exitfunction
	endif
	
	tNode = path.nodes[path.nodes.length]
	fnode = path.nodes[0]
	
	g.x = round(fnode.pos.x / TO_TILE_X) - gGrid.offset.x
	g.y = round(fnode.pos.y / TO_TILE_Y) - gGrid.offset.y
	g.z = round(fnode.pos.z / TO_TILE_Z) - gGrid.offset.z
	
	cell = gGrid.cells[round(g.z), round(g.x), round(g.y)]
	
	dist.x = abs(tNode.pos.x - fNode.pos.x)
	dist.z = abs(tNode.pos.z - fNode.pos.z)
	
	if dist.x > dist.z
		look = LOOK_RIGHT_LEFT
	else
		look = LOOK_BACK_FORWARD
	endif
	
	t.tweens.insert(CreateTweenObject(1))
	t.tweens.insert(CreateTweenObject(1)	)
			
	for i=0 to 3
		pickup.id = CloneObject(env.pickup.id)
		pickup.typ = random(1,4)
		
		SetObjectVisible(pickup.id, 1)
		
		node = path.nodes[i]				
		node.pos.y = node.pos.y + GetObjectHeight(pickup.id) * .8
		
		if look = LOOK_RIGHT_LEFT
			node.pos.x = path.nodes[0].pos.x
		else
			node.pos.z = path.nodes[0].pos.z
		endif			
		
		SetObjectPos(pickup.id, node.pos)
		
		node = path.nodes[4]		
								
		if look = LOOK_RIGHT_LEFT		
			SetObjectLookAt(pickup.id, node.pos.x, GetObjectY(pickup.id), GetObjectZ(pickup.id), 0)
		else
			SetObjectLookAt(pickup.id, GetObjectX(pickup.id), GetObjectY(pickup.id), node.pos.z, 0)
		endif			
				
		SetTweenObjectAngleY(t.tweens[0], GetObjectAngleY(pickup.id) + 15, GetObjectAngleY(pickup.id) - 15, TweenSmooth1())
		SetTweenObjectAngleZ(t.tweens[0], GetObjectAngleZ(pickup.id) - 15, GetObjectAngleZ(pickup.id) + 15, TweenSmooth1())	
		SetTweenObjectAngleX(t.tweens[0], GetObjectAngleX(pickup.id) + 15, GetObjectAngleX(pickup.id) - 15, TweenSmooth1())		

		SetTweenObjectAngleY(t.tweens[1], GetObjectAngleY(pickup.id) - 15, GetObjectAngleY(pickup.id) + 15, TweenSmooth1())
		SetTweenObjectAngleZ(t.tweens[1], GetObjectAngleZ(pickup.id) + 15, GetObjectAngleZ(pickup.id) - 15, TweenSmooth1())	
		SetTweenObjectAngleX(t.tweens[1], GetObjectAngleX(pickup.id) - 15, GetObjectAngleX(pickup.id) + 15, TweenSmooth1())		

		t.chain = CreateTweenChain()		
		t.id = pickup.id
		AddTweenChainObject(t.chain, t.tweens[0], pickup.id, 0)
		AddTweenChainObject(t.chain, t.tweens[1], pickup.id, 0)		
		gTweens.insert(t)		
		gPickups.insert(pickup.id)
		cell.pickups.insert(pickup)		
	next
	
	gGrid.cells[round(g.z), round(g.x), round(g.y)] = cell
	
endfunction