Move Buff implementation to Buffs.cpp

This commit is contained in:
gsemaj 2022-07-17 18:39:25 -07:00
parent cc190efc63
commit 462ced6685
2 changed files with 63 additions and 54 deletions

View File

@ -2,8 +2,66 @@
#include "PlayerManager.hpp"
#include <assert.h>
#include <set>
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) {
if(self.kind != EntityKind::PLAYER)

View File

@ -4,9 +4,7 @@
#include "Entities.hpp"
#include <assert.h>
#include <vector>
#include <set>
/* forward declaration(s) */
struct BuffStack;
@ -39,45 +37,9 @@ private:
std::vector<BuffStack> stacks;
public:
void 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 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);
}
void onTick();
void onExpire();
void addStack(BuffStack* stack);
/*
* 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
* the buff stack that is most "dominant".
*/
BuffStack* getDominantBuff() {
assert(!stacks.empty());
BuffStack* dominant = nullptr;
for(BuffStack& stack : stacks) {
if(stack.buffClass > dominant->buffClass)
dominant = &stack;
}
return dominant;
}
BuffStack* getDominantBuff();
/*
* In general, a Buff object won't exist
@ -106,9 +59,7 @@ public:
* stacks will be empty for a brief moment
* when the last stack is popped.
*/
bool isStale() {
return stacks.empty();
}
bool isStale();
Buff(EntityRef pSelf, BuffStack* firstStack)
: self(pSelf) {