Reworked LearnedAttacks class, added C interface.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-02 15:23:08 +01:00
parent 60cbc80549
commit 3bca3c0673
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 37 additions and 8 deletions

View File

@ -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();
}

View File

@ -2,8 +2,15 @@
using namespace CreatureLib::Library;
void LearnableAttacks::AddLevelMove(uint8_t level, AttackData* attack) { _learnedByLevel[level].push_back(attack); }
const std::vector<const AttackData*>& 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<const AttackData*>& LearnableAttacks::GetAttacksForLevel(uint8_t level) const {
return _learnedByLevel.at(level);
}

View File

@ -1,19 +1,21 @@
#ifndef CREATURELIB_LEARNABLEATTACKS_HPP
#define CREATURELIB_LEARNABLEATTACKS_HPP
#include <unordered_map>
#include <vector>
#include "../Attacks/AttackData.hpp"
namespace CreatureLib::Library {
class LearnableAttacks {
std::vector<std::vector<const AttackData*>> _learnedByLevel;
std::unordered_map<uint8_t, std::vector<const AttackData*>> _learnedByLevel;
public:
LearnableAttacks(uint8_t maxLevel) : _learnedByLevel(std::vector<std::vector<const AttackData*>>(maxLevel)) {}
LearnableAttacks(size_t levelAttackCapacity)
: _learnedByLevel(std::unordered_map<uint8_t, std::vector<const AttackData*>>(levelAttackCapacity)) {}
void AddLevelMove(uint8_t level, AttackData* attack);
void AddLevelMove(uint8_t level, const AttackData* attack);
const std::vector<const AttackData*>& GetMovesForLevel(uint8_t level) const;
const std::vector<const AttackData*>& GetAttacksForLevel(uint8_t level) const;
};
}