' ------------------------------------------------------------------------------------------------
' Quad Type
' ------------------------------------------------------------------------------------------------
Type TQuad

	Field id:String = ""									' unique identifier

	Field x:Float = 0.0										' position x
	Field y:Float = 0.0										' position y
	Field z:Float = 0.0										' position z
	
	Field sx:Int = 0										' screen position X
	Field sy:Int = 0										' screen position Z
	
	Field p:Float
	Field c:Float
	Field n:Float

	Field hash:String

	Field size:Float = 1.0									' base size
	Field scalex:Float = 1.0								' current size X
	Field scaley:Float = 1.0								' current size Y
	Field distance:Float = 0.0								' distance to cam
	Field hidden:Int = 0									' hidden flag
	
	Field RGB:Int[] = [255, 255, 255]						' star vertex color

	Field RGB1:Int[] = [255, 255, 255]						' nebula vertex color
	Field RGB2:Int[] = [255, 255, 255]						' nebula vertex color
	Field RGB3:Int[] = [255, 255, 255]						' nebula vertex color
	Field RGB4:Int[] = [255, 255, 255]						' nebula vertex color

	Field Alpha:Float = 1.0									' vertex alpha
	Field Alpha1:Float = 1.0								' vertex alpha #1
	Field Alpha2:Float = 1.0								' vertex alpha #2
	Field Alpha3:Float = 1.0								' vertex alpha #3
	Field Alpha4:Float = 1.0								' vertex alpha #4

	Field v:Int = 0											' vertex counter
	Field mesh:TMesh = Null									' mesh pointer
	Field surf:TSurface = Null								' surface pointer

	Field category:String									' quad category (star, etc...)
	Field starclass:String									' star class
	Field sid:Int											' star class (numerical)
	Field giant:Int = 0										' flag for star giant
	Field dwarf:Int = 0										' flag for white dwarf
	Field multi:Int = 1										' flag for multiple star system
	
	Field rotation:Float
			
	Global List:TList = CreateList()



	' --------------------------------------------------------------------------------------------
	' METHOD: Create new Object in TList
	' --------------------------------------------------------------------------------------------
	Method New()

		ListAddLast(List, Self)

	End Method

	
	
	' --------------------------------------------------------------------------------------------
	' METHOD: Remove Object in TList
	' --------------------------------------------------------------------------------------------
	Method Remove()

		ListRemove(List, Self)

	End Method



	' --------------------------------------------------------------------------------------------
	' METHOD: Add new Quad
	' --------------------------------------------------------------------------------------------
	Method Add(col:Int = 0, row:Int = 0)

		' surface setup
		If surf = Null Then surf = surf
		If v + 4 > 32768 Then
			v = 0
			surf = CreateSurface(mesh)
		EndIf

		' add Vertices
		Local V0:Int = AddVertex(surf, 0, 0, 0, 1, 0)
		Local V1:Int = AddVertex(surf, 0, 0, 0, 1, 1)
		Local V2:Int = AddVertex(surf, 0, 0, 0, 0, 1)
		Local V3:Int = AddVertex(surf, 0, 0, 0, 0, 0)

		' color vertices
		VertexColor surf, V0, RGB[0], RGB[1], RGB[2], Alpha
		VertexColor surf, V1, RGB[0], RGB[1], RGB[2], Alpha
		VertexColor surf, V2, RGB[0], RGB[1], RGB[2], Alpha
		VertexColor surf, V3, RGB[0], RGB[1], RGB[2], Alpha

		' connect triangles
		AddTriangle surf, V0, V1, V2
		AddTriangle surf, V0, V2, V3
		
		VertexTexCoords surf, V0, col + 1, row
		VertexTexCoords surf, V1, col + 1, row + 1
		VertexTexCoords surf, V2, col, row + 1
		VertexTexCoords surf, V3, col, row

		' increase vertex counter
		If v >= 4 Then v = V0 + 4 Else v = V0

	End Method



	' --------------------------------------------------------------------------------------------
	' METHOD: Hide a Quad
	' --------------------------------------------------------------------------------------------
	Method Hide()

		distance = 2 ^ 32

		' set vertices
		VertexCoords surf, v + 0, distance, distance, distance
		VertexCoords surf, v + 1, distance, distance, distance
		VertexCoords surf, v + 2, distance, distance, distance
		VertexCoords surf, v + 3, distance, distance, distance

		'hidden = 1

	End Method



	' --------------------------------------------------------------------------------------------
	' METHOD: Update a Quad
	' --------------------------------------------------------------------------------------------
	Method Update(target:TEntity)
	
		If Alpha <= 0 Then
		
			Hide
			hidden = 1
						
		Else

			TFormVector scalex, 0, 0, target, Null
			Local X1:Float = TFormedX()
			Local Y1:Float = TFormedY()
			Local Z1:Float = TFormedZ()
	    
			TFormVector 0, scaley, 0, target, Null
			Local X2:Float = TFormedX()
			Local Y2:Float = TFormedY()
			Local Z2:Float = TFormedZ()
			
			' set vertices
			VertexCoords surf, v + 0, x - x1 - x2, y - y1 - y2, z - z1 - z2
			VertexCoords surf, v + 1, x - x1 + x2, y - y1 + y2, z - z1 + z2
			VertexCoords surf, v + 2, x + x1 + x2, y + y1 + y2, z + z1 + z2
			VertexCoords surf, v + 3, x + x1 - x2, y + y1 - y2, z + z1 - z2
		
			' set colors
			VertexColor surf, v + 0, RGB[0], RGB[1], RGB[2], Alpha
			VertexColor surf, v + 1, RGB[0], RGB[1], RGB[2], Alpha
			VertexColor surf, v + 2, RGB[0], RGB[1], RGB[2], Alpha
			VertexColor surf, v + 3, RGB[0], RGB[1], RGB[2], Alpha
			
			hidden = 0

		EndIf
			
	End Method

	
	
	' --------------------------------------------------------------------------------------------
	' METHOD: Update a Quad
	' --------------------------------------------------------------------------------------------
	Method UpdateRotation(target:TEntity)
	
		If (Alpha1 + Alpha2 + Alpha3 + Alpha4) <= 0 Then
			
			Hide
			
		Else
					
			PositionEntity PIVOT, x, y, z
	
			PointEntity PIVOT, target
			RotateEntity PIVOT, EntityPitch(target), EntityYaw(target), rotation
			
			TFormVector scalex, 0, 0, PIVOT, Null
			Local X1:Float = TFormedX()
			Local Y1:Float = TFormedY()
			Local Z1:Float = TFormedZ()
	    
			TFormVector 0, scaley, 0, PIVOT, Null
			Local X2:Float = TFormedX()
			Local Y2:Float = TFormedY()
			Local Z2:Float = TFormedZ()
			
			' set vertices
			VertexCoords surf, v + 0, x - x1 - x2, y - y1 - y2, z - z1 - z2
			VertexCoords surf, v + 1, x - x1 + x2, y - y1 + y2, z - z1 + z2
			VertexCoords surf, v + 2, x + x1 + x2, y + y1 + y2, z + z1 + z2
			VertexCoords surf, v + 3, x + x1 - x2, y + y1 - y2, z + z1 - z2
		
			' set colors
			VertexColor surf, v + 0, RGB1[0], RGB1[1], RGB1[2], Alpha1
			VertexColor surf, v + 1, RGB2[0], RGB2[1], RGB2[2], Alpha2
			VertexColor surf, v + 2, RGB3[0], RGB3[1], RGB3[2], Alpha3
			VertexColor surf, v + 3, RGB4[0], RGB4[1], RGB4[2], Alpha4
			
		End If

	End Method


End Type