diff --git a/entities/Player3D.tscn b/entities/Player3D.tscn new file mode 100644 index 0000000..9fa5522 --- /dev/null +++ b/entities/Player3D.tscn @@ -0,0 +1,34 @@ +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://entities/PlayerEntity.gd" type="Script" id=1] +[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=2] +[ext_resource path="res://components/MovableComponent.tscn" type="PackedScene" id=4] + +[sub_resource type="CapsuleMesh" id=4] +radius = 0.2 +mid_height = 0.5 +radial_segments = 16 + +[sub_resource type="CapsuleShape" id=5] +radius = 0.25 +height = 0.5 + +[node name="Player" type="KinematicBody"] +script = ExtResource( 1 ) + +[node name="Camera" type="Camera" parent="."] +transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 7.29903, 4.46831 ) +current = true +fov = 60.0 +script = ExtResource( 2 ) + +[node name="MeshInstance" type="MeshInstance" parent="."] +transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 ) +mesh = SubResource( 4 ) + +[node name="Collision" type="CollisionShape" parent="."] +transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 ) +shape = SubResource( 5 ) + +[node name="Movable" parent="." instance=ExtResource( 4 )] +max_speed = 3 diff --git a/entities/PlayerEntity.gd b/entities/PlayerEntity.gd index 91d7560..e5fd676 100644 --- a/entities/PlayerEntity.gd +++ b/entities/PlayerEntity.gd @@ -43,7 +43,6 @@ func _physics_process(_delta): return var physics_velocity = move_and_slide(movable_component.vel) - movable_component.pos = self.position movable_component.vel = physics_velocity @@ -59,8 +58,8 @@ func _on_position_updated(new_position): func set_movable_target(target): if not movable_component == null: movable_component.target = target - - + + func _unhandled_input_disabled(event): print ("Player unhandled input...") if event is InputEventMouseButton and event.pressed: diff --git a/project.godot b/project.godot index 040ac1b..32fa79f 100644 --- a/project.godot +++ b/project.godot @@ -9,17 +9,17 @@ config_version=4 _global_script_classes=[ { -"base": "Node", +"base": "Reference", "class": "ClickableComponent", "language": "GDScript", "path": "res://components/ClickableComponent.gd" }, { -"base": "KinematicBody2D", +"base": "Reference", "class": "CollisionLine", "language": "GDScript", "path": "res://utils/CollisionLine.gd" }, { -"base": "Node", +"base": "Reference", "class": "ColorComponent", "language": "GDScript", "path": "res://components/ColorComponent.gd" @@ -54,7 +54,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://utils/SpringDamper.gd" }, { -"base": "Sprite", +"base": "Reference", "class": "TintedSpriteComponent", "language": "GDScript", "path": "res://components/TintedSpriteComponent.gd" @@ -74,7 +74,7 @@ _global_script_class_icons={ [application] -config/name="GodotComponentTest" +config/name="i3sc1 GodotComponentTest" run/main_scene="res://scenes/HexGrid3DTest.tscn" config/icon="res://icon.png" @@ -82,6 +82,8 @@ config/icon="res://icon.png" window/size/width=320 window/size/height=400 +window/size/test_width=1280 +window/size/test_height=768 window/stretch/mode="viewport" window/stretch/aspect="expand" @@ -108,6 +110,10 @@ Right={ ] } +[mono] + +project/assembly_name="GodotComponentTest" + [physics] common/enable_pause_aware_picking=true diff --git a/scenes/AdaptiveWorldStream.gd b/scenes/AdaptiveWorldStream.gd new file mode 100644 index 0000000..f6245b9 --- /dev/null +++ b/scenes/AdaptiveWorldStream.gd @@ -0,0 +1,125 @@ +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() diff --git a/scenes/AdaptiveWorldStream.tscn b/scenes/AdaptiveWorldStream.tscn new file mode 100644 index 0000000..ddf779c --- /dev/null +++ b/scenes/AdaptiveWorldStream.tscn @@ -0,0 +1,143 @@ +[gd_scene load_steps=11 format=2] + +[ext_resource path="res://scenes/AdaptiveWorldStream.gd" type="Script" id=1] +[ext_resource path="res://entities/Player3D.tscn" type="PackedScene" id=2] +[ext_resource path="res://scenes/World.gd" type="Script" id=3] +[ext_resource path="res://scenes/StreamContainer.gd" type="Script" id=4] + +[sub_resource type="CylinderMesh" id=3] +top_radius = 0.5 +bottom_radius = 0.5 +height = 0.1 +radial_segments = 6 +rings = 1 + +[sub_resource type="SpatialMaterial" id=4] +render_priority = 1 +flags_transparent = true +albedo_color = Color( 1, 1, 1, 0.407843 ) +roughness = 0.0 + +[sub_resource type="OpenSimplexNoise" id=5] +period = 15.0 + +[sub_resource type="NoiseTexture" id=6] +flags = 3 +width = 100 +height = 100 +noise = SubResource( 5 ) + +[sub_resource type="CubeMesh" id=1] + +[sub_resource type="SpatialMaterial" id=2] +params_blend_mode = 3 +albedo_color = Color( 1, 1, 1, 0.156863 ) + +[node name="AdaptiveWorldStream" type="Spatial"] +script = ExtResource( 1 ) + +[node name="TileHighlight" type="MeshInstance" parent="."] +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0.2, 0 ) +cast_shadow = 0 +mesh = SubResource( 3 ) +material/0 = SubResource( 4 ) + +[node name="Player" parent="." instance=ExtResource( 2 )] + +[node name="World" type="Spatial" parent="."] +script = ExtResource( 3 ) + +[node name="Control" type="Control" parent="."] +margin_right = 40.0 +margin_bottom = 40.0 + +[node name="HBoxContainer" type="HBoxContainer" parent="Control"] +margin_right = 40.0 +margin_bottom = 40.0 + +[node name="WorldMapRect" type="TextureRect" parent="Control/HBoxContainer"] +margin_right = 100.0 +margin_bottom = 100.0 +rect_min_size = Vector2( 100, 100 ) +texture = SubResource( 6 ) +expand = true + +[node name="GenerateWorldButton" type="Button" parent="Control/HBoxContainer"] +margin_left = 104.0 +margin_right = 175.0 +margin_bottom = 100.0 +text = "Generate" + +[node name="Label" type="Label" parent="Control/HBoxContainer"] +margin_left = 179.0 +margin_top = 43.0 +margin_right = 202.0 +margin_bottom = 57.0 +rect_pivot_offset = Vector2( -335, -33 ) +text = "Tile" + +[node name="tile_label" type="Label" parent="Control/HBoxContainer"] +margin_left = 206.0 +margin_top = 43.0 +margin_right = 226.0 +margin_bottom = 57.0 +text = "0,0" + +[node name="Label2" type="Label" parent="Control/HBoxContainer"] +margin_left = 230.0 +margin_top = 43.0 +margin_right = 296.0 +margin_bottom = 57.0 +text = "Tile Offset" + +[node name="tile_offset_label" type="Label" parent="Control/HBoxContainer"] +margin_left = 300.0 +margin_top = 43.0 +margin_right = 320.0 +margin_bottom = 57.0 +text = "0,0" + +[node name="Label3" type="Label" parent="Control/HBoxContainer"] +margin_left = 324.0 +margin_top = 43.0 +margin_right = 363.0 +margin_bottom = 57.0 +text = "#Tiles" + +[node name="num_tiles_label" type="Label" parent="Control/HBoxContainer"] +margin_left = 367.0 +margin_top = 43.0 +margin_right = 375.0 +margin_bottom = 57.0 +text = "0" + +[node name="Label5" type="Label" parent="Control/HBoxContainer"] +margin_left = 379.0 +margin_top = 43.0 +margin_right = 428.0 +margin_bottom = 57.0 +text = "#Active" + +[node name="num_active_tiles_label" type="Label" parent="Control/HBoxContainer"] +margin_left = 432.0 +margin_top = 43.0 +margin_right = 440.0 +margin_bottom = 57.0 +text = "0" + +[node name="StreamContainer" type="Spatial" parent="."] +script = ExtResource( 4 ) +world_rect = Rect2( 0, 0, 25, 18 ) + +[node name="ActiveTiles" type="Spatial" parent="StreamContainer"] + +[node name="Bounds" type="MeshInstance" parent="StreamContainer"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) +mesh = SubResource( 1 ) +skeleton = NodePath("../..") +material/0 = SubResource( 2 ) + +[connection signal="world_generated" from="World" to="." method="_on_World_world_generated"] +[connection signal="pressed" from="Control/HBoxContainer/GenerateWorldButton" to="." method="_on_GenerateWorldButton_pressed"] + +[editable path="Player"] diff --git a/scenes/HexGrid3DTest.gd b/scenes/HexGrid3DTest.gd index cc03a03..b815b6b 100644 --- a/scenes/HexGrid3DTest.gd +++ b/scenes/HexGrid3DTest.gd @@ -13,9 +13,10 @@ onready var world = $World var player_velocity = Vector3.ZERO var player_speed = 5 var target_coordinate = Vector3.ZERO - +var is_first_process = true func _ready(): + print("_ready(): ", OS.get_ticks_msec()) for node in world.get_children(): if node is Island: node.generate() @@ -24,6 +25,9 @@ func _ready(): func _process(_delta): + if is_first_process: + print("_process(): ", OS.get_ticks_msec()) + is_first_process = false fps_label.text = "FPS: " + str(Performance.get_monitor(Performance.TIME_FPS)) player_pos_label.text = "Pos: " + str(player.transform.origin) diff --git a/scenes/HexGrid3DTest.tscn b/scenes/HexGrid3DTest.tscn index 97c3bc2..8d3fcc2 100644 --- a/scenes/HexGrid3DTest.tscn +++ b/scenes/HexGrid3DTest.tscn @@ -1,11 +1,9 @@ -[gd_scene load_steps=17 format=2] +[gd_scene load_steps=13 format=2] -[ext_resource path="res://entities/PlayerEntity.gd" type="Script" id=1] +[ext_resource path="res://entities/Player3D.tscn" type="PackedScene" id=1] [ext_resource path="res://scenes/HexGrid3DTest.gd" type="Script" id=2] -[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=3] [ext_resource path="res://scenes/NoiseWorld.gd" type="Script" id=4] [ext_resource path="res://assets/water_diffuse.png" type="Texture" id=5] -[ext_resource path="res://components/MovableComponent.tscn" type="PackedScene" id=6] [sub_resource type="CylinderMesh" id=7] top_radius = 0.5 @@ -129,21 +127,11 @@ height = 1024 seamless = true noise = SubResource( 11 ) -[sub_resource type="CapsuleMesh" id=4] -radius = 0.2 -mid_height = 0.5 -radial_segments = 16 - -[sub_resource type="CapsuleShape" id=5] -radius = 0.25 -height = 0.5 - [node name="HexGrid3DTest" type="Spatial"] script = ExtResource( 2 ) [node name="World" type="Spatial" parent="."] script = ExtResource( 4 ) -world_size = 50 [node name="Scene" type="Spatial" parent="World"] @@ -159,14 +147,15 @@ mesh = SubResource( 7 ) material/0 = SubResource( 8 ) [node name="Water" type="MeshInstance" parent="World"] -transform = Transform( 25, 0, 0, 0, 25, 0, 0, 0, 25, 0, 0, 0 ) +transform = Transform( 50, 0, 0, 0, 50, 0, 0, 0, 50, 0, 0, 0 ) +visible = false cast_shadow = 0 mesh = SubResource( 1 ) skeleton = NodePath("../..") material/0 = SubResource( 3 ) [node name="ClckArea" type="Area" parent="World"] -transform = Transform( 25, 0, 0, 0, 25, 0, 0, 0, 25, 0, 0, 0 ) +transform = Transform( 50, 0, 0, 0, 50, 0, 0, 0, 50, 0, 0, 0 ) [node name="CollisionShape" type="CollisionShape" parent="World/ClckArea"] transform = Transform( 2.5, 0, 0, 0, 1, 0, 0, 0, 2.5, 0, 0, 0 ) @@ -223,26 +212,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="Player" type="KinematicBody" parent="."] -script = ExtResource( 1 ) - -[node name="Camera" type="Camera" parent="Player"] -transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 7.29903, 4.46831 ) -current = true -fov = 60.0 -script = ExtResource( 3 ) - -[node name="MeshInstance" type="MeshInstance" parent="Player"] -transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 ) -mesh = SubResource( 4 ) -material/0 = null - -[node name="CollisionShape" type="CollisionShape" parent="Player"] -transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 ) -shape = SubResource( 5 ) - -[node name="Movable" parent="Player" instance=ExtResource( 6 )] -max_speed = 3 +[node name="Player" parent="." instance=ExtResource( 1 )] [connection signal="input_event" from="World/ClckArea" to="World" method="_on_ClckArea_input_event"] [connection signal="mouse_entered" from="World/ClckArea" to="World" method="_on_ClckArea_mouse_entered"] diff --git a/scenes/HexTile3D.tscn b/scenes/HexTile3D.tscn index 987976a..3d5d38a 100644 --- a/scenes/HexTile3D.tscn +++ b/scenes/HexTile3D.tscn @@ -11,8 +11,8 @@ radial_segments = 6 rings = 1 [sub_resource type="CylinderShape" id=5] -radius = 0.5 height = 1.0 +radius = 0.5 [node name="HexTile3D" type="Spatial"] transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0, 0 ) diff --git a/scenes/HexTile3DPatch.tscn b/scenes/HexTile3DPatch.tscn index a63e3ff..83bb931 100644 --- a/scenes/HexTile3DPatch.tscn +++ b/scenes/HexTile3DPatch.tscn @@ -5,6 +5,7 @@ [node name="Spatial" type="Spatial"] [node name="HexTile3D1" parent="." instance=ExtResource( 1 )] +transform = Transform( -0.0119554, 0, 0.999929, 0, 1, 0, -0.999929, 0, -0.0119554, 0, 0, 0 ) [node name="HexTile3D2" parent="." instance=ExtResource( 1 )] transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0, 0.866 ) @@ -61,7 +62,7 @@ transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0.75, 0 transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0.75, 0, 7.361 ) [node name="HexTile3D20" parent="." instance=ExtResource( 1 )] -transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0.75, 0, 8.227 ) +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0.75, 0, -0.414769 ) [node name="HexTile3D21" parent="." instance=ExtResource( 1 )] transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 1.5, 0, 0 ) @@ -121,7 +122,7 @@ transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 2.25, 0 transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 2.25, 0, 7.361 ) [node name="HexTile3D40" parent="." instance=ExtResource( 1 )] -transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 2.25, 0, 8.227 ) +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 2.25, 0, -0.414769 ) [node name="HexTile3D41" parent="." instance=ExtResource( 1 )] transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 3, 0, 0 ) @@ -181,7 +182,7 @@ transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 3.75, 0 transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 3.75, 0, 7.361 ) [node name="HexTile3D60" parent="." instance=ExtResource( 1 )] -transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 3.75, 0, 8.227 ) +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 3.75, 0, -0.414769 ) [node name="HexTile3D61" parent="." instance=ExtResource( 1 )] transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 4.5, 0, 0 ) @@ -241,7 +242,7 @@ transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 5.25, 0 transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 5.25, 0, 7.361 ) [node name="HexTile3D80" parent="." instance=ExtResource( 1 )] -transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 5.25, 0, 8.227 ) +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 5.25, 0, -0.414769 ) [node name="HexTile3D81" parent="." instance=ExtResource( 1 )] transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 6, 0, 0 ) @@ -301,4 +302,4 @@ transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 6.75, 0 transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 6.75, 0, 7.361 ) [node name="HexTile3D100" parent="." instance=ExtResource( 1 )] -transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 6.75, 0, 8.227 ) +transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 6.75, 0, -0.414769 ) diff --git a/scenes/NoiseWorld.gd b/scenes/NoiseWorld.gd index c854847..7ccbb72 100644 --- a/scenes/NoiseWorld.gd +++ b/scenes/NoiseWorld.gd @@ -37,7 +37,6 @@ func _ready(): generate() pass - func _process(_delta): pass @@ -53,9 +52,6 @@ func do_resize(new_size): if water == null: return - print ("water node: " + str(water)) - water.transform = Transform.IDENTITY.scaled(Vector3(new_size, new_size, new_size) * 0.5) - var click_area = $ClckArea click_area.transform = water.transform @@ -143,14 +139,7 @@ func generate_base_tiles(): -# for i in range (-world_size / 2, world_size / 2): -# for j in range (-world_size / 2, world_size / 2): -# var tile = GameTile.new() -# tile.type = GameTile.TileType.Water -# tile.offset_coords = Vector2(i,j) -# -# var game_tile = add_tile_for_hex(tile) -# print (str(i) + ", " + str(j) + ": " + str(game_tile.transform.origin)) + func _on_ClckArea_input_event(_camera, _event, position, _normal, _shape_idx): diff --git a/scenes/RandomWalkWorld.tscn b/scenes/RandomWalkWorld.tscn index 303c22d..9124f83 100644 --- a/scenes/RandomWalkWorld.tscn +++ b/scenes/RandomWalkWorld.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=9 format=2] -[ext_resource path="res://scenes/World.gd" type="Script" id=1] +[ext_resource path="res://scenes/RandomWalkWorld.gd" type="Script" id=1] [ext_resource path="res://assets/water_diffuse.png" type="Texture" id=2] [sub_resource type="CylinderMesh" id=7] diff --git a/scenes/StreamContainer.gd b/scenes/StreamContainer.gd new file mode 100644 index 0000000..a51597f --- /dev/null +++ b/scenes/StreamContainer.gd @@ -0,0 +1,84 @@ +extends Spatial + +onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new() +onready var hextile3d = preload("res://scenes/HexTile3D.tscn") +onready var active_tiles = $ActiveTiles + + +export var world_rect: Rect2 = Rect2() setget set_rect +var offset_coord_rect: Rect2 = Rect2() + +var bottom_left_cell: HexCell = HexCell.new() +var top_right_cell: HexCell = HexCell.new() + +var tiles = {} +var tiles_by_offset_coord = {} + +onready var bounds = $Bounds + + +func _ready(): + set_rect(world_rect) + + +func is_hex_coord_in_rect (coord: Vector2): + var has_point = offset_coord_rect.has_point(coord) + var rect_end = offset_coord_rect.end + var manual = coord.x >= offset_coord_rect.position.x and coord.x < offset_coord_rect.end.x and coord.y >= offset_coord_rect.position.y and coord.y < offset_coord_rect.end.y + return has_point || manual + + +func create_hextile3d_at (coord: Vector2) -> HexTile3D: + if not coord in tiles_by_offset_coord.keys(): + var new_hextile3d = hextile3d.instance() + active_tiles.add_child(new_hextile3d) + new_hextile3d.game_tile.offset_coords = coord + tiles_by_offset_coord[coord] = new_hextile3d + + return tiles_by_offset_coord[coord] + + +func cleanup_tiles(): + var num_deleted = 0 + var children = active_tiles.get_children() + for child in children: + var tile_offset_coords = child.game_tile.offset_coords + if not is_hex_coord_in_rect(tile_offset_coords): + tiles_by_offset_coord.erase(tile_offset_coords) + child.queue_free() + active_tiles.remove_child(child) + num_deleted = num_deleted + 1 + +# print ("deleted ", num_deleted, " tiles") + + +func set_rect(rect: Rect2): + world_rect = rect + + if bounds == null: + return + + var world_rect_center = rect.get_center() + + bounds.transform = Transform( + Vector3 (world_rect.size.x / 2, 0, 0), + Vector3(0, 1, 0), Vector3(0, 0, world_rect.size.y / 2), + Vector3(world_rect_center.x, 0, world_rect_center.y) + ) + + bottom_left_cell = hexgrid.get_hex_at(Vector2(rect.position.x, rect.end.y)) + top_right_cell = hexgrid.get_hex_at(Vector2(rect.end.x, rect.position.y)) + + offset_coord_rect = Rect2( + bottom_left_cell.offset_coords.x, bottom_left_cell.offset_coords.y, + top_right_cell.offset_coords.x - bottom_left_cell.offset_coords.x, top_right_cell.offset_coords.y - bottom_left_cell.offset_coords.y + ) + +# print ("----") +# print ("world_rect: ", world_rect) +# print ("cells: ", bottom_left_cell.offset_coords, " to ", top_right_cell.offset_coords) +# print ("offset_coord_rect: ", offset_coord_rect.position, " size: ", offset_coord_rect.size) + + cleanup_tiles() + + return world_rect diff --git a/scenes/World.gd b/scenes/World.gd new file mode 100644 index 0000000..e753336 --- /dev/null +++ b/scenes/World.gd @@ -0,0 +1,45 @@ +extends Spatial + +export var size: int = 1024 +var heightmap: Image = Image.new() + +signal world_generated + +func _ready(): +# init_heightmap() + init_noisemap() + pass + + +func init_heightmap(): + heightmap.create(size, size, 0, Image.FORMAT_RF) + heightmap.lock() + for i in range (size): + for j in range (size): + if (i + j) % 2 == 0: + heightmap.set_pixel(i, j, Color(0.1, 0.0, 0.0)) + else: + heightmap.set_pixel(i, j, Color(0.0, 0.0, 0.0)) + heightmap.unlock() + + +func init_noisemap(): + var noise_texture = NoiseTexture.new() + var noise_generator = OpenSimplexNoise.new() + + noise_generator.seed = randi() + print ("seed: ", noise_generator.seed) + noise_generator.octaves = 3 + noise_generator.period = 5 + noise_generator.persistence = 0.5 + noise_generator.lacunarity = 2 + noise_texture.seamless = true + + heightmap.copy_from(noise_generator.get_seamless_image(size)) + + emit_signal("world_generated") + + +func get_height(coord: Vector2) -> float: + return heightmap.get_pixel(coord.x, coord.y).r * 3 + diff --git a/scenes/generate_hextile3d_patch.py b/scenes/generate_hextile3d_patch.py new file mode 100755 index 0000000..1a680a8 --- /dev/null +++ b/scenes/generate_hextile3d_patch.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 + +import math + +size = 25 + +# converts from even-q vertical layout to world coords +def offset_to_world_coords(offset_coordinate_x, offset_coordinate_y): + z_offset = 0 + if offset_coordinate_x % 2 != 0: + z_offset = -math.sqrt(3) * 0.25 + + return [ + offset_coordinate_x * 0.75, + 0., + z_offset + offset_coordinate_y * math.sqrt(3) * 0.5] + + +# [gd_scene load_steps=2 format=2] +# +# [ext_resource path="res://scenes/HexTile3D.tscn" type="PackedScene" id=1] +# +# [node name="Spatial" type="Spatial"] +# +# [node name="HexTile3D1" parent="." instance=ExtResource( 1 )] +# +# [node name="HexTile3D2" parent="." instance=ExtResource( 1 )] +# transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0, 0.866 ) + + + +print (""" +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scenes/HexTile3D.tscn" type="PackedScene" id=1] + +[node name="Spatial" type="Spatial"] + +""" +) + +tile_index = 0 +for x_i in range (size): + for y_i in range (size): + coord = offset_to_world_coords(x_i, y_i) + + print ("[node name=\"HexTile3D{tile_index}\" parent=\".\" instance=ExtResource( 1)]".format(tile_index = tile_index )) + print ("transform = Transform( 0, 0, 1, 0, 1, 0, -1, 0, 0, {x_coord}, 0, {z_coord} )".format(x_coord = coord[0], z_coord = coord[2])) + + tile_index = tile_index + 1 +