Skip to content

Commit 9687b85

Browse files
committed
rework again to skills with groupings
1 parent 2e2fc97 commit 9687b85

File tree

10 files changed

+65
-61
lines changed

10 files changed

+65
-61
lines changed

cpp-backend-new/include/combatants/Combatant.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Combatant
1616
Combatant(int troops, Stats stats);
1717

1818
[[nodiscard]] bool checkEffectActive(EffectType effect_type) const;
19-
void addSkill(const Skill& skill, CombatantEvent combat_event);
19+
void addSkill(const Skill* skill);
2020
void addStatusEffect(TimedEffect timed_effect, EffectType type);
2121

2222
void setTroops(int troops);
@@ -25,7 +25,6 @@ class Combatant
2525
[[nodiscard]] double getAttack() const;
2626
[[nodiscard]] double getDefense() const;
2727
[[nodiscard]] double getHealth() const;
28-
2928
private:
3029
int troops;
3130
Stats stats;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <map>
66
#include <memory>
77
#include "orchestration/CombatantEvent.hpp"
8-
#include "skills/SkillGrouping.hpp"
8+
#include "skills/Skill.hpp"
99
#include "utils/NumberGenerator.hpp"
1010
// forward declaration for combatant to avoid circular dependency
1111
class Combatant;
@@ -14,15 +14,14 @@ class CombatPublisher
1414
{
1515
public:
1616
CombatPublisher();
17-
bool subToEvent(const SkillGrouping& skill_grouping);
18-
bool unsubToEvent(const SkillGrouping& skill_grouping);
17+
bool subToEvent(const Skill* skill);
18+
bool unsubToEvent(const Skill* skill);
1919
void publishEvent(CombatantEvent event, Combatant& self, Combatant& target, NumberGenerator& number_generator) const;
2020

21-
// Takes ownership of the skill
22-
void addSkillGrouping(std::unique_ptr<SkillGrouping> skill_grouping);
21+
// Subscribe a skill reference (does not take ownership)
22+
void addSkill(const Skill* skill);
2323
private:
24-
std::vector<std::unique_ptr<const SkillGrouping>> owned_skill_groupings;
25-
std::map<CombatantEvent, std::vector<const SkillGrouping&>> combat_event_subscribers;
24+
std::map<CombatantEvent, std::vector<const Skill*>> combat_event_subscribers;
2625
};
2726

2827
#endif

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ class SetupPublisherLoader
2424
bool loadJson();
2525

2626
/**
27-
* @brief Loads a combat_publisher with
27+
* @brief Loads and owns all Skills from JSON, provides them to CombatPublishers via references
28+
* Ownership of all Skills remains with SetupPublisherLoader
2829
*
29-
* @param combat_publisher
30-
* @param commanders
30+
* @param combat_publisher The publisher to subscribe skills to
31+
* @param combatant_setup The setup containing commander information
3132
*/
32-
void loadPublisher(CombatPublisher& combat_publisher, const CombatantSetup& combatant_setup) const;
33+
void loadPublisher(CombatPublisher& combat_publisher, const CombatantSetup& combatant_setup);
3334
private:
3435

3536
static constexpr std::string_view skills_path = RESOURCE_DIR "/skills.json";
3637
json commander_data;
3738
json skill_data;
3839
json mount_slot_1_data;
3940
json mount_slot_2_data;
41+
std::vector<std::unique_ptr<Skill>> owned_skills;
4042
/*
4143
4244
std::vector<Skill> loadSkills(const json& skill_data, CommanderName commander_name, CombatPublisher& combat_publisher) const;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class Skill
2323
double chance
2424
);
2525

26+
Skill(
27+
28+
);
29+
2630
virtual ~Skill() = default;
2731

2832
virtual void onDependent(
@@ -33,6 +37,8 @@ class Skill
3337

3438
virtual bool operator==(const Skill& other) const = 0;
3539

40+
// Checks if a skill should trigger
41+
// Checks RNG + Trigger condition
3642
[[nodiscard]]
3743
bool checkCondition(
3844
const Combatant& friendly,

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
#include "utils/NumberGenerator.hpp"
88
#include <memory>
99

10-
class SkillGrouping
10+
class SkillGrouping : Skill
1111
{
1212
public:
13-
SkillGrouping(CombatantEvent dependent, double chance);
13+
SkillGrouping(
14+
SkillType skill_type,
15+
Condition skill_condition,
16+
CombatantEvent dependent,
17+
SkillTarget target,
18+
double chance
19+
);
20+
21+
SkillGrouping(
22+
CombatantEvent dependent,
23+
double chance
24+
);
1425

1526
/**
1627
* @brief Called when a dependent is triggered
@@ -23,25 +34,16 @@ class SkillGrouping
2334
Combatant& self,
2435
Combatant& target,
2536
NumberGenerator& number_generator
26-
) const;
37+
) const override;
2738

2839
/**
2940
* @brief Add a skill to the groupings internal vector
3041
*
3142
* @param skill skill to add
3243
*/
3344
void addSkill(std::unique_ptr<Skill> skill);
34-
35-
[[nodiscard]] CombatantEvent getDependentEvent() const;
3645
private:
3746
std::vector<std::unique_ptr<Skill>> skills;
38-
39-
const CombatantEvent dependent;
40-
const bool always_triggers;
41-
const double chance;
42-
43-
[[nodiscard]]
44-
bool checkShouldTrigger(NumberGenerator& number_generator) const;
4547
};
4648

4749
#endif

cpp-backend-new/src/combatants/Combatant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ bool Combatant::checkEffectActive(const EffectType effect_type) const
2727
return status_controller.checkEffectActive(effect_type);
2828
}
2929

30-
void Combatant::addSkill(const Skill& skill, const CombatantEvent combat_event)
30+
void Combatant::addSkill(const Skill* skill)
3131
{
32-
combat_publisher.subToEvent(skill, combat_event);
32+
combat_publisher.subToEvent(skill);
3333
}
3434

3535
void Combatant::setTroops(int troops)

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ 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 SkillGrouping&>();
10+
combat_event_subscribers[static_cast<CombatantEvent>(i)] = std::vector<const Skill*>();
1111
}
1212
}
1313

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

19-
for (const SkillGrouping& skill_grouping : current_groupings)
19+
for (const auto skill : current_skills)
2020
{
2121
// equivalence logic, return false if is in already
2222
}
2323

24-
current_groupings.push_back(new_skill_grouping);
24+
current_skills.push_back(skill);
2525

2626
return true;
2727
}
2828

29-
bool CombatPublisher::unsubToEvent(const SkillGrouping& remove_skill_grouping)
29+
bool CombatPublisher::unsubToEvent(const Skill* skill)
3030
{
31-
CombatantEvent event = remove_skill_grouping.getDependentEvent();
32-
std::vector<const SkillGrouping&>& current_groupings = combat_event_subscribers[event];
31+
CombatantEvent event = skill->getSkillDependent();
32+
std::vector<const Skill*>& current_skills = combat_event_subscribers[event];
3333

34-
for (const SkillGrouping& skill_grouping : current_groupings)
34+
for (const Skill* skill : current_skills)
3535
{
3636
// equivalent logic, erase and return true if already in
3737
}
@@ -42,14 +42,14 @@ bool CombatPublisher::unsubToEvent(const SkillGrouping& remove_skill_grouping)
4242

4343
void CombatPublisher::publishEvent(const CombatantEvent event, Combatant& self, Combatant& target, NumberGenerator& number_generator) const
4444
{
45-
const std::vector<const SkillGrouping&>& skills = combat_event_subscribers.at(event);
46-
for (const Skill& skill : skills)
45+
const std::vector<const Skill*>& 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::addSkillGrouping(std::unique_ptr<SkillGrouping> skill_grouping)
52+
void CombatPublisher::addSkill(const Skill* skill)
5353
{
54-
owned_skill_groupings.push_back(std::move(skill_grouping));
55-
}
54+
subToEvent(skill);
55+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool SetupPublisherLoader::loadJson()
3333
}
3434
}
3535

36-
void SetupPublisherLoader::loadPublisher(CombatPublisher& combat_publisher, const CombatantSetup& combatant_setup) const
36+
void SetupPublisherLoader::loadPublisher(CombatPublisher& combat_publisher, const CombatantSetup& combatant_setup)
3737
{
3838
SkillParser parser;
3939

@@ -45,7 +45,9 @@ void SetupPublisherLoader::loadPublisher(CombatPublisher& combat_publisher, cons
4545
auto skills = parser.loadSkills(skill_data, combatant_setup.commanders[i], is_primary);
4646
for (auto& skill : skills)
4747
{
48-
combat_publisher.addSkill(std::move(skill));
48+
// Store ownership in this loader, provide reference to publisher
49+
combat_publisher.addSkill(skill.get());
50+
owned_skills.push_back(std::move(skill));
4951
}
5052
}
5153

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bool Skill::checkCondition(
3535
);
3636
}
3737

38+
3839
SkillTarget Skill::getSkillTarget() const
3940
{
4041
return target;
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#include "skills/SkillGrouping.hpp"
22

3-
SkillGrouping::SkillGrouping(CombatantEvent dependent, double chance)
4-
: dependent { dependent },
5-
chance { chance },
6-
always_triggers { chance == 1.0 }
3+
SkillGrouping::SkillGrouping(
4+
SkillType skill_type,
5+
Condition skill_condition,
6+
CombatantEvent dependent,
7+
SkillTarget target,
8+
double chance
9+
)
10+
: Skill(skill_type, skill_condition, dependent, target, chance)
711
{}
812

913
void SkillGrouping::onDependent(Combatant& self, Combatant& target, NumberGenerator& number_generator) const
1014
{
1115
// early return if shouldn't trigger
12-
if (!checkShouldTrigger(number_generator))
16+
if (!Skill::checkCondition(self, target, number_generator))
1317
{
1418
return;
1519
}
@@ -20,18 +24,7 @@ void SkillGrouping::onDependent(Combatant& self, Combatant& target, NumberGenera
2024
}
2125
}
2226

23-
CombatantEvent SkillGrouping::getDependentEvent() const
24-
{
25-
return dependent;
26-
}
27-
2827
void SkillGrouping::addSkill(std::unique_ptr<Skill> skill)
2928
{
3029
skills.push_back(std::move(skill));
3130
}
32-
33-
inline bool SkillGrouping::checkShouldTrigger(NumberGenerator& number_generator) const
34-
{
35-
return always_triggers || number_generator.getRandomDouble() < chance;
36-
}
37-

0 commit comments

Comments
 (0)