From 0194d7334505c777e69eba5cee64669c6d8fee42 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 17 Jun 2021 23:07:57 +0200 Subject: [PATCH] Optimized grid tile rendering --- scenes/World.gd | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/scenes/World.gd b/scenes/World.gd index 9c4d1db..8f3a830 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -1,11 +1,12 @@ extends Node2D -onready var HexTile = get_node("HexTile") +onready var HexGridDrawer = get_node("HexGridDrawer") onready var WorldCamera = get_node("Camera") onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue") onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var PlayerChar = get_node("PlayerChar") +onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue") var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() @@ -20,22 +21,35 @@ var target = Vector2() # Called when the node enters the scene tree for the first time. func _ready(): HexGrid.hex_scale = Vector2(hex_size, hex_size) - HexTile.hex_scale = Vector2(hex_size, hex_size) # Set player starting position PlayerChar.position = HexGrid.get_hex_center(Vector2(0,0)) + HexGridDrawer.hex_scale = Vector2(hex_size, hex_size) func _draw(): - for r in range(hex_grid_bbox[0][0], hex_grid_bbox[1][0]): - for c in range(hex_grid_bbox[0][1], hex_grid_bbox[1][1]): - var coords = HexGrid.get_hex_center(Vector2(r,c)) - draw_set_transform(coords, 0, Vector2.ONE) - draw_polygon (HexTile.HexPoints, HexTile.HexColors[HexTile.TileType.None]) - draw_polyline(HexTile.HexPoints, "#888", 1 * WorldCamera.zoom.x) + var view_grid_corners = calc_grid_view_rect() + + var tl_tile_coords = HexGrid.get_hex_center(view_grid_corners[0]) + var tr_tile_coords = HexGrid.get_hex_center(view_grid_corners[1]) + var bl_tile_coords = HexGrid.get_hex_center(view_grid_corners[2]) + var br_tile_coords = HexGrid.get_hex_center(view_grid_corners[3]) + + print (tl_tile_coords, bl_tile_coords) + + var nc = ceil ((tr_tile_coords[0] - tl_tile_coords[0]) * 1.5 / hex_size) + var nr = ceil ((bl_tile_coords[1] - tr_tile_coords[1]) / hex_size) + 2 + + for c in range (nc): + var dx = 0.75 * hex_size * c + var dy = (c % 2) * sqrt(3) * 0.25 * hex_size #(c % 2) * sqrt(3) * hex_size * 0.75 + for r in range (nr): + draw_set_transform(tl_tile_coords + Vector2(dx, r * hex_size * sqrt(3) * 0.5 + dy), 0, Vector2.ONE) + draw_polygon (HexGridDrawer.HexPoints, HexGridDrawer.HexColors[HexGridDrawer.TileType.None]) + draw_polyline(HexGridDrawer.HexPoints, "#888", 1 * WorldCamera.zoom.x) var coords = HexGrid.get_hex_center (hex_hover) draw_set_transform(coords, 0, Vector2.ONE) - draw_polyline(HexTile.HexPoints, "#f00", 2 * WorldCamera.zoom.x) + draw_polyline(HexGridDrawer.HexPoints, "#f00", 2 * WorldCamera.zoom.x) func screen_to_world(pos: Vector2): pos = pos - OS.get_window_safe_area().size * 0.5 @@ -47,6 +61,16 @@ func screen_to_hex(pos: Vector2): func world_to_screen(pos: Vector2): return pos * WorldCamera.zoom + WorldCamera.offset +func calc_grid_view_rect(): + var view_tl = Vector2(-hex_size,-hex_size) + var view_br = OS.get_window_safe_area().size - view_tl + + var top_left = screen_to_hex (view_tl) + var top_right = screen_to_hex (Vector2(view_br.x, view_tl.y)) + var bottom_left = screen_to_hex (Vector2(view_tl.x, view_br.y)) + var bottom_right = screen_to_hex (view_br) + return [top_left, top_right, bottom_left, bottom_right] + func _unhandled_input(event): if event is InputEventMouseButton: # Move main character @@ -54,7 +78,7 @@ func _unhandled_input(event): PlayerChar.target = HexGrid.get_hex_center(screen_to_hex(event.position)) # Move camera - if event.pressed and event.button_index == 3: + if event.pressed and event.button_index == 2: is_dragging = true drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position) else: