Advice Required - 2d game

Started by
4 comments, last by Woop 24 years, 4 months ago
three basic methods will get the job done

i'll present them in increasing order of complexity

1. make the map tilebased-split the map into a grid of tiles (commonly used tile size is 64), and have clicking on a tile (which you can easily calculate from the mouse x,y by dividing each by 64) trigger the desired response

2. rectangular hotspots-for areas that trigger responses, store a rectangle that describes that area. when the mouse is clicked, check to see if the mouse is within any of the rectangles, and if so, trigger the response.

3. oddly shaped hotspots-similar to rectangular hotspots, only you also use a monochrome bitmap representation of the actual thing you are clicking on (your example was a mine entrance, you would make an image of the mine entrance where all of the pixels of the mine entrance were white, and the rest black). when a mouse click occurs, you again check against all of the rectangles, and if it is in the rectangle, you check the pixel color of the monochrome image that corresponds to the mouse's position within that rectangle. if white, trigger the event, if black, dont.

Get off my lawn!

Advertisement
Thanks for that rapid response.

I have to say I like the sound of that last one. I guess that is the best way to do it, and I will keep it in mind.

Since this is my first big attempt at getting something completed, I will go along with the second method for now.

Thanks again for the reply.

Woop

wow, so many icons to choose from...

I store events in a linked list, and use a bit array to tell whether there's an event at all on that tile. if not, don't even bother searching list. if so, search list. it cuts down on useless searching of the list. i used a linked list because events can be added or removed dynamically.

so if your map is 64x64 (4096 tiles), your bit array needs to have 4096 entries as well, but each entry needs only be a bit, not a whole byte or word (or more?), so the memory requirement for that is only 512 bytes. Memory well spent in my opinion.

Since searching a linked list is on the slow side (as opposed to arrays), using this method only accesses the list when you know you'll be finding something.

if you're lazy you make a restriction that maps dimensions have to be multiples of 8 (so the bit array fits nicely), but you can work around that if you want.

hey, could you explain more about making a bit array like that? thanks


mmm... icons....

------------------

_________________Gecko___


_________________Gecko___Gecko Design
Any helpful suggestions much appreciated.

I am attempting to write my first game. The main view will be looking at a 'map' type overview of the game world, a bit like in Heroes of Might and Magic.

So I can create the graphics for my map, including all the features with which the player can interact.

My question is about the best way to check where a section of the map graphic actually becomes something the player can interact with.

For example, let's say there is a mine at a certain location. The player should be able to click on that spot and an action should occur.

At the moment I am thinking I just load the whole map as a graphic, and then will have to have some sort of array storing the location of each feature and what type of feature it is. Then when the player clicks somehwere in the game window, there will be a quick translation between the window co-ordinate and the actual map co-ordinate, then I can look up in the array whether this corresponds to the location of a feature. Then the code can act accordingly.

To all those out there with much more experience does this seem the right way of going about this type of programming? It's my first attempt at ever doing anything graphical in nature, so suggestions/comments are welcomed.

Thanks a lot,

Woop

consider a 8x8 map: (i hope this works)
code:
OxOOOOOxOOOOOOxOOxOOOOOxOOOxOOxOOxOOOOOxOOOOOOOOOOOOOxOxOOOOOOOO

O = normal tile
x = event tile

Now, here is how the bits of the bit array look like, along with the decimal value of that byte:
(note that the bits are reversed for more efficient processing)

code:
01234567 <- bit order-01000001  13000000010   6401000001  13000010010   7201000001  13000000000    000000101  16000000000    0

Ok, let's say in a zero-based coordinate system, you're at location (x=6, y=3) and want to find out if this tile has an event attached to it. here's what you do:

1) find out the "absolute" tile, which is just (y*width)+x, so it's tile index (3*8)+6 = 30 from the beginning of the map. Ok, now you have a number 30. What do you do with it?

2) Divide it by 8 to find out which byte you need to look at.

code:
bit_array_index = 30 >> 3; // = 3

3) Now to find out which bit you need to look at, get the remainder of dividing by 8 (modulo 8).

code:
bit_to_test_for = 30 & 7; // = 6

4) Finally, test this value against you bit array:

code:
if (bit_array[bit_array_index] & (1 << bit_to_test_for))  process_event();

I left out a small detail, that the bit array is created inside the map editor, although it can change during the game

Also, it's very easy with this method to use 2 bits, or 4 bits per tile (but nothing else because they dont divide up very well), so if you need to store additional flags along with the "event bit" just scale your division and modulo down to what you need, and look at not just 1 bit, but 2 or 4 bits when you test it. Here's for quick reference:

1 bits = divide by 8, modulo 8
2 bits = divide by 4, modulo 4
4 bits = divide by 2, modulo 2

Hope I didn't make any typos! (it was inevitable, I made typos)

[This message has been edited by foofightr (edited December 02, 1999).]

This topic is closed to new replies.

Advertisement