Skip to content

Commit

Permalink
Final 0.3 commit
Browse files Browse the repository at this point in the history
- Updated readme
- Reorganized code
  • Loading branch information
jozzzzep committed Jun 8, 2021
1 parent 13ac584 commit c5668b3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 68 deletions.
31 changes: 16 additions & 15 deletions Cooldown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ public class Cooldown
/// 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
/// - 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);
}

/// <summary>
/// Determines if the cooldown is currently active (timer higher than zero)
/// <para> To activate >> <see cref="Activate()"/> or <see cref="Activate(float)"/></para>
Expand Down Expand Up @@ -61,22 +72,12 @@ static CooldownsManager CDM
}

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

private void Update()
{
// only if the cooldown is active (timer higher than zero)
if (IsActive)
{
// decreases the cooldown by the time
_cooldownTimer = Mathf.Clamp(_cooldownTimer - Time.deltaTime, 0f, _duration);
}
}
Expand Down
3 changes: 0 additions & 3 deletions CooldownsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ private void Update()

internal void AddToManager(Action call)
{
// delegate has no subscribers
if (cooldownsUpdates == null)
{
// assign the first subscriber
cooldownsUpdates = call;
}
else
{
// add an additional subscriber
cooldownsUpdates += call;
}
}
Expand Down
84 changes: 34 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
![img](https://i.imgur.com/cSOJR5d.png)
<p align="center">
<img src="https://img.shields.io/codefactor/grade/github/JosepeDev/CooldownAPI/main">
<img src="https://img.shields.io/github/languages/code-size/JosepeDev/CooldownAPI">
<img src="https://img.shields.io/github/license/JosepeDev/CooldownAPI">
<img src="https://img.shields.io/github/v/release/JosepeDev/CooldownAPI">
</p>
<p align="center">
<img src="https://img.shields.io/github/followers/JosepeDev?style=social">
<img src="https://img.shields.io/github/watchers/JosepeDev/CooldownAPI?style=social">
<img src="https://img.shields.io/github/stars/JosepeDev/CooldownAPI?style=social">
</p>

### 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.
Expand All @@ -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)
{
Expand All @@ -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
Expand All @@ -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**
Expand All @@ -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()**
Expand Down

0 comments on commit c5668b3

Please sign in to comment.