Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

when4

seleting units in a tile based game?

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a simple tile based game on a 8x8 grid, and when you click on a grid square it opens a window showing what units are on that square, then you can move them to another square or whatever... so 1) what is the best way to go about this...a seperate struct for each (64) squares on the grid holding what units are there? 2) How do I Make it so that when you click on one square you get its info... i was thinking making a RECT for each grid square, and using 64 if statements for what RECT you click on, but that seemed a little excessive... Thanks in advance for any help

Share this post


Link to post
Share on other sites
Advertisement
That approach is equal to suicide!!

Now, I haven''t coded in C for a few years but I hacve in Delphi because of school so I won''t be giving you any code cause I''m not sure about the syntax of the top of my head, but this is a better approach:

1. Create your own data type for a grid square. It should be structured type (if you know what it is) and inside keep variables which will store which units are there on the square.

2. Declare an 8x8 array of that data type.
In Delphi you would define it like this:
grid : array[1..8,1..8] of MySquare;

3. Now, for simplicity, lets take it that top left corner of the grid is at mouse coordinates (0,0) and both width an height of the square are length L.
X and Y are mouse coordinates at which the user clicked.
Sx and Sy define the square in the array(grid) like this : Grid[Sx,Sy] and

Sx:= X div L + 1; (div is integer division)
Sy:= Y div L + 1;

Like that by calculating Sx and Sy you know which square in the array user clicked on.

Sorry if this was a bit confusing but if someone writes u some C-code it will be clearer, that is if you code in C.


------------------
Ni u soldima sve



Share this post


Link to post
Share on other sites
Not too sure if this is the kind of info your after but you could have a 2d array of a struct/class to hold your grid world. Each entry in this 2d array holds all the info about that square (units that square etc...)

To get the grid square the user has selected, (assuming all the grid squares are the same shape for example 32x32)then you can do something like.


grid_position_x = (int) (mouse_click.x / 32)
grid_position_y = (int) (mouse_click.y / 32)


this takes the location you clicked/selected (mouse_click) and divides the results by the x or y respectively and converts to an int (for rounding down).

gird_position_x and grid_position_y can now be used to access the right element in your 2d array.

Not sure if this makes any sense, need coffee :-)

Hope this helps

Share this post


Link to post
Share on other sites
yeah thats what im looking for, i think i got the gist of it, but.. that still means 64 structs? one for each grid square?

Share this post


Link to post
Share on other sites
Well not just any old 64 structs but an 8x8 array of them would be easiest. That way when you can use the grid_x and grid_y coordinates you calculated as indices in the array and you can easily access adjacent grid squares (offset the current coordinates by +/- 1).

[edited by - Dobbs on February 7, 2003 2:25:38 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!