diff --git a/scenes/collectables/Energy.cs b/scenes/collectables/Energy.cs new file mode 100644 index 0000000..e5c34bf --- /dev/null +++ b/scenes/collectables/Energy.cs @@ -0,0 +1,25 @@ +using Godot; +using System; + +public class Energy : Area2D +{ + Event eventBus; + + public override void _Ready() + { + eventBus = GetNode("/root/Event"); + GetNode("AnimatedSprite").Play(); + } + + public void OnEnergyBodyEntered(Node body) + { + Disconnect("body_entered", this, "OnEnergyBodyEntered"); + SetCollisionMaskBit((int) Game.PhysicsLayer.Player, false); + SetCollisionLayerBit((int) Game.PhysicsLayer.Collectable, false); + if (String.Equals(body.Name, "Player")) + { + eventBus.EmitSignal("EnergyCollected", 1); + } + QueueFree(); + } +} diff --git a/scenes/collectables/Energy.tscn b/scenes/collectables/Energy.tscn index ea2493b..5c11984 100644 --- a/scenes/collectables/Energy.tscn +++ b/scenes/collectables/Energy.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://assets/collectable/energy.png" type="Texture" id=1] +[ext_resource path="res://scenes/collectables/Energy.cs" type="Script" id=2] [sub_resource type="AtlasTexture" id=1] atlas = ExtResource( 1 ) @@ -22,13 +23,16 @@ animations = [ { extents = Vector2( 2.5, 6.5 ) [node name="Energy" type="Area2D"] +collision_layer = 8 +script = ExtResource( 2 ) [node name="AnimatedSprite" type="AnimatedSprite" parent="."] frames = SubResource( 3 ) animation = "glow" -playing = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] visible = false position = Vector2( -0.5, -0.5 ) shape = SubResource( 4 ) + +[connection signal="body_entered" from="." to="." method="OnEnergyBodyEntered"]