Skip to content

Commit

Permalink
Merge pull request #3 from JosepeDev/0-3
Browse files Browse the repository at this point in the history
Update v0.3
  • Loading branch information
jozzzzep authored Jun 8, 2021
2 parents 73279b9 + c5668b3 commit b8b5d23
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 198 deletions.
209 changes: 114 additions & 95 deletions Cooldown.cs
Original file line number Diff line number Diff line change
@@ -1,112 +1,131 @@
using UnityEngine;

public class Cooldown
namespace CooldownAPI
{
/// A class for handling a single cooldown in Unity using a timer
/// It is recommended to create a CooldownManager inside a class you want to use cooldowns in.
///
/// WIKI & INFO: https://github.com/JosepeDev/CooldownAPI
///
/// Properies:
/// - IsActive - Determines if the cooldown is currently active (timer higher than zero)
/// - Duration - Returns the value of the default duration of the Cooldown object.
///
/// Methods:
/// - Activate() - Activates the cooldown with the default duration saved in the object.
/// - Activate(float duration) - Activates the cooldown with a custom duration.
/// - Deactivate() - Resets the timer (deactivates the cooldown).
/// - ChangeDuration() - Changes the deafult cooldown duration saved in the object.

#region Variables and Properties

/// <summary>
/// Determines if the cooldown is currently active (timer higher than zero)
/// <para> To activate >> <see cref="Activate()"/> or <see cref="Activate(float)"/></para>
/// </summary>
public bool IsActive
{
get => _cooldownTimer > 0;
}

/// <summary>
/// Returns the value of the default duration of the <see cref="Cooldown"/> object.
/// <para> You can change its value at anytime with <see cref="ChangeDuration(float)"/></para>
/// </summary>
public float Duration
public class Cooldown
{
get => _duration;
}

// variables
private float _cooldownTimer; // The current value of the cooldown (higher than 0 = active)
private float _duration; // Set this value on the declaration of the object (it is the default duration value)
/// A class for handling a cooldown in Unity using a timer
/// Tutorial and Examples: https://github.com/JosepeDev/CooldownAPI
///
/// Properies:
/// - IsActive - Determines if the cooldown is currently active (timer higher than zero)
/// - Duration - Returns the value of the default duration of the Cooldown object.
/// - Timer - Returns the current value from the cooldown's timer
///
/// Methods:
/// - Activate() - Activates the cooldown with the default duration saved in the object.
/// - Activate(float duration) - Activates the cooldown with a custom duration.
/// - Deactivate() - Resets the timer (deactivates the cooldown).
/// - ChangeDuration() - Changes the deafult cooldown duration saved in the object.

/// <summary>
/// Used for creating a new cooldown
/// </summary>
/// <param name="duration">The default duration of the cooldown</param>
public Cooldown(float duration)
{
_cooldownTimer = 0;
_duration = duration;
CDM.AddToManager(Update);
}

#endregion
/// <summary>
/// Determines if the cooldown is currently active (timer higher than zero)
/// <para> To activate >> <see cref="Activate()"/> or <see cref="Activate(float)"/></para>
/// </summary>
public bool IsActive
{
get => _cooldownTimer > 0f;
}

#region Constructors
/// <summary>
/// Returns the value of the default duration of the <see cref="Cooldown"/> object.
/// <para> You can change its value at anytime with <see cref="ChangeDuration(float)"/></para>
/// </summary>
public float Duration
{
get => _duration;
}

/// <summary>
/// NOT RECOMMENDED - Use the <see cref="CooldownsManager"/> class for creating a cooldown. <see cref="CooldownsManager.NewCooldown(float)"/>
/// </summary>
/// <param name="_duration">The default duration of the cooldown</param>
public Cooldown(float _duration)
{
_cooldownTimer = 0;
this._duration = _duration;
}
/// <summary>
/// Returns the current value from the cooldown's timer
/// </summary>
public float Timer
{
get => _cooldownTimer;
}

#endregion
private float _cooldownTimer;
private float _duration; // the default duration value

#region Methods
static CooldownsManager _cdm;
static CooldownsManager CDM
{
get
{
if (_cdm == null)
{
_cdm = Initialize();
}
return _cdm;
}
}

/// <summary>
/// <para> DON'T YOU THIS METHOD IF YOU USE THE <see cref="CooldownsManager"/>. (and you should use the <see cref="CooldownsManager"/>) </para>
/// The most important method. Call it on Update() inside a MonoBehaviour.
/// </summary>
public void Update()
{
// only if the cooldown is active (timer higher than zero)
if (IsActive)
/// <summary>
/// Do not call manually
/// </summary>
private void Update()
{
// decreases the cooldown by the time
Mathf.Clamp(_cooldownTimer -= Time.deltaTime, 0, _duration);
if (IsActive)
{
_cooldownTimer = Mathf.Clamp(_cooldownTimer - Time.deltaTime, 0f, _duration);
}
}
}

/// <summary>
/// <para> Activates the cooldown with the default duration saved in the object. </para>
/// <para> Call this method with parameters to activate the cooldown with a custom duration. <see cref="Activate(float)"/></para>
/// </summary>
public void Activate() => Activate(_duration);
/// <summary>
/// <para> Activates the cooldown with the default duration saved in the object. </para>
/// <para> Call this method with parameters to activate the cooldown with a custom duration. <see cref="Activate(float)"/></para>
/// </summary>
public void Activate() => Activate(_duration);

/// <summary>
/// <para> Activates the cooldown with a custom duration. </para>
/// <para> Call this method without parameters to activate the cooldown with the default duration. <see cref="Activate()"/></para>
/// </summary>
/// <param name="customDuration">A float represents the duration in seconds you want the cooldown to be active</param>
public void Activate(float customDuration)
{
_cooldownTimer = customDuration;
}

/// <summary>
/// <para> Activates the cooldown with a custom duration. </para>
/// <para> Call this method without parameters to activate the cooldown with the default duration. <see cref="Activate()"/></para>
/// </summary>
/// <param name="customDuration">A float represents the duration in seconds you want the cooldown to be active</param>
public void Activate(float customDuration)
{
_cooldownTimer = customDuration;
}
/// <summary>
/// Resets the timer (deactivates the cooldown).
/// </summary>
public void Deactivate()
{
_cooldownTimer = 0;
}

/// <summary>
/// Resets the timer (deactivates the cooldown).
/// </summary>
public void Deactivate()
{
_cooldownTimer = 0;
}
/// <summary>
/// <para> Changes the deafult cooldown duration saved in the object. </para>
/// <para> The deafult duration is set upon initialization and can be changed at anytime. </para>
/// <para> You can get the value of the deafult duration from the property <see cref="Duration"/></para>
/// </summary>
/// <param name="newDurationValue"></param>
public void ChangeDuration(float newDurationValue)
{
_duration = newDurationValue;
}

/// <summary>
/// <para> Changes the deafult cooldown duration saved in the object. </para>
/// <para> The deafult duration is set upon initialization and can be changed at anytime. </para>
/// <para> You can get the value of the deafult duration from the propertie <see cref="Duration"/></para>
/// </summary>
/// <param name="newDurationValue"></param>
public void ChangeDuration(float newDurationValue)
{
_duration = newDurationValue;
private static CooldownsManager Initialize()
{
var cdm = Object.FindObjectOfType<CooldownsManager>();
if (cdm == null)
{
var obj = new GameObject("CooldownsManager");
return obj.AddComponent<CooldownsManager>();
}
return cdm;
}
}

#endregion
}
}
73 changes: 20 additions & 53 deletions CooldownsManager.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,31 @@
using System;
using UnityEngine;

public class CooldownsManager
namespace CooldownAPI
{
/// A class for handling and managing multiple cooldowns efficiently.
///
/// WIKI & INFO: https://github.com/JosepeDev/CooldownAPI
///
/// Methods:
/// - Update() - IMPORTANT Call this method on Update() inside a MonoBehaviour to Update all the cooldowns
/// - NewCooldown() - Creates and returns a new cooldown, just input the cooldown's duration.

#region Variables

// when called it'll decrease every cooldown you created with this object
private Action cooldownsUpdateMethods;

#endregion

#region Methods

/// <summary>
/// Returns a new <see cref="Cooldown"/> object and subscribes it to the <see cref="CooldownsManager"/> it has been created with.
/// <para> When you call the <see cref="Update()"/>, it updates all the cooldowns you created with <see cref="NewCooldown(float)"/> at once </para>
/// </summary>
/// <param name="duration"> The default duration you want the <see cref="Cooldown"/> object to have.</param>
/// <returns></returns>
public Cooldown NewCooldown(float duration)
internal class CooldownsManager : MonoBehaviour
{
// create a new cooldown
Cooldown returnThisCooldown = new Cooldown(duration);

// add to Action delegate
AssignCooldownToDelegate(returnThisCooldown);
/// The class that manages and calls all the cooldowns
/// No need to use or call it manually
/// Tutorial and Examples: https://github.com/JosepeDev/CooldownAPI

// return the new cooldown
return returnThisCooldown;
}
private Action cooldownsUpdates;

/// <summary>
/// IMPORTANT >> Call this method on Update() inside a MonoBehaviour to Update all the cooldowns
/// </summary>
public void Update()
{
cooldownsUpdateMethods();
}

private void AssignCooldownToDelegate(Cooldown cooldown)
{
// delegate has no subscribers
if (cooldownsUpdateMethods == null)
private void Update()
{
// assign the first subscriber
cooldownsUpdateMethods = cooldown.Update;
cooldownsUpdates();
}
else

internal void AddToManager(Action call)
{
// add an additional subscriber
cooldownsUpdateMethods += cooldown.Update;
if (cooldownsUpdates == null)
{
cooldownsUpdates = call;
}
else
{
cooldownsUpdates += call;
}
}
}

#endregion
}
}
Loading

0 comments on commit b8b5d23

Please sign in to comment.