diff --git a/game/addons/world_generator/raycast/world_gen_raycast.gd b/game/addons/world_generator/raycast/world_gen_raycast.gd new file mode 100644 index 00000000..2a803731 --- /dev/null +++ b/game/addons/world_generator/raycast/world_gen_raycast.gd @@ -0,0 +1,32 @@ +tool +extends Reference +class_name WorldGenRaycast + +var current_index : int = -1 +var base_resources : Array = Array() +var local_positions : PoolVector2Array = PoolVector2Array() +var local_uvs : PoolVector2Array = PoolVector2Array() + +func get_local_position() -> Vector2: + return local_positions[current_index] + +func get_local_uv() -> Vector2: + return local_uvs[current_index] + +# WorldGenBaseResource (can't explicitly add -> cyclic dependency) +func get_resource(): + return base_resources[current_index] + +func next() -> bool: + current_index += 1 + + return base_resources.size() > current_index + +func size() -> int: + return base_resources.size() + +# base_resource -> WorldGenBaseResource +func add_data(base_resource, local_pos : Vector2, local_uv : Vector2) -> void: + base_resources.append(base_resource) + local_positions.append(local_pos) + local_uvs.append(local_uv) diff --git a/game/addons/world_generator/resources/world_gen_base_resource.gd b/game/addons/world_generator/resources/world_gen_base_resource.gd index b381383c..bb856165 100644 --- a/game/addons/world_generator/resources/world_gen_base_resource.gd +++ b/game/addons/world_generator/resources/world_gen_base_resource.gd @@ -104,16 +104,16 @@ func _setup_terra_library(library : TerrainLibrary, pseed : int) -> void: func generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool) -> void: var p : Vector2 = Vector2(chunk.get_position_x(), chunk.get_position_z()) - var stack : Array = get_hit_stack(p) + var raycast : WorldGenRaycast = get_hit_stack(p) - if stack.size() == 0: + if raycast.size() == 0: _generate_terra_chunk_fallback(chunk, pseed, spawn_mobs) return - for i in range(stack.size()): - stack[i]._generate_terra_chunk(chunk, pseed, spawn_mobs, stack, i) + while raycast.next(): + raycast.get_resource()._generate_terra_chunk(chunk, pseed, spawn_mobs, raycast) -func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, stack : Array, stack_index : int) -> void: +func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, raycast : WorldGenRaycast) -> void: pass func _generate_terra_chunk_fallback(chunk: TerrainChunk, pseed : int, spawn_mobs: bool) -> void: @@ -130,31 +130,33 @@ func generate_map(pseed : int) -> Image: return img -func add_to_map(var img : Image, pseed : int) -> void: +func add_to_map(img : Image, pseed : int) -> void: _add_to_map(img, pseed) for c in get_content(): if c: c.add_to_map(img, pseed) -func _add_to_map(var img : Image, pseed : int) -> void: +func _add_to_map(img : Image, pseed : int) -> void: pass -func get_hit_stack(var pos : Vector2) -> Array: +func get_hit_stack(pos : Vector2, raycast : WorldGenRaycast = null) -> WorldGenRaycast: var r : Rect2 = get_rect() var local_pos : Vector2 = pos - rect.position r.position = Vector2() - var result : Array = Array() + if !raycast: + raycast = WorldGenRaycast.new() if r.has_point(local_pos): - result.append(self) + var local_uv : Vector2 = local_pos / rect.size + raycast.add_data(self, local_pos, local_uv) for c in get_content(): if c: - result.append_array(c.get_hit_stack(local_pos)) + c.get_hit_stack(local_pos, raycast) - return result + return raycast func get_editor_rect_border_color() -> Color: return Color(1, 1, 1, 1) diff --git a/game/project.godot b/game/project.godot index 5f5fb357..f5439a23 100644 --- a/game/project.godot +++ b/game/project.godot @@ -245,6 +245,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://addons/world_generator/resources/world_gen_base_resource.gd" }, { +"base": "Reference", +"class": "WorldGenRaycast", +"language": "GDScript", +"path": "res://addons/world_generator/raycast/world_gen_raycast.gd" +}, { "base": "Resource", "class": "WorldGenWorld", "language": "GDScript", @@ -308,6 +313,7 @@ _global_script_class_icons={ "UIGuiChildModule": "", "UIWindowModule": "", "WorldGenBaseResource": "", +"WorldGenRaycast": "", "WorldGenWorld": "", "WorldGeneratorSettings": "", "Zone": "" diff --git a/game/scripts/world_generator/continents/test_continent.gd b/game/scripts/world_generator/continents/test_continent.gd index b64abcfb..abdf66d0 100644 --- a/game/scripts/world_generator/continents/test_continent.gd +++ b/game/scripts/world_generator/continents/test_continent.gd @@ -50,7 +50,7 @@ func get_editor_additional_text() -> String: func _setup_terra_library(library : TerrainLibrary, pseed : int) -> void: pass -func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, stack : Array, stack_index : int) -> void: +func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, raycast : WorldGenRaycast) -> void: voxel_scale = chunk.voxel_scale current_seed = pseed diff --git a/game/scripts/world_generator/subzones/spawner.gd b/game/scripts/world_generator/subzones/spawner.gd index fc7f3b28..a3046297 100644 --- a/game/scripts/world_generator/subzones/spawner.gd +++ b/game/scripts/world_generator/subzones/spawner.gd @@ -14,7 +14,7 @@ func _setup() -> void: func get_spawn_chunk_position() -> Vector2: return Vector2(main_chunk_pos_x, main_chunk_pos_z) -func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, stack : Array, stack_index : int) -> void: +func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, raycast : WorldGenRaycast) -> void: if !spawn_mobs: return