cirnogodot/Scenes/Fragola.cs

58 lines
1.2 KiB
C#
Raw Permalink Normal View History

2024-05-02 12:50:08 +02:00
using Godot;
using System;
using System.Diagnostics;
2025-02-24 14:30:14 +01:00
public partial class Fragola : RigidBody2D
2024-05-02 12:50:08 +02:00
{
[Signal]
public delegate void HitEventHandler();
2024-05-15 15:48:48 +02:00
private AnimatedSprite2D _animatedSprite;
private bool _isAlive = true;
2024-05-02 12:50:08 +02:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
2024-05-15 15:48:48 +02:00
_animatedSprite = GetNode<AnimatedSprite2D>("./AnimatedSprite2D");
2024-05-02 12:50:08 +02:00
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
private void _on_body_entered(Node body)
{
Debug.WriteLine("Collision");
2024-05-26 11:40:35 +02:00
2024-05-02 12:50:08 +02:00
EmitSignal(SignalName.Hit);
2024-05-26 11:40:35 +02:00
2024-05-02 12:50:08 +02:00
}
private void _on_area_2d_area_entered(Area2D area)
{
// Replace with function body.
Debug.WriteLine("Collision area");
EmitSignal(SignalName.Hit);
2024-05-15 15:48:48 +02:00
2024-05-26 11:40:35 +02:00
if (_isAlive)
2024-05-15 15:48:48 +02:00
{
Explode();
2024-05-26 11:40:35 +02:00
}
2024-05-15 15:48:48 +02:00
}
private void Explode()
{
_isAlive = false;
_animatedSprite.Play("explode");
//Hide();
DisableCollision();
}
private void DisableCollision()
{
// Must be deferred as we can't change physics properties on a physics callback.
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
2024-05-02 12:50:08 +02:00
}
}