tool extends Spatial var GameTile = load("res://utils/GameTile.gd") var HexCell = preload("res://addons/gdhexgrid/HexCell.gd") onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() onready var HexTile3D = preload("res://scenes/HexTile3D.tscn") onready var HexTile3DPatch = preload("res://scenes/HexTile3DPatch.tscn") var IslandGenerator = preload("res://utils/IslandGenerator.gd").new() onready var click_area = $ClckArea/CollisionShape onready var water = $Water onready var highlight = $Highlight onready var noise = $Noise onready var tiles = $Tiles var has_tiles_initialized = false var tile_map = {} var active_tile = null var island_count = 5 var tick_tock_timer = 0 export var world_size = 80 setget do_resize export var generate_flag: bool = false setget do_generate func tick(): tick_tock_timer = OS.get_ticks_msec() func tock(): return OS.get_ticks_msec() - tick_tock_timer func _ready(): assert(get_node("Tiles") != null) do_resize(world_size) generate() pass func _process(_delta): pass func do_generate(_flag): generate_flag = false func do_resize(new_size): print ("resizing world to size " + str (new_size)) var water = $Water if water == null: return var click_area = $ClckArea click_area.transform = water.transform world_size = new_size return world_size func reset(): var tiles = get_node("Tiles") if tiles == null: return print ("clearing old tiles: " + str(tiles.get_child_count())) for tile in tiles.get_children(): tiles.remove_child(tile) tile.queue_free() tile_map = {} func generate(): print ("generating world with size " + str(world_size)) tick() reset() print ("Reset duration: " + str(tock() / 1000.0)) tick() generate_base_tiles() print ("Generate tiles: " + str(tock() / 1000.0)) tick() apply_noise_as_height() print ("Apply noise : " + str(tock() / 1000.0)) func apply_noise_as_height(): var texture : Texture = noise.texture yield(texture, "changed") var noise = texture.get_data() noise.lock() var vec1 = Vector2.ONE vec1 = vec1 * Vector2(0.5, 0.2) for coord in tile_map.keys(): var tile = tile_map[coord] var tex_coord_normalized = (coord / world_size) * 0.5 + Vector2(0.5, 0.5) var tex_coord = tex_coord_normalized * texture.get_size() # -25 ... 25 -> 0 ... 1 # (coord / (size * 0.5)) * 0.5 + 0.5: -1 .. 1 var noise_value = texture.get_data().get_pixelv(tex_coord)[0] - 0.5 # if noise_value < 0.5: # noise_value = noise_value * 1.5 # else: # noise_value = 1.0 if noise_value < 0.0: tile.transform.origin.y = noise_value * 5 - 0.1 else: tile.transform.origin.y = noise_value * 1.0 + 0.4 if noise_value <= 0.1: tile.set_tiletype(GameTile.TileType.Water) noise.unlock() func generate_base_tiles(): var num_patch_tiles = world_size / 10 var offset_z = 8.660254 var offset_x = 7.5 for i in range (-num_patch_tiles / 2, num_patch_tiles / 2): for j in range (-num_patch_tiles / 2, num_patch_tiles / 2): var patch_tile = HexTile3DPatch.instance() patch_tile.transform.origin = Vector3(i * offset_x, 0.0, j * offset_z) add_child(patch_tile) for child in patch_tile.get_children(): var tile = GameTile.new() tile.type = GameTile.TileType.Water child.game_tile = tile var tile_coord = hexgrid.get_hex_at(child.transform.origin).offset_coords + Vector2(i * 10, -j * 10) tile_map[tile_coord] = child func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx): var hex_at_position = hexgrid.get_hex_at(position) var offset_coords = hex_at_position.offset_coords var hex_center = hexgrid.get_hex_center_from_offset(offset_coords) highlight.transform.origin = Vector3 (hex_center.x, 0.0, hex_center.y) var world_tile = null if offset_coords in tile_map.keys(): world_tile = tile_map[offset_coords] if active_tile != null and world_tile != active_tile: pass if active_tile == world_tile: return if world_tile == null: return active_tile = world_tile func _on_ClckArea_mouse_entered(): highlight.visible = true func _on_ClckArea_mouse_exited(): highlight.visible = false