Skip to content

Commit

Permalink
Remove bones when to many are in the level.
Browse files Browse the repository at this point in the history
Fix bug where skeleton bursts into bones two times when dying.
  • Loading branch information
dardanbujupaj committed Apr 16, 2022
1 parent a9af554 commit 075304a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion default_theme.tres
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[ext_resource path="res://fonts/Beholden-Regular.ttf" type="DynamicFontData" id=1]

[sub_resource type="DynamicFont" id=1]
size = 36
size = 48
outline_size = 2
outline_color = Color( 0, 0, 0, 1 )
font_data = ExtResource( 1 )
Expand Down
38 changes: 34 additions & 4 deletions scenes/skeleton/Bone.gd
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
extends RigidBody2D

const MAX_BONES = 128
const MAX_MONITORING = 16

# Declare member variables here. Examples:
# var a: int = 2
# var b: String = "text"
var bone_number: int


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
randomize()

$ResetMonitoringTimer.wait_time = rand_range(0.0, 0.5)
$ResetMonitoringTimer.start()

bone_number = get_tree().get_nodes_in_group("bones").size()
$AudioStreamPlayer2D.pitch_scale = 1.0 + rand_range(-0.3, 0.4)


func _process(delta: float) -> void:
if get_tree().get_nodes_in_group("bones").size() > bone_number + MAX_BONES:
$RemovalTimer.wait_time = rand_range(0.0, 1.0)
$RemovalTimer.start()


func set_texture(texture: Texture) -> void:
var image := texture.get_data()

Expand All @@ -36,11 +47,30 @@ func set_texture(texture: Texture) -> void:
$Sprite.position = -center_of_mass

position += center_of_mass


func _on_Bone_body_entered(body: Node) -> void:
var velocity = linear_velocity.length()

if velocity > 10:
set_deferred("contact_monitor", false)
$AudioStreamPlayer2D.volume_db = linear2db(clamp(velocity, 0, 200) / 200)

$AudioStreamPlayer2D.play()


# Disable contact monitoring for sound
func _on_ResetMonitoringTimer_timeout() -> void:
if get_tree().get_nodes_in_group("bones").size() < bone_number + MAX_MONITORING:
set_deferred("contact_monitor", true)

$ResetMonitoringTimer.wait_time = rand_range(0.5, 1.5)
$ResetMonitoringTimer.start()
else:
set_deferred("contact_monitor", false)
set_deferred("contacts_reported", 0)
$ResetMonitoringTimer.stop()


func _on_RemovalTimer_timeout() -> void:
queue_free()
11 changes: 9 additions & 2 deletions scenes/skeleton/Bone.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
[ext_resource path="res://audio/effects/klack_01.ogg" type="AudioStream" id=1]
[ext_resource path="res://scenes/skeleton/Bone.gd" type="Script" id=2]

[node name="Bone" type="RigidBody2D"]
[node name="Bone" type="RigidBody2D" groups=["bones"]]
collision_layer = 8
collision_mask = 10
contacts_reported = 1
contact_monitor = true
script = ExtResource( 2 )

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
Expand All @@ -18,4 +17,12 @@ centered = false
[node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource( 1 )

[node name="ResetMonitoringTimer" type="Timer" parent="."]
one_shot = true

[node name="RemovalTimer" type="Timer" parent="."]
one_shot = true

[connection signal="body_entered" from="." to="." method="_on_Bone_body_entered"]
[connection signal="timeout" from="ResetMonitoringTimer" to="." method="_on_ResetMonitoringTimer_timeout"]
[connection signal="timeout" from="RemovalTimer" to="." method="_on_RemovalTimer_timeout"]
5 changes: 5 additions & 0 deletions scenes/skeleton/Skeleton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var velocity: Vector2

onready var animation_tree = $AnimationTree

onready var hit_area = $HitArea


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
Expand Down Expand Up @@ -77,6 +79,8 @@ func can_jump():


func die() -> void:
hit_area.set_collision_mask_bit(2, false)

$AudioStreamPlayer2D.stop()

var nodes_to_check = get_children()
Expand All @@ -91,6 +95,7 @@ func die() -> void:
position = start_position
velocity = Vector2()

hit_area.call_deferred("set_collision_mask_bit", 2, true)



Expand Down
2 changes: 0 additions & 2 deletions scenes/skeleton/Skeleton.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2766,7 +2766,6 @@ offset = Vector2( -22, 84 )

[node name="ShankRight" type="Sprite" parent="Sprites"]
position = Vector2( 27, -67 )
scale = Vector2( 1, 1 )
texture = ExtResource( 17 )
offset = Vector2( -18, -53 )

Expand All @@ -2777,7 +2776,6 @@ offset = Vector2( -5, 2 )

[node name="FootRight" type="Sprite" parent="Sprites"]
position = Vector2( 12, -3 )
scale = Vector2( 1, 1 )
texture = ExtResource( 23 )
offset = Vector2( -3, -117 )

Expand Down

0 comments on commit 075304a

Please sign in to comment.