add energy code

This commit is contained in:
Chris Cromer 2022-11-10 00:17:42 -03:00
parent 65149fcb42
commit c2d1da5746
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 31 additions and 2 deletions

View File

@ -0,0 +1,25 @@
using Godot;
using System;
public class Energy : Area2D
{
Event eventBus;
public override void _Ready()
{
eventBus = GetNode<Event>("/root/Event");
GetNode<AnimatedSprite>("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();
}
}

View File

@ -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://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] [sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
@ -22,13 +23,16 @@ animations = [ {
extents = Vector2( 2.5, 6.5 ) extents = Vector2( 2.5, 6.5 )
[node name="Energy" type="Area2D"] [node name="Energy" type="Area2D"]
collision_layer = 8
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 3 ) frames = SubResource( 3 )
animation = "glow" animation = "glow"
playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false visible = false
position = Vector2( -0.5, -0.5 ) position = Vector2( -0.5, -0.5 )
shape = SubResource( 4 ) shape = SubResource( 4 )
[connection signal="body_entered" from="." to="." method="OnEnergyBodyEntered"]