Move `Buff` implementation to Buffs.cpp

This commit is contained in:
gsemaj 2022-07-17 18:39:25 -07:00
parent 98634d5aa2
commit 704d6a2452
2 changed files with 63 additions and 54 deletions

View File

@ -2,8 +2,66 @@
#include "PlayerManager.hpp" #include "PlayerManager.hpp"
#include <assert.h>
#include <set>
using namespace Buffs; using namespace Buffs;
void Buff::onTick() {
assert(!stacks.empty());
std::set<BuffCallback> callbacks;
auto it = stacks.begin();
while(it != stacks.end()) {
BuffStack& stack = *it;
if(stack.onTick != nullptr && callbacks.count(stack.onTick) == 0) {
// unique callback
stack.onTick(self, &stack);
callbacks.insert(stack.onTick);
}
if(stack.durationTicks > 0) stack.durationTicks--;
if(stack.durationTicks == 0) {
it = stacks.erase(it);
stack.onExpire(self, &stack);
} else it++;
}
}
void Buff::onExpire() {
assert(!stacks.empty());
std::set<BuffCallback> callbacks;
while(!stacks.empty()) {
BuffStack stack = stacks.back();
stacks.pop_back();
if(stack.onExpire != nullptr && callbacks.count(stack.onExpire) == 0) {
// execute unique callback
callbacks.insert(stack.onExpire);
stack.onExpire(self, &stack);
}
}
}
void Buff::addStack(BuffStack* stack) {
stacks.push_back(*stack);
}
BuffStack* Buff::getDominantBuff() {
assert(!stacks.empty());
BuffStack* dominant = nullptr;
for(BuffStack& stack : stacks) {
if(stack.buffClass > dominant->buffClass)
dominant = &stack;
}
return dominant;
}
bool Buff::isStale() {
return stacks.empty();
}
static void timeBuffUpdate(EntityRef self, BuffStack* buff, int type) { static void timeBuffUpdate(EntityRef self, BuffStack* buff, int type) {
if(self.kind != EntityKind::PLAYER) if(self.kind != EntityKind::PLAYER)

View File

@ -4,9 +4,7 @@
#include "Entities.hpp" #include "Entities.hpp"
#include <assert.h>
#include <vector> #include <vector>
#include <set>
/* forward declaration(s) */ /* forward declaration(s) */
struct BuffStack; struct BuffStack;
@ -39,45 +37,9 @@ private:
std::vector<BuffStack> stacks; std::vector<BuffStack> stacks;
public: public:
void onTick() { void onTick();
assert(!stacks.empty()); void onExpire();
void addStack(BuffStack* stack);
std::set<BuffCallback> callbacks;
auto it = stacks.begin();
while(it != stacks.end()) {
BuffStack& stack = *it;
if(stack.onTick != nullptr && callbacks.count(stack.onTick) == 0) {
// unique callback
stack.onTick(self, &stack);
callbacks.insert(stack.onTick);
}
if(stack.durationTicks > 0) stack.durationTicks--;
if(stack.durationTicks == 0) {
it = stacks.erase(it);
stack.onExpire(self, &stack);
} else it++;
}
}
void onExpire() {
assert(!stacks.empty());
std::set<BuffCallback> callbacks;
while(!stacks.empty()) {
BuffStack stack = stacks.back();
stacks.pop_back();
if(stack.onExpire != nullptr && callbacks.count(stack.onExpire) == 0) {
// execute unique callback
callbacks.insert(stack.onExpire);
stack.onExpire(self, &stack);
}
}
}
void addStack(BuffStack* stack) {
stacks.push_back(*stack);
}
/* /*
* Why do this madness? Let me tell you why. * Why do this madness? Let me tell you why.
@ -88,16 +50,7 @@ public:
* to a single bitfield. So we use a "buff class" and pick * to a single bitfield. So we use a "buff class" and pick
* the buff stack that is most "dominant". * the buff stack that is most "dominant".
*/ */
BuffStack* getDominantBuff() { BuffStack* getDominantBuff();
assert(!stacks.empty());
BuffStack* dominant = nullptr;
for(BuffStack& stack : stacks) {
if(stack.buffClass > dominant->buffClass)
dominant = &stack;
}
return dominant;
}
/* /*
* In general, a Buff object won't exist * In general, a Buff object won't exist
@ -106,9 +59,7 @@ public:
* stacks will be empty for a brief moment * stacks will be empty for a brief moment
* when the last stack is popped. * when the last stack is popped.
*/ */
bool isStale() { bool isStale();
return stacks.empty();
}
Buff(EntityRef pSelf, BuffStack* firstStack) Buff(EntityRef pSelf, BuffStack* firstStack)
: self(pSelf) { : self(pSelf) {