0

I have a 2D tilemap that is generated in chunks consisting of 2x2 cells each. I can reference the chunk itself and get the index of each particular cell within the chunk. However, I'd like to also store the index of the first tile within that chunk, which is not automatically generated.

enter image description here

For example, clicking on the highlighted chunk would always produce "0", clicking on the next one would produce "2", and clicking on the one under it would always produce "20". Red numbers indicate the tile/cell's index. The yellow outline demonstrates an actual chunk.

Within the confines of the chunk, what is the best way to get 0, 2, 4, 6, 8, 20, and so on? The code that generates this is in Actionscript 3, and is a basic dual for loop.

EDIT:

I thought about it for a moment and decided to add in my index search code. I'm not entirely sure if this will help, especially since it is for finding individual cell index and not a particular index location in chunks.

        public function mousePosistion():Number
    {
        var mouseColX: Number = 0;
        var mouseColY: Number = 0;

        mouseColY = Math.ceil(this.mouseY / 64);
        mouseColX = Math.ceil(this.mouseX / 64);

        var mouseIndex:Number = mouseColX + (20 * mouseColY);

        return mouseIndex;
    }

Note: It's formatted for the actual map which is at 20 width, not 10 as in the example.

Merlin
  • 929
  • 12
  • 33

2 Answers2

0

Off the top of my head, just by looking at the image you have you could go:

[in pseudocode]
if tileIndex tens digit is odd, minus 10
if tileIndex ones digit is odd, minus 1
mitim
  • 3,169
  • 5
  • 21
  • 25
0

I figured it out after a little time. Using the dual For loop, the index calculation came out to this: baseIndex = (X * 2) + (Y * 20); Matches each index on the basic tiles perfectly. 2 is the size of the super, 20 is the width of the map doubled. To expand this into a variable based solution:

 baseIndex = (X * chunkSize) + (Y * (mapSize * 2));
Merlin
  • 929
  • 12
  • 33