• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Snapping projectile to nearest empty slot in a ds_grid

M

Matt93

Guest
I'm currently making a Bubble Shooter game. The room is set up with a ds_grid:

Code:
global.grid = ds_grid_create(20, 15);
I'm trying to get the bubbles I shoot to snap to the nearest empty slot in the ds_grid. The bubble needs to land, then I need to detect which slot is closest to its x and y coordinates, whether this is empty, and pop the bubble into that slot. How would I do this in code? I don't know very much about ds_grids, so any advice with them would be amazing. This has been confusing me for ages and is getting quite frustrating. Any help would be really great.

Cheers!
 
T

TDSrock

Guest
As we are discussing in the other thread itś all about handling the 2D array that is the grid. knowing where what is. and how far away that means it is compared to everything else.
 

FrostyCat

Redemption Seeker
No, there's no need for another grid to store the coordinates, just use div and be done.

First, after declaring the grid, use ds_grid_clear() to set all cells to some value such as -1 for empty. Then assign each colour a number so that it can be inserted into the grid later.

Assume that the grid starts at (64, 64) and has 32x32 cells. Then the conversion from on-screen coordinates to in-grid coordinates is:
Code:
gx = (x - 64) div 32;
gy = (y - 64) div 32;
For the best visual effect, the sprites for the bubbles should have their origins set dead centre.

From this point on, everything you do is just a matter of referencing or setting global.grid[# gx, gy].
 
Top