From 7ef67351fb2a50ee1d70575565fa25b60a93e3b6 Mon Sep 17 00:00:00 2001 From: magnetotail Date: Sat, 12 Jun 2021 14:46:23 +0200 Subject: [PATCH] Random spawning Guests A guest timer counts down and emits a signal to the world. The world uses the street map to determine a new random guest position and spawns it --- GuestTimer.gd | 16 ++++++++++++++++ World.gd | 23 +++++++++++++++-------- World.tscn | 9 ++++++++- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 GuestTimer.gd diff --git a/GuestTimer.gd b/GuestTimer.gd new file mode 100644 index 0000000..4dd555e --- /dev/null +++ b/GuestTimer.gd @@ -0,0 +1,16 @@ +extends Timer + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/World.gd b/World.gd index aa00cbf..f4cfb4c 100644 --- a/World.gd +++ b/World.gd @@ -1,18 +1,25 @@ extends Node2D +onready var streets = $Streets -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" +var Guest = load("res://Guest.tscn") + +var rng = RandomNumberGenerator.new() func _on_Guest_picked_up(destinationColor,name): print('Picked Up %s with name %s' % [destinationColor,name]) -# Called when the node enters the scene tree for the first time. func _ready(): - pass # Replace with function body. + rng.randomize() + pass -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass + +func _on_GuestTimer_timeout(): + var used_cells = streets.get_used_cells() + var new_guest_cell = used_cells[rng.randi_range(0, used_cells.size())] + var guest_position = streets.map_to_world(new_guest_cell) + print('new guest at: %s' % [guest_position]) + var guest = Guest.instance(); + guest.position = guest_position; + add_child(guest) diff --git a/World.tscn b/World.tscn index eca8a38..2b1203c 100644 --- a/World.tscn +++ b/World.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=22 format=2] +[gd_scene load_steps=23 format=2] [ext_resource path="res://assets/city_tiles/Street.png" type="Texture" id=1] [ext_resource path="res://assets/city_tiles/Buildings.png" type="Texture" id=2] @@ -7,6 +7,7 @@ [ext_resource path="res://Guest.tscn" type="PackedScene" id=5] [ext_resource path="res://World.gd" type="Script" id=6] [ext_resource path="res://assets/placeholder_white.png" type="Texture" id=7] +[ext_resource path="res://GuestTimer.gd" type="Script" id=8] [sub_resource type="TileSet" id=1] 0/name = "Street.png 0" @@ -230,4 +231,10 @@ position = Vector2( 761.14, 102.634 ) [node name="Guest3" parent="." instance=ExtResource( 5 )] position = Vector2( 758.143, 376.075 ) +[node name="GuestTimer" type="Timer" parent="."] +wait_time = 5.0 +autostart = true +script = ExtResource( 8 ) + [connection signal="picked_up" from="Guest" to="." method="_on_Guest_picked_up"] +[connection signal="timeout" from="GuestTimer" to="." method="_on_GuestTimer_timeout"]