// File: object.agc
// Created: 20-05-13


function	LoadObjWithChildren(no as integer, name as string, image as string, scale as float, shadow as integer)
	names	as string[]
	i 		as integer
	folder	as string
	base	as string
	file	as string
	objs	as tObject[]
	obj		as tObject
	parent	as tObject
	
	folder = GetFolderName(name)
	file = GetFullFileName(name)
	
	names = ProcessModel(folder, file)
	
	if GetFileExists(image)
		obj.image = loadImage(image)
	endif

	parent.id = LoadTextureObject(no+1, folder, GetFullFileName(names[0]), obj.image, scale, shadow)
	parent.name = GetBaseName(names[0])
	objs.insert(parent)
	
	for i=1 to names.length
		obj.id = LoadTextureObject(no+i+1, folder, GetFullFileName(names[i]), obj.image, scale, shadow)
		obj.name = GetBaseName(names[i])
		objs.insert(obj)		
		CenterObject(obj.id)
		FixObjectToObject(obj.id, parent.id)
	next
	
endfunction objs
	
function LoadTextureObject(no as integer, folder as string, name as string, img as integer, scale as float, shadow as integer)
	filename as string
	id		 as integer
	
	filename = folder+"/"+name
	
	id = no
	
	LoadObject(no, filename)
	
	SetObjectScalePermanent(id, scale, scale, scale)
	
	select shadow
		case CAST_SHADOW
			SetObjectCastShadow(id, 1)
			SetObjectReceiveShadow(id, 0)			
		endcase			
		case CAST_RECEIVE_SHADOW
			SetObjectCastShadow(id, 1)
			SetObjectReceiveShadow(id, 1)						
		endcase			
		case RECEIVE_SHADOW			
			SetObjectCastShadow(id, 0)
			SetObjectReceiveShadow(id, 1)						
		endcase
		case NO_SHADOW
			SetObjectCastShadow(id, 0)
			SetObjectReceiveShadow(id, 0)						
		endcase
	endselect
	
	SetObjectImage(id, img, 1)
	
endfunction id

function GetBaseName(s as string)
	r as string	
	i as integer
	
	for i=len(s) to 1 step -1
		if mid(s,i,1) = "/" or mid(s,i,1) = "\" 
			inc i
			exit
		endif
	next
	
	s = mid(s,i, -1)
	
	if i > 0
		s = mid(s, 1, i)
	endif

		
	r = GetStringToken(s, ".", 1)
	
endfunction r

function GetFullFileName(s as string)
	i as integer
	
	for i=len(s) to 1 step -1
		if mid(s,i,1) = "/" or mid(s,i,1) = "\" 
			inc i
			exit
		endif
	next
	
	if i > 0
		s = mid(s, i, -1)
	endif
	
endfunction s

function GetFolderName(s as string)
	i as integer
	
	for i=len(s) to 1 step -1
		if mid(s,i,1) = "/" or mid(s,i,1) = "\" 
			dec i
			exit
		endif
	next
	
	if i > 0
		s = mid(s, 1, i)
	endif
	
endfunction s
