Move Script ownership to script holder, added OnRemove script hook.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-02-05 14:52:50 +01:00
parent cd7ddcf78e
commit e45a36d78e
9 changed files with 19 additions and 22 deletions

View File

@@ -42,6 +42,7 @@ namespace CreatureLib::Battling {
for (auto s : _sides) {
delete s;
}
delete _currentTurnQueue;
}
[[nodiscard]] const BattleLibrary* GetLibrary() const;

View File

@@ -114,6 +114,8 @@ void Battling::Creature::Damage(uint32_t damage, Battling::DamageSource source)
void Battling::Creature::OverrideActiveTalent(const std::string& talent) {
_hasOverridenTalent = true;
_activeTalent->OnRemove();
delete _activeTalent;
_overridenTalentName = talent;
_activeTalent = this->_library->LoadScript(ScriptResolver::ScriptCategory::Talent, talent);
}

View File

@@ -67,6 +67,8 @@ namespace CreatureLib::Battling {
for (auto attack : _attacks) {
delete attack;
}
delete _activeTalent;
delete _status;
};
virtual void Initialize() {

View File

@@ -67,7 +67,7 @@ namespace CreatureLib::Battling {
}
}
virtual ~ExecutingAttack() = default;
virtual ~ExecutingAttack() { delete _script; };
TargetData& GetAttackDataForTarget(Creature* creature) { return _targets[creature]; }

View File

@@ -22,6 +22,7 @@ namespace CreatureLib::Battling {
virtual ~Script() = default;
virtual void Stack(){};
virtual void OnRemove(){};
const std::string& GetName() { return _name; }

View File

@@ -11,6 +11,12 @@ namespace CreatureLib::Battling {
std::unordered_map<std::string, size_t> _lookup;
public:
~ScriptSet() {
for (auto s : _scripts) {
delete s;
}
}
void Add(Script* script) {
auto f = _lookup.find(script->GetName());
if (f != _lookup.end()) {
@@ -24,12 +30,18 @@ namespace CreatureLib::Battling {
void Remove(const std::string& key) {
auto find = _lookup.find(key);
if (find != _lookup.end()) {
auto script = _scripts[find->second];
script->OnRemove();
delete script;
_scripts.erase(_scripts.begin() + find.operator*().second);
_lookup.erase(key);
}
}
void Clear() {
for (auto s : _scripts) {
delete s;
}
_scripts.clear();
_lookup.clear();
}