From 69933d58ae7547533e7e8cc48ac55e020fac7487 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 15 Sep 2022 14:15:23 +0200 Subject: [PATCH] Tweaking island generation --- addons/gdhexgrid/HexGrid.gd | 7 +- project.godot | 14 +++- scenes/HexGrid3DTest.gd | 2 +- scenes/HexGrid3DTest.tscn | 1 - scenes/HexTile3D.gd | 7 ++ scenes/Island.gd | 158 +++++++++++++++++++++++++++++++++--- 6 files changed, 170 insertions(+), 19 deletions(-) diff --git a/addons/gdhexgrid/HexGrid.gd b/addons/gdhexgrid/HexGrid.gd index 700aaf1..25ef8b4 100644 --- a/addons/gdhexgrid/HexGrid.gd +++ b/addons/gdhexgrid/HexGrid.gd @@ -212,7 +212,12 @@ func get_hex_center_from_offset(offset): var hex = HexCell.new() hex.offset_coords = offset return hex_transform * hex.axial_coords - + +func get_hex_from_offset(offset): + var hex = HexCell.new() + hex.offset_coords = offset + return hex + func get_hex_at(coords): # Returns a HexCell at the given Vector2/3 on the projection plane # If the given value is a Vector3, its x,z coords will be used diff --git a/project.godot b/project.godot index a9603bc..e0fd92b 100644 --- a/project.godot +++ b/project.godot @@ -9,22 +9,27 @@ config_version=4 _global_script_classes=[ { -"base": "Reference", +"base": "Node", "class": "ClickableComponent", "language": "GDScript", "path": "res://components/ClickableComponent.gd" }, { -"base": "Reference", +"base": "KinematicBody2D", "class": "CollisionLine", "language": "GDScript", "path": "res://utils/CollisionLine.gd" }, { -"base": "Reference", +"base": "Node", "class": "ColorComponent", "language": "GDScript", "path": "res://components/ColorComponent.gd" }, { "base": "Spatial", +"class": "HexTile3D", +"language": "GDScript", +"path": "res://scenes/HexTile3D.gd" +}, { +"base": "Spatial", "class": "Island", "language": "GDScript", "path": "res://scenes/Island.gd" @@ -34,7 +39,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://utils/SpringDamper.gd" }, { -"base": "Reference", +"base": "Sprite", "class": "TintedSpriteComponent", "language": "GDScript", "path": "res://components/TintedSpriteComponent.gd" @@ -43,6 +48,7 @@ _global_script_class_icons={ "ClickableComponent": "", "CollisionLine": "", "ColorComponent": "", +"HexTile3D": "", "Island": "", "SpringDamper": "", "TintedSpriteComponent": "" diff --git a/scenes/HexGrid3DTest.gd b/scenes/HexGrid3DTest.gd index fe82415..3f17bcc 100644 --- a/scenes/HexGrid3DTest.gd +++ b/scenes/HexGrid3DTest.gd @@ -11,7 +11,7 @@ onready var player = $Player onready var world = $World var player_velocity = Vector3.ZERO -var player_speed = 1 +var player_speed = 5 var target_coordinate = Vector3.ZERO func _ready(): diff --git a/scenes/HexGrid3DTest.tscn b/scenes/HexGrid3DTest.tscn index b05f871..4945adc 100644 --- a/scenes/HexGrid3DTest.tscn +++ b/scenes/HexGrid3DTest.tscn @@ -179,7 +179,6 @@ __meta__ = { } [node name="World" type="Spatial" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ) [node name="Island" parent="World" instance=ExtResource( 4 )] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ) diff --git a/scenes/HexTile3D.gd b/scenes/HexTile3D.gd index 7737f65..5824758 100644 --- a/scenes/HexTile3D.gd +++ b/scenes/HexTile3D.gd @@ -1,12 +1,19 @@ extends Spatial +class_name HexTile3D onready var is_mouse_over = false onready var mesh = $Mesh var old_material_override = Material.new() +export var hex = null setget set_hex, get_hex signal tile_selected signal tile_mouse_entered +func set_hex(_hex): + hex = _hex + +func get_hex(): + return hex func _on_Area_mouse_entered(): is_mouse_over = true diff --git a/scenes/Island.gd b/scenes/Island.gd index bcadec2..9adca45 100644 --- a/scenes/Island.gd +++ b/scenes/Island.gd @@ -8,24 +8,158 @@ export var level_size: int = 10 signal island_tile_selected signal island_tile_hover -func generate(): - var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() - var HexTile3D = preload("res://scenes/HexTile3D.tscn") +var tiles_by_offset = {} + +func reset(): var tiles = $Tiles for node in tiles.get_children(): tiles.remove_child(node) node.queue_free() - 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) + 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")