rokojori_action_library/Runtime/Actions/Conditional/CoolDown.cs

89 lines
1.5 KiB
C#
Raw Normal View History

2025-07-22 14:08:22 +00:00
using Godot;
using System.Collections.Generic;
namespace Rokojori
{
[Tool]
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/ConditionalAction.svg")]
public partial class CoolDown : Action
{
[Export]
public Action action;
2026-02-26 14:06:27 +00:00
[Export]
public Action onCoolingDownAction;
2025-07-22 14:08:22 +00:00
[Export]
public Duration coolDownDuration;
2026-02-26 14:06:27 +00:00
[Export]
public bool retriggerCoolDown = false;
2025-07-22 14:08:22 +00:00
bool _isCoolingDown = false;
2025-10-24 11:38:51 +00:00
int _coolDownID = -1;
2026-02-26 14:06:27 +00:00
bool _canRelease = false;
public bool CanRelease()
{
return _canRelease;
}
2025-11-26 14:23:59 +00:00
public void RegisterCoolDown()
{
_isCoolingDown = coolDownDuration != null;
var te = TimeLineManager.ScheduleEventIn(
coolDownDuration.timeLine,
coolDownDuration.GetDurationInSeconds(),
ev =>
{
if ( _coolDownID != ev.id )
{
return;
}
_isCoolingDown = false;
}
);
_coolDownID = te.id;
}
2025-10-24 11:38:51 +00:00
public void ResetCoolDown()
{
if ( _coolDownID == -1 )
{
return;
}
_isCoolingDown = false;
_coolDownID = -1;
}
2025-07-22 14:08:22 +00:00
protected override void _OnTrigger()
{
if ( _isCoolingDown )
{
2026-02-26 14:06:27 +00:00
onCoolingDownAction?.Trigger();
_canRelease = false;
if ( retriggerCoolDown )
{
RegisterCoolDown();
}
2025-07-22 14:08:22 +00:00
return;
}
2026-02-26 14:06:27 +00:00
_canRelease = true;
2025-11-26 14:23:59 +00:00
2025-07-22 14:08:22 +00:00
Trigger( action );
2025-11-26 14:23:59 +00:00
RegisterCoolDown();
2025-07-22 14:08:22 +00:00
2026-02-26 14:06:27 +00:00
2025-07-22 14:08:22 +00:00
}
}
}