add initial project files and configurations, including EventBus, systems, and resources
This commit is contained in:
115
Code/Systems/HazardController.cs
Normal file
115
Code/Systems/HazardController.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Godot;
|
||||
using MaxEffort.Code.Core;
|
||||
using MaxEffort.Code.Data;
|
||||
|
||||
namespace MaxEffort.Code.Systems;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class HazardController : Node2D
|
||||
{
|
||||
[Export] private AnimatedSprite2D _animSprite;
|
||||
[Export] private Area2D _clickArea;
|
||||
[Export] private CollisionShape2D _clickShape; // Reference to resize it
|
||||
[Export] private Label _nameLabel; // Can still use UI nodes inside Node2D
|
||||
|
||||
private HazardDef _data;
|
||||
private float _timeActive = 0f;
|
||||
private bool _isResolved = false;
|
||||
private int _currentHealth = 1;
|
||||
|
||||
public void Initialize(HazardDef data)
|
||||
{
|
||||
_data = data;
|
||||
_currentHealth = data.ClicksToResolve;
|
||||
|
||||
if (_animSprite != null && data.Animations != null)
|
||||
{
|
||||
_animSprite.SpriteFrames = data.Animations;
|
||||
|
||||
var texture = data.Animations.GetFrameTexture(data.IdleAnimName, 0);
|
||||
if (texture != null && _clickShape != null)
|
||||
{
|
||||
var rect = new RectangleShape2D();
|
||||
rect.Size = texture.GetSize();
|
||||
_clickShape.Shape = rect;
|
||||
}
|
||||
|
||||
_animSprite.Play(data.IdleAnimName);
|
||||
}
|
||||
|
||||
if (_nameLabel != null) _nameLabel.Text = data.DisplayName;
|
||||
|
||||
if (_clickArea != null)
|
||||
{
|
||||
_clickArea.InputEvent += OnInputEvent;
|
||||
}
|
||||
|
||||
Scale = Vector2.Zero;
|
||||
var tween = CreateTween();
|
||||
tween.SetTrans(Tween.TransitionType.Back).SetEase(Tween.EaseType.Out);
|
||||
tween.TweenProperty(this, "scale", Vector2.One, 0.4f);
|
||||
|
||||
if (!string.IsNullOrEmpty(data.WalkAnimName))
|
||||
{
|
||||
_animSprite?.Play(data.WalkAnimName);
|
||||
tween.TweenCallback(Callable.From(() => _animSprite?.Play(data.IdleAnimName)));
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (_isResolved) return;
|
||||
_timeActive += (float)delta;
|
||||
|
||||
if (_timeActive >= _data.TimeToFail)
|
||||
{
|
||||
EventBus.PublishLiftCompleted(false);
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (_clickArea != null) _clickArea.InputEvent -= OnInputEvent;
|
||||
}
|
||||
|
||||
private void OnInputEvent(Node viewport, InputEvent @event, long shapeIdx)
|
||||
{
|
||||
if (_isResolved) return;
|
||||
|
||||
if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed && mouseEvent.ButtonIndex == MouseButton.Left)
|
||||
{
|
||||
TakeDamage();
|
||||
}
|
||||
}
|
||||
|
||||
private void TakeDamage()
|
||||
{
|
||||
_currentHealth--;
|
||||
|
||||
var tween = CreateTween();
|
||||
tween.TweenProperty(this, "scale", new Vector2(1.2f, 0.8f), 0.05f);
|
||||
tween.TweenProperty(this, "scale", Vector2.One, 0.05f);
|
||||
|
||||
if (_animSprite != null)
|
||||
{
|
||||
_animSprite.Modulate = Colors.Red;
|
||||
tween.Parallel().TweenProperty(_animSprite, "modulate", Colors.White, 0.1f);
|
||||
}
|
||||
|
||||
if (_currentHealth <= 0)
|
||||
{
|
||||
Resolve();
|
||||
}
|
||||
}
|
||||
|
||||
private void Resolve()
|
||||
{
|
||||
_isResolved = true;
|
||||
EventBus.PublishHazardResolved(_data.Type);
|
||||
|
||||
var tween = CreateTween();
|
||||
tween.TweenProperty(this, "scale", Vector2.Zero, 0.2f);
|
||||
tween.TweenCallback(Callable.From(QueueFree));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user