Quick Grid Related Problem

BlitzMax Forums/BlitzMax Beginners Area/Quick Grid Related Problem

Matt Vinyl(Posted 2008) [#1]
Hi all,

Just a quicky here, that I hope someone can advise me on. I have a grid that is 100x64 of 48x48 tiles. I just need to be able to set a variable to show me which X 'tile' I'm in, and which Y 'tile' I'm in. So it's the CaveX and CaveY variables, at the bottom of my code snippet.

This works fine when I don't scroll the screen, but goes skewhiff when I scroll around my map.

Here's the simple grid / movement routine.

	For x=0 To CaveWidth 'Draw a basic grid as a guide
		For y=0 To CaveHeight
			DrawRect (x*TileWidth)-OffsetX,(y*TileHeight)-OffsetY,TileWidth,TileHeight
		Next
	Next
	SetColor 0,0,0
	For x=0 To CaveWidth
		For y=0 To CaveHeight
			DrawRect ((x*TileWidth)+1)-OffsetX,((y*TileHeight)+1)-OffsetY,TileWidth-1,TileHeight-1
		Next
	Next
	
	If KeyDown(Key_Right) Then 'Move right across the map
		OffsetX:+4
	End If
	If KeyDown(Key_Left) Then 'Move left across the map
		OffsetX:-4
	End If
	If KeyDown(Key_Up) Then 'Move up the map
		OffsetY:-4
	End If
	If KeyDown(Key_Down) Then 'Move down the map
		OffsetY:+4
	End If

	SetColor 255,255,255
	
	CaveX=Floor(MouseX())/48
	CaveY=Floor((MouseY()+40)/40)-1


Cheers!
M


Jesse(Posted 2008) [#2]
I don't know what you are doing, but I think it should be like this:
        CaveX=Floor((MouseX()+offsetx)/48)
	CaveY=Floor((MouseY()+offsety)/48)



Matt Vinyl(Posted 2008) [#3]
Hi,

Cheers for that, I gave it a try, but it does'nt work correctly, as it shifts the 'current' square over to the right when I scroll.

Thanks though!
M.


Jesse(Posted 2008) [#4]
I tryed it like this and it works:

maybe if you show more code, I can figure out what you are doing wrong.


Matt Vinyl(Posted 2008) [#5]
Not a problem, your help is appreciated! :)

'Cave Editor v0.1
'(C) Matt Vinyl 
'Version Date: 01/06/2008


'--------General Declarations

Graphics 1024,768 'Screen width and height in pixels

SetBlend Alphablend

Const TileWidth=48 'Width of tile in pixels
Const TileHeight=48 'Height of tile in pixels
Const CaveWidth=100 'Width of cave in tiles
Const CaveHeight=64 'Height of cave in tiles

Type Tile 'Set up the tile type
	Field X 'X position of the tile
	Field Y 'Y position of the tile
	Field Tile 'The tile number
	Field State 'Tile state, has tile been parsed
End Type

Global Cave[CaveWidth,CaveHeight] 'Dimension the cave

Global OffsetX=0 'Movement offset flag (width)
Global OffsetY=0 'Movement offset flag (height)

Global CaveX=0 'X of currently selected grid tile
Global CaveY=0 'Y of currently selected grid tile

Global CurrentTile=1 'The currently selected tile to place in the map

Global TileCount=102

Global State=0 'A flag that denotes the current state of the programme
'0=Startup
'99=User as quit

HideMouse 'Hide the default mouse cursor


'--------Media Declarations

Global Tiles=LoadAnimImage ("gfx\tiles.png",48,48,0,TileCount) 'Load in the tile graphics


'--------Core Loop

Repeat

	Cls
	Fnc_Grid
	Fnc_Quit()
	Fnc_Debug()
	Flip

Until State=99 'Carry out loop until user quits


'--------Functions

Function Fnc_Quit() 'Quit the programme

	If KeyHit(Key_Escape) Then State=99

End Function

Function Fnc_Debug() 'Debug routines, to be removed before final version

	DrawText "MouseX:",0,0
	DrawText MouseX(),104,0
	DrawText "MouseY:",0,12
	DrawText MouseY(),104,12
	DrawText "CaveX:",0,24
	DrawText CaveX,104,24
	DrawText "CaveY:",0,36
	DrawText CaveY,104,36
	DrawText "Current Tile:",0,48
	DrawText CurrentTile,104,48

End Function

Function Fnc_Grid() 'Control moving around the grid
	
	SetColor 100,100,100
	
	For x=0 To CaveWidth 'Draw a basic grid as a guide
		For y=0 To CaveHeight
			DrawRect (x*TileWidth)-OffsetX,(y*TileHeight)-OffsetY,TileWidth,TileHeight
		Next
	Next
	SetColor 0,0,0
	For x=0 To CaveWidth
		For y=0 To CaveHeight
			DrawRect ((x*TileWidth)+1)-OffsetX,((y*TileHeight)+1)-OffsetY,TileWidth-1,TileHeight-1
		Next
	Next
	
	If KeyDown(Key_Right) Then 'Move right across the map
		OffsetX:+4
	End If
	If KeyDown(Key_Left) Then 'Move left across the map
		OffsetX:-4
	End If
	If KeyDown(Key_Up) Then 'Move up the map
		OffsetY:-4
	End If
	If KeyDown(Key_Down) Then 'Move down the map
		OffsetY:+4
	End If

	SetColor 255,255,255
	
	'CaveX=Floor((MouseX()+OffsetX)/48)
	'CaveY=Floor((MouseY()+OffsetY)/48)
	'CaveX=Floor(MouseX())/48 'Work out current grid square X
	'CaveY=Floor((MouseY()+40)/40)-1 'Work out current grid square Y
	DrawImage Tiles,CaveX*48,CaveY*48,CurrentTile 'Show current tile in position X Y

	If KeyHit(Key_Equals) Then 'Cycle through available tiles
		If CurrentTile<TileCount-1 Then 
			CurrentTile:+1
		Else
			CurrentTile=0
		End If
	End If
	If KeyHit(Key_Minus) Then
		If CurrentTile>0 Then 
			CurrentTile:-1
		Else
			CurrentTile=TileCount-1
		End If
	End If

End Function



Here's what I have so far. Note that I have commented out the two attempts I've tried so far.


Jesse(Posted 2008) [#6]
do you want it to snap to grid?
do you want it to maintain aproximate screen position?
do you want it to move with the grid and disregard current mouse position?


Jesse(Posted 2008) [#7]
is this what you want?

[edit]
my mistake. minor adjustment on tipo.
that is why I always use superstrict


Matt Vinyl(Posted 2008) [#8]
Ah, thankyou, that is spot on! It's those Mod / Floor / Math functions that always throw me!

Superstrict: Yes, I really should get into the habit of using that. ;)

Cheers!
M