Jibble

Author Topic: Help with my map grid code.  (Read 426 times)

Help with my map grid code.
« on: 17 Jan 2018, 10:12 »
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: Adventure Game Studio
  1.  
  2. 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.
  3.     grid_current=1;
  4.   }
  5. else if ((player.x>=grid_width*(2-1))&&(player.x<=grid_width*2)){
  6.     grid_current=2;
  7.   }
  8. else if ((player.x>=grid_width*(3-1))&&(player.x<=grid_width*3)){
  9.     grid_current=3;
  10.   }

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.
« Last Edit: 17 Jan 2018, 10:41 by Egmundo Huevoz »

Khris

  • partook in silencing a crtitc despite facts
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Help with my map grid code.
« Reply #1 on: 17 Jan 2018, 10:45 »
Code: [Select]
  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.)
« Last Edit: 17 Jan 2018, 10:49 by Khris »
Fail at Floaty Rog' now!  still having to deal with what games are going through

Re: Help with my map grid code.
« Reply #2 on: 17 Jan 2018, 11:15 »
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: Adventure Game Studio
  1.   int x = (2560-player.x) / grid_width; // replace OX, OY with coordinates of grid in room
  2.   int y = (1600-player.y) / grid_height;
  3.   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

  • partook in silencing a crtitc despite facts
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Help with my map grid code.
« Reply #3 on: 17 Jan 2018, 11:52 »
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: Adventure Game Studio
  1.   int x = (player.x - 128) / grid_width;
  2.   int y = (player.y - 300) / grid_height;
  3.   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: [Select]
  for (int i = 1; i < 10; i++) {
    if (player.x >= grid_width * (i - 1) && player.x < grid_width * i) {
      grid_current = i;
      break;
    }
  }
Fail at Floaty Rog' now!  still having to deal with what games are going through