cirnogodot/Scripts/Components/FSM/Enemy/TurretAnimationModule.cs

113 lines
3.6 KiB
C#
Raw Normal View History

2025-05-24 22:45:17 +02:00
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Enums;
using Godot;
namespace Cirno.Scripts.Components.FSM.Enemy;
public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBody2D>
{
private IStateMachine<EnemyState, CharacterBody2D> _machine;
[Export] public PlayerAnimationProvider AnimationProvider { get; set; }
2025-05-26 11:13:22 +02:00
2025-05-24 22:45:17 +02:00
[Export] public EnemyStorageModule StorageModule { get; set; }
[Export] public ActorResourceProvider HealthProvider { get; set; }
2025-05-26 11:13:22 +02:00
[Export] public Vector2 TurretFacingDirection { get; set; } = Vector2.Down;
2025-05-24 22:45:17 +02:00
[Export] public float SweepAngle = 90f; // In degrees
2025-05-26 11:13:22 +02:00
[Export] public float SweepSpeed = 1f; // Speed of sweeping
[Export] public bool Debug = false; // Enable debug lines
2025-05-24 22:45:17 +02:00
//[Export] public NodePath SpritePath;
2025-05-26 11:13:22 +02:00
2025-05-24 22:45:17 +02:00
private float _currentAngle;
private float _sweepDirection = 1f;
private float _raycastLength;
2025-05-26 11:13:22 +02:00
2025-05-24 22:45:17 +02:00
public override void EnterState(EnemyState state)
{
2025-05-26 11:13:22 +02:00
//AnimationProvider.SetAnimation(StorageModule.AimingDirection);
//AnimationProvider.SetAnimation(Vector2.Zero);
2025-05-24 22:45:17 +02:00
if (HealthProvider is not null)
{
HealthProvider.ResourceDecreased += HealthProviderOnResourceDecreased;
}
}
private void HealthProviderOnResourceDecreased(float oldValue, float newValue, float maxValue)
{
AnimationProvider?.Blink();
}
public override void ExitState(EnemyState state)
{
2025-05-26 11:13:22 +02:00
//AnimationProvider.SetAnimation(Vector2.Zero);
2025-05-24 22:45:17 +02:00
if (HealthProvider is not null)
{
HealthProvider.ResourceDecreased -= HealthProviderOnResourceDecreased;
}
}
public override void Init(IStateMachine<EnemyState, CharacterBody2D> machine)
{
_machine = machine;
}
public override void Process(double delta)
{
2025-05-26 11:13:22 +02:00
if (StorageModule.AimingDirection == Vector2.Zero || TurretFacingDirection == Vector2.Zero)
return;
2025-05-24 22:45:17 +02:00
2025-05-26 11:13:22 +02:00
// Calculate the angle between the turret's facing direction and the aim direction
float angleToTarget = TurretFacingDirection.AngleTo(StorageModule.AimingDirection); // radians
// Clamp the angle to within the sweep cone
float halfSweepRad = Mathf.DegToRad(SweepAngle / 2f);
float clampedAngle = Mathf.Clamp(angleToTarget, -halfSweepRad, halfSweepRad);
// Convert to degrees before passing to SweepSprite
float clampedAngleDeg = Mathf.RadToDeg(clampedAngle);
// Update the sprite based on this clamped angle
AnimationProvider.SweepSprite(clampedAngleDeg, SweepAngle);
//AnimationProvider.SetAnimation(StorageModule.AimingDirection);
2025-05-24 22:45:17 +02:00
}
public override void PhysicsProcess(double delta)
{
}
private void Sweep(float delta)
{
_currentAngle += _sweepDirection * SweepSpeed * delta;
// Clamp angle within the sweep range
float halfAngle = SweepAngle / 2f;
if (_currentAngle > halfAngle || _currentAngle < -halfAngle)
{
_sweepDirection *= -1f;
_currentAngle = Mathf.Clamp(_currentAngle, -halfAngle, halfAngle);
}
}
2025-05-26 11:13:22 +02:00
2025-06-10 16:33:43 +02:00
// private void DrawDebugLine(Vector2 endPoint)
// {
// // Request the node to redraw
// QueueRedraw();
// _debugLineEndPoint = endPoint;
// }
2025-05-24 22:45:17 +02:00
private void UpdateSpriteDirection()
{
AnimationProvider.SweepSprite(_currentAngle, SweepAngle);
}
2025-05-26 11:13:22 +02:00
2025-05-24 22:45:17 +02:00
private Vector2 _debugLineEndPoint;
2025-06-10 16:33:43 +02:00
// public override void _Draw()
// {
// if (Debug)
// {
// DrawLine(Vector2.Zero, ToLocal(_debugLineEndPoint), Colors.Red, 2);
// }
// }
2025-05-24 22:45:17 +02:00
}