Skip to content

Commit 2e2fc97

Browse files
committed
sstart on skill grouping refactor
1 parent 4c7a895 commit 2e2fc97

4 files changed

Lines changed: 46 additions & 33 deletions

File tree

cpp-backend-new/include/orchestration/CombatPublisher.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
#include <map>
66
#include <memory>
77
#include "orchestration/CombatantEvent.hpp"
8-
#include "skills/Skill.hpp"
8+
#include "skills/SkillGrouping.hpp"
99
#include "utils/NumberGenerator.hpp"
1010
// forward declaration for combatant to avoid circular dependency
1111
class Combatant;
1212

1313
class CombatPublisher
1414
{
15-
public:
16-
CombatPublisher();
17-
bool subToEvent(const Skill& skill, CombatantEvent combat_event);
18-
bool unsubToEvent(const Skill& skill, CombatantEvent combat_event);
19-
void publishEvent(CombatantEvent event, Combatant& self, Combatant& target, NumberGenerator& number_generator) const;
20-
21-
// Takes ownership of the skill
22-
void addSkill(std::unique_ptr<Skill> skill);
23-
private:
24-
std::vector<std::unique_ptr<Skill>> owned_skills;
25-
std::map<CombatantEvent, std::vector<const Skill*>> combat_event_subscribers;
15+
public:
16+
CombatPublisher();
17+
bool subToEvent(const SkillGrouping& skill_grouping);
18+
bool unsubToEvent(const SkillGrouping& skill_grouping);
19+
void publishEvent(CombatantEvent event, Combatant& self, Combatant& target, NumberGenerator& number_generator) const;
20+
21+
// Takes ownership of the skill
22+
void addSkillGrouping(std::unique_ptr<SkillGrouping> skill_grouping);
23+
private:
24+
std::vector<std::unique_ptr<const SkillGrouping>> owned_skill_groupings;
25+
std::map<CombatantEvent, std::vector<const SkillGrouping&>> combat_event_subscribers;
2626
};
2727

2828
#endif

cpp-backend-new/include/skills/SkillGrouping.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class SkillGrouping
3131
* @param skill skill to add
3232
*/
3333
void addSkill(std::unique_ptr<Skill> skill);
34+
35+
[[nodiscard]] CombatantEvent getDependentEvent() const;
3436
private:
3537
std::vector<std::unique_ptr<Skill>> skills;
3638

cpp-backend-new/src/orchestration/CombatPublisher.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,49 @@ CombatPublisher::CombatPublisher()
77
{
88
for (int i = 0; i < static_cast<int>(CombatantEvent::COUNT); ++i)
99
{
10-
combat_event_subscribers[static_cast<CombatantEvent>(i)] = std::vector<const Skill*>();
10+
combat_event_subscribers[static_cast<CombatantEvent>(i)] = std::vector<const SkillGrouping&>();
1111
}
1212
}
1313

14-
bool CombatPublisher::subToEvent(const Skill& skill, const CombatantEvent event)
14+
bool CombatPublisher::subToEvent(const SkillGrouping& new_skill_grouping)
1515
{
16-
std::vector<const Skill*>& skills = combat_event_subscribers[event];
16+
CombatantEvent event = new_skill_grouping.getDependentEvent();
17+
std::vector<const SkillGrouping&>& current_groupings = combat_event_subscribers[event];
1718

18-
if (std::ranges::find(skills, &skill) != skills.end())
19+
for (const SkillGrouping& skill_grouping : current_groupings)
1920
{
20-
// skill already inside the vector
21-
return false;
21+
// equivalence logic, return false if is in already
2222
}
2323

24-
skills.push_back(&skill);
24+
current_groupings.push_back(new_skill_grouping);
25+
2526
return true;
2627
}
2728

28-
bool CombatPublisher::unsubToEvent(const Skill& skill, const CombatantEvent event)
29+
bool CombatPublisher::unsubToEvent(const SkillGrouping& remove_skill_grouping)
2930
{
30-
std::vector<const Skill*>& skills = combat_event_subscribers[event];
31+
CombatantEvent event = remove_skill_grouping.getDependentEvent();
32+
std::vector<const SkillGrouping&>& current_groupings = combat_event_subscribers[event];
3133

32-
auto iterator = std::ranges::find(skills, &skill);
33-
if (iterator == skills.end())
34+
for (const SkillGrouping& skill_grouping : current_groupings)
3435
{
35-
// skill not inside the vector
36-
return false;
36+
// equivalent logic, erase and return true if already in
3737
}
3838

39-
skills.erase(iterator);
40-
return true;
39+
// not inside loop
40+
return false;
4141
}
4242

4343
void CombatPublisher::publishEvent(const CombatantEvent event, Combatant& self, Combatant& target, NumberGenerator& number_generator) const
4444
{
45-
const std::vector<const Skill*>& skills = combat_event_subscribers.at(event);
46-
for (const Skill* skill : skills)
45+
const std::vector<const SkillGrouping&>& skills = combat_event_subscribers.at(event);
46+
for (const Skill& skill : skills)
4747
{
48-
skill->onDependent(self, target, number_generator);
48+
skill.onDependent(self, target, number_generator);
4949
}
5050
}
5151

52-
void CombatPublisher::addSkill(std::unique_ptr<Skill> skill)
52+
void CombatPublisher::addSkillGrouping(std::unique_ptr<SkillGrouping> skill_grouping)
5353
{
54-
owned_skills.push_back(std::move(skill));
54+
owned_skill_groupings.push_back(std::move(skill_grouping));
5555
}

cpp-backend-new/src/skills/SkillGrouping.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ void SkillGrouping::onDependent(Combatant& self, Combatant& target, NumberGenera
2020
}
2121
}
2222

23+
CombatantEvent SkillGrouping::getDependentEvent() const
24+
{
25+
return dependent;
26+
}
27+
28+
void SkillGrouping::addSkill(std::unique_ptr<Skill> skill)
29+
{
30+
skills.push_back(std::move(skill));
31+
}
32+
2333
inline bool SkillGrouping::checkShouldTrigger(NumberGenerator& number_generator) const
2434
{
2535
return always_triggers || number_generator.getRandomDouble() < chance;
26-
}
36+
}
37+

0 commit comments

Comments
 (0)