Skip to content

Commit

Permalink
Revamp cooldown system
Browse files Browse the repository at this point in the history
  • Loading branch information
natan-dot-com committed Jan 4, 2022
1 parent 02caa5b commit 18a026f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 54 deletions.
31 changes: 5 additions & 26 deletions lib/backend/ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#include <atomic>
#include <mutex>

#include <iostream>

#define FIREBALL_THREAD_ID 1001
#define DESTAURA_THREAD_ID 1002

#define CD_CLICK_MILIS 500
#define CD_FIREBALL_SECS 10
#define CD_DESTAURA_SECS 20
Expand All @@ -38,15 +33,9 @@ typedef struct _TurnResults {
typedef struct _ThreadInstance {
std::thread currThread;
std::atomic<bool> executionFlag;
int maxCooldown;
int cooldownProgress;
int threadID;

_ThreadInstance(int _cooldownProgress = 0, int threadID = -1) {
_ThreadInstance() {
this->executionFlag = false;
this->cooldownProgress = _cooldownProgress;
this->maxCooldown = _cooldownProgress;
this->threadID = threadID;
}

void toggleUsage() {
Expand All @@ -56,15 +45,6 @@ typedef struct _ThreadInstance {
bool isInUse() {
return this->executionFlag;
}

void decreaseCooldown() {
if (this->cooldownProgress == 0) {
this->cooldownProgress = this->maxCooldown;
}
else {
this->cooldownProgress--;
}
}
} ThreadInstance;

class Context {
Expand All @@ -77,8 +57,7 @@ class Context {
std::mutex healthBarMut;
int currFloor;

void startCooldown(ThreadInstance *cooldownThread);
void clickCooldown();
void startCooldown(ThreadInstance *cooldownThread, const int timeAmount, const int MODE);
bool proccessMonsterDamage(const int dealtDamage, int &gainedExp, int &gainedCoins, bool &isLevelUp);
void damageDestructionAura(const int dealtDamage, int &gainedExp, int &gainedCoins, bool &isLevelUp);

Expand All @@ -94,9 +73,9 @@ class Context {
bool saveGame(std::string targetFilename);

// Damage/skills related methods
TurnResults *evokeDamageOnClick();
TurnResults *evokeFireball();
TurnResults *evokeDestructionAura();
void evokeDamageOnClick();
void evokeFireball();
void evokeDestructionAura();

// Skills management
bool updateFireball();
Expand Down
50 changes: 22 additions & 28 deletions src/backend/ctx.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "./lib/backend/ctx.h"
#include "./lib/game.h"

#define MODE_SECS -1
#define MODE_MILIS -2

// Main constructor for context class
Context::Context() {
currFloor = 1;
Expand All @@ -9,31 +12,31 @@ Context::Context() {
currEnemyInstance = new Enemy(currFloor);
errHandler = new Error();

fireballExec = new ThreadInstance(CD_FIREBALL_SECS, FIREBALL_THREAD_ID);
fireballExec = new ThreadInstance(CD_FIREBALL_SECS);
clickExec = new ThreadInstance();
destAuraExec = new ThreadInstance(CD_DESTAURA_SECS, DESTAURA_THREAD_ID);
destAuraExec = new ThreadInstance(CD_DESTAURA_SECS);
destAuraDmg = new ThreadInstance();
}

// Start the current skill cooldown on its representative thread
// - Toggles the usage indicator of the predetermined thread (as 'cooldownThread')
// - Sleeps thread for 'timeAmount' seconds/milisseconds, depending of the used 'MODE'.
void Context::startCooldown(ThreadInstance *cooldownThread) {
void Context::startCooldown(ThreadInstance *cooldownThread, const int timeAmount, const int MODE) {
cooldownThread->toggleUsage();
while (cooldownThread->cooldownProgress > 0) {
cooldownThread->decreaseCooldown();
std::this_thread::sleep_for(std::chrono::seconds(1));
if (timeAmount > 0) {
switch (MODE) {
case MODE_SECS:
this_thread::sleep_for(std::chrono::seconds(timeAmount));
break;

case MODE_MILIS:
this_thread::sleep_for(std::chrono::milliseconds(timeAmount));
break;
}
}
cooldownThread->decreaseCooldown();
cooldownThread->toggleUsage();
}

void Context::clickCooldown() {
this->clickExec->toggleUsage();
std::this_thread::sleep_for(std::chrono::milliseconds(CD_CLICK_MILIS));
this->clickExec->toggleUsage();
}

// Proccess and refresh the monster HP bar after an attack.
// - Also immediately evoke a new enemy if the current one is dead after the attack.
// - Returns bool indicating whether the current enemy is dead ('true') or not ('false').
Expand All @@ -58,7 +61,7 @@ bool Context::proccessMonsterDamage(const int dealtDamage, int &gainedExp, int &
// - Cooldown between clicks: 0.5s (500ms) on 'clickExec' thread.
// - Returns all the details from the current action, such as the damage dealt,
// the experience gained, if the player leveled up.
TurnResults *Context::evokeDamageOnClick() {
void Context::evokeDamageOnClick() {
if (!this->clickExec->isInUse()) {

// Re the functionality of previously used thread
Expand All @@ -80,18 +83,15 @@ TurnResults *Context::evokeDamageOnClick() {
}

// Cooldown: 0.5s delay for each damage dealt
this->clickExec->currThread = std::thread(&Context::clickCooldown, this);

return new TurnResults(dealtDamage, gainedExp, gainedCoins, isLevelUp);
this->clickExec->currThread = std::thread(&Context::startCooldown, this, this->clickExec, CD_CLICK_MILIS, MODE_MILIS);
}
return NULL;
}

// Casts a fireball, dealing extra damage.
// - Cooldown between fireball casts: 10s on 'fireballExec' thread.
// - Returns all the details from the current action, such as the damage dealt,
// the experience gained, if the player leveled up.
TurnResults *Context::evokeFireball() {
void Context::evokeFireball() {
if (!this->fireballExec->isInUse()) {

// Re the functionality of previously used thread
Expand All @@ -113,11 +113,8 @@ TurnResults *Context::evokeFireball() {
}

// Cooldown: 10s
this->fireballExec->currThread = std::thread(&Context::startCooldown, this, this->fireballExec);

return new TurnResults(dealtDamage, gainedExp, gainedCoins, isLevelUp);
this->fireballExec->currThread = std::thread(&Context::startCooldown, this, this->fireballExec, CD_FIREBALL_SECS, MODE_SECS);
}
return NULL;
}

// Auxiliary function which proccess the total damage dealt in 'destAuraDmg' thread.
Expand Down Expand Up @@ -149,7 +146,7 @@ void Context::damageDestructionAura(const int dealtDamage, int &gainedExp, int &
// - Cooldown between destruction aura damage ticks: 1s on 'destAuraDmg' thread.
// - Returns all the details from the current action, such as the damage dealt,
// the experience gained, if the player leveled up.
TurnResults *Context::evokeDestructionAura() {
void Context::evokeDestructionAura() {
if (!this->destAuraExec->isInUse()) {

// Proccess destruction aura DoT-based damage system
Expand All @@ -172,11 +169,8 @@ TurnResults *Context::evokeDestructionAura() {
this->destAuraExec->currThread.join();
}
// Cooldown: 20s
this->destAuraExec->currThread = std::thread(&Context::startCooldown, this, this->destAuraExec);

return new TurnResults(dealtDamage, gainedExp, gainedCoins, isLevelUp);
this->destAuraExec->currThread = std::thread(&Context::startCooldown, this, this->destAuraExec, CD_DESTAURA_SECS, MODE_SECS);
}
return NULL;
}

// Upgrade skills methods
Expand Down

0 comments on commit 18a026f

Please sign in to comment.