Working airplane ability revamp
This commit is contained in:
@@ -21,6 +21,10 @@ namespace Minigames.Airplane.Abilities
|
||||
protected readonly bool canReuse;
|
||||
protected bool showDebugLogs;
|
||||
|
||||
// Active duration configuration
|
||||
protected readonly float maxActiveDuration;
|
||||
protected readonly bool hasActiveDuration;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
@@ -28,13 +32,15 @@ namespace Minigames.Airplane.Abilities
|
||||
/// <summary>
|
||||
/// Base constructor for abilities. Called by subclasses.
|
||||
/// </summary>
|
||||
protected BaseAirplaneAbility(string name, Sprite icon, float cooldown, bool reusable = true)
|
||||
protected BaseAirplaneAbility(string name, Sprite icon, float cooldown, bool reusable = true, float activeDuration = 0f)
|
||||
{
|
||||
abilityName = name;
|
||||
abilityIcon = icon;
|
||||
cooldownDuration = cooldown;
|
||||
canReuse = reusable;
|
||||
showDebugLogs = false;
|
||||
maxActiveDuration = activeDuration;
|
||||
hasActiveDuration = activeDuration > 0f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -45,6 +51,8 @@ namespace Minigames.Airplane.Abilities
|
||||
public Sprite AbilityIcon => abilityIcon;
|
||||
public float CooldownDuration => cooldownDuration;
|
||||
public bool CanReuse => canReuse;
|
||||
public bool HasActiveDuration => hasActiveDuration;
|
||||
public float MaxActiveDuration => maxActiveDuration;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -54,12 +62,22 @@ namespace Minigames.Airplane.Abilities
|
||||
protected bool isActive;
|
||||
protected bool isOnCooldown;
|
||||
protected float cooldownTimer;
|
||||
protected float initialCooldownDuration; // Track initial cooldown for fill calculation
|
||||
|
||||
// Active duration tracking
|
||||
protected float activeDurationTimer;
|
||||
protected float activeDurationUsed; // Track how long ability was active
|
||||
|
||||
public bool IsActive => isActive;
|
||||
public bool IsOnCooldown => isOnCooldown;
|
||||
public float CooldownRemaining => cooldownTimer;
|
||||
public float InitialCooldownDuration => initialCooldownDuration;
|
||||
public bool CanActivate => !isOnCooldown && !isActive && currentAirplane != null && currentAirplane.IsFlying;
|
||||
|
||||
// Active duration state
|
||||
public float ActiveDurationRemaining => activeDurationTimer;
|
||||
public float ActiveProgress => hasActiveDuration && maxActiveDuration > 0f ? activeDurationTimer / maxActiveDuration : 0f;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
@@ -67,6 +85,7 @@ namespace Minigames.Airplane.Abilities
|
||||
public event Action<BaseAirplaneAbility> OnAbilityActivated;
|
||||
public event Action<BaseAirplaneAbility> OnAbilityDeactivated;
|
||||
public event Action<float, float> OnCooldownChanged; // (remaining, total)
|
||||
public event Action<float, float> OnActiveDurationChanged; // (remaining, total)
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -82,6 +101,9 @@ namespace Minigames.Airplane.Abilities
|
||||
isActive = false;
|
||||
isOnCooldown = false;
|
||||
cooldownTimer = 0f;
|
||||
initialCooldownDuration = 0f;
|
||||
activeDurationTimer = 0f;
|
||||
activeDurationUsed = 0f;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
@@ -109,10 +131,37 @@ namespace Minigames.Airplane.Abilities
|
||||
}
|
||||
}
|
||||
|
||||
OnCooldownChanged?.Invoke(cooldownTimer, cooldownDuration);
|
||||
OnCooldownChanged?.Invoke(cooldownTimer, initialCooldownDuration);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update active duration timer. Called every frame by ability manager when ability is active.
|
||||
/// Auto-deactivates when duration expires.
|
||||
/// </summary>
|
||||
public virtual void UpdateActiveDuration(float deltaTime)
|
||||
{
|
||||
if (!isActive || !hasActiveDuration) return;
|
||||
|
||||
activeDurationTimer -= deltaTime;
|
||||
activeDurationUsed += deltaTime;
|
||||
|
||||
if (activeDurationTimer <= 0f)
|
||||
{
|
||||
activeDurationTimer = 0f;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Active duration expired, auto-deactivating");
|
||||
}
|
||||
|
||||
// Auto-deactivate when duration expires
|
||||
Deactivate();
|
||||
}
|
||||
|
||||
OnActiveDurationChanged?.Invoke(activeDurationTimer, maxActiveDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleanup when airplane is destroyed or flight ends.
|
||||
/// </summary>
|
||||
@@ -127,6 +176,9 @@ namespace Minigames.Airplane.Abilities
|
||||
isActive = false;
|
||||
isOnCooldown = false;
|
||||
cooldownTimer = 0f;
|
||||
initialCooldownDuration = 0f;
|
||||
activeDurationTimer = 0f;
|
||||
activeDurationUsed = 0f;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
@@ -155,12 +207,26 @@ namespace Minigames.Airplane.Abilities
|
||||
isActive = false;
|
||||
OnAbilityDeactivated?.Invoke(this);
|
||||
|
||||
// Calculate and start dynamic cooldown
|
||||
float dynamicCooldown = CalculateDynamicCooldown();
|
||||
StartCooldown(dynamicCooldown);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Deactivated");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate cooldown duration based on ability usage.
|
||||
/// Override in subclasses for custom dynamic cooldown logic.
|
||||
/// Default: returns base cooldown duration.
|
||||
/// </summary>
|
||||
protected virtual float CalculateDynamicCooldown()
|
||||
{
|
||||
return cooldownDuration;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Helpers
|
||||
@@ -180,6 +246,14 @@ namespace Minigames.Airplane.Abilities
|
||||
}
|
||||
|
||||
isActive = true;
|
||||
|
||||
// Reset active duration timer if ability has active duration
|
||||
if (hasActiveDuration)
|
||||
{
|
||||
activeDurationTimer = maxActiveDuration;
|
||||
activeDurationUsed = 0f;
|
||||
}
|
||||
|
||||
OnAbilityActivated?.Invoke(this);
|
||||
|
||||
if (showDebugLogs)
|
||||
@@ -191,15 +265,16 @@ namespace Minigames.Airplane.Abilities
|
||||
/// <summary>
|
||||
/// Start cooldown timer (called by subclasses after execution).
|
||||
/// </summary>
|
||||
protected virtual void StartCooldown()
|
||||
protected virtual void StartCooldown(float? customCooldown = null)
|
||||
{
|
||||
isOnCooldown = true;
|
||||
cooldownTimer = cooldownDuration;
|
||||
OnCooldownChanged?.Invoke(cooldownTimer, cooldownDuration);
|
||||
cooldownTimer = customCooldown ?? cooldownDuration;
|
||||
initialCooldownDuration = cooldownTimer; // Store the initial cooldown value
|
||||
OnCooldownChanged?.Invoke(cooldownTimer, initialCooldownDuration);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Cooldown started: {cooldownDuration}s");
|
||||
Logging.Debug($"[{abilityName}] Cooldown started: {cooldownTimer}s");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user