1
Fork 0
mirror of https://github.com/Steffo99/swear-jar.git synced 2024-11-21 23:34:18 +00:00

Spawn coins at random positions along the shape extents

This commit is contained in:
Steffo 2023-10-01 02:18:08 +02:00
parent abfadcee49
commit a10335c0b9
Signed by: steffo
GPG key ID: 2A24051445686895
3 changed files with 30 additions and 17 deletions

View file

@ -78,18 +78,18 @@ texture_filter = 1
[node name="GravityFromGyro" parent="UI/GameContainer/Game/Bottle" instance=ExtResource("2_m7p4p")] [node name="GravityFromGyro" parent="UI/GameContainer/Game/Bottle" instance=ExtResource("2_m7p4p")]
[node name="Spawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")] [node name="TimeSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")]
position = Vector2(-24, -490) position = Vector2(0, -490)
scene = ExtResource("2_dv01l") 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 autostart = true
[node name="ButtonSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")] [node name="ButtonSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")]
position = Vector2(24, -490) position = Vector2(0, -490)
scene = ExtResource("2_dv01l") 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="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"]

View file

@ -8,21 +8,34 @@ var buffer: int = 0
@export var buffer_cap: int @export var buffer_cap: int
@onready var area: Area2D = $Area @onready var area: Area2D = $Area
@onready var shape: CollisionShape2D = $Area/Shape
@onready var rng: RandomNumberGenerator = RandomNumberGenerator.new()
func spawn(): func spawn():
buffer += 1 buffer += 1
if buffer > buffer_cap: if buffer > buffer_cap:
print("Hit buffer!")
buffer = buffer_cap 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): func _physics_process(_delta):
if buffer > 0: if buffer > 0:
var overlapping_bodies = area.get_overlapping_bodies() _do_spawn()
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)

View file

@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://spawner/spawner.gd" id="1_xqfmg"] [ext_resource type="Script" path="res://spawner/spawner.gd" id="1_xqfmg"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_p13i4"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_p13i4"]
size = Vector2(14, 2) size = Vector2(32, 2)
[node name="Spawner" type="Node2D"] [node name="Spawner" type="Node2D"]
script = ExtResource("1_xqfmg") script = ExtResource("1_xqfmg")
@ -11,7 +11,7 @@ script = ExtResource("1_xqfmg")
[node name="Area" type="Area2D" parent="."] [node name="Area" type="Area2D" parent="."]
collision_mask = 4 collision_mask = 4
[node name="CoinShape" type="CollisionShape2D" parent="Area"] [node name="Shape" type="CollisionShape2D" parent="Area"]
position = Vector2(0, -3) position = Vector2(0, -3)
scale = Vector2(3, 3) scale = Vector2(3, 3)
shape = SubResource("RectangleShape2D_p13i4") shape = SubResource("RectangleShape2D_p13i4")