From 337dcdaa49a226cc4968598284344872ddb6d923 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 31 Jul 2022 19:41:12 +0200 Subject: [PATCH] Now dungeons won't get spawned randomly by the zone, instead they are spawned by a new subzone class. --- .../subzones/dungeon_spawner.gd | 96 +++++++++++++++++++ .../world_generator/zones/test_zone.gd | 12 +-- game/worlds/test_world/test_world.tres | 21 +++- 3 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 game/scripts/world_generator/subzones/dungeon_spawner.gd diff --git a/game/scripts/world_generator/subzones/dungeon_spawner.gd b/game/scripts/world_generator/subzones/dungeon_spawner.gd new file mode 100644 index 00000000..ed2596f2 --- /dev/null +++ b/game/scripts/world_generator/subzones/dungeon_spawner.gd @@ -0,0 +1,96 @@ +tool +extends SubZone + +export(PackedScene) var dungeon_teleporter : PackedScene + +var voxel_scale : float = 1 +var current_seed : int = 0 + +var main_chunk_pos_x : int = 0 +var main_chunk_pos_z : int = 0 + +func _setup() -> void: + main_chunk_pos_x = get_parent_pos().x + get_rect().position.x + get_rect().size.x / 2 + main_chunk_pos_z = get_parent_pos().y + get_rect().position.y + get_rect().size.y / 2 + +func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, raycast : WorldGenRaycast) -> void: + voxel_scale = chunk.voxel_scale + current_seed = pseed + + var cx : int = chunk.get_position_x() + var cz : int = chunk.get_position_z() + + var chunk_seed : int = 123 + (cx * 231) + (cz * 123) + + var rng : RandomNumberGenerator = RandomNumberGenerator.new() + rng.seed = chunk_seed + + if chunk.position_x == main_chunk_pos_x && chunk.position_z == main_chunk_pos_z: + spawn_dungeon(chunk, chunk_seed, spawn_mobs) + +func spawn_dungeon(chunk: TerrainChunk, dungeon_seed : int, spawn_mobs : bool) -> void: + var world_space_data_coordinates_x : int = chunk.position_x * chunk.size_x + var world_space_data_coordinates_z : int = chunk.position_z * chunk.size_z + + var vpx : int = 6 + var vpz : int = 6 + + var x : float = (world_space_data_coordinates_x + vpx) * chunk.voxel_scale + var z : float = (world_space_data_coordinates_z + vpz) * chunk.voxel_scale + + var vh : int = chunk.get_voxel(vpx, vpz, TerrainChunkDefault.DEFAULT_CHANNEL_ISOLEVEL) + + var orx : int = (randi() % 3) + 2 + var orz : int = (randi() % 3) + 2 + + for wx in range(vpx - orx, vpx + orx + 1): + for wz in range(vpz - orz, vpz + orz + 1): + chunk.set_voxel(vh, wx, wz, TerrainChunkDefault.DEFAULT_CHANNEL_ISOLEVEL) + var vwh : float = chunk.get_voxel_scale() * chunk.get_world_height() * (vh / 255.0) + + var dt : Spatial = dungeon_teleporter.instance() + chunk.voxel_world.add_child(dt) + dt.owner_chunk = chunk + + var level : int = 2 + + if chunk.get_voxel_world().has_method("get_mob_level"): + level = chunk.get_voxel_world().get_mob_level() + + dt.min_level = level - 1 + dt.max_level = level + 1 + dt.dungeon_seed = dungeon_seed + dt.spawn_mobs = spawn_mobs + dt.transform = Transform(Basis().scaled(Vector3(chunk.voxel_scale, chunk.voxel_scale, chunk.voxel_scale)), Vector3(x, vwh, z)) + + +func get_editor_rect_border_color() -> Color: + return Color(0.8, 0.8, 0.8, 1) + +func get_editor_rect_color() -> Color: + return Color(0.8, 0.8, 0.8, 0.9) + +func get_editor_rect_border_size() -> int: + return 2 + +func get_editor_font_color() -> Color: + return Color(0, 0, 0, 1) + +func get_editor_class() -> String: + return "DungeonSpawner" + +func get_editor_additional_text() -> String: + return "DungeonSpawner" + +func get_dungeon_teleporter() -> PackedScene: + return dungeon_teleporter + +func set_dungeon_teleporter(ed : PackedScene) -> void: + dungeon_teleporter = ed + emit_changed() + +func setup_property_inspector(inspector) -> void: + .setup_property_inspector(inspector) + + inspector.add_h_separator() + inspector.add_slot_resource("get_dungeon_teleporter", "set_dungeon_teleporter", "Dungeon Teleporter", "PackedScene") diff --git a/game/scripts/world_generator/zones/test_zone.gd b/game/scripts/world_generator/zones/test_zone.gd index 16cf7940..e222085b 100644 --- a/game/scripts/world_generator/zones/test_zone.gd +++ b/game/scripts/world_generator/zones/test_zone.gd @@ -99,12 +99,12 @@ func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, r gen_terra_chunk(chunk, rng, raycast) - if chunk.position_x == 0 && chunk.position_z == 0: - #test - spawn_dungeon(chunk, chunk_seed, spawn_mobs) - else: - if rng.randi() % 10 == 0: - spawn_dungeon(chunk, chunk_seed, spawn_mobs) +# if chunk.position_x == 0 && chunk.position_z == 0: +# #test +# spawn_dungeon(chunk, chunk_seed, spawn_mobs) +# else: +# if rng.randi() % 10 == 0: +# spawn_dungeon(chunk, chunk_seed, spawn_mobs) if not Engine.editor_hint and spawn_mobs and rng.randi() % 4 == 0: var level : int = 1 diff --git a/game/worlds/test_world/test_world.tres b/game/worlds/test_world/test_world.tres index 189dccfe..0f0bb07e 100644 --- a/game/worlds/test_world/test_world.tres +++ b/game/worlds/test_world/test_world.tres @@ -1,4 +1,4 @@ -[gd_resource type="Resource" load_steps=14 format=2] +[gd_resource type="Resource" load_steps=17 format=2] [ext_resource path="res://scripts/world_generator/worlds/ocean_base_world.gd" type="Script" id=1] [ext_resource path="res://scripts/world_generator/subzones/spawner.gd" type="Script" id=2] @@ -10,21 +10,36 @@ [ext_resource path="res://entity_classes/naturalist/entities/4_naturalist_vendor.tres" type="EntityData" id=8] [ext_resource path="res://entity_classes/naturalist/entities/3_naturalist_trainer.tres" type="EntityData" id=9] [ext_resource path="res://worlds/test_world/noises/base_ocean_noise.tres" type="FastnoiseNoiseParams" id=10] +[ext_resource path="res://scripts/world_generator/subzones/dungeon_spawner.gd" type="Script" id=11] [sub_resource type="Resource" id=2] resource_name = "Spawner" script = ExtResource( 2 ) -rect = Rect2( 8.375, 2.6875, 5, 5 ) +rect = Rect2( 10.5469, 4.9375, 0.625, 0.5625 ) locked = false trainer = ExtResource( 9 ) vendor = ExtResource( 8 ) +[sub_resource type="Resource" id=4] +resource_name = "DungeonSpawner" +script = ExtResource( 11 ) +rect = Rect2( 10.6562, 5.01562, 0.421875, 0.421875 ) +locked = false +dungeon_teleporter = ExtResource( 4 ) + +[sub_resource type="Resource" id=5] +resource_name = "DungeonSpawner2" +script = ExtResource( 11 ) +rect = Rect2( 13.689, 6.71965, 1.30323, 1.0916 ) +locked = false +dungeon_teleporter = ExtResource( 4 ) + [sub_resource type="Resource" id=3] resource_name = "TestForest" script = ExtResource( 7 ) rect = Rect2( 8.75557, 6.875, 21.9944, 11.2839 ) locked = false -subzones = [ SubResource( 2 ) ] +subzones = [ SubResource( 2 ), SubResource( 4 ), SubResource( 5 ) ] zone_radius = 0.5 zone_bevel = 0.3 zone_base = 0.0