Help with my map grid code.

Started by Egmundo Huevoz, Wed 17/01/2018 10:12:10

Previous topic - Next topic

Egmundo Huevoz

Hello, i'm making a map with a grid system. In each of the grids, different things happen. I track the grid the player is in with the int "grid_current". The thing is, I can't figure out how to make the grid_current change according to the player's coordinates. I mean, I do, but manually. And I don't want to have to write 250 of those, I'm sure there's an easier way.

grid_width is another int which tracks the, well... grid's width (laugh) I have another one for grid_height, but right now I'm making the 1st row of grids. I'm sure I can extrapolate from the width.

My code so far is this:
Code: ags
  
if ((player.x>=grid_width*1-1)&&(player.x<=grid_width*1)){ //the player is in grid 1. his X coordinate is bigger than the left side of the grid, and smaller than the right side of the grid.
    grid_current=1;
  }
else if ((player.x>=grid_width*(2-1))&&(player.x<=grid_width*2)){
    grid_current=2;
  }
else if ((player.x>=grid_width*(3-1))&&(player.x<=grid_width*3)){
    grid_current=3;
  }


I call the function in room_repexec. It works, but as I said, I'm sure there's a way to make it automatic.

Thanks in advance.

Khris

#1
Code: ags
  int x = (player.x - OX) / grid_width; // replace OX, OY with coordinates of grid in room
  int y = (player.y - OY) / grid_height;
  grid_current = y * number_of_columns + x + 1; // start counting at 1


(Also, even without math-based solutions like this, it's still pretty obvious that a simple for loop will automate the code. You're copy-pasting the block, then incrementing a number by 1. It's exactly what a for loop does.)

Egmundo Huevoz

Hi Khris! Long time no see. It worked! As always... Thank you!!

A couple of optional follow-up questions, answer if you have the time and the inclination, if not, I can live with that (laugh)
1) I moded the code because it was giving me negative numbers. Still, it starts counting at the bottom right grid (as in, the bottom-right square is 260, and the top-left square is 1). I would like for the top-left to be 1.
Code: ags
  int x = (2560-player.x) / grid_width; // replace OX, OY with coordinates of grid in room
  int y = (1600-player.y) / grid_height;
  grid_current = y * 20 + x + 1; // start counting at 1

2) Could you show me how to do it with "for"? I thought it was what I needed, but I can't understand the manual's explanation. :(


P.s: I imagine some day you're gonna stop pointing that gun at the roof and shoot me. (laugh)

Khris

I guess I should've been more clear: OX, OY is the pixel position of the grid's top left corner in room coordinates.
Which means player.x - OX will calculate the player's pixel position relative to the grid's left edge, which is then converted into the column number by dividing it by the width of a column.

So the code should probably look something like this:
Code: ags
  int x = (player.x - 128) / grid_width;
  int y = (player.y - 300) / grid_height;
  grid_current = y * 20 + x + 1; // start counting at 1

By reversing the coordinate calculation, you also reversed the numbering of the squares.

And just for reference, here's the for loop, for just the first row:
Code: ags
  for (int i = 1; i < 10; i++) {
    if (player.x >= grid_width * (i - 1) && player.x < grid_width * i) {
      grid_current = i;
      break;
    }
  }

SMF spam blocked by CleanTalk