diff --git a/Cooldown.cs b/Cooldown.cs
index a003741..dbb8649 100644
--- a/Cooldown.cs
+++ b/Cooldown.cs
@@ -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
-
- ///
- /// Determines if the cooldown is currently active (timer higher than zero)
- /// To activate >> or
- ///
- public bool IsActive
- {
- get => _cooldownTimer > 0;
- }
-
- ///
- /// Returns the value of the default duration of the object.
- /// You can change its value at anytime with
- ///
- 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.
+
+ ///
+ /// Used for creating a new cooldown
+ ///
+ /// The default duration of the cooldown
+ public Cooldown(float duration)
+ {
+ _cooldownTimer = 0;
+ _duration = duration;
+ CDM.AddToManager(Update);
+ }
- #endregion
+ ///
+ /// Determines if the cooldown is currently active (timer higher than zero)
+ /// To activate >> or
+ ///
+ public bool IsActive
+ {
+ get => _cooldownTimer > 0f;
+ }
- #region Constructors
+ ///
+ /// Returns the value of the default duration of the object.
+ /// You can change its value at anytime with
+ ///
+ public float Duration
+ {
+ get => _duration;
+ }
- ///
- /// NOT RECOMMENDED - Use the class for creating a cooldown.
- ///
- /// The default duration of the cooldown
- public Cooldown(float _duration)
- {
- _cooldownTimer = 0;
- this._duration = _duration;
- }
+ ///
+ /// Returns the current value from the cooldown's timer
+ ///
+ 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;
+ }
+ }
- ///
- /// DON'T YOU THIS METHOD IF YOU USE THE . (and you should use the )
- /// The most important method. Call it on Update() inside a MonoBehaviour.
- ///
- public void Update()
- {
- // only if the cooldown is active (timer higher than zero)
- if (IsActive)
+ ///
+ /// Do not call manually
+ ///
+ 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);
+ }
}
- }
- ///
- /// Activates the cooldown with the default duration saved in the object.
- /// Call this method with parameters to activate the cooldown with a custom duration.
- ///
- public void Activate() => Activate(_duration);
+ ///
+ /// Activates the cooldown with the default duration saved in the object.
+ /// Call this method with parameters to activate the cooldown with a custom duration.
+ ///
+ public void Activate() => Activate(_duration);
+
+ ///
+ /// Activates the cooldown with a custom duration.
+ /// Call this method without parameters to activate the cooldown with the default duration.
+ ///
+ /// A float represents the duration in seconds you want the cooldown to be active
+ public void Activate(float customDuration)
+ {
+ _cooldownTimer = customDuration;
+ }
- ///
- /// Activates the cooldown with a custom duration.
- /// Call this method without parameters to activate the cooldown with the default duration.
- ///
- /// A float represents the duration in seconds you want the cooldown to be active
- public void Activate(float customDuration)
- {
- _cooldownTimer = customDuration;
- }
+ ///
+ /// Resets the timer (deactivates the cooldown).
+ ///
+ public void Deactivate()
+ {
+ _cooldownTimer = 0;
+ }
- ///
- /// Resets the timer (deactivates the cooldown).
- ///
- public void Deactivate()
- {
- _cooldownTimer = 0;
- }
+ ///
+ /// Changes the deafult cooldown duration saved in the object.
+ /// The deafult duration is set upon initialization and can be changed at anytime.
+ /// You can get the value of the deafult duration from the property
+ ///
+ ///
+ public void ChangeDuration(float newDurationValue)
+ {
+ _duration = newDurationValue;
+ }
- ///
- /// Changes the deafult cooldown duration saved in the object.
- /// The deafult duration is set upon initialization and can be changed at anytime.
- /// You can get the value of the deafult duration from the propertie
- ///
- ///
- public void ChangeDuration(float newDurationValue)
- {
- _duration = newDurationValue;
+ private static CooldownsManager Initialize()
+ {
+ var cdm = Object.FindObjectOfType();
+ if (cdm == null)
+ {
+ var obj = new GameObject("CooldownsManager");
+ return obj.AddComponent();
+ }
+ return cdm;
+ }
}
-
- #endregion
-}
\ No newline at end of file
+}
diff --git a/CooldownsManager.cs b/CooldownsManager.cs
index 0bd1b98..b7631d2 100644
--- a/CooldownsManager.cs
+++ b/CooldownsManager.cs
@@ -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
-
- ///
- /// Returns a new object and subscribes it to the it has been created with.
- /// When you call the , it updates all the cooldowns you created with at once
- ///
- /// The default duration you want the object to have.
- ///
- 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;
- ///
- /// IMPORTANT >> Call this method on Update() inside a MonoBehaviour to Update all the cooldowns
- ///
- 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
-}
\ No newline at end of file
+}
diff --git a/README.md b/README.md
index 1080835..b4b6d0e 100644
--- a/README.md
+++ b/README.md
@@ -1,46 +1,39 @@
![img](https://i.imgur.com/cSOJR5d.png)
+
+
+
+
+
+
+
+
+
+
+
### Content
- [**Setup & Examples**](#setup-and-examples)
- [**Documentations**](#documentations)
- - [CooldownsManager](#cooldownsmanager)
- [Cooldown](#cooldown)
# Setup And Examples
-Let's say you want to add a cooldown to some behaviour in your game.
-First, create and initialize a **CooldownsManager** object. (Do this inside a **MonoBehaviour** class)
-```csharp
-CooldownsManager cooldownsManager = new CooldownsManager();
-```
-Very simple.
-Now the last step, call the method **Update() of your CooldownsManager** inside the **Update()** method
-```csharp
-void Update()
-{
- cooldownsManager.Update();
-}
-```
-You're all set and can add cooldowns.
-Let's start with creating a cooldown.
-Let's say we have an **attack method** in our game for the **player**.
-When the player **presses** a key, we want to **attack**.
-But we also want to **apply a cooldown** when the player **attacks**.
-And also to **prevent** the player from attacking **if** the cooldown **is active**.
-First let's **create** a **Cooldown** for the attack and call it "**attackCooldown**"
-Also, let's create a **float** variable for the **amount of seconds** between attacks.
-Let's say we want 3.25 seconds cooldown duration.
+Let's say we have an **attack method** in our game for the **player**. When the player **presses** a key, we want to **attack**. But we also want to **apply a cooldown** when the player **attacks** to **prevent** the player from attacking too quickly.
+
+First let's **declare** a **Cooldown** for the attack and call it "**attackCooldown**"
+Also, let's declare a **float** variable for the **amount of seconds** between attacks.
+Let's say we want the duration of the cooldown to be 3.25 seconds.
```csharp
float attackCoodldownDuration = 3.25f;
Cooldown attackCooldown;
```
-Now to **initialize** it. All you got to to is to **set** its value to the method **NewCooldown()**
+Now to **initialize** it. All you got to to is to **call** its constructor.
When you **initialize** a Cooldown object, you set its **default duration** value.
-And it's the float variable we initialized before.
-Initialize all your cooldowns inside **Start()** or **Awake()**
+And it's the float variable we declared before.
+Make sure you initialize your cooldowns inside **Start()** or **Awake()**
```csharp
void Awake()
{
- attackCooldown = cooldownsManager.NewCooldown(attackCooldownDuration);
+ attackCooldown = new Cooldown(attackCooldownDuration);
}
```
Done. The cooldown is ready.
@@ -55,9 +48,9 @@ void Attack()
}
```
Now, let's **prevent** the player from attacking **if** the cooldown **is active**.
-We can do this by checking the value **IsActive**.
+We can do this by checking the value **IsActive** in the cooldown.
```csharp
-if (player is pressing the attack button)
+if (//player is pressing the attack button)
{
if (attackCooldown.IsActive == false)
{
@@ -66,8 +59,8 @@ if (player is pressing the attack button)
}
```
A **Cooldown** object contains **two** properties. A **timer** and a default **duration**.
-The Cooldown is considered "**active**" when its **timer** is equal to **zero**.
-The timer is **decreasing** when the **Update()** method of the **CooldownsManager** is called.
+The Cooldown is considered "**active**" when its **timer** equal to **zero**.
+The timer is always **decreasing** until it reaches zero.
Let's say we want to add a special, shorter attack, and apply a **short cooldown** for it with a **different duration**.
We can do it with the **Activate()** method by **inputting** a **custom duration** to it.
```csharp
@@ -83,31 +76,19 @@ You can also **change** the **default duration** at any time with the method **C
```csharp
attackCooldown.ChangeDuration(8f);
```
-You can also **manually** disable the cooldown and reset its timer to zero.
+You can also **manually** deactivate the cooldown and reset its timer to zero.
Use the method **Deactivate()**
```
attackCooldown.Deactivate();
```
# Documentations
-### CooldownsManager
-A class for handling and managing multiple cooldowns efficiently.
-![img](https://i.imgur.com/s6orwHe.jpg)
-
-- Methods
- - **Update()**
- Call this method inside Update() in a MonoBehaviour inherited class.
- Updates the value of all the cooldowns that are subscribed to the specified manager.
- Decreases all the cooldown timers by the time, at once.
- A cooldown is subscribed automatically to the CooldownsManager it has been created with.
-
- - **NewCooldown(float duration)**
- Returns a new Cooldown object. Better than creating a Cooldown with a constructor.
- Subscribes the cooldown created to the Update() method of the CooldownsManager it has been created with.
-
### Cooldown
-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.
-![img](https://i.imgur.com/kHITH1f.jpg)
+A class for handling a cooldown in Unity using a timer.
+
+- Constructors
+ - **Cooldown(float duration)**
+ Used for creating a new cooldown
+ Input the default duration of the cooldown
- Properties
- **IsActive**
@@ -118,6 +99,9 @@ It is recommended to create a CooldownManager inside a class you want to use coo
Returns the default duration value of the Cooldown object.
The default duration is set upon initialization.
It used when you call the Activate() method without inputting parameters.
+
+ - **Timer**
+ Returns the current value from the cooldown's timer
- Methods
- **Activate()**