From 462ced668569829d2236395e0f818f02fd5fe6de Mon Sep 17 00:00:00 2001 From: gsemaj Date: Sun, 17 Jul 2022 18:39:25 -0700 Subject: [PATCH] Move `Buff` implementation to Buffs.cpp --- src/Buffs.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Buffs.hpp | 59 +++++---------------------------------------------- 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/src/Buffs.cpp b/src/Buffs.cpp index 133d2da..69ae3d8 100644 --- a/src/Buffs.cpp +++ b/src/Buffs.cpp @@ -2,8 +2,66 @@ #include "PlayerManager.hpp" +#include +#include + using namespace Buffs; +void Buff::onTick() { + assert(!stacks.empty()); + + std::set 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 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) diff --git a/src/Buffs.hpp b/src/Buffs.hpp index 0b55396..86953f0 100644 --- a/src/Buffs.hpp +++ b/src/Buffs.hpp @@ -4,9 +4,7 @@ #include "Entities.hpp" -#include #include -#include /* forward declaration(s) */ struct BuffStack; @@ -39,45 +37,9 @@ private: std::vector stacks; public: - void onTick() { - assert(!stacks.empty()); - - std::set 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 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) {