Introduced max number of tries for guest spawning.

Also spawn zone now adjustable by exclusionzone size
This commit is contained in:
Sammy 2021-06-12 18:59:42 +02:00
parent 298dd2f0ae
commit eeda21349e
4 changed files with 19 additions and 11 deletions

View File

@ -10,6 +10,7 @@ signal dropped_off_idle
signal dropped_off_fail signal dropped_off_fail
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
onready var sprite = $Sprite onready var sprite = $Sprite
onready var exclusionZoneShape = $ExclusionZone/CollisionShape2D
var colorList = [ var colorList = [
Color.yellow, Color.yellow,

View File

@ -4,7 +4,7 @@
[ext_resource path="res://Guest.gd" type="Script" id=2] [ext_resource path="res://Guest.gd" type="Script" id=2]
[sub_resource type="CircleShape2D" id=1] [sub_resource type="CircleShape2D" id=1]
radius = 150.29 radius = 124.702
[sub_resource type="CapsuleShape2D" id=2] [sub_resource type="CapsuleShape2D" id=2]
radius = 6.04475 radius = 6.04475
@ -15,6 +15,9 @@ radius = 24.5545
[node name="Guest" type="KinematicBody2D"] [node name="Guest" type="KinematicBody2D"]
script = ExtResource( 2 ) script = ExtResource( 2 )
__meta__ = {
"_edit_horizontal_guides_": [ ]
}
[node name="ExclusionZone" type="Area2D" parent="."] [node name="ExclusionZone" type="Area2D" parent="."]

View File

@ -8,13 +8,17 @@ var Guest = load("res://Guest.tscn")
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var spawn_tries = 0 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): func _on_Guest_picked_up(destinationColor,name):
print('Picked Up %s with name %s' % [destinationColor,name]) print('Picked Up %s with name %s' % [destinationColor,name])
func _ready(): func _ready():
rng.randomize() rng.randomize()
pass print(radius_guests)
func _physics_process(delta): func _physics_process(delta):
pass pass
@ -24,22 +28,27 @@ func _on_GuestTimer_timeout():
func create_new_guest(): func create_new_guest():
if guests.get_children().size() >= 7: if guests.get_children().size() >= max_guests:
return return
var position_found = false; var position_found = false;
var new_guest_position var new_guest_position
while !position_found: while !position_found:
if spawn_tries >= MAX_SPAWN_TRIES:
print("Max spawn tries reached!")
return
position_found = true; position_found = true;
var new_guest_cell = used_cells[rng.randi_range(0, used_cells.size()) -1] var new_guest_cell = used_cells[rng.randi_range(0, used_cells.size()) -1]
new_guest_position = streets.map_to_world(new_guest_cell) new_guest_position = streets.map_to_world(new_guest_cell)
for guest in guests.get_children(): 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 position_found = false
spawn_tries+=1
break break
var new_guest = Guest.instance() var new_guest = Guest.instance()
new_guest.position = new_guest_position; new_guest.position = new_guest_position;
guests.add_child(new_guest) guests.add_child(new_guest)
spawn_tries = 0

View File

@ -181,6 +181,7 @@ points = PoolVector2Array( 0, 0, 16, 0, 16, 16, 0, 16 )
[node name="World" type="Node2D"] [node name="World" type="Node2D"]
script = ExtResource( 6 ) script = ExtResource( 6 )
max_guests = 30
[node name="TextureRect" type="TextureRect" parent="."] [node name="TextureRect" type="TextureRect" parent="."]
modulate = Color( 0.0352941, 0.121569, 0, 1 ) modulate = Color( 0.0352941, 0.121569, 0, 1 )
@ -222,7 +223,7 @@ position = Vector2( 520.662, 230.739 )
position = Vector2( 166.312, 322.885 ) position = Vector2( 166.312, 322.885 )
[node name="GuestTimer" type="Timer" parent="."] [node name="GuestTimer" type="Timer" parent="."]
wait_time = 5.0 wait_time = 1.019
autostart = true autostart = true
[node name="Guests" type="Node" parent="."] [node name="Guests" type="Node" parent="."]
@ -230,11 +231,5 @@ autostart = true
[node name="Guest" parent="Guests" instance=ExtResource( 5 )] [node name="Guest" parent="Guests" instance=ExtResource( 5 )]
position = Vector2( 337.119, 100.387 ) 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="timeout" from="GuestTimer" to="." method="_on_GuestTimer_timeout"]
[connection signal="picked_up" from="Guests/Guest" to="." method="_on_Guest_picked_up"] [connection signal="picked_up" from="Guests/Guest" to="." method="_on_Guest_picked_up"]