GMS 2 [solved] Draw border around specific grid cells

Discussion in 'Programming' started by trentallain, Jun 10, 2019.

  1. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    471
    Code:
    // Look for attack positions
    for (var u = _begin_x; u < _end_x+1; u ++) {
        for (var v = _begin_y; v < _end_y+1; v ++) {
            // If the location is within range
            if global.grid_potential_range[# u, v] != -1 {
                // If the enemy is at that location
                if global.grid_potential_range[# u, v] == 0 {
                    draw_sprite(spr_grid_hover,2,19+19*u,19+19*v);
                }
                // If there are no more spaces in range right
                if global.grid_potential_range[# u + 1, v] != -1 {
                    draw_sprite_ext(spr_grid_range_border,0,19+19*u+10,19+19*v+10,1,1,0,c_white,1);
                }
                // Up
                if global.grid_potential_range[# u, v - 1] != -1 {
                    draw_sprite_ext(spr_grid_range_border,0,19+19*u+10,19+19*v+10,1,1,90,c_white,1);
                }
                // Left
                if global.grid_potential_range[# u - 1, v] != -1 {
                    draw_sprite_ext(spr_grid_range_border,0,19+19*u+10,19+19*v+10,1,1,180,c_white,1);
                }
                // Down
                if global.grid_potential_range[# u, v + 1] != -1 {
                    draw_sprite_ext(spr_grid_range_border,0,19+19*u+10,19+19*v+10,1,1,270,c_white,1);
                }
            }
        }
    }
    Can someone find what is wrong with this please? What I'm trying to do is draw a sprite to create a border around grid cells that you can attack to. All works fine and looks like they are in the correct positions, however the sprite doesn't rotate as expected. The sprite is the same with as 1 grid cell, with a centered origin and the border is on the right of the sprite.

    This is what it looks like (the red):
    Untitledw.png
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,086
    Do you want to mark all cells that are in attack range or do you want to create a border on the most-outward cells?
    What result are you expecting, can you make an image that shows how you want it?

    EDIT:
    Also, you seem to be having a lot of magic numbers in your code.
    like
    Code:
    19 + 19 * u + 10
    and
    19 + 19 * v + 10
    
    these are oddly specific.
    Why start with 19, why add 10.
    Why are your cells 19x19 pixels?
     
    Last edited: Jun 10, 2019
  3. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    471
    278060245.png
    So it should create a border like this, with the purple being the starting grid X and Y, and the yellow being an enemy in the picture.
     
  4. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    471
    That's a 19 grid offset, 10 to put the origin in the centre of the cell. They are 19x19 so that it can have a 1px border.
     
  5. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,086
    I see.
    In your code, you compare these cells against -1.
    What is -1? Is it a cell that's unreachable? or a free cell?

    Try reversing the logic and checking the cells equal to -1 to then draw a border.

    What you can do to avoid this coordinate mess is using sub-images instead of rotation, then you can keep your sprite origin at x0 and y0.
     
  6. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    471
    -1 are the cells that aren't in range. 0 is if there is an enemy, and it is in range. 1 is if it is in range but no enemy.
     
  7. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,086
    Can you try this code
    Code:
    /*
    -1 = not in range
     0 = enemy at cell
     1 = in range
    */
    
    for (var u = _begin_x; u < _end_x+1; u++) {
    for (var v = _begin_y; v < _end_y+1; v++) {
       
        // If the location is within range
        if global.grid_potential_range[# u, v] != -1 {
            // get base positions
            var xx = 19 + 19 * u;
            var yy = 19 + 19 * v;
           
            // If the enemy is at that location
            if global.grid_potential_range[# u, v] == 0 {
                draw_sprite(spr_grid_hover, 2, xx, yy);
            }
           
            // check neighbour cells
            for (n = 0; n < 4; n++) {
               
                // get cell coordinates around base cell
                var dir = n * 90;
                var uu = u + lengthdir_x(1, dir);
                var vv = v + lengthdir_y(1, dir);
               
                // check if unreachable (or enemy there) and draw border
                /*
                version with sub-images
                if (global.grid_potential_range[# uu, vv] != 1) {
                    draw_sprite(spr_grid_range_border, n, xx, yy);
                }
                */
               
                // version with rotation
                if (global.grid_potential_range[# uu, vv] != 1) {
                    draw_sprite_ext(spr_grid_range_border, 0, xx+10, yy+10, 1, 1, dir, c_white, 1);
                }
            }
        }
    }}
    
    
     
    trentallain likes this.
  8. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    471
    Thanks it worked perfectly!
     
  9. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    471
    PS: To stop the out of bounds error add this line:
    Code:
    // Stop out of bounds error
    if uu == -1 || vv == -1 || uu == _grid_w || vv == _grid_h {
        draw_sprite_ext(spr_grid_range_border, 0, xx+10, yy+10, 1, 1, dir, c_white, 1);
    }
    else if global.grid_potential_range[# uu, vv] != 1 {
        draw_sprite_ext(spr_grid_range_border, 0, xx+10, yy+10, 1, 1, dir, c_white, 1);
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice