From 3bca3c06737918d56e2de91656d5281519b2e42e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 2 Mar 2020 15:23:08 +0100 Subject: [PATCH] Reworked LearnedAttacks class, added C interface. --- CInterface/Library/LearnableAttacks.cpp | 20 +++++++++++++++++++ src/Library/CreatureData/LearnableAttacks.cpp | 15 ++++++++++---- src/Library/CreatureData/LearnableAttacks.hpp | 10 ++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 CInterface/Library/LearnableAttacks.cpp diff --git a/CInterface/Library/LearnableAttacks.cpp b/CInterface/Library/LearnableAttacks.cpp new file mode 100644 index 0000000..1995642 --- /dev/null +++ b/CInterface/Library/LearnableAttacks.cpp @@ -0,0 +1,20 @@ +#include "../../src/Library/CreatureData/LearnableAttacks.hpp" +#define export extern "C" +using namespace CreatureLib::Library; + +export LearnableAttacks* CreatureLib_LearnableAttacks_Construct(size_t levelAttackCapacity) { + return new LearnableAttacks(levelAttackCapacity); +}; + +export void CreatureLib_LearnableAttacks_Destruct(LearnableAttacks* p) { delete p; } + +export void CreatureLib_LearnableAttacks_AddLevelMove(LearnableAttacks* p, uint8_t level, const AttackData* attack) { + p->AddLevelMove(level, attack); +} + +export const AttackData* const* CreatureLib_LearnableAttacks_GetAttacksForLevel(LearnableAttacks* p, uint8_t level) { + return p->GetAttacksForLevel(level).data(); +} +export size_t CreatureLib_LearnableAttacks_GetAttacksForLevelCount(LearnableAttacks* p, uint8_t level) { + return p->GetAttacksForLevel(level).size(); +} \ No newline at end of file diff --git a/src/Library/CreatureData/LearnableAttacks.cpp b/src/Library/CreatureData/LearnableAttacks.cpp index 0912720..6362e04 100644 --- a/src/Library/CreatureData/LearnableAttacks.cpp +++ b/src/Library/CreatureData/LearnableAttacks.cpp @@ -2,8 +2,15 @@ using namespace CreatureLib::Library; -void LearnableAttacks::AddLevelMove(uint8_t level, AttackData* attack) { _learnedByLevel[level].push_back(attack); } - -const std::vector& LearnableAttacks::GetMovesForLevel(uint8_t level) const { - return _learnedByLevel[level]; +void LearnableAttacks::AddLevelMove(uint8_t level, const AttackData* attack) { + auto find = _learnedByLevel.find(level); + if (find != _learnedByLevel.end()) { + find->second.push_back(attack); + } else { + _learnedByLevel.insert({level, {attack}}); + } +} + +const std::vector& LearnableAttacks::GetAttacksForLevel(uint8_t level) const { + return _learnedByLevel.at(level); } diff --git a/src/Library/CreatureData/LearnableAttacks.hpp b/src/Library/CreatureData/LearnableAttacks.hpp index efcafbf..b7364a4 100644 --- a/src/Library/CreatureData/LearnableAttacks.hpp +++ b/src/Library/CreatureData/LearnableAttacks.hpp @@ -1,19 +1,21 @@ #ifndef CREATURELIB_LEARNABLEATTACKS_HPP #define CREATURELIB_LEARNABLEATTACKS_HPP +#include #include #include "../Attacks/AttackData.hpp" namespace CreatureLib::Library { class LearnableAttacks { - std::vector> _learnedByLevel; + std::unordered_map> _learnedByLevel; public: - LearnableAttacks(uint8_t maxLevel) : _learnedByLevel(std::vector>(maxLevel)) {} + LearnableAttacks(size_t levelAttackCapacity) + : _learnedByLevel(std::unordered_map>(levelAttackCapacity)) {} - void AddLevelMove(uint8_t level, AttackData* attack); + void AddLevelMove(uint8_t level, const AttackData* attack); - const std::vector& GetMovesForLevel(uint8_t level) const; + const std::vector& GetAttacksForLevel(uint8_t level) const; }; }