tool extends Spatial class_name Island export var generate: bool = false setget do_generate export var level_size: int = 10 signal island_tile_selected signal island_tile_hover var tiles_by_offset = {} func reset(): var tiles = $Tiles for node in tiles.get_children(): tiles.remove_child(node) node.queue_free() tiles_by_offset = {} func create_tile_for_hex(hex): var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() var HexTile3D = preload("res://scenes/HexTile3D.tscn") var tile = HexTile3D.instance() var pos = hexgrid.get_hex_center_from_offset(hex.offset_coords) var hex_pos = hexgrid.get_hex_center_from_offset(hex.offset_coords) var height = 0.0 tile.transform.origin = Vector3(pos.x, height, pos.y) tile.connect("tile_selected", self, "on_tile_selected") tile.connect("tile_mouse_entered", self, "on_tile_hover") return tile func generate_random_walk (length): var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() var HexTile3D = preload("res://scenes/HexTile3D.tscn") var tiles = $Tiles var cur_hex_coord = Vector2(0,0) var i = length while (i >= 0): var current_hex = hexgrid.get_hex_from_offset(cur_hex_coord) var tile = create_tile_for_hex(current_hex) tiles.add_child(tile) tiles_by_offset[cur_hex_coord] = tile # var pos = hexgrid.get_hex_center_from_offset(cur_hex_coord) # var tile = HexTile3D.instance() # var height = 0.0 # tile.transform.origin = Vector3(pos.x, height, pos.y) # tile.connect("tile_selected", self, "on_tile_selected") # tile.connect("tile_mouse_entered", self, "on_tile_hover") # tiles.add_child(tile) # # var current_hex_from_axial = hexgrid.get_hex_at(current_hex.axial_coords) # var neighbours = current_hex.get_all_adjacent() cur_hex_coord = neighbours[randi() %6].offset_coords # for neighbour in neighbours: # tile = create_tile_for_hex(neighbour) # tiles.add_child(tile) # var rand_idx = randi() % 6 # cur_hex_coord = neighbours[rand_idx].offset_coords i = i - 1 func extrude_island(): var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() var tiles = $Tiles var old_tiles = tiles tiles = {} var old_tiles_by_offset = tiles_by_offset # for offset_coord in tiles_by_offset.keys(): # var hex = hexgrid.get_hex_from_offset(offset_coord) # var neighbours = current_hex.get_all_adjacent() # cur_hex_coord = neighbours[randi() %6].offset_coords # # # # for tile in old_tiles: # var hex_coord = Globals.WorldToHex(tile) # tiles[tile] = "Sand" # var neighbours = Globals.HexGrid.get_hex_at(hex_coord).get_all_adjacent() # for cell in neighbours: # var world_coord = Globals.HexToWorld(hex_coord + cell.axial_coords) # tiles[world_coord] = "Sand" # # #func mark_grass(): # var old_tiles = tiles # tiles = {} # # for tile in old_tiles: # var hex_coord = Globals.WorldToHex(tile) # # var neighbours = Globals.HexGrid.get_hex_at(hex_coord).get_all_adjacent() # var is_center_cell = true # for cell in neighbours: # var world_coord = Globals.HexToWorld(hex_coord + cell.axial_coords) # if not world_coord in old_tiles.keys(): # is_center_cell = false # # if is_center_cell: # tiles[tile] = "Grass" # else: # tiles[tile] = "Sand" # # # #func mark_deep_grass(): # var old_tiles = tiles # tiles = {} # # for tile in old_tiles: # if old_tiles[tile] != "Grass": # tiles[tile] = old_tiles[tile] # continue # # var hex_coord = Globals.WorldToHex(tile) # # var neighbours = Globals.HexGrid.get_hex_at(hex_coord).get_all_adjacent() # var is_center_cell = true # for cell in neighbours: # var world_coord = Globals.HexToWorld(hex_coord + cell.axial_coords) # if world_coord in old_tiles.keys() and old_tiles[world_coord] != "Grass": # is_center_cell = false # break # # if is_center_cell: # tiles[tile] = "DeepGrass" # else: # tiles[tile] = "Grass" func generate(): var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() var HexTile3D = preload("res://scenes/HexTile3D.tscn") var tiles = $Tiles reset() generate_random_walk(level_size) # extrude_island() # mark_grass() # mark_deep_grass() # # for i in range(-level_size / 2, level_size / 2): # for j in range (-level_size / 2, level_size / 2): # var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j)) # var tile = HexTile3D.instance() # var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 + 0.4 + rand_range(-0.3, 0.3) # tile.transform.origin = Vector3(pos.x, height, pos.y) # tile.connect("tile_selected", self, "on_tile_selected") # tile.connect("tile_mouse_entered", self, "on_tile_hover") # tiles.add_child(tile) print ("Generated island with " + str(tiles.get_child_count()) + " nodes") func do_generate(flag: bool): print ("Generating Level") generate() func on_tile_selected(tile): emit_signal("island_tile_selected", self, tile) func on_tile_hover(tile): emit_signal("island_tile_hover", self, tile)