Advertisement
Badwrong

GameMaker - Draw Grid Movement Range

Feb 12th, 2023
2,739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GameMaker solution for drawing mp_grid movement
  2. Author: Nathan Hunter, badwronggames@gmail.com
  3.  
  4. //////////////////////////////////////////////
  5.  
  6. MIT License
  7.  
  8. Copyright (c) 2023 Nathan Hunter
  9.  
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16.  
  17. The above copyright notice and this permission notice shall be included in all
  18. copies or substantial portions of the Software.
  19.  
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. SOFTWARE.
  27.  
  28. //////////////////////////////////////////////
  29. SETUP: Copy the code below into a script file
  30. USE: Call the functions mp_grid_draw_move_range() or mp_grid_draw_move_range_at_pixel()
  31.  
  32.  
  33.  
  34. /// @function                       mp_grid_draw_move_range(_sprite, _image_index, _x, _y, _grid, _range)
  35. /// @param {asset}   _sprite        The sprite to draw, should be same size as the mp_grid's cells
  36. /// @param {integer} _image_index   The index of the sprite to use
  37. /// @param {integer} _x             The x position in the grid (not room coordinates)
  38. /// @param {integer} _y             The y position in the grid (not room coordinates)
  39. /// @param {index}   _grid          The mp_grid to use
  40. /// @param {integer} _range         The movement range
  41. /// @description                    Draws a sprite at all valid cells in an mp_grid within the given movement range from a cell at x and y
  42. function mp_grid_draw_move_range(_sprite, _image_index, _x, _y, _grid, _range)
  43. {
  44.     // A grid to track visisted cells and its size
  45.     static _visited = ds_grid_create(0, 0);
  46.     static _size = 0;
  47.    
  48.     // Size is double plus one on both horizontal and vertical
  49.     var _new_size = _range * 2 + 1,
  50.         _start    = _range;
  51.        
  52.     // Resize the grid if size changed
  53.     if (_size != _new_size)
  54.     {
  55.         _size = _new_size;
  56.         ds_grid_resize(_visited, _new_size, _new_size);
  57.     }
  58.    
  59.     // Clear all to -1
  60.     ds_grid_clear(_visited, -1);
  61.    
  62.     // Begin recursive call at this cell
  63.     __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x, _y, _grid, _range, _visited, _start, _start);
  64. }
  65.  
  66. /// @function                       mp_grid_draw_move_range_at_pixel(_sprite, _image_index, _x, _y, _grid, _range)
  67. /// @param {asset}   _sprite        The sprite to draw, should be same size as the mp_grid's cells
  68. /// @param {integer} _image_index   The index of the sprite to use
  69. /// @param {integer} _x             The x position in the room
  70. /// @param {integer} _y             The y position in the room
  71. /// @param {index}   _grid          The mp_grid to use
  72. /// @param {integer} _range         The movement range
  73. /// @description                    Draws a sprite at all valid cells in an mp_grid within the given movement range from the x and y position
  74. function mp_grid_draw_move_range_at_pixel(_sprite, _image_index, _x, _y, _grid, _range)
  75. {
  76.     // Wrapper function of mp_grid_draw_move_range() that uses absolute coordinates instead grid cell position
  77.     mp_grid_draw_move_range(_sprite, _image_index, _x div sprite_get_width(_sprite), _y div sprite_get_height(_sprite), _grid, _range);
  78. }
  79.  
  80. /// @function       __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x, _y, _grid, _range)
  81. /// @description    Recursive function call for mp_grid_draw_move_range functions. DO NOT call directly!
  82. function __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x, _y, _grid, _range, _visited, _vx, _vy)
  83. {
  84.     // Stop here if this cell is occupied
  85.     if (mp_grid_get_cell(_grid, _x, _y) == -1)
  86.         return;
  87.    
  88.     // If a equal or greater cost is here then its already been checked
  89.     var _cost = _visited[# _vx, _vy];
  90.     if (_cost >= _range)
  91.         return;
  92.    
  93.     // Nothing was drawn yet if still -1
  94.     if (_cost == -1)
  95.         draw_sprite(_sprite, _image_index,
  96.             _x * sprite_get_width(_sprite)  + sprite_get_xoffset(_sprite),
  97.             _y * sprite_get_height(_sprite) + sprite_get_yoffset(_sprite));
  98.    
  99.     // Track the current range in this cell
  100.     _visited[# _vx, _vy] = _range;
  101.        
  102.     // Check if finished
  103.     if (--_range == -1)
  104.         return;
  105.    
  106.     // Call recursion on the fource adjacent cells
  107.     __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x + 1, _y, _grid, _range, _visited, _vx + 1, _vy); 
  108.     __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x - 1, _y, _grid, _range, _visited, _vx - 1, _vy); 
  109.     __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x, _y + 1, _grid, _range, _visited, _vx, _vy + 1); 
  110.     __mp_grid_draw_move_range_recursive(_sprite, _image_index, _x, _y - 1, _grid, _range, _visited, _vx, _vy - 1);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement