Advertisement
Guest User

Hexagonal grid glass for Godot

a guest
May 24th, 2021
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 20.24 KB | None | 0 0
  1. extends Node
  2.  
  3. # Edited 24.05.2021
  4. # Add some useful functions
  5. # and cleaned up code
  6.  
  7. enum HexTypes{
  8.     vert,
  9.     hor,
  10. }
  11.  
  12. enum GridTypes{
  13.     rect,
  14.     hex
  15. }
  16.  
  17. enum Views{
  18.     iso,
  19.     front,
  20. }
  21.  
  22. const hex_type = HexTypes.vert
  23. const grid_type = GridTypes.hex
  24. const view = Views.iso
  25. const size = 24
  26. const iso_scale = 2.0
  27. const hex_map_size = Vector2(9, 7)
  28.  
  29. var hex_basis:Transform2D
  30. var grid_basis:Transform2D
  31.  
  32. var grid_map_size:Vector2
  33.  
  34. class PriorityStack:
  35.    
  36.     var items:Array
  37.    
  38.     func _init():
  39.         items = Array()
  40.        
  41.     func empty() -> bool:
  42.         return items.size() == 0
  43.        
  44.     func put(item, priority:int) -> void:
  45.         if empty():
  46.             items.append([item, priority])
  47.         elif priority <= items[0][1]:
  48.             items.insert(0, [item, priority])
  49.         elif priority > items[-1][1]:
  50.             items.append([item, priority])
  51.         else:
  52.             for i in range(len(items)):
  53.                 if priority <= items[i][1]:
  54.                     items.insert(i, [item, priority])
  55.                     break
  56.                    
  57.     func take():
  58.         return items.pop_front()[0]
  59.  
  60. func _init():
  61.     var short = int(size*sqrt(3)/2) # 1/2 from short hex diagonal
  62.     var long = int(size/2) # 1/4 from long hex diagonal
  63.    
  64.     hex_basis = Transform2D()
  65.     grid_basis = Transform2D()
  66.    
  67.     grid_map_size = Vector2()
  68.    
  69.     match hex_type:
  70.         HexTypes.vert:
  71.             match view:
  72.                 Views.iso:
  73.                     var pw = int(long*cos(PI/4))
  74.                     var ph = int(short*cos(PI/4))
  75.                    
  76.                     grid_basis.x = Vector2(pw, pw/iso_scale)
  77.                     grid_basis.y = Vector2(-ph, ph/iso_scale)
  78.                    
  79.                 Views.front:
  80.                     grid_basis.x = Vector2(long, 0)
  81.                     grid_basis.y = Vector2(0, short/iso_scale)
  82.                 _:
  83.                     grid_basis.x = Vector2(long, 0)
  84.                     grid_basis.y = Vector2(0, short/iso_scale)
  85.            
  86.             hex_basis.x = grid_basis.x*3 + grid_basis.y
  87.             hex_basis.y = grid_basis.y*2
  88.            
  89.         HexTypes.hor:
  90.             match view:
  91.                 Views.iso:
  92.                     var pw = int(short*cos(PI/4))
  93.                     var ph = int(long*cos(PI/4))
  94.                    
  95.                     grid_basis.x = Vector2(pw, pw/iso_scale)
  96.                     grid_basis.y = Vector2(-ph, ph/iso_scale)
  97.                    
  98.                 Views.front:
  99.                     grid_basis.x = Vector2(short, 0)
  100.                     grid_basis.y = Vector2(0, long/iso_scale)
  101.                    
  102.                 _:
  103.                     grid_basis.x = Vector2(short, 0)
  104.                     grid_basis.y = Vector2(0, long/iso_scale)
  105.            
  106.             hex_basis.x = grid_basis.x*2
  107.             hex_basis.y = grid_basis.x+grid_basis.y*3
  108.            
  109.     match grid_type:
  110.         GridTypes.rect:
  111.             match hex_type:
  112.                 HexTypes.vert:
  113.                     grid_map_size.x = hex_map_size.x*3+1
  114.                     grid_map_size.y = hex_map_size.y*2
  115.                 HexTypes.hor:
  116.                     grid_map_size.x = hex_map_size.x*2
  117.                     grid_map_size.y = hex_map_size.y*3+1
  118.                    
  119.         GridTypes.hex:
  120.             var diagonal = hex_map_size.x*2-1
  121.             match hex_type:
  122.                 HexTypes.vert:
  123.                     grid_map_size.x = diagonal*3+1
  124.                     grid_map_size.y = diagonal*2
  125.                 HexTypes.hor:
  126.                     grid_map_size.x = diagonal*2
  127.                     grid_map_size.y = diagonal*3+1
  128.  
  129. # ###########################################
  130. # _____CONVAERSATIONS_________
  131. # Here are functions for conversations
  132. # between screen and grid values
  133. # ###########################################
  134.  
  135. func cell2pixel(cell:Vector2) -> Vector2:
  136.     return cell.x*grid_basis.x + cell.y*grid_basis.y
  137.  
  138. func pixel2cell(pixel:Vector2, rounding=true) -> Vector2:
  139.     var res = grid_basis.affine_inverse().xform(pixel)
  140.     if rounding: res = res.floor()
  141.     return res
  142.  
  143. func hex2pixel(hex:Vector2) -> Vector2:
  144.     return hex.x*hex_basis.x + hex.y*hex_basis.y
  145.  
  146. func pixel2hex(pixel:Vector2) -> Vector2:
  147.     return round_hex(hex_basis.affine_inverse().xform(pixel))
  148.  
  149. func round_hex(hex:Vector2) -> Vector2:
  150.     var rx = round(hex.x)
  151.     var ry = round(hex.y)
  152.     var rz = round(-hex.x-hex.y)
  153.    
  154.     var x_diff = abs(hex.x-rx)
  155.     var y_diff = abs(hex.y-ry)
  156.     var z_diff = abs(-hex.x-hex.y-rz)
  157.     if x_diff > y_diff and x_diff > z_diff:
  158.         rx = -ry-rz
  159.     elif y_diff > z_diff:
  160.         ry = -rx-rz
  161.     return Vector2(rx, ry)
  162.  
  163. func get_center_cell(hex:Vector2) -> Vector2:
  164.     if hex_type == HexTypes.vert:
  165.         return Vector2(hex.x*3, hex.y*2+hex.x)
  166.     else: # Horizontal
  167.         return Vector2(hex.x*2+hex.y, hex.y*3)
  168.  
  169. func linear2iso(angle:float) -> float:
  170.     return pixel2cell(Vector2(cos(angle), sin(angle)), false).angle()
  171.    
  172. func iso2linear(angle:float) -> float:
  173.     return cell2pixel(Vector2(cos(angle), sin(angle))).angle()
  174.  
  175. # ###########################################
  176. # _____GRID_MANIPULATING_________
  177. # Here are functions for working
  178. # at grid
  179. # ###########################################
  180.  
  181. func direct_hex(hex1:Vector2, hex2:Vector2) -> Vector2:
  182.     var dx = hex2.x - hex1.x
  183.     var dy = hex2.y - hex1.y
  184.     var dz = -hex2.x-hex2.y + hex1.x+hex1.y
  185.     if dx == 0:
  186.         return Vector2(0, sign(dy))
  187.     elif dy == 0:
  188.         return Vector2(sign(dx), 0)
  189.     elif dz == 0:
  190.         return Vector2(sign(dx), sign(dy))
  191.     else:
  192.         if abs(dz) > abs(dx) and abs(dz) > abs(dy):
  193.             if abs(dx) > abs(dy):
  194.                 return Vector2(sign(dx), 0)
  195.             else:
  196.                 return Vector2(0, sign(dy))
  197.         elif abs(dy) > abs(dx):
  198.             if abs(dz) > abs(dx):
  199.                 return Vector2(0, sign(dy))
  200.             else:
  201.                 return Vector2(sign(dx), sign(dy))
  202.         else:
  203.             if abs(dy) > abs(dz):
  204.                 return Vector2(sign(dx), sign(dy))
  205.             else:
  206.                 return Vector2(sign(dx), 0)
  207.  
  208. func in_map(hex:Vector2) -> bool:
  209.     match grid_type:
  210.         GridTypes.hex:
  211.             if hex_type == HexTypes.hor:
  212.                 return _in_hex_grid_hor(hex)
  213.             else: # Vertical
  214.                 return _in_hex_grid_vert(hex)
  215.         GridTypes.rect:
  216.             if hex_type == HexTypes.vert:
  217.                 return _in_rect_grid_vert(hex)
  218.             else: # Hor orientation
  219.                 return _in_rect_grid_hor(hex)
  220.         _:
  221.             return true
  222.  
  223. func get_map_center() -> Vector2:
  224.     match grid_type:
  225.         GridTypes.hex:
  226.             if hex_type == HexTypes.hor:
  227.                 return _get_hor_hex_map_center()
  228.             else: # Vertical
  229.                 return _get_vert_hex_map_center()
  230.         GridTypes.rect:
  231.             return Vector2() # Rect map has no center
  232.         _:
  233.             return Vector2()
  234.  
  235. func can_stand(hex:Vector2, obsts:PoolVector2Array) -> bool:
  236.     return in_map(hex) and not (hex in obsts)
  237.  
  238. func neighbors(hex_pos:Vector2, obsts:PoolVector2Array) -> PoolVector2Array:
  239.     var res:PoolVector2Array = []
  240.     var _neighbors = PoolVector2Array([Vector2(-1, 0), Vector2(1, -1), Vector2(0, -1), Vector2(1, 0), Vector2(0, 1), Vector2(-1, 1)])
  241.     for i in _neighbors:
  242.         if can_stand(i+hex_pos, obsts):
  243.             res.append(i+hex_pos)
  244.     return res
  245.  
  246. func find_path(start:Vector2, goal:Vector2, obsts:PoolVector2Array) -> PoolVector2Array:
  247.     var frontier = PriorityStack.new()
  248.     frontier.put(start, 0)
  249.     var came_from = {}
  250.     var cost_so_far = {}
  251.     came_from[start] = start
  252.     cost_so_far[start] = 0
  253.    
  254.     var current:Vector2
  255.     var new_cost:int
  256.    
  257.     if not can_stand(goal, obsts):
  258.         return PoolVector2Array()
  259.        
  260.     while not frontier.empty():
  261.         current = frontier.take()
  262.        
  263.         if current == goal:
  264.             break
  265.            
  266.         for next in neighbors(current, obsts):
  267.             new_cost = cost_so_far[current] + 1
  268.                
  269.             if not (next in cost_so_far) or new_cost < cost_so_far[next]:
  270.                 cost_so_far[next] = new_cost
  271.                 frontier.put(next, new_cost+hex_distance(goal, next))
  272.                 came_from[next] = current
  273.                
  274.     if frontier.empty() and current != goal:
  275.         return PoolVector2Array()
  276.        
  277.     current = goal
  278.     var path = PoolVector2Array([current])
  279.    
  280.     while current != start:
  281.         current = came_from[current]
  282.         path.append(current)
  283.    
  284.     path.invert()
  285.     path.remove(0) # removes first position
  286.     return path
  287.  
  288. func rast_line(hex1:Vector2, hex2:Vector2) -> PoolVector2Array:
  289.     var N = hex_distance(hex1, hex2)
  290.     if N == 0: return PoolVector2Array([hex1])
  291.     var res = PoolVector2Array()
  292.     for i in range(N+1):
  293.         res.append(round_hex(lerp(hex1, hex2, i/N)))
  294.     return res
  295.  
  296. func hex_distance(hex1:Vector2, hex2:Vector2) -> int:
  297.     var dif = (hex2-hex1)
  298.     return int(abs(dif.x) + abs(dif.y) + abs(-dif.x-dif.y))/2
  299.  
  300.  
  301. # ###########################################
  302. # _____DRAWING_________
  303. # Here are functions for drawing
  304. # at specific grid according to settings
  305. # ###########################################
  306.  
  307. func draw_hex(hex:Vector2, surf:RID, color:Color, width=1.0, antialiasing=false):
  308.     match hex_type:
  309.         HexTypes.vert:
  310.             _draw_vert_hex(hex, surf, color, width, antialiasing)
  311.         HexTypes.hor:
  312.             _draw_hor_hex(hex, surf, color, width, antialiasing)
  313.  
  314. func fill_hex(hex:Vector2, surf:RID, color:Color, antialiasing):
  315.     if hex_type == HexTypes.hor:
  316.         _fill_hor_hex(hex, surf, color, antialiasing)
  317.     else: # Vertical
  318.         _fill_vert_hex(hex, surf, color, antialiasing)
  319.  
  320. func draw_cell(cell:Vector2, surf:RID, color:Color, width=1.0, antialiasing=false):
  321.     var pixel = cell2pixel(cell)
  322.     var points = PoolVector2Array([
  323.         pixel,
  324.         pixel+grid_basis.x,
  325.         pixel+grid_basis.x+grid_basis.y,
  326.         pixel+grid_basis.y,
  327.         pixel,
  328.     ])
  329.     VisualServer.canvas_item_add_polyline(surf, points, [color], width, antialiasing)
  330.    
  331. func fill_cell(cell:Vector2, surf:RID, color:Color, antialiasing=false):
  332.     var pixel = cell2pixel(cell)
  333.     var points = PoolVector2Array([
  334.         pixel,
  335.         pixel+grid_basis.x,
  336.         pixel+grid_basis.x+grid_basis.y,
  337.         pixel+grid_basis.y,
  338.     ])
  339.     VisualServer.canvas_item_add_polygon(surf, points, [color], [], RID(), RID(), antialiasing)
  340.  
  341. func draw_auxiliary_grid(surf:RID, color:Color, width=1.0, antialiasing=false):
  342.     var offset:Vector2
  343.     if hex_type == HexTypes.hor:
  344.         offset = grid_basis.x+grid_basis.y*2
  345.         if grid_type == GridTypes.hex and int(hex_map_size.x)%2 == 0:
  346.             offset += grid_basis.x
  347.     else:
  348.         offset = grid_basis.x*2+grid_basis.y
  349.         if grid_type == GridTypes.hex and int(hex_map_size.x)%2 == 0:
  350.             offset += grid_basis.y
  351.     for i in grid_map_size.x+1:
  352.         VisualServer.canvas_item_add_line(surf, grid_basis.x*i-offset, grid_basis.x*i+grid_basis.y*grid_map_size.y-offset, color, width, antialiasing)
  353.     for i in grid_map_size.y+1:
  354.         VisualServer.canvas_item_add_line(surf, grid_basis.y*i-offset, grid_basis.x*grid_map_size.x+grid_basis.y*i-offset, color, width, antialiasing)
  355.  
  356. func draw_grid(surf:RID, color:Color, width=1.0, antialiasing=false):
  357.     match grid_type:
  358.         GridTypes.rect:
  359.             match hex_type:
  360.                 HexTypes.vert:
  361.                     _draw_vert_rect_grid(surf, color, width, antialiasing)
  362.                 HexTypes.hor:
  363.                     _draw_hor_rect_grid(surf, color, width, antialiasing)
  364.         GridTypes.hex:
  365.             match hex_type:
  366.                 HexTypes.hor:
  367.                     _draw_hor_hex_grid(surf, color, width, antialiasing)
  368.                 HexTypes.vert:
  369.                     _draw_vert_hex_grid(surf, color, width, antialiasing)
  370.  
  371. func draw_line(surf:RID, hex1:Vector2, hex2:Vector2, color:Color, width=1.0, antialiasing=false):
  372.     var p1 = hex2pixel(hex1)
  373.     var p2 = hex2pixel(hex2)
  374.     VisualServer.canvas_item_add_line(surf, p1, p2, color, width, antialiasing)
  375.  
  376. func draw_polyline(surf:RID, hexes:PoolVector2Array, color:Color, width=1.0, antialiasing=false):
  377.     if len(hexes) < 2: return
  378.     var points = PoolVector2Array()
  379.     for i in hexes:
  380.         points.append(hex2pixel(i))
  381.     VisualServer.canvas_item_add_polyline(surf, points, [color], width, antialiasing)
  382.  
  383. # ###########################################
  384. # _____CALCULATING______
  385. # Here are functions for calculate several
  386. # types of stuff
  387. # ###########################################
  388.  
  389. func _get_vert_hex_vertices(hex:Vector2) -> PoolVector2Array:
  390.     var pixel = hex2pixel(hex)
  391.     return PoolVector2Array([
  392.         pixel+2*grid_basis.x,
  393.         pixel+grid_basis.x+grid_basis.y,
  394.         pixel-grid_basis.x+grid_basis.y,
  395.         pixel-2*grid_basis.x,
  396.         pixel-grid_basis.x-grid_basis.y,
  397.         pixel+grid_basis.x-grid_basis.y
  398.     ])
  399.  
  400. func _get_hor_hex_vertices(hex:Vector2) -> PoolVector2Array:
  401.     var pixel = hex2pixel(hex)
  402.     return PoolVector2Array([
  403.         pixel+grid_basis.x-grid_basis.y,
  404.         pixel+grid_basis.x+grid_basis.y,
  405.         pixel+2*grid_basis.y,
  406.         pixel-grid_basis.x+grid_basis.y,
  407.         pixel-grid_basis.x-grid_basis.y,
  408.         pixel-2*grid_basis.y,
  409.     ])
  410.  
  411. func _get_hor_hex_map_center() -> Vector2:
  412.     return Vector2(int((hex_map_size.x-1)/2), hex_map_size.x-1)
  413.  
  414. func _get_vert_hex_map_center() -> Vector2:
  415.     return Vector2(hex_map_size.x-1, int((hex_map_size.x-1)/2))
  416.  
  417. func _in_hex_grid_hor(hex:Vector2) -> bool:
  418.     var center = _get_hor_hex_map_center()
  419.     var diag = int(hex_map_size.x*2 - 1)
  420.     hex -= center # Vector2 passed by value
  421.     if hex.y < 0:
  422.         return hex.x >= -diag/2+abs(hex.y) and hex.x <= diag/2 and hex.y >= -diag/2 and hex.y <= diag/2
  423.     else:
  424.         return hex.x >= -diag/2 and hex.x <= diag/2-abs(hex.y) and hex.y >= -diag/2 and hex.y <= diag/2
  425.  
  426. func _in_hex_grid_vert(hex:Vector2) -> bool:
  427.     var center = _get_vert_hex_map_center()
  428.     var diag = int(hex_map_size.x*2 - 1)
  429.     hex -= center # Vector2 passed by value
  430.     if hex.x < 0:
  431.         return hex.y >= -diag/2+abs(hex.x) and hex.y <= diag/2 and hex.x >= -diag/2 and hex.x <= diag/2
  432.     else:
  433.         return hex.y >= -diag/2 and hex.y <= diag/2-abs(hex.x) and hex.x >= -diag/2 and hex.x <= diag/2
  434.  
  435. func _in_rect_grid_hor(hex:Vector2) -> bool:
  436.     return hex.x >= -floor(hex.y/2) and hex.x < hex_map_size.x-ceil(hex.y/2) and hex.y < hex_map_size.y and hex.y >= 0
  437.  
  438. func _in_rect_grid_vert(hex:Vector2) -> bool:
  439.     return hex.x >= 0 and hex.x < hex_map_size.x and hex.y >= -floor(hex.x/2) and hex.y < hex_map_size.y-ceil(hex.x/2)
  440.  
  441. # ###########################################
  442. # _____DRAWING_________
  443. # Here are functions for drawing several
  444. # types of content
  445. # ###########################################
  446.  
  447. func _draw_hor_hex(hex:Vector2, surf:RID, color:Color, width=1.0, antialiasing=false):
  448.     var points = _get_hor_hex_vertices(hex)
  449.     points.append(points[0])
  450.     VisualServer.canvas_item_add_polyline(surf, points, [color], width, antialiasing)
  451.    
  452. func _draw_vert_hex(hex:Vector2, surf:RID, color:Color, width=1.0, antialiasing=false):
  453.     var points = _get_vert_hex_vertices(hex)
  454.     points.append(points[0])
  455.     VisualServer.canvas_item_add_polyline(surf, points, [color], width, antialiasing)
  456.    
  457. func _fill_hor_hex(hex:Vector2, surf:RID, color:Color, antialiasing=false):
  458.     var points = _get_hor_hex_vertices(hex)
  459.     VisualServer.canvas_item_add_polygon(surf, points, [color], [], RID(), RID(), antialiasing)
  460.    
  461. func _fill_vert_hex(hex:Vector2, surf:RID, color:Color, antialiasing=false):
  462.     var points = _get_vert_hex_vertices(hex)
  463.     VisualServer.canvas_item_add_polygon(surf, points, [color], [], RID(), RID(), antialiasing)
  464.  
  465. func _draw_vert_rect_grid(surf:RID, color:Color, width=1.0, antialiasing=false):
  466.     var offset = grid_basis.x*2+grid_basis.y
  467.     # Drawing horizontal lines
  468.     for i in range(1, grid_map_size.x, 3):
  469.         for j in range(1-i%2, grid_map_size.y+1, 2):
  470.             VisualServer.canvas_item_add_line(surf, grid_basis.x*i+grid_basis.y*j-offset, grid_basis.x*(i+2)+grid_basis.y*j-offset, color, width, antialiasing)
  471.            
  472.     # Drawing vertices
  473.     for i in range(0, grid_map_size.x, 3):
  474.         for j in range(grid_map_size.y):
  475.             if int(hex_map_size.x)%2 == 1 or not(i == grid_map_size.x-1 and (j == 0 or j == grid_map_size.y-1)):
  476.                 if j%2 == i%2:
  477.                     VisualServer.canvas_item_add_line(surf, grid_basis.x*(i+1)+grid_basis.y*j-offset, grid_basis.x*i+grid_basis.y*(j+1)-offset, color, width, antialiasing)
  478.                 else:
  479.                     VisualServer.canvas_item_add_line(surf, grid_basis.x*i+grid_basis.y*j-offset, grid_basis.x*(i+1)+grid_basis.y*(j+1)-offset, color, width, antialiasing)
  480.  
  481. func _draw_hor_rect_grid(surf:RID, color:Color, width=1.0, antialiasing=false):
  482.     var offset = grid_basis.x+grid_basis.y*2
  483.     # Drawing vertical lines
  484.     for i in range(1, grid_map_size.y, 3):
  485.         for j in range(1-i%2, grid_map_size.x+1, 2):
  486.             VisualServer.canvas_item_add_line(surf, grid_basis.x*j+grid_basis.y*i-offset, grid_basis.x*j+grid_basis.y*(i+2)-offset, color, width, antialiasing)
  487.  
  488.     # Drawing vertices
  489.     for i in range(0, grid_map_size.y, 3):
  490.         for j in range(grid_map_size.x):
  491.             if int(hex_map_size.y)%2 == 1 or not (i == grid_map_size.y-1 and (j == 0 or j == grid_map_size.x-1)):
  492.                 if i%2 == j%2:
  493.                     VisualServer.canvas_item_add_line(surf, grid_basis.x*j+grid_basis.y*(i+1)-offset, grid_basis.x*(j+1)+grid_basis.y*i-offset, color, width, antialiasing)
  494.                 else:
  495.                     VisualServer.canvas_item_add_line(surf, grid_basis.x*j+grid_basis.y*i-offset, grid_basis.x*(j+1)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  496.  
  497. func _draw_hor_hex_grid(surf:RID, color:Color, width=1.0, antialiasing=false):
  498.     var parity = int(hex_map_size.x)%2
  499.     var offset = grid_basis.x+grid_basis.y*2 + grid_basis.x*(1-parity)
  500.     var start
  501.     for i in range(0, grid_map_size.y/2, 3): # Drawing vertices
  502.         start = hex_map_size.x - i/3 - 1
  503.         for j in range(start, grid_map_size.x/2):
  504.             if abs(i%2 - j%2) != parity:
  505.                 # Down diagonal
  506.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*j+grid_basis.y*(i+1)-offset, grid_basis.x*(j+1)+grid_basis.y*i-offset, color, width, antialiasing)
  507.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(i+1)-offset, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*i-offset, color, width, antialiasing)
  508.  
  509.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*j+grid_basis.y*(grid_map_size.y-i-1)-offset, grid_basis.x*(j+1)+grid_basis.y*(grid_map_size.y-i)-offset, color, width, antialiasing)
  510.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(grid_map_size.y-i-1)-offset, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(grid_map_size.y-i)-offset, color, width, antialiasing)
  511.             else:
  512.                 # Top diagonal
  513.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(j)+grid_basis.y*(i)-offset, grid_basis.x*(j+1)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  514.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(i)-offset, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  515.  
  516.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(j)+grid_basis.y*(grid_map_size.y-i)-offset, grid_basis.x*(j+1)+grid_basis.y*(grid_map_size.y-i-1)-offset, color, width, antialiasing)
  517.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(grid_map_size.y-i)-offset, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(grid_map_size.y-i-1)-offset, color, width, antialiasing)
  518.  
  519.     for i in range(1, grid_map_size.y, 3):
  520.         if i <= grid_map_size.y/2:
  521.             start = hex_map_size.x-1 - i/3
  522.         else:
  523.             start = (i-grid_map_size.y/2)/3
  524.  
  525.         for j in range(abs(parity-i%2), grid_map_size.x+1, 2):
  526.             if j >= start and j <= grid_map_size.x-start:
  527.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*j+grid_basis.y*i-offset, grid_basis.x*j+grid_basis.y*(i+2)-offset, color, width, antialiasing)
  528.  
  529. func _draw_vert_hex_grid(surf:RID, color:Color, width=1.0, antialiasing=false):
  530.     var parity = int(hex_map_size.x)%2
  531.     var offset = grid_basis.x*2+grid_basis.y + (1-parity)*grid_basis.y
  532.     var start
  533.     for j in range(0, grid_map_size.x/2, 3): # Drawing vertices
  534.         start = hex_map_size.x - j/3 - 1
  535.         for i in range(start, grid_map_size.y/2):
  536.             if abs(i%2 - j%2) != parity:
  537.                 # Down diagonal
  538.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(j+1)+grid_basis.y*(i)-offset, grid_basis.x*(j)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  539.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(i)-offset, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  540.  
  541.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(j+1)+grid_basis.y*(grid_map_size.y-i)-offset, grid_basis.x*(j)+grid_basis.y*(grid_map_size.y-i-1)-offset, color, width, antialiasing)
  542.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(grid_map_size.y-i)-offset, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(grid_map_size.y-i-1)-offset, color, width, antialiasing)
  543.             else:
  544.                 # Top diagonal
  545.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(j)+grid_basis.y*(i)-offset, grid_basis.x*(j+1)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  546.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(i)-offset, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(i+1)-offset, color, width, antialiasing)
  547.  
  548.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(j)+grid_basis.y*(grid_map_size.y-i)-offset, grid_basis.x*(j+1)+grid_basis.y*(grid_map_size.y-i-1)-offset, color, width, antialiasing)
  549.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*(grid_map_size.x-j)+grid_basis.y*(grid_map_size.y-i)-offset, grid_basis.x*(grid_map_size.x-j-1)+grid_basis.y*(grid_map_size.y-i-1)-offset, color, width, antialiasing)
  550.  
  551.     for i in range(1, grid_map_size.x, 3):
  552.         if i <= grid_map_size.x/2:
  553.             start = hex_map_size.x-1 - i/3
  554.         else:
  555.             start = (i-grid_map_size.x/2)/3
  556.  
  557.         for j in range(abs(parity-i%2), grid_map_size.y+1, 2):
  558.             if j >= start and j <= grid_map_size.y-start:
  559.                 VisualServer.canvas_item_add_line(surf, grid_basis.x*i+grid_basis.y*j-offset, grid_basis.x*(i+2)+grid_basis.y*(j)-offset, color, width, antialiasing)
  560.  
  561.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement