• 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!

Finding an offset for a mini map

flerpyderp

Member
I already have a metroidvania style map in my game, and I'm now trying to create a mini map version of it which displays a 5x3 cell portion of the full map, with the player marker in the center.

The way the map works is having the full map drawn as one sprite, then using a for loop to draw 8x8 sprites over every 8x8 cell that is not yet discovered by the player. The player's position on the grid is updated in the step event, and once that cell coordinate has been visited, the corresponding 8x8 sprite is no longer drawn, revealing the map underneath.

For the mini map, I have it displaying the 5x3 portion of the map using draw_sprite_part, but I'm struggling to figure out how to find the offset for the "hidden tiles", and only draw 5x3 of them relative to the player's position on the grid.

Create event:
GML:
//Map grid
for (var i = 0; i <= cellsH - 1; i++)
{
    for (var j = 0; j <= cellsV - 1; j++)
    {
        mapGrid[i,j] = 0;
    }
}
Step event:
GML:
//Update the grid
if (!global.map)
{
    localX = obj_player.x;
    localY = obj_player.y;
    mapGrid[(global.gridX + (localX div inGameH)),(global.gridY + (localY div inGameV))] = 1;
}

Draw GUI event for the main map:
GML:
//Center of screen
var mw = sprite_get_width(map_main)/2;
var mh = sprite_get_height(map_main)/2;
var vw = view_wview[0]/2;
var vh = view_hview[0]/2;
var ox = vw - mw;
var oy = vh - mh;

//Map
if (global.map)
{
    //Main
    draw_sprite(map_main,0,ox,oy);

    //Hidden
    for (var i = 0; i <= cellsH - 1; i++;)
    {
        for (var j = 0; j <= cellsV - 1; j++)
        {
            if (mapGrid[i,j] == 0)
            {
                draw_sprite(map_hide,1,ox+margin+(cell*(i)),oy+margin+(cell*(j)));
            }
        }
    }
    
    //Player marker
    draw_sprite(map_player,0,ox+margin+(cell*(global.gridX+(localX div inGameH))),oy+margin+(cell*(global.gridY+(localY div inGameV))));
}

Draw GUI for mini map:
GML:
//Mini map
var mx = view_wview[0] - 80;                                //Top left x
var my = 16;                                                //Top left y
var mw = cell * 5;                                          //Width
var mh = cell * 3;                                          //Height
var px = mx+(cell*2);                                       //Player marker x
var py = my+cell;                                           //Player marker y
var ox = cell*(global.gridX+(localX div inGameH));          //Offset x based on player position on grid
var oy = cell*(global.gridY+(localY div inGameV));          //Offset y based on player position on grid

//Main
draw_sprite_part(map_main,0,ox,oy+cell,mw,mh,mx,my);

//Hidden
/*
    need to figure out the offset
*/

for (var i = 0; i <= (mw/cell) - 1; i++;)
{
    for (var j = 0; j <= (mh/cell) - 1; j++)
    {
        if (mapGrid[i,j] == 0)
        {
            draw_sprite(map_hide,0,mx+(cell*i),my+(cell*j));
        }
    }
}

//Player marker
draw_sprite(map_player,0,px,py);

The for loop above is currently only checking cells from 0, 0 to the max width and height of the mini map. I need its position to shift relative to the player's position, the way the main sprite of the map does. I can't quite get my head around it, and have tried adding the offset to the array positions like "mapGrid[i+(ox/cell),j+(oy/cell)" which results in out of bounds errors, and I've tried starting the for loop from the offset instead of from 0, which I couldn't get to work either.

I'm running out of ideas, any suggestions are appreciated.
 
Last edited:
I am not sure if clearly understand your issue but to convert the players position into a grid position you can use this:

GML:
var mw = cell * 5;
var mh = cell * 3; 
player_x_in_grid = x div mw;
player_y_in_grid = y div mh;
let me know if this helped you
 
Top