mirror of
https://github.com/Steffo99/swear-jar.git
synced 2024-11-22 07:44:17 +00:00
Buffer area overlapping bodies as well so spawns are more natural
This commit is contained in:
parent
c9c57a1ff9
commit
e80e81b938
3 changed files with 54 additions and 41 deletions
75
root.tscn
75
root.tscn
|
@ -16,6 +16,45 @@ grow_horizontal = 2
|
||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
size_flags_vertical = 8
|
size_flags_vertical = 8
|
||||||
|
|
||||||
|
[node name="GameContainer" type="Control" parent="UI"]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 7
|
||||||
|
anchor_left = 0.5
|
||||||
|
anchor_top = 1.0
|
||||||
|
anchor_right = 0.5
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 0
|
||||||
|
|
||||||
|
[node name="Game" type="Node2D" parent="UI/GameContainer"]
|
||||||
|
texture_filter = 1
|
||||||
|
|
||||||
|
[node name="Bottle" parent="UI/GameContainer/Game" instance=ExtResource("1_4fmd3")]
|
||||||
|
|
||||||
|
[node name="GravityFromGyro" parent="UI/GameContainer/Game/Bottle" instance=ExtResource("2_m7p4p")]
|
||||||
|
|
||||||
|
[node name="TimeSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")]
|
||||||
|
position = Vector2(0, -480)
|
||||||
|
scene = ExtResource("2_dv01l")
|
||||||
|
buffer_cap = 1
|
||||||
|
spawn_position_range_x = 32.0
|
||||||
|
spawn_rotation_range = 15.0
|
||||||
|
overlapping_bodies_collision_mask = 4
|
||||||
|
overlapping_body_count_limit = 4
|
||||||
|
|
||||||
|
[node name="Timer" type="Timer" parent="UI/GameContainer/Game/TimeSpawner"]
|
||||||
|
wait_time = 0.03
|
||||||
|
autostart = true
|
||||||
|
|
||||||
|
[node name="ButtonSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")]
|
||||||
|
position = Vector2(0, -480)
|
||||||
|
scene = ExtResource("2_dv01l")
|
||||||
|
buffer_cap = 50
|
||||||
|
spawn_position_range_x = 32.0
|
||||||
|
spawn_rotation_range = 15.0
|
||||||
|
overlapping_bodies_collision_mask = 4
|
||||||
|
overlapping_body_count_limit = 4
|
||||||
|
|
||||||
[node name="Rows" type="VBoxContainer" parent="UI"]
|
[node name="Rows" type="VBoxContainer" parent="UI"]
|
||||||
layout_mode = 1
|
layout_mode = 1
|
||||||
anchors_preset = 15
|
anchors_preset = 15
|
||||||
|
@ -61,39 +100,5 @@ size_flags_horizontal = 8
|
||||||
text = "Shop"
|
text = "Shop"
|
||||||
alignment = 2
|
alignment = 2
|
||||||
|
|
||||||
[node name="GameContainer" type="Control" parent="UI"]
|
|
||||||
layout_mode = 1
|
|
||||||
anchors_preset = 7
|
|
||||||
anchor_left = 0.5
|
|
||||||
anchor_top = 1.0
|
|
||||||
anchor_right = 0.5
|
|
||||||
anchor_bottom = 1.0
|
|
||||||
grow_horizontal = 2
|
|
||||||
grow_vertical = 0
|
|
||||||
|
|
||||||
[node name="Game" type="Node2D" parent="UI/GameContainer"]
|
|
||||||
texture_filter = 1
|
|
||||||
|
|
||||||
[node name="Bottle" parent="UI/GameContainer/Game" instance=ExtResource("1_4fmd3")]
|
|
||||||
|
|
||||||
[node name="GravityFromGyro" parent="UI/GameContainer/Game/Bottle" instance=ExtResource("2_m7p4p")]
|
|
||||||
|
|
||||||
[node name="TimeSpawner" parent="UI/GameContainer/Game" instance=ExtResource("3_pubxn")]
|
|
||||||
position = Vector2(0, -480)
|
|
||||||
scene = ExtResource("2_dv01l")
|
|
||||||
buffer_cap = 1
|
|
||||||
spawn_position_range_x = 32.0
|
|
||||||
spawn_rotation_range = 15.0
|
|
||||||
|
|
||||||
[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(0, -480)
|
|
||||||
scene = ExtResource("2_dv01l")
|
|
||||||
buffer_cap = 50
|
|
||||||
spawn_position_range_x = 32.0
|
|
||||||
spawn_rotation_range = 15.0
|
|
||||||
|
|
||||||
[connection signal="pressed" from="UI/Rows/UpperButtons/SpawnButton" to="UI/GameContainer/Game/ButtonSpawner" method="spawn"]
|
|
||||||
[connection signal="timeout" from="UI/GameContainer/Game/TimeSpawner/Timer" to="UI/GameContainer/Game/TimeSpawner" method="spawn"]
|
[connection signal="timeout" from="UI/GameContainer/Game/TimeSpawner/Timer" to="UI/GameContainer/Game/TimeSpawner" method="spawn"]
|
||||||
|
[connection signal="pressed" from="UI/Rows/UpperButtons/SpawnButton" to="UI/GameContainer/Game/ButtonSpawner" method="spawn"]
|
||||||
|
|
|
@ -13,6 +13,9 @@ var buffer: int = 0
|
||||||
@export_range(0, 90) var spawn_rotation_range: float
|
@export_range(0, 90) var spawn_rotation_range: float
|
||||||
@onready var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
@onready var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||||
|
|
||||||
|
@export_flags_2d_physics var overlapping_bodies_collision_mask: int
|
||||||
|
@export_range(0, 16) var overlapping_body_count_limit: int
|
||||||
|
|
||||||
|
|
||||||
func spawn():
|
func spawn():
|
||||||
buffer += 1
|
buffer += 1
|
||||||
|
@ -20,6 +23,14 @@ func spawn():
|
||||||
print("Hit buffer!")
|
print("Hit buffer!")
|
||||||
buffer = buffer_cap
|
buffer = buffer_cap
|
||||||
|
|
||||||
|
func _count_overlapping_bodies() -> int:
|
||||||
|
var overlapping_bodies = area.get_overlapping_bodies()
|
||||||
|
var overlapping_body_count = 0
|
||||||
|
for overlapping_body in overlapping_bodies:
|
||||||
|
if overlapping_body.collision_layer && overlapping_bodies_collision_mask:
|
||||||
|
overlapping_body_count += 1
|
||||||
|
return overlapping_body_count
|
||||||
|
|
||||||
|
|
||||||
func _select_spawn_position() -> Vector2:
|
func _select_spawn_position() -> Vector2:
|
||||||
return Vector2(rng.randf_range(-spawn_position_range_x, +spawn_position_range_x), 0)
|
return Vector2(rng.randf_range(-spawn_position_range_x, +spawn_position_range_x), 0)
|
||||||
|
@ -28,9 +39,7 @@ func _select_spawn_rotation() -> float:
|
||||||
return rng.randf_range(-spawn_rotation_range, spawn_rotation_range)
|
return rng.randf_range(-spawn_rotation_range, spawn_rotation_range)
|
||||||
|
|
||||||
func _do_spawn():
|
func _do_spawn():
|
||||||
var overlapping_bodies = area.get_overlapping_bodies()
|
if _count_overlapping_bodies() > overlapping_body_count_limit:
|
||||||
for overlapping_body in overlapping_bodies:
|
|
||||||
if overlapping_body.collision_layer && 0b100:
|
|
||||||
return
|
return
|
||||||
var scene_instant = scene.instantiate()
|
var scene_instant = scene.instantiate()
|
||||||
scene_instant.position = _select_spawn_position()
|
scene_instant.position = _select_spawn_position()
|
||||||
|
|
|
@ -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(96, 32)
|
size = Vector2(96, 96)
|
||||||
|
|
||||||
[node name="Spawner" type="Node2D"]
|
[node name="Spawner" type="Node2D"]
|
||||||
script = ExtResource("1_xqfmg")
|
script = ExtResource("1_xqfmg")
|
||||||
|
@ -12,5 +12,4 @@ script = ExtResource("1_xqfmg")
|
||||||
collision_mask = 4
|
collision_mask = 4
|
||||||
|
|
||||||
[node name="Shape" type="CollisionShape2D" parent="Area"]
|
[node name="Shape" type="CollisionShape2D" parent="Area"]
|
||||||
position = Vector2(0, -16)
|
|
||||||
shape = SubResource("RectangleShape2D_p13i4")
|
shape = SubResource("RectangleShape2D_p13i4")
|
||||||
|
|
Loading…
Reference in a new issue