Add FallOnDetectionComponent and CleanupOnCollisionComponent; implement falling detection and cleanup on collision
This commit is contained in:
46
scripts/components/CleanupOnCollisionComponent.cs
Normal file
46
scripts/components/CleanupOnCollisionComponent.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class CleanupOnCollisionComponent : Node
|
||||
{
|
||||
[Export(PropertyHint.Range, "0, 5, 0.1")] public float CleanupDelay { get; set; } = 0.5f;
|
||||
|
||||
private RigidBody2D _body;
|
||||
private CleanupComponent _cleanupComponent;
|
||||
private bool _isCleaningUp = false;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_body = Owner as RigidBody2D;
|
||||
if (_body == null)
|
||||
{
|
||||
GD.PrintErr("CleanupOnCollisionComponent must be attached to a RigidBody2D.");
|
||||
SetProcess(false);
|
||||
return;
|
||||
}
|
||||
|
||||
_cleanupComponent = Owner.GetNode<CleanupComponent>("CleanupComponent");
|
||||
if (_cleanupComponent == null)
|
||||
{
|
||||
GD.PrintErr("CleanupOnCollisionComponent requires a CleanupComponent on the same node.");
|
||||
SetProcess(false);
|
||||
return;
|
||||
}
|
||||
|
||||
_body.BodyEntered += (_) => OnBodyEntered();
|
||||
}
|
||||
|
||||
private async void OnBodyEntered()
|
||||
{
|
||||
if (_isCleaningUp) return;
|
||||
|
||||
_isCleaningUp = true;
|
||||
|
||||
await ToSignal(GetTree().CreateTimer(CleanupDelay), Timer.SignalName.Timeout);
|
||||
|
||||
_cleanupComponent?.CleanUp();
|
||||
}
|
||||
|
||||
}
|
||||
1
scripts/components/CleanupOnCollisionComponent.cs.uid
Normal file
1
scripts/components/CleanupOnCollisionComponent.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://chusyr5vwgwf0
|
||||
47
scripts/components/FallOnDetectionComponent.cs
Normal file
47
scripts/components/FallOnDetectionComponent.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FallOnDetectionComponent : Node2D
|
||||
{
|
||||
[Export] public Area2D DetectionArea { get; set; }
|
||||
[Export] public RigidBody2D TargetBody { get; set; }
|
||||
[Export] public float FallDelay { get; set; } = 0.2f;
|
||||
|
||||
private bool _hasTriggered = false;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (DetectionArea == null)
|
||||
{
|
||||
GD.PrintErr("FallOnDetectionComponent: DetectionArea is not set.");
|
||||
SetProcess(false);
|
||||
return;
|
||||
}
|
||||
if (TargetBody == null)
|
||||
{
|
||||
GD.PrintErr("FallOnDetectionComponent: TargetBody is not set.");
|
||||
SetProcess(false);
|
||||
return;
|
||||
}
|
||||
|
||||
DetectionArea.BodyEntered += OnBodyEntered;
|
||||
}
|
||||
|
||||
private async void OnBodyEntered(Node2D body)
|
||||
{
|
||||
if (_hasTriggered) return;
|
||||
_hasTriggered = true;
|
||||
|
||||
if (FallDelay > 0)
|
||||
{
|
||||
await ToSignal(GetTree().CreateTimer(FallDelay), Timer.SignalName.Timeout);
|
||||
}
|
||||
|
||||
if (IsInstanceValid(TargetBody))
|
||||
{
|
||||
TargetBody.GravityScale = 1.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
scripts/components/FallOnDetectionComponent.cs.uid
Normal file
1
scripts/components/FallOnDetectionComponent.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://co05ugnvx0v3e
|
||||
Reference in New Issue
Block a user