Advertisement
SirCumferance

GameMaker - Hexagonal Grid Drawing

Jan 6th, 2022
2,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Hexagon Manipulation
  2.  
  3. // Macros
  4. // Manupulate hex_size to chance the size
  5. #macro hex_size 0.5
  6. #macro hex_side 16*hex_size
  7. #macro hex_radius_type_normal 1
  8. #macro hex_radius_type_offset 2
  9. #macro hex_radius_type_extra 3
  10. #macro hex_apothem 13.8564*hex_size
  11.  
  12. // This will result in a point from the x and y the determined distance and angle
  13. function Vector2(vx, vy, vdist, vdir) constructor {
  14.     x = vx+lengthdir_x(vdist, vdir);
  15.     y = vy+lengthdir_y(vdist, vdir);
  16. }
  17.  
  18. // This is to draw a single hexagon
  19. // You can change whatever it is inside this and it will be replicated to every hexagon in the grid
  20. function draw_hex(xHex, yHex) {
  21.    
  22.     // Draw the six lines of the hexagon
  23.     for (var sp = 0 ; sp < 6 ; sp++) {
  24.         var s1a = sp*60;
  25.         var s2a = (sp+1)*60;
  26.        
  27.         // This is to get the points of the side of the line to be drawed
  28.         var s1 = new Vector2(xHex, yHex, hex_side, s1a);
  29.         var s2 = new Vector2(xHex, yHex, hex_side, s2a);
  30.        
  31.         // Draw a single line of the hexagon
  32.         draw_line_width(s1.x, s1.y, s2.x, s2.y, 1);
  33.        
  34.         // Honestly I just put this just in case
  35.         delete s1;
  36.         delete s2;
  37.     }
  38. }
  39.  
  40. // This is to draw a single hexagon with two lines depending on the
  41. // magnitude(number of hex of the lines) and the angle (needs to be (angle mod 60 == 0) to work)
  42. function draw_hex_vertex(xHex, yHex, magnitude, angle) {
  43.     magnitude = is_undefined(magnitude) ? 0 :  abs(magnitude);
  44.     angle = is_undefined(angle) ? 0 :  abs(angle);
  45.     var cc = draw_get_color();
  46.     draw_set_color(c_purple);
  47.     var hexM = 1;
  48.    
  49.     // Draw the number of hexagons in the desired angle
  50.     repeat(magnitude) {
  51.         var hexDist = (hex_apothem*2*(hexM+0));
  52.         var hexVector1 = new Vector2(xHex, yHex, hexDist, angle-30);
  53.         var hexVector2 = new Vector2(xHex, yHex, hexDist, angle+30);
  54.        
  55.         draw_hex(hexVector1.x, hexVector1.y);
  56.         draw_hex(hexVector2.x, hexVector2.y);
  57.        
  58.         // Honestly I just put this just in case
  59.         delete hexVector1;
  60.         delete hexVector2;
  61.        
  62.         hexM++;
  63.     }
  64.    
  65.     // Draw the central hexagon of the vertex
  66.     draw_set_color(cc);
  67.     draw_hex(xHex, yHex);
  68. }
  69.  
  70. // Draw a central hexagon surrounded by a number of hexagons delimited by the radius
  71. function draw_hex_radius(xHex, yHex, radius) {
  72.     radius = is_undefined(radius) ? 0 :  abs(radius);
  73.     if (radius > 0) {
  74.         for (var hr = 0 ; hr < radius ; hr++) {
  75.             var hexTotal = (hr+1)*6
  76.             for (var sp = 0 ; sp < hexTotal ; sp++) {
  77.                
  78.                 var sa = (sp*(360 div clamp(hexTotal, 6, 12)))+90;
  79.                
  80.                 // Determine the type of hexagon
  81.                 var modRType = hex_radius_type_extra;
  82.                 if (((sa-90) mod 60) == 0) {
  83.                     modRType = hex_radius_type_normal;
  84.                 } else if ((sa mod 60) == 0) {
  85.                     modRType = hex_radius_type_offset;
  86.                 }
  87.                
  88.                 // Draw the hexagon if not extra (purple ones)
  89.                 if (modRType != hex_radius_type_extra) {
  90.                     if (modRType == hex_radius_type_normal) {
  91.                         var hexDist = (hex_apothem*2*(hr+1));
  92.                         var hexVector = new Vector2(xHex, yHex, hexDist, sa);
  93.                        
  94.                         // Draw the hexagon if normal (green or red)
  95.                         var cc = ((hr mod 2) == 0) ? c_green : c_red;
  96.                         draw_set_color(cc);
  97.                         draw_hex(hexVector.x, hexVector.y);
  98.                     } else if ((hr-1) < floor(radius/2)) {
  99.                         var hexDist = (hex_side*3*(hr));
  100.                         var hexVector = new Vector2(xHex, yHex, hexDist, sa);
  101.                        
  102.                         // Draw the hexagon if offset (yellow)
  103.                         draw_set_color(c_yellow);
  104.                         draw_hex_vertex(hexVector.x, hexVector.y, radius-(2*(hr)), sa);
  105.                     }
  106.                    
  107.                     // Honestly I just put this just in case
  108.                     delete hexVector;
  109.                 }
  110.             }
  111.         }
  112.     }
  113.    
  114.     // Draw central hexagon
  115.     draw_set_color(c_ltgrey);
  116.     draw_hex(xHex, yHex);
  117. }
  118.  
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement