SirCumferance icon

GameMaker - Hexagonal Grid Drawing

SirCumferance | PRO | 01/06/22 04:22:52 PM UTC | 0 ⭐ | 12337 👁️ | Never ⏰ | []
Game Maker |

0 B

|

None

|

0 👍

/

0 👎

// Hexagon Manipulation
 
// Macros
// Manupulate hex_size to chance the size
#macro hex_size 0.5
#macro hex_side 16*hex_size
#macro hex_radius_type_normal 1
#macro hex_radius_type_offset 2
#macro hex_radius_type_extra 3
#macro hex_apothem 13.8564*hex_size
 
// This will result in a point from the x and y the determined distance and angle
function Vector2(vx, vy, vdist, vdir) constructor {
    x = vx+lengthdir_x(vdist, vdir);
    y = vy+lengthdir_y(vdist, vdir);
}
 
// This is to draw a single hexagon
// You can change whatever it is inside this and it will be replicated to every hexagon in the grid
function draw_hex(xHex, yHex) {
    
    // Draw the six lines of the hexagon
    for (var sp = 0 ; sp < 6 ; sp++) {
        var s1a = sp*60;
        var s2a = (sp+1)*60;
        
        // This is to get the points of the side of the line to be drawed
        var s1 = new Vector2(xHex, yHex, hex_side, s1a);
        var s2 = new Vector2(xHex, yHex, hex_side, s2a);
        
        // Draw a single line of the hexagon
        draw_line_width(s1.x, s1.y, s2.x, s2.y, 1);
        
        // Honestly I just put this just in case
        delete s1;
        delete s2;
    }
}
 
// This is to draw a single hexagon with two lines depending on the
// magnitude(number of hex of the lines) and the angle (needs to be (angle mod 60 == 0) to work)
function draw_hex_vertex(xHex, yHex, magnitude, angle) {
    magnitude = is_undefined(magnitude) ? 0 :  abs(magnitude);
    angle = is_undefined(angle) ? 0 :  abs(angle);
    var cc = draw_get_color();
    draw_set_color(c_purple);
    var hexM = 1;
    
    // Draw the number of hexagons in the desired angle
    repeat(magnitude) {
        var hexDist = (hex_apothem*2*(hexM+0));
        var hexVector1 = new Vector2(xHex, yHex, hexDist, angle-30);
        var hexVector2 = new Vector2(xHex, yHex, hexDist, angle+30);
        
        draw_hex(hexVector1.x, hexVector1.y);
        draw_hex(hexVector2.x, hexVector2.y);
        
        // Honestly I just put this just in case
        delete hexVector1;
        delete hexVector2;
        
        hexM++;
    }
    
    // Draw the central hexagon of the vertex
    draw_set_color(cc);
    draw_hex(xHex, yHex);
}
 
// Draw a central hexagon surrounded by a number of hexagons delimited by the radius
function draw_hex_radius(xHex, yHex, radius) {
    radius = is_undefined(radius) ? 0 :  abs(radius);
    if (radius > 0) {
        for (var hr = 0 ; hr < radius ; hr++) {
            var hexTotal = (hr+1)*6
            for (var sp = 0 ; sp < hexTotal ; sp++) {
                
                var sa = (sp*(360 div clamp(hexTotal, 6, 12)))+90;
                
                // Determine the type of hexagon
                var modRType = hex_radius_type_extra;
                if (((sa-90) mod 60) == 0) {
                    modRType = hex_radius_type_normal;
                } else if ((sa mod 60) == 0) {
                    modRType = hex_radius_type_offset;
                }
                
                // Draw the hexagon if not extra (purple ones)
                if (modRType != hex_radius_type_extra) {
                    if (modRType == hex_radius_type_normal) {
                        var hexDist = (hex_apothem*2*(hr+1));
                        var hexVector = new Vector2(xHex, yHex, hexDist, sa);
                        
                        // Draw the hexagon if normal (green or red)
                        var cc = ((hr mod 2) == 0) ? c_green : c_red;
                        draw_set_color(cc);
                        draw_hex(hexVector.x, hexVector.y);
                    } else if ((hr-1) < floor(radius/2)) {
                        var hexDist = (hex_side*3*(hr));
                        var hexVector = new Vector2(xHex, yHex, hexDist, sa);
                        
                        // Draw the hexagon if offset (yellow)
                        draw_set_color(c_yellow);
                        draw_hex_vertex(hexVector.x, hexVector.y, radius-(2*(hr)), sa);
                    }
                    
                    // Honestly I just put this just in case
                    delete hexVector;
                }
            }
        }
    }
    
    // Draw central hexagon
    draw_set_color(c_ltgrey);
    draw_hex(xHex, yHex);
}
 
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎