putting tiles on board

Blitz3D Forums/Blitz3D Programming/putting tiles on board


Had to fix:
n\x = row
n\z = col

to

n\x = row_pos
n\z = col_pos



Yeah, sorry about that. Strangest thing is, I had just put my head down to go to sleep last night when this bug suddenly popped into my head. Bizarre. :)

Note that you now don't need the boardx and boardz arrays as they're redundant.

I see you intend to change to using a hex-tiled board at some point so I put this little demo together. Hope it's of some use.

;
; 3D hex tile grid demo by big10p (A.K.A. Chris Chadwick) 2004
;

	Graphics3D 640,480,32,2
	SetBuffer BackBuffer()

	WireFrame 0
	AntiAlias 0

	SeedRnd MilliSecs()

	Global frame_count%
	Global fps%
	Global slowest_fps%
	Global fps_timeout%
	Global frame_time%
	Global slowest_frame%
	Global frame_start%
	fps_timer = CreateTimer(60)
	slowmo% = False
	wiref% = False

	piv = CreatePivot()
	cam = CreateCamera(piv)
	PositionEntity cam,0,0,-18
	TurnEntity piv,45,0,0
	
	light = CreateLight()
	TurnEntity light,10,10,0
	
	Type cellT
		Field ent
		Field id%
		Field x#,z#
	End Type
	
	Dim board.cellT(0,0)

	Const CELL_YELLOW%	= 0
	Const CELL_BLUE%	= 1
	Const CELL_GAP#		= 0.1
	Global cell_mesh
	Global cell_size#
	Global board_rows%, board_cols%
	create_cell_mesh()
	create_board(10,10)
	
	g = 0
	
	; --- Main loop ---
	
	While Not KeyHit(1)
		frame_start = MilliSecs()

		If KeyHit(28) Then slowmo = Not slowmo
		If KeyHit(14) Then wiref = Not wiref : WireFrame wiref

		If MouseHit(1) And g=0
			e = CameraPick(cam,MouseX(),MouseY())
			If e
				cell.cellT = Object.cellT(EntityName(e))
				If cell\id = CELL_YELLOW
					EntityColor e,0,0,255
					cell\id = CELL_BLUE
				Else
					EntityColor e,255,255,0
					cell\id = CELL_YELLOW
				EndIf
				g = 5
			EndIf
		EndIf
		
		If g
			display_board(CELL_GAP + Sin(g))
			g = g + 5
			If g > 180 Then g = 0
		EndIf
		
		RenderWorld
		show_info()
				
		frame_time = MilliSecs() - frame_start	
		
		WaitTimer(fps_timer)
		Flip(1)

		If slowmo Then Delay 200
	Wend

	ClearWorld	

	End


;
;
;
Function create_cell_mesh(size#=2)

	cell_size = size
	hs# = size/2

	temp = CreateCylinder(6,1)	
	FitMesh temp,-hs,-(hs/2),-hs,size,hs,size
	cell_mesh = CopyMesh(temp)		; Make single surface.
	FreeEntity temp
	HideEntity cell_mesh
	
End Function


;
;
;
Function create_board(rows, cols, gap#=CELL_GAP)

	board_rows = rows
	board_cols = cols
	Dim board(rows-1,cols-1)
	
	total_size# = cell_size + gap
	
	x_off# = total_size
	z_off# = total_size - (total_size/4)
	start_x# = -(((((cols-1) * x_off)) / 2) + total_size/4)
	start_z# = (((rows-1) * z_off) / 2)
	
	cell_t = 0
	z_pos# = start_z
	For r = 1 To rows
		x_pos# = start_x + ((Not(r And 1)) * total_size/2)
		For c = 1 To cols
			cell.cellT = New cellT
			board(r-1,c-1) = cell
			If cell_t
				cell\ent = CopyEntity(cell_mesh)
				cell\id = CELL_YELLOW
				EntityColor cell\ent,255,255,0
			Else
				cell\ent = CopyEntity(cell_mesh)
				cell\id = CELL_BLUE
				EntityColor cell\ent,0,0,255
			EndIf			
			NameEntity cell\ent,Handle(cell) 
			cell\x = x_pos
			cell\z = z_pos
			PositionEntity cell\ent,x_pos,0,z_pos
			EntityPickMode cell\ent,2
			x_pos = x_pos + x_off
			cell_t = Not cell_t
		Next
		z_pos = z_pos - z_off
	Next

End Function


;
;
;
Function display_board(gap#=.1)

	total_size# = cell_size + gap
	
	x_off# = total_size
	z_off# = total_size - (total_size/4)
	start_x# = -(((((board_cols-1) * x_off)) / 2) + total_size/4)
	start_z# = (((board_rows-1) * z_off) / 2)
	
	z_pos# = start_z
	For r = 1 To board_rows
		x_pos# = start_x + ((Not(r And 1)) * total_size/2)
		For c = 1 To board_cols
			cell.cellT = board(r-1,c-1)
			cell\x = x_pos
			cell\z = z_pos
			PositionEntity cell\ent,x_pos,0,z_pos
			x_pos = x_pos + x_off
		Next
		z_pos = z_pos - z_off
	Next

End Function


;
; Display debug info
;
Function show_info()
	
	If fps_timeout
		frame_count = frame_count + 1

		If MilliSecs() > fps_timeout Then
			fps_timeout = MilliSecs() + 1000 
			fps = frame_count 
			frame_count = 0 
		
			If fps < slowest_fps Or slowest_fps = 0 Then slowest_fps = fps
		EndIf 
		
		If frame_time > slowest_frame Then slowest_frame = frame_time
		
		Color 0,255,0
		Text 10,10," Triangles: " + TrisRendered()
		Color 255,255,0
		Text 10,25," Millisecs: " + frame_time
		Text 10,40,"   Slowest: " + slowest_frame
		Color 0,255,255
		Text 10,55,"       FPS: " + fps
		Text 10,70,"     Worst: " + slowest_fps
		Color 255,255,255
		Text 10,100,"Click on a cell to toggle it's color"		
	Else
		; First call initialization.
		fps_timeout = MilliSecs() + 1000 
	EndIf
	
End Function