From eeda21349e2fd1ddf12136481891eb7af0689f6f Mon Sep 17 00:00:00 2001 From: magnetotail Date: Sat, 12 Jun 2021 18:59:42 +0200 Subject: [PATCH] Introduced max number of tries for guest spawning. Also spawn zone now adjustable by exclusionzone size --- Guest.gd | 1 + Guest.tscn | 5 ++++- World.gd | 15 ++++++++++++--- World.tscn | 9 ++------- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Guest.gd b/Guest.gd index ee615fe..2159d1b 100644 --- a/Guest.gd +++ b/Guest.gd @@ -10,6 +10,7 @@ signal dropped_off_idle signal dropped_off_fail var rng = RandomNumberGenerator.new() onready var sprite = $Sprite +onready var exclusionZoneShape = $ExclusionZone/CollisionShape2D var colorList = [ Color.yellow, diff --git a/Guest.tscn b/Guest.tscn index b858cdb..a8085ef 100644 --- a/Guest.tscn +++ b/Guest.tscn @@ -4,7 +4,7 @@ [ext_resource path="res://Guest.gd" type="Script" id=2] [sub_resource type="CircleShape2D" id=1] -radius = 150.29 +radius = 124.702 [sub_resource type="CapsuleShape2D" id=2] radius = 6.04475 @@ -15,6 +15,9 @@ radius = 24.5545 [node name="Guest" type="KinematicBody2D"] script = ExtResource( 2 ) +__meta__ = { +"_edit_horizontal_guides_": [ ] +} [node name="ExclusionZone" type="Area2D" parent="."] diff --git a/World.gd b/World.gd index 5aacc71..f31cddb 100644 --- a/World.gd +++ b/World.gd @@ -8,13 +8,17 @@ var Guest = load("res://Guest.tscn") var rng = RandomNumberGenerator.new() var spawn_tries = 0 +const MAX_SPAWN_TRIES = 50 + +onready var radius_guests = guests.get_child(0).exclusionZoneShape.shape.radius * 2 +export var max_guests = 10 func _on_Guest_picked_up(destinationColor,name): print('Picked Up %s with name %s' % [destinationColor,name]) func _ready(): rng.randomize() - pass + print(radius_guests) func _physics_process(delta): pass @@ -24,22 +28,27 @@ func _on_GuestTimer_timeout(): func create_new_guest(): - if guests.get_children().size() >= 7: + if guests.get_children().size() >= max_guests: return var position_found = false; var new_guest_position while !position_found: + if spawn_tries >= MAX_SPAWN_TRIES: + print("Max spawn tries reached!") + return position_found = true; var new_guest_cell = used_cells[rng.randi_range(0, used_cells.size()) -1] new_guest_position = streets.map_to_world(new_guest_cell) for guest in guests.get_children(): - if (new_guest_position - guest.position).length() < 300: + if (new_guest_position - guest.position).length() < radius_guests: position_found = false + spawn_tries+=1 break var new_guest = Guest.instance() new_guest.position = new_guest_position; guests.add_child(new_guest) + spawn_tries = 0 diff --git a/World.tscn b/World.tscn index 37c5479..4f3727d 100644 --- a/World.tscn +++ b/World.tscn @@ -181,6 +181,7 @@ points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 ) [node name="World" type="Node2D"] script = ExtResource( 6 ) +max_guests = 30 [node name="TextureRect" type="TextureRect" parent="."] modulate = Color( 0.0352941, 0.121569, 0, 1 ) @@ -222,7 +223,7 @@ position = Vector2( 520.662, 230.739 ) position = Vector2( 166.312, 322.885 ) [node name="GuestTimer" type="Timer" parent="."] -wait_time = 5.0 +wait_time = 1.019 autostart = true [node name="Guests" type="Node" parent="."] @@ -230,11 +231,5 @@ autostart = true [node name="Guest" parent="Guests" instance=ExtResource( 5 )] position = Vector2( 337.119, 100.387 ) -[node name="Guest2" parent="Guests" instance=ExtResource( 5 )] -position = Vector2( 761.14, 102.634 ) - -[node name="Guest3" parent="Guests" instance=ExtResource( 5 )] -position = Vector2( 777.143, 410.075 ) - [connection signal="timeout" from="GuestTimer" to="." method="_on_GuestTimer_timeout"] [connection signal="picked_up" from="Guests/Guest" to="." method="_on_Guest_picked_up"]