From 0ae9d239a49ae87230dd5d7c7725b0373da8a02e Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 4 Aug 2022 19:40:21 +0200 Subject: [PATCH] Reworked how spawn positions work in the world_generator addon. Now all resources have ovverridable methods, and all of them are collected based on those. Also now the spawner works in local space. --- .../resources/world_gen_base_resource.gd | 25 +++++++++++++++++++ .../world_generator/subzones/spawner.gd | 21 ++++++++-------- .../worlds/ocean_base_world.gd | 1 - .../world_generators/TerrainWorldGenerator.gd | 9 ++++--- game/worlds/test_world/test_world.tres | 6 ++--- 5 files changed, 43 insertions(+), 19 deletions(-) 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 bb856165..ef9634d5 100644 --- a/game/addons/world_generator/resources/world_gen_base_resource.gd +++ b/game/addons/world_generator/resources/world_gen_base_resource.gd @@ -57,6 +57,31 @@ func create_content(item_name : String = "") -> void: func remove_content_entry(entry : WorldGenBaseResource) -> void: pass +func is_spawner() -> bool: + return _is_spawner() + +func _is_spawner() -> bool: + return false + +func get_spawn_local_position() -> Vector2: + return _get_spawn_local_position() + +func _get_spawn_local_position() -> Vector2: + return Vector2() + +func get_spawn_positions(var parent_position : Vector2 = Vector2()) -> Array: + if is_spawner(): + return [ [ resource_name, parent_position + rect.position + get_spawn_local_position() ] ] + + var spawners : Array + var p : Vector2 = parent_position + rect.position + + for c in get_content(): + if c: + spawners.append_array(c.get_spawn_positions(p)) + + return spawners + func get_content_with_name(name : String) -> WorldGenBaseResource: if resource_name == name: return self diff --git a/game/scripts/world_generator/subzones/spawner.gd b/game/scripts/world_generator/subzones/spawner.gd index a3046297..580eb42b 100644 --- a/game/scripts/world_generator/subzones/spawner.gd +++ b/game/scripts/world_generator/subzones/spawner.gd @@ -4,15 +4,11 @@ extends SubZone export (EntityData) var trainer : EntityData export (EntityData) var vendor : EntityData -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 _is_spawner() -> bool: + return true -func get_spawn_chunk_position() -> Vector2: - return Vector2(main_chunk_pos_x, main_chunk_pos_z) +func _get_spawn_local_position() -> Vector2: + return Vector2(get_rect().size.x / 2, get_rect().size.y / 2) func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, raycast : WorldGenRaycast) -> void: if !spawn_mobs: @@ -21,10 +17,13 @@ func _generate_terra_chunk(chunk: TerrainChunk, pseed : int, spawn_mobs: bool, r if trainer == null || vendor == null: return - if chunk.position_x == main_chunk_pos_x && chunk.position_z == main_chunk_pos_z: - var pos : Vector3 = Vector3(main_chunk_pos_x * chunk.get_size_x() * chunk.voxel_scale + 4, 50 * chunk.voxel_scale, main_chunk_pos_z * chunk.get_size_z() * chunk.voxel_scale + 4) + var p : Vector2i = Vector2i(get_rect().size.x / 2, get_rect().size.y / 2) + var lp : Vector2i = raycast.get_local_position() + + if p == lp: + var pos : Vector3 = Vector3(chunk.get_position_x() * chunk.get_size_x() * chunk.voxel_scale + 4, 50 * chunk.voxel_scale, chunk.get_position_z() * chunk.get_size_z() * chunk.voxel_scale + 4) ESS.entity_spawner.spawn_mob(trainer.id, 1, pos) - pos = Vector3(main_chunk_pos_x * chunk.get_size_x() * chunk.voxel_scale + 2, 50 * chunk.voxel_scale, main_chunk_pos_z * chunk.get_size_z() * chunk.voxel_scale + 2) + pos = Vector3(chunk.get_position_x() * chunk.get_size_x() * chunk.voxel_scale + 2, 50 * chunk.voxel_scale, chunk.get_position_z() * chunk.get_size_z() * chunk.voxel_scale + 2) ESS.entity_spawner.spawn_mob(vendor.id, 1, pos) func get_trainer() -> EntityData: diff --git a/game/scripts/world_generator/worlds/ocean_base_world.gd b/game/scripts/world_generator/worlds/ocean_base_world.gd index e8f3035d..eed929df 100644 --- a/game/scripts/world_generator/worlds/ocean_base_world.gd +++ b/game/scripts/world_generator/worlds/ocean_base_world.gd @@ -20,7 +20,6 @@ func setup_property_inspector(inspector) -> 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 raycast : WorldGenRaycast = get_hit_stack(p) _generate_terra_chunk(chunk, pseed, spawn_mobs, raycast) diff --git a/game/scripts/world_generators/TerrainWorldGenerator.gd b/game/scripts/world_generators/TerrainWorldGenerator.gd index d2d28f9b..7351fc48 100644 --- a/game/scripts/world_generators/TerrainWorldGenerator.gd +++ b/game/scripts/world_generators/TerrainWorldGenerator.gd @@ -41,11 +41,12 @@ func setup(world : TerrainWorld, level_seed : int, spawn_mobs : bool, library: T func get_spawn_chunk_position() -> Vector2: if world_gen_world != null: - var spawner = world_gen_world.get_content_with_name("Spawner") + var spawners : Array = world_gen_world.get_spawn_positions() + + if spawners.size() > 0: + var v : Vector2 = spawners[0][1] + return v - if spawner: - return spawner.get_spawn_chunk_position() - return Vector2() func _generate_chunk(chunk : TerrainChunk) -> void: diff --git a/game/worlds/test_world/test_world.tres b/game/worlds/test_world/test_world.tres index 0f0bb07e..71d49007 100644 --- a/game/worlds/test_world/test_world.tres +++ b/game/worlds/test_world/test_world.tres @@ -15,7 +15,7 @@ [sub_resource type="Resource" id=2] resource_name = "Spawner" script = ExtResource( 2 ) -rect = Rect2( 10.5469, 4.9375, 0.625, 0.5625 ) +rect = Rect2( 9.6719, 4.34375, 2.09375, 1.6875 ) locked = false trainer = ExtResource( 9 ) vendor = ExtResource( 8 ) @@ -23,7 +23,7 @@ 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 ) +rect = Rect2( 10.4375, 4.76562, 0.734375, 0.734375 ) locked = false dungeon_teleporter = ExtResource( 4 ) @@ -50,7 +50,7 @@ prop_tree2 = ExtResource( 5 ) [sub_resource type="Resource" id=1] resource_name = "Continent" script = ExtResource( 3 ) -rect = Rect2( 250, 250, 40, 25 ) +rect = Rect2( 241, 247, 40, 25 ) locked = false zones = [ SubResource( 3 ) ] continent_radius = 0.5