Implements system for saving Evolutions.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-01-02 13:45:39 +01:00
parent 23bcec9b75
commit 5dc6695f9e
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,74 @@
#ifndef PKMNLIB_EVOLUTIONDATA_HPP
#define PKMNLIB_EVOLUTIONDATA_HPP
#include <Library/Gender.hpp>
#include <any>
#include <string>
#include <utility>
#include <vector>
#include "../TimeOfDay.hpp"
#include "EvolutionMethod.hpp"
namespace PkmnLib::Library {
class PokemonSpecies;
class Move;
class Item;
class EvolutionData {
private:
const PokemonSpecies* _evolvesInto;
EvolutionMethod _method;
std::vector<std::any> _evolutionData; // This can probably be done in a better way, can't think of it now.
EvolutionData(EvolutionMethod method, std::vector<std::any> data, const PokemonSpecies* next)
: _evolvesInto(next), _method(method), _evolutionData(std::move(data)) {}
public:
static EvolutionData CreateLevelEvolution(uint8_t level, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::Level, {std::any(level)}, into);
}
static EvolutionData CreateFriendshipEvolution(uint8_t friendship, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::HighFriendship, {std::any(friendship)}, into);
}
static EvolutionData CreateKnownMoveEvolution(const Move* move, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::KnownMove, {std::any(move)}, into);
}
static EvolutionData CreateLocationEvolution(const std::string& location, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::LocationBased, {std::any(location)}, into);
}
static EvolutionData CreateTimeEvolution(TimeOfDay time, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::TimeBased, {std::any(time)}, into);
}
static EvolutionData CreateHeldItemEvolution(const Item* item, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::HoldsItem, {std::any(item)}, into);
}
static EvolutionData CreateGenderBasedEvolution(CreatureLib::Library::Gender gender, uint8_t level,
const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::IsGenderAndLevel, {std::any(gender), std::any(level)}, into);
}
static EvolutionData CreateItemUseEvolution(const Item* item, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::EvolutionItemUse, {std::any(item)}, into);
}
static EvolutionData CreateItemUseWithGenderEvolution(const Item* item, CreatureLib::Library::Gender gender,
const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::EvolutionItemUseWithGender, {std::any(item), std::any(gender)}, into);
}
static EvolutionData CreateTradeEvolution(const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::Trade, {}, into);
}
static EvolutionData CreateTradeWithItemEvolution(const Item* item, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::TradeWithHeldItem, {std::any(item)}, into);
}
static EvolutionData CreateTradeWithSpeciesEvolution(const PokemonSpecies* traded, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::TradeWithSpecificPokemon, {std::any(traded)}, into);
}
static EvolutionData CreateCustomEvolution(std::vector<std::any> data, const PokemonSpecies* into) {
return EvolutionData(EvolutionMethod::Custom, std::move(data), into);
}
[[nodiscard]] inline const PokemonSpecies* GetNewSpecies() const { return _evolvesInto; }
[[nodiscard]] inline EvolutionMethod GetMethod() const { return _method; }
[[nodiscard]] inline const std::any& GetData(size_t index) const { return _evolutionData.at(index); }
};
}
#endif // PKMNLIB_EVOLUTIONDATA_HPP

View File

@ -0,0 +1,23 @@
#ifndef PKMNLIB_EVOLUTIONMETHOD_HPP
#define PKMNLIB_EVOLUTIONMETHOD_HPP
#include <cstdint>
namespace PkmnLib::Library{
enum class EvolutionMethod : uint8_t{
Level,
HighFriendship,
KnownMove,
LocationBased,
TimeBased,
HoldsItem,
IsGenderAndLevel,
EvolutionItemUse,
EvolutionItemUseWithGender,
Trade,
TradeWithHeldItem,
TradeWithSpecificPokemon,
Custom
};
}
#endif // PKMNLIB_EVOLUTIONMETHOD_HPP

View File

@ -1,12 +1,14 @@
#ifndef PKMNLIB_POKEMONSPECIES_HPP #ifndef PKMNLIB_POKEMONSPECIES_HPP
#define PKMNLIB_POKEMONSPECIES_HPP #define PKMNLIB_POKEMONSPECIES_HPP
#include <Battling/Models/Creature.hpp> #include <Battling/Models/Creature.hpp>
#include "../Evolutions/EvolutionData.hpp"
#include "PokemonForme.hpp" #include "PokemonForme.hpp"
namespace PkmnLib::Library { namespace PkmnLib::Library {
class PokemonSpecies : public CreatureLib::Library::CreatureSpecies { class PokemonSpecies : public CreatureLib::Library::CreatureSpecies {
private: private:
uint8_t _baseHappiness; uint8_t _baseHappiness;
std::vector<EvolutionData> _evolutions;
public: public:
PokemonSpecies(uint16_t id, const std::string& name, const PokemonForme* defaultVariant, float genderRatio, PokemonSpecies(uint16_t id, const std::string& name, const PokemonForme* defaultVariant, float genderRatio,
@ -23,6 +25,9 @@ namespace PkmnLib::Library {
inline const PokemonForme* GetForme(const std::string& key) const { inline const PokemonForme* GetForme(const std::string& key) const {
return reinterpret_cast<const PokemonForme*>(CreatureSpecies::GetVariant(key)); return reinterpret_cast<const PokemonForme*>(CreatureSpecies::GetVariant(key));
} }
inline void AddEvolution(EvolutionData data) { _evolutions.push_back(data); }
const std::vector<EvolutionData>& GetEvolutions() const { return _evolutions; }
}; };
} }

14
src/Library/TimeOfDay.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef PKMNLIB_TIMEOFDAY_HPP
#define PKMNLIB_TIMEOFDAY_HPP
#include <cstdint>
namespace PkmnLib::Library{
enum class TimeOfDay : uint8_t {
Night,
Morning,
Afternoon,
Evening
};
}
#endif // PKMNLIB_TIMEOFDAY_HPP

View File

@ -119,6 +119,31 @@ TEST_CASE("Able to get species base happiness", "library") {
delete species; delete species;
} }
TEST_CASE("Able to set and get evolution", "library") {
auto species = new PkmnLib::Library::PokemonSpecies(
1, "foo",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0}, CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"}, {"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100);
auto species2 = new PkmnLib::Library::PokemonSpecies(
2, "bar",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0}, CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"}, {"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100);
species->AddEvolution(PkmnLib::Library::EvolutionData::CreateLevelEvolution(16, species2));
auto evolutions = species->GetEvolutions();
REQUIRE(evolutions.size() == 1);
auto evo = evolutions[0];
CHECK(evo.GetMethod() == PkmnLib::Library::EvolutionMethod::Level);
CHECK(evo.GetNewSpecies() == species2);
CHECK(std::any_cast<uint8_t>(evo.GetData(0)) == 16);
delete species;
delete species2;
}
#endif #endif