Add new components: EnemyDeathComponent, EnemyWaveTriggerComponent, and ExitDoorComponent
This commit is contained in:
42
scripts/components/EnemyDeathComponent.cs
Normal file
42
scripts/components/EnemyDeathComponent.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Mr.BrickAdventures.scripts.components;
|
||||||
|
|
||||||
|
public partial class EnemyDeathComponent : Node
|
||||||
|
{
|
||||||
|
[Export] public float TweenDuration { get; set; } = 0.5f;
|
||||||
|
[Export] public CollisionShape2D CollisionShape { get; set; }
|
||||||
|
[Export] public HealthComponent Health { get; set; }
|
||||||
|
|
||||||
|
public override void _Ready()
|
||||||
|
{
|
||||||
|
if (CollisionShape == null)
|
||||||
|
{
|
||||||
|
GD.PushError("EnemyDeathComponent: CollisionShape is not set.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Health == null)
|
||||||
|
{
|
||||||
|
GD.PushError("EnemyDeathComponent: Health is not set.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Health.Death += OnDeath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDeath()
|
||||||
|
{
|
||||||
|
CallDeferred(nameof(Die));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Die()
|
||||||
|
{
|
||||||
|
CollisionShape.SetDisabled(true);
|
||||||
|
var tween = CreateTween();
|
||||||
|
tween.TweenProperty(Owner, "scale", Vector2.Zero, TweenDuration);
|
||||||
|
await ToSignal(tween, Tween.SignalName.Finished);
|
||||||
|
Owner.QueueFree();
|
||||||
|
}
|
||||||
|
}
|
51
scripts/components/EnemyWaveTriggerComponent.cs
Normal file
51
scripts/components/EnemyWaveTriggerComponent.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Mr.BrickAdventures.scripts.components;
|
||||||
|
|
||||||
|
public partial class EnemyWaveTriggerComponent : Node
|
||||||
|
{
|
||||||
|
[Export] public Area2D Area2D { get; set; }
|
||||||
|
[Export] public PathFollow2D PathFollowNode { get; set; }
|
||||||
|
[Export] public float Speed { get; set; } = 100f;
|
||||||
|
[Export] public bool Loop { get; set; } = false;
|
||||||
|
[Export] public bool ActivateOnEnter { get; set; } = true;
|
||||||
|
|
||||||
|
private bool _isActive = false;
|
||||||
|
|
||||||
|
public override void _Ready()
|
||||||
|
{
|
||||||
|
Area2D.BodyEntered += OnBodyEntered;
|
||||||
|
|
||||||
|
if (PathFollowNode == null) return;
|
||||||
|
|
||||||
|
PathFollowNode.SetProgress(0f);
|
||||||
|
PathFollowNode.SetProcess(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void _Process(double delta)
|
||||||
|
{
|
||||||
|
if (!_isActive || PathFollowNode == null) return;
|
||||||
|
|
||||||
|
var progress = PathFollowNode.Progress;
|
||||||
|
progress += (float)(delta * Speed);
|
||||||
|
PathFollowNode.SetProgress(progress);
|
||||||
|
|
||||||
|
if (!(PathFollowNode.ProgressRatio >= 1f) || Loop) return;
|
||||||
|
|
||||||
|
_isActive = false;
|
||||||
|
PathFollowNode.SetProcess(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnBodyEntered(Node2D body)
|
||||||
|
{
|
||||||
|
if (ActivateOnEnter) StartWave();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartWave()
|
||||||
|
{
|
||||||
|
if (PathFollowNode == null) return;
|
||||||
|
|
||||||
|
PathFollowNode.SetProcess(true);
|
||||||
|
_isActive = true;
|
||||||
|
}
|
||||||
|
}
|
51
scripts/components/ExitDoorComponent.cs
Normal file
51
scripts/components/ExitDoorComponent.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using Godot;
|
||||||
|
using Mr.BrickAdventures.Autoloads;
|
||||||
|
|
||||||
|
namespace Mr.BrickAdventures.scripts.components;
|
||||||
|
|
||||||
|
public partial class ExitDoorComponent : Node
|
||||||
|
{
|
||||||
|
[Export] public bool Locked { get; set; } = true;
|
||||||
|
[Export] public Area2D ExitArea { get; set; }
|
||||||
|
[Export] public Sprite2D DoorSprite { get; set; }
|
||||||
|
[Export] public AudioStreamPlayer2D OpenDoorSfx { get; set; }
|
||||||
|
[Export] public int OpenedDoorFrame { get; set; } = 0;
|
||||||
|
|
||||||
|
[Signal] public delegate void ExitTriggeredEventHandler();
|
||||||
|
|
||||||
|
private GameManager _gameManager;
|
||||||
|
|
||||||
|
public override void _Ready()
|
||||||
|
{
|
||||||
|
if (ExitArea == null)
|
||||||
|
{
|
||||||
|
GD.PushError("ExitDoorComponent: ExitArea is not set.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ExitArea.BodyEntered += OnExitAreaBodyEntered;
|
||||||
|
|
||||||
|
_gameManager = GetNode<GameManager>("/root/gameManager");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnExitAreaBodyEntered(Node2D body)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Unlock()
|
||||||
|
{
|
||||||
|
Locked = false;
|
||||||
|
if (DoorSprite != null)
|
||||||
|
{
|
||||||
|
DoorSprite.Frame = OpenedDoorFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenDoorSfx?.Play();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GoToNextLevel()
|
||||||
|
{
|
||||||
|
_gameManager.OnLevelComplete();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user