

--->
#Map2D!  To use this class:
'''
Base level, simply sets squares and lines:

map.SetSizes( widthI, heightI, squareSize, defaultState )
map.AddMapRect( left, bottom, width, height, inState = 1 )
	--fills map shapes

map.AddMapLines( inV )

map;AddRect( inVals, inStyle )
map;AddFloatRect()
map.AddCircle()
map;AddTriangle( inVals, inRot, inStyle )



Top level, customized per game for graphics and map interface:
For dd, we can place rects and define a style for each - a graphic map drawer will use these to generate map graphics, and we can keep the drawer simple for the time being.

So, initially, in editor, we draw rects and specify properties.  we also need to do the same for triangles [1x1, 1x2, 2x2] and circles [dot, rad], and non-integer rects.  We will wait on custom lined shapes, as we need to figure out if lua can get classes by string, etc.

In the end, the map is saved as those shapes with graphical properties [not counting entities yet].


At runtime, the map is fed to a graphics creator, and it will automatically use data to construct and draw the graphics for the map.  Most of this will be rectangular-based quad placement, a few layers to change alpha/color of, a few blend modes, and some 3d objects where appropriate maybe!

There is likely to be up to 50 different 'pieces' in more complex map drawings - each piece could have 3+ layers, so that's 150 draw calls for just the map...  perhaps each 'style' of rect can share their geometry pieces over certain areas to reduce # of draw calls...  I think doing each individually for now is fine, it's hard to imagine it taking significant load.

Also we'll need a strategy for drawing empty space in a map, or just a background - simple enough, tons of value yayyyyy!


'''


---

--->
#To use Map2D, we either LoadMap() or add shapes through the appropriate functions - both of these populate self.mapObjs.  When map is fully defined, we call ParseMap() which uses self.mapObjs to define lines in the engine, define our grid space occupancy, and create whatever graphics are needed.

#We should get an editor up and running, even if it is really primitive.
---




MAP2D_RECT 			= 0
MAP2D_FLOATRECT 	= 1
MAP2D_CIRCLE 		= 2
MAP2D_TRIANGLE 		= 3


class Map2D:
	def Init( inSelf ):
		if ( inSelf ):
			inSelf = self
		SetSpaceMapper( bx_SPACEMAPPER2D )
		nx, ny = 16, 16
		if ( random() < .5 ):
			nx, ny = 10, 10
		nx *= 15
		ny *= 15
		self:Reset( nx, ny, 1.0 * .2 )
		
	def Reset( inNumWidth, inNumHeight, inSquareSize ):
		self.widthI = inNumWidth
		self.heightI = inNumHeight
		self.squareSize = inSquareSize
		
		self.widthF = ( inNumWidth * inSquareSize )
		self.heightF = ( inNumHeight * inSquareSize )
		
		Set2DSpaceBounds( 0, 0, self.widthF, self.heightF, 16, 16 )
		
		self.mapObjs = {}
		
		self.drawLines = {}
		
	def SaveMap( inFName ):
		--Figure out how to 'pickle' an array, or probably write a function to pickle and unpickle an array to/from a file.
		_data = pickle( self.mapObjs )
		
		--print data
		io.output(io.open( inFName, 'wb' ))
		io.write( data )
		io.close()
	def LoadMap( inFName ):
		
		local inp = assert( io.open( inFName, "rb" ) )
		_data = inp:read("*all")
		inp:close()
		
		self.mapObjs = unpickle( data )
		self:ParseMap()
	def LoadTestGeom():	
		x = 1
		y = 1.5
		x2 = 1.5
		y2 = 1
		insert( self.drawLines, { x, y, x2, y2 } )
		
		x = 1.5
		y = 2
		x2 = 1.5
		y2 = 1
		insert( self.drawLines, { x, y, x2, y2 } )
		
		
		
		for i, v in pairs( self.drawLines ) do
			AddSpaceObject( "line2d", v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ] )
		
		
	def DrawGrid():
		h = self.heightF
		w = self.widthF
		sqSz = self.squareSize
		for x = 0, self.widthI:
			DrawLine( x * sqSz, 0, 0, x * sqSz, h, 0 )
		for y = 0, self.heightI:
			DrawLine( 0, y * sqSz, 0, w, y * sqSz, 0 )
		
	def ParseMap():
		self.drawLines = {}
		--Add lines appropriately
		for i, v in pairs( self.mapObjs ):
			_type = v[ 1 ]
			vals = v[ 2 ]
			if ( type == MAP2D_RECT ):
				x = vals[ 1 ] * self.squareSize
				y = vals[ 2 ] * self.squareSize
				w = vals[ 3 ] * self.squareSize
				h = vals[ 4 ] * self.squareSize
				insert( self.drawLines, { x, y, x + w, y } )
				insert( self.drawLines, { x + w, y, x + w, y + h } )
				insert( self.drawLines, { x + w, y + h, x, y + h } )
				insert( self.drawLines, { x, y + h, x, y } )
			if ( type == MAP2D_CIRCLE ):
				num = 42.0
				r = vals[ 3 ]
				
				last = { 0, r }
				pt = { vals[ 1 ], vals[ 2 ] }
				for i = 1, num:
					pct = i / num
					new = { r * sin( pct * PI2 ), r * cos( pct * PI2 ) }
					--if ( i == 6 or true ):
					insert( self.drawLines, { pt[ 1 ] + new[ 1 ], pt[ 2 ] + new[ 2 ], pt[ 1 ] + last[ 1 ], pt[ 2 ] + last[ 2 ] } )
					--pct -= .03
					--new = { r * sin( pct * PI2 ), r * cos( pct * PI2 ) }
					last[ 1 ] = new[ 1 ]
					last[ 2 ] = new[ 2 ]
			if ( type == MAP2D_TRIANGLE ):
				x = vals[ 1 ] * self.squareSize
				y = vals[ 2 ] * self.squareSize
				w = vals[ 3 ] * self.squareSize
				h = vals[ 4 ] * self.squareSize
				rot = vals[ 5 ]
				print rot
				if ( rot < 2 ):
					insert( self.drawLines, { x, y, x + w, y } )
				if ( rot == 0 or rot == 3 ):
					insert( self.drawLines, { x, y + h, x, y } )
				if ( rot > 0 and rot < 3 ):
					insert( self.drawLines, { x + w, y, x + w, y + h } )
				if ( rot > 1 and rot < 4 ):
					insert( self.drawLines, { x + w, y + h, x, y + h } )
				if ( rot % 2 == 1 ):
					insert( self.drawLines, { x, y, x + w, y + h } )
				else:
					insert( self.drawLines, { x + w, y, x, y + h } )
		
		for i, v in pairs( self.drawLines ) do
			AddSpaceObject( "line2d", v[ 1 ], v[ 2 ], v[ 3 ], v[ 4 ] )
		
	def AddRect( inMins, inSizes, inStyle ):
		nObj = { MAP2D_RECT, { inMins[ 1 ], inMins[ 2 ], inSizes[ 1 ], inSizes[ 2 ], inStyle } }
		insert( self.mapObjs, nObj )
		
	def AddFloatRect( inMins, inMaxs, inStyle ):
		
	def AddCircle( inPoint, inRad, inStyle ):
		nObj = { MAP2D_CIRCLE, { inPoint[ 1 ], inPoint[ 2 ], inRad, inStyle } }
		insert( self.mapObjs, nObj )
		
	def AddTriangle( inMins, inSizes, inRot, inStyle ):
		nObj = { MAP2D_TRIANGLE, { inMins[ 1 ], inMins[ 2 ], inSizes[ 1 ], inSizes[ 2 ], inRot, inStyle } }
		insert( self.mapObjs, nObj )
		
	def Draw( t, dt, inPass ):
		if ( inPass == 0 ):
			v = .5
			Color( v, v, v, 1 )
			sqSz = self.squareSize
			if ( true ):
				for i, v in pairs( self.mapObjs ):
					_type = v[ 1 ]
					x = v[ 2 ][ 1 ]
					y = v[ 2 ][ 2 ]
					if ( type == MAP2D_RECT ):
						w = v[ 2 ][ 3 ]
						h = v[ 2 ][ 4 ]
						
						Push()
						Move( sqSz * ( x + .5 * w ), sqSz * ( y + .5 * h ), 0 )
						Scale( sqSz * .5 * w, sqSz * .5 * h, 1 )
						Geom( 'quad' )
						Pop()
					if ( type == MAP2D_TRIANGLE ):
						w = v[ 2 ][ 3 ]
						h = v[ 2 ][ 4 ]
						rot = v[ 2 ][ 5 ]
						
						Push()
						Move( sqSz * ( x + .5 * w ), sqSz * ( y + .5 * h ), 0 )
						Scale( sqSz * .5 * w, sqSz * .5 * h, 1 )
						Rotate( 0, 0, 1, PIhalf * rot )
						Geom( 'triangle' )
						Pop()
					
					if ( type == MAP2D_CIRCLE ):
						r = v[ 2 ][ 3 ]
						
						Push()
						Move( x, y, 0 )
						Scale( r, r, 1 )
						Geom( 'circle2' )
						Pop()
					
					
			Color( 1 )		
			for i, v in pairs( self.drawLines ) do
				DrawLine( v[ 1 ], v[ 2 ], 0, v[ 3 ], v[ 4 ], 0 )
			

--done goofd!			
class DDMap2D( Map2D ):
	def Init():
		Map2D : Init( self )
	def Draw( t, dt, inPass ):
		Map2D : Draw( t, dt, inPass )
		
		


