Skip to content

Commit

Permalink
Tweak some code and docstrings
Browse files Browse the repository at this point in the history
- Directly connect signals to target methods when possible
- Improve coin animations so the audio can't trigger twice in the editor and so
the coins' properties are reset every time you start the game
  • Loading branch information
NathanLovato committed Nov 18, 2019
1 parent c687ff1 commit ea90101
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
4 changes: 4 additions & 0 deletions platformer-2d-rework/src/Actors/Gun.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
extends Position2D
class_name Gun
"""
Represents a weapon that spawns and shoots bullets.
The Cooldown timer controls the cooldown duration between shots.
"""


onready var sound_shoot: AudioStreamPlayer2D = $Shoot
Expand Down
3 changes: 0 additions & 3 deletions platformer-2d-rework/src/Objects/Bullet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ class_name Bullet
onready var animation_player: AnimationPlayer = $AnimationPlayer


func _on_Timer_timeout():
destroy()

func destroy() -> void:
animation_player.play("destroy")

Expand Down
2 changes: 1 addition & 1 deletion platformer-2d-rework/src/Objects/Bullet.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ autostart = true
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/destroy = SubResource( 6 )
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]
[connection signal="timeout" from="Timer" to="." method="destroy"]
24 changes: 12 additions & 12 deletions platformer-2d-rework/src/Objects/Coin.gd
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
extends Area2D
class_name Coin

"""
Collectible that disappears when the player touches it.
"""

onready var animation_player: AnimationPlayer = $AnimationPlayer


"""
If a body comes in contact with the Coin, we play the animation
that will remove it from the game.
The Coins only look for collisions on the Player layer as shown in the
collision mask.
This prevents other characters such as enemies picking them up.
We set monitoring to false in the picked animation so the Coin isn't picked up again.
The Coins only detects collisions with the Player thanks to its collision mask.
This prevents other characters such as enemies from picking up coins.
"""
func _on_body_entered(body: PhysicsBody2D):
func _on_body_entered(body: Node) -> void:
picked()


"""
When the player collides with a coin, the coin plays its 'picked' animation.
The animation takes cares of making the coin disappear, but also deactivates its collisions
and frees it from memory, saving us from writing more complex code.
Click the AnimationPlayer node to see the animation timeline.
"""
func picked() -> void:
animation_player.play("picked")
43 changes: 40 additions & 3 deletions platformer-2d-rework/src/Objects/Coin.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ tracks/3/loop_wrap = true
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/keys = {
"times": PoolRealArray( 0.01 ),
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 1,
"update": 2,
"values": [ true ]
}
tracks/4/type = "value"
Expand Down Expand Up @@ -99,10 +99,47 @@ tracks/0/keys = {
"update": 1,
"values": [ 0, 1, 2, 3, 2, 1, 0 ]
}
tracks/1/type = "value"
tracks/1/path = NodePath("Sprite:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 2,
"values": [ Vector2( 0, 0 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("Sprite:self_modulate")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 2,
"values": [ Color( 1, 1, 1, 1 ) ]
}
tracks/3/type = "value"
tracks/3/path = NodePath(".:monitoring")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 2,
"values": [ true ]
}

[sub_resource type="CircleShape2D" id=3]

[node name="Coin" type="Area2D"]
monitorable = false
collision_layer = 0
script = ExtResource( 4 )

Expand All @@ -121,4 +158,4 @@ shape = SubResource( 3 )

[node name="Pickup" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource( 3 )
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
[connection signal="body_entered" from="." to="." method="_on_body_entered" flags=6]
1 change: 1 addition & 0 deletions platformer-2d-rework/src/Platforms/MovingPlatform.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends Node2D
class_name MovingPlatform


onready var animation_player: AnimationPlayer = $AnimationPlayer


Expand Down

0 comments on commit ea90101

Please sign in to comment.