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.

This commit is contained in:
Relintai 2022-08-04 19:40:21 +02:00
parent ada7cf03e7
commit 0ae9d239a4
5 changed files with 43 additions and 19 deletions

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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:

View File

@ -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