From a10335c0b9c58c450886ea25a4461fa107c64594 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 1 Oct 2023 02:18:08 +0200 Subject: [PATCH] Spawn coins at random positions along the shape extents --- root.tscn | 14 +++++++------- spawner/spawner.gd | 29 +++++++++++++++++++++-------- spawner/spawner.tscn | 4 ++-- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/root.tscn b/root.tscn index 1f2efea..dfbeef2 100644 --- a/root.tscn +++ b/root.tscn @@ -78,18 +78,18 @@ texture_filter = 1 [node name="GravityFromGyro" parent="UI/GameContainer/Game/Bottle" instance=ExtResource("2_m7p4p")] -[node name="Spawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")] -position = Vector2(-24, -490) +[node name="TimeSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")] +position = Vector2(0, -490) scene = ExtResource("2_dv01l") -buffer_cap = 10 +buffer_cap = 1 -[node name="Timer" type="Timer" parent="UI/GameContainer/Game/Spawner"] +[node name="Timer" type="Timer" parent="UI/GameContainer/Game/TimeSpawner"] autostart = true [node name="ButtonSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")] -position = Vector2(24, -490) +position = Vector2(0, -490) scene = ExtResource("2_dv01l") -buffer_cap = 10 +buffer_cap = 50 [connection signal="pressed" from="UI/Rows/UpperButtons/SpawnButton" to="UI/GameContainer/Game/ButtonSpawner" method="spawn"] -[connection signal="timeout" from="UI/GameContainer/Game/Spawner/Timer" to="UI/GameContainer/Game/Spawner" method="spawn"] +[connection signal="timeout" from="UI/GameContainer/Game/TimeSpawner/Timer" to="UI/GameContainer/Game/TimeSpawner" method="spawn"] diff --git a/spawner/spawner.gd b/spawner/spawner.gd index f188599..3bd99bc 100644 --- a/spawner/spawner.gd +++ b/spawner/spawner.gd @@ -8,21 +8,34 @@ var buffer: int = 0 @export var buffer_cap: int @onready var area: Area2D = $Area +@onready var shape: CollisionShape2D = $Area/Shape + +@onready var rng: RandomNumberGenerator = RandomNumberGenerator.new() func spawn(): buffer += 1 if buffer > buffer_cap: + print("Hit buffer!") buffer = buffer_cap +func _select_spawn_position() -> Vector2: + var variance = shape.shape.size.x / 2 + var x = rng.randf_range(-variance, +variance) + return Vector2(x, 0) + +func _do_spawn(): + var overlapping_bodies = area.get_overlapping_bodies() + for overlapping_body in overlapping_bodies: + if overlapping_body.collision_layer && 0b100: + return + var scene_instant = scene.instantiate() + scene_instant.position = _select_spawn_location() + add_child(scene_instant) + buffer -= 1 + + func _physics_process(_delta): if buffer > 0: - var overlapping_bodies = area.get_overlapping_bodies() - for overlapping_body in overlapping_bodies: - if overlapping_body.collision_layer && 0b100: - return - var scene_instant = scene.instantiate() - scene_instant.position = Vector2.ZERO - buffer -= 1 - add_child(scene_instant) + _do_spawn() diff --git a/spawner/spawner.tscn b/spawner/spawner.tscn index 101db56..56a8b1d 100644 --- a/spawner/spawner.tscn +++ b/spawner/spawner.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://spawner/spawner.gd" id="1_xqfmg"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_p13i4"] -size = Vector2(14, 2) +size = Vector2(32, 2) [node name="Spawner" type="Node2D"] script = ExtResource("1_xqfmg") @@ -11,7 +11,7 @@ script = ExtResource("1_xqfmg") [node name="Area" type="Area2D" parent="."] collision_mask = 4 -[node name="CoinShape" type="CollisionShape2D" parent="Area"] +[node name="Shape" type="CollisionShape2D" parent="Area"] position = Vector2(0, -3) scale = Vector2(3, 3) shape = SubResource("RectangleShape2D_p13i4")