optimize energy scene and code

This commit is contained in:
Chris Cromer 2022-11-19 23:06:17 -03:00
parent 9e11c80f4f
commit c188688bcf
2 changed files with 21 additions and 4 deletions

View File

@ -4,11 +4,12 @@ using System;
public class Energy : Area2D
{
private Event _eventBus;
private AnimatedSprite _sprite;
public override void _Ready()
{
_eventBus = GetNode<Event>("/root/Event");
GetNode<AnimatedSprite>("AnimatedSprite").Play();
_sprite = GetNode<AnimatedSprite>("AnimatedSprite");
}
public void OnEnergyBodyEntered(Node body)
@ -22,4 +23,14 @@ public class Energy : Area2D
}
QueueFree();
}
public void OnVisibilityNotifier2DScreenEntered()
{
_sprite.Play();
}
public void OnVisibilityNotifier2DScreenExited()
{
_sprite.Stop();
}
}

View File

@ -20,7 +20,7 @@ animations = [ {
} ]
[sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 2.5, 6.5 )
extents = Vector2( 2.5, 5.75 )
[node name="Energy" type="Area2D"]
collision_layer = 8
@ -29,10 +29,16 @@ script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 3 )
animation = "glow"
centered = false
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false
position = Vector2( -0.5, -0.5 )
position = Vector2( 7.5, 7.25 )
shape = SubResource( 4 )
[node name="VisibilityNotifier2D" type="VisibilityNotifier2D" parent="."]
position = Vector2( 14, 11 )
rect = Rect2( -10, -10, 7, 13 )
[connection signal="body_entered" from="." to="." method="OnEnergyBodyEntered"]
[connection signal="screen_entered" from="VisibilityNotifier2D" to="." method="OnVisibilityNotifier2DScreenEntered"]
[connection signal="screen_exited" from="VisibilityNotifier2D" to="." method="OnVisibilityNotifier2DScreenExited"]