extends Spatial onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() onready var hex_tile_3d = preload("res://scenes/HexTile3D.tscn") onready var player = $Player onready var player_move_component = $Player/Movable # GUI onready var tile_label = $Control/HBoxContainer/tile_label onready var tile_offset_label = $Control/HBoxContainer/tile_offset_label onready var num_tiles_label = $Control/HBoxContainer/num_tiles_label onready var num_active_tiles_label = $Control/HBoxContainer/num_active_tiles_label onready var world_map_texture = $Control/HBoxContainer/WorldMapRect onready var tile_highlight = $TileHighlight onready var stream_container = $StreamContainer onready var stream_active_tiles = $StreamContainer/ActiveTiles onready var world = $World # Streaming variables var last_tile = HexCell.new() var current_tile = HexCell.new() var current_tile_offset = Vector2(0,0) var top_left_cell = HexCell.new() var bottom_right_cell = HexCell.new() var stream_world_rect = Rect2() func _ready(): update_streaming_tiles() func update_streaming_tiles(): var stream_rect_position = hexgrid.get_hex_center3(current_tile) stream_world_rect = Rect2( stream_rect_position.x - stream_container.world_rect.size.x / 2, stream_rect_position.z - stream_container.world_rect.size.y / 2, stream_container.world_rect.size.x, stream_container.world_rect.size.y ) stream_container.world_rect = stream_world_rect top_left_cell = hexgrid.get_hex_at(stream_world_rect.position) bottom_right_cell = hexgrid.get_hex_at(stream_world_rect.end) # print ("update_streaming_tiles: cells: ", top_left_cell.offset_coords, " to ", bottom_right_cell.offset_coords) world.heightmap.lock() for cell_x in range (top_left_cell.offset_coords.x, bottom_right_cell.offset_coords.x + 1): for cell_y in range (bottom_right_cell.offset_coords.y, top_left_cell.offset_coords.y + 1): if cell_x < 0 || cell_x >= world.size || cell_y < 0 || cell_y >= world.size: continue var tile_3d = stream_container.create_hextile3d_at(Vector2(cell_x, cell_y)) var hex_center = hexgrid.get_hex_center_from_offset(Vector2(cell_x, cell_y)) var height = world.get_height(Vector2(cell_x, cell_y)) tile_3d.transform.origin = Vector3(hex_center.x, height, hex_center.y) world.heightmap.unlock() num_tiles_label.text = str(len(stream_container.tiles_by_offset_coord.values())) num_active_tiles_label.text = str(stream_active_tiles.get_child_count()) func _process(_delta): last_tile = current_tile var player_coord = player.transform.origin current_tile = hexgrid.get_hex_at(Vector2(player_coord.x, player_coord.z)) world.heightmap.lock() player.transform.origin.y = world.get_height(current_tile.offset_coords) player.transform.origin.y = 2.0 world.heightmap.unlock() var player_hex_offset_coord = current_tile.offset_coords tile_label.text = "%d, %d" % [player_hex_offset_coord.x, player_hex_offset_coord.y] current_tile_offset = Vector2(player_coord.x, player_coord.z) - hexgrid.get_hex_center_from_offset(current_tile.offset_coords) tile_offset_label.text = "%2.2f, %2.2f" % [current_tile_offset.x, current_tile_offset.y] tile_highlight.transform.origin = Vector3(hexgrid.get_hex_center3(current_tile)) if current_tile.offset_coords != last_tile.offset_coords: update_streaming_tiles() func _input(event): var velocity = Vector3(0, 0, 0) if event is InputEventKey: if Input.is_action_pressed("Forward"): velocity.z = -5 elif Input.is_action_pressed("Back"): velocity.z = 5 else: velocity.z = 0 if Input.is_action_pressed("Left"): velocity.x = -5 elif Input.is_action_pressed("Right"): velocity.x = 5 else: velocity.x = 0 player_move_component.vel = velocity get_tree().set_input_as_handled() func _on_World_world_generated(): if world == null || world.heightmap == null: return var world_texture = ImageTexture.new() world_texture.create_from_image(world.heightmap) world_map_texture.texture = world_texture update_streaming_tiles() func _on_GenerateWorldButton_pressed(): world.init_noisemap()