PirateTreasureHunt/scenes/Grid.gd

61 lines
2.0 KiB
GDScript

extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
export(Array) var view_grid_corners = [] setget set_view_grid_corners
var view_camera = null setget set_view_camera
var last_grid_corners = []
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func set_view_grid_corners(value: Array):
view_grid_corners = value
return view_grid_corners
func set_view_camera(value: Camera2D):
view_camera = value
return view_camera
func calc_grid_view_rect():
var view_tl = Vector2(-Globals.hex_size,-Globals.hex_size)
var view_br = OS.get_window_safe_area().size - view_tl
var top_left = Globals.ScreenToHex(view_tl, view_camera)
var top_right = Globals.ScreenToHex(Vector2(view_br.x, view_tl.y), view_camera)
var bottom_left = Globals.ScreenToHex(Vector2(view_tl.x, view_br.y), view_camera)
var bottom_right = Globals.ScreenToHex(view_br, view_camera)
return [top_left, top_right, bottom_left, bottom_right]
func _process(_delta):
var view_grid_corners = calc_grid_view_rect()
if view_grid_corners != last_grid_corners:
update()
func _draw():
var view_grid_corners = calc_grid_view_rect()
last_grid_corners = view_grid_corners
var tl_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[0])
var tr_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[1])
var bl_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[2])
var br_tile_coords = Globals.HexGrid.get_hex_center(view_grid_corners[3])
var nc = ceil ((tr_tile_coords[0] - tl_tile_coords[0]) * 1.5 / Globals.hex_size)
var nr = ceil ((bl_tile_coords[1] - tr_tile_coords[1]) / Globals.hex_size) + 2
for c in range (nc):
var dx = 0.75 * Globals.hex_size * c
var dy = (c % 2) * sqrt(3) * 0.25 * Globals.hex_size
for r in range (nr):
draw_set_transform(tl_tile_coords + Vector2(dx, r * Globals.hex_size * sqrt(3) * 0.5 + dy), 0, Vector2.ONE)
draw_polyline(HexTileDrawer.HexPoints, "#888", 1 * view_camera.zoom.x)