Initial support for setting battle choices.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
3938500595
commit
99315174a8
|
@ -1,5 +1,21 @@
|
||||||
#include "Battle.hpp"
|
#include "Battle.hpp"
|
||||||
|
#include "../TurnChoices/AttackTurnChoice.hpp"
|
||||||
|
|
||||||
const CreatureLib::Battling::BattleLibrary *CreatureLib::Battling::Battle::GetLibrary() const {
|
const CreatureLib::Battling::BattleLibrary *CreatureLib::Battling::Battle::GetLibrary() const {
|
||||||
return _library;
|
return _library;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CreatureLib::Battling::Battle::CanUse(CreatureLib::Battling::BaseTurnChoice *choice) {
|
||||||
|
if (choice->GetKind() == TurnChoiceKind::Attack)
|
||||||
|
//HOOK: change number of uses needed.
|
||||||
|
return static_cast<AttackTurnChoice*>(choice)->GetAttack()->GetRemainingUses() > 1;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CreatureLib::Battling::Battle::TrySetChoice(CreatureLib::Battling::BaseTurnChoice *choice) {
|
||||||
|
if (!CanUse(choice))
|
||||||
|
return false;
|
||||||
|
choice->GetUser()->GetBattleSide()->SetChoice(choice);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "BattleSide.hpp"
|
#include "BattleSide.hpp"
|
||||||
#include "../Library/BattleLibrary.hpp"
|
#include "../Library/BattleLibrary.hpp"
|
||||||
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
|
||||||
namespace CreatureLib::Battling {
|
namespace CreatureLib::Battling {
|
||||||
class Battle {
|
class Battle {
|
||||||
|
@ -11,6 +12,9 @@ namespace CreatureLib::Battling {
|
||||||
std::vector<BattleSide*> _sides;
|
std::vector<BattleSide*> _sides;
|
||||||
public:
|
public:
|
||||||
const BattleLibrary* GetLibrary() const;
|
const BattleLibrary* GetLibrary() const;
|
||||||
|
|
||||||
|
virtual bool CanUse(BaseTurnChoice* choice);
|
||||||
|
virtual bool TrySetChoice(BaseTurnChoice* choice);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1 +1,35 @@
|
||||||
#include "BattleSide.hpp"
|
#include "BattleSide.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
|
bool BattleSide::AllChoicesSet() const {
|
||||||
|
for (uint8_t i = 0; i < _creaturesPerSide; i++){
|
||||||
|
if (_choices[i] == nullptr){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BattleSide::ResetChoices() {
|
||||||
|
for (uint8_t i = 0; i < _creaturesPerSide; i++){
|
||||||
|
_choices[i] = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<const BaseTurnChoice *>& BattleSide::GetChoices() const{
|
||||||
|
return _choices;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BattleSide::SetChoice(const BaseTurnChoice *choice) {
|
||||||
|
auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser());
|
||||||
|
if (find ==_creatures.end())
|
||||||
|
throw "User not found";
|
||||||
|
uint8_t index = std::distance(_creatures.begin(),find);
|
||||||
|
_choices[index] = choice;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BattleSide::SetCreature(Creature *creature, uint8_t index) {
|
||||||
|
_creatures[index] = creature;
|
||||||
|
}
|
||||||
|
|
|
@ -3,10 +3,28 @@
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Creature.hpp"
|
#include "Creature.hpp"
|
||||||
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
|
||||||
namespace CreatureLib::Battling{
|
namespace CreatureLib::Battling{
|
||||||
class BattleSide {
|
class BattleSide {
|
||||||
|
uint8_t _creaturesPerSide;
|
||||||
std::vector<Creature*> _creatures;
|
std::vector<Creature*> _creatures;
|
||||||
|
std::vector<const BaseTurnChoice*> _choices;
|
||||||
|
public:
|
||||||
|
BattleSide(uint8_t creaturesPerSide)
|
||||||
|
: _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide), _choices(creaturesPerSide)
|
||||||
|
{
|
||||||
|
ResetChoices();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool AllChoicesSet() const;
|
||||||
|
[[nodiscard]] const std::vector<const BaseTurnChoice*>& GetChoices() const;
|
||||||
|
|
||||||
|
void SetChoice(const BaseTurnChoice* choice);
|
||||||
|
void ResetChoices();
|
||||||
|
|
||||||
|
void SetCreature(Creature* creature, uint8_t index);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,11 @@ const std::string &Battling::Creature::GetTalent() const {
|
||||||
return __Variant->GetTalent(_talentIndex);
|
return __Variant->GetTalent(_talentIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Battling::Creature::SetBattleData(Battling::Battle *battle, Battling::BattleSide *side) {
|
||||||
|
_battle = battle;
|
||||||
|
_side = side;
|
||||||
|
}
|
||||||
|
|
||||||
//region Stat APIs
|
//region Stat APIs
|
||||||
|
|
||||||
void Battling::Creature::ChangeStatBoost(Core::Statistic stat, int8_t diffAmount){
|
void Battling::Creature::ChangeStatBoost(Core::Statistic stat, int8_t diffAmount){
|
||||||
|
@ -98,4 +103,13 @@ void Battling::Creature::RecalculateBoostedStat(Core::Statistic stat) {
|
||||||
this->_boostedStats.SetStat(stat, s);
|
this->_boostedStats.SetStat(stat, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Battling::Battle *Battling::Creature::GetBattle() const{
|
||||||
|
return _battle;
|
||||||
|
}
|
||||||
|
|
||||||
|
Battling::BattleSide *Battling::Creature::GetBattleSide() const {
|
||||||
|
return _side;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//endregion
|
//endregion
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace CreatureLib::Battling{
|
namespace CreatureLib::Battling{
|
||||||
// Forward declare battle class
|
// Forward declare battle class
|
||||||
class Battle;
|
class Battle;
|
||||||
|
class BattleSide;
|
||||||
|
|
||||||
class Creature {
|
class Creature {
|
||||||
GetProperty(const Library::CreatureSpecies*, Species);
|
GetProperty(const Library::CreatureSpecies*, Species);
|
||||||
|
@ -29,6 +30,7 @@ namespace CreatureLib::Battling{
|
||||||
Core::StatisticSet<uint32_t > _boostedStats;
|
Core::StatisticSet<uint32_t > _boostedStats;
|
||||||
|
|
||||||
Battle* _battle;
|
Battle* _battle;
|
||||||
|
BattleSide* _side;
|
||||||
BattleLibrary* _library;
|
BattleLibrary* _library;
|
||||||
|
|
||||||
std::string _nickname = "";
|
std::string _nickname = "";
|
||||||
|
@ -46,6 +48,11 @@ namespace CreatureLib::Battling{
|
||||||
|
|
||||||
void ChangeLevel(int8_t amount);
|
void ChangeLevel(int8_t amount);
|
||||||
|
|
||||||
|
void SetBattleData(Battle* battle, BattleSide* side);
|
||||||
|
|
||||||
|
Battle* GetBattle() const;
|
||||||
|
BattleSide* GetBattleSide() const;
|
||||||
|
|
||||||
//region Stat APIs
|
//region Stat APIs
|
||||||
|
|
||||||
void SetBattle(Battle* battle);
|
void SetBattle(Battle* battle);
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef CREATURELIB_ATTACKTURNCHOICE_HPP
|
||||||
|
#define CREATURELIB_ATTACKTURNCHOICE_HPP
|
||||||
|
|
||||||
|
#include "BaseTurnChoice.hpp"
|
||||||
|
#include "../Models/LearnedAttack.hpp"
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling{
|
||||||
|
class AttackTurnChoice : public BaseTurnChoice {
|
||||||
|
LearnedAttack* _attack;
|
||||||
|
public:
|
||||||
|
AttackTurnChoice(Creature* c) : BaseTurnChoice(c){}
|
||||||
|
|
||||||
|
inline LearnedAttack* GetAttack() const{
|
||||||
|
return _attack;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //CREATURELIB_ATTACKTURNCHOICE_HPP
|
|
@ -0,0 +1,21 @@
|
||||||
|
#ifndef CREATURELIB_BASETURNCHOICE_HPP
|
||||||
|
#define CREATURELIB_BASETURNCHOICE_HPP
|
||||||
|
|
||||||
|
#include "TurnChoiceKind.hpp"
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling{
|
||||||
|
class BaseTurnChoice {
|
||||||
|
Creature* _user;
|
||||||
|
protected:
|
||||||
|
BaseTurnChoice(Creature* user) : _user(user){};
|
||||||
|
public:
|
||||||
|
virtual ~BaseTurnChoice() = default;
|
||||||
|
[[nodiscard]] virtual TurnChoiceKind GetKind() const = 0;
|
||||||
|
[[nodiscard]] inline Creature* GetUser() const{
|
||||||
|
return _user;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //CREATURELIB_BASETURNCHOICE_HPP
|
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef CREATURELIB_PASSTURNCHOICE_HPP
|
||||||
|
#define CREATURELIB_PASSTURNCHOICE_HPP
|
||||||
|
|
||||||
|
#include "BaseTurnChoice.hpp"
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling {
|
||||||
|
class PassTurnChoice : public BaseTurnChoice{
|
||||||
|
public:
|
||||||
|
PassTurnChoice(Creature* c) : BaseTurnChoice(c){}
|
||||||
|
|
||||||
|
TurnChoiceKind GetKind() const override {
|
||||||
|
return TurnChoiceKind ::Pass;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //CREATURELIB_PASSTURNCHOICE_HPP
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef CREATURELIB_TURNCHOICEKIND_HPP
|
||||||
|
#define CREATURELIB_TURNCHOICEKIND_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling {
|
||||||
|
enum class TurnChoiceKind : uint8_t {
|
||||||
|
Pass,
|
||||||
|
Attack,
|
||||||
|
Item,
|
||||||
|
Switch,
|
||||||
|
RunAway
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif //CREATURELIB_TURNCHOICEKIND_HPP
|
|
@ -0,0 +1,68 @@
|
||||||
|
#ifdef TESTS_BUILD
|
||||||
|
|
||||||
|
#include "../TestLibrary/TestLibrary.cpp"
|
||||||
|
#include "../../src/Battling/Models/BattleSide.hpp"
|
||||||
|
#include "../../src/Battling/Models/CreateCreature.hpp"
|
||||||
|
#include "../../src/Battling/TurnChoices/PassTurnChoice.hpp"
|
||||||
|
|
||||||
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
|
TEST_CASE( "Set Choice one-sized side", "[Battling]" ) {
|
||||||
|
auto side = BattleSide(1);
|
||||||
|
auto c = CreateCreature(GetLibrary(), "testSpecies1", 5).Create();
|
||||||
|
side.SetCreature(c, 0);
|
||||||
|
auto choice = new PassTurnChoice(c);
|
||||||
|
side.SetChoice(choice);
|
||||||
|
delete choice;
|
||||||
|
delete c;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Set Choice one-sized side, validate all choices set", "[Battling]" ) {
|
||||||
|
auto side = BattleSide(1);
|
||||||
|
auto c = CreateCreature(GetLibrary(), "testSpecies1", 5).Create();
|
||||||
|
side.SetCreature(c, 0);
|
||||||
|
auto choice = new PassTurnChoice(c);
|
||||||
|
REQUIRE_FALSE(side.AllChoicesSet());
|
||||||
|
side.SetChoice(choice);
|
||||||
|
REQUIRE(side.AllChoicesSet());
|
||||||
|
delete choice;
|
||||||
|
delete c;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Set Choice two-sized side", "[Battling]" ) {
|
||||||
|
auto side = BattleSide(2);
|
||||||
|
auto c1 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create();
|
||||||
|
auto c2 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create();
|
||||||
|
side.SetCreature(c1, 0);
|
||||||
|
side.SetCreature(c2, 1);
|
||||||
|
auto choice1 = new PassTurnChoice(c1);
|
||||||
|
auto choice2 = new PassTurnChoice(c2);
|
||||||
|
side.SetChoice(choice1);
|
||||||
|
side.SetChoice(choice2);
|
||||||
|
delete choice1;
|
||||||
|
delete choice2;
|
||||||
|
delete c1;
|
||||||
|
delete c2;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Set Choice two-sized side, validate all choices set", "[Battling]" ) {
|
||||||
|
auto side = BattleSide(2);
|
||||||
|
auto c1 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create();
|
||||||
|
auto c2 = CreateCreature(GetLibrary(), "testSpecies1", 5).Create();
|
||||||
|
side.SetCreature(c1, 0);
|
||||||
|
side.SetCreature(c2, 1);
|
||||||
|
auto choice1 = new PassTurnChoice(c1);
|
||||||
|
auto choice2 = new PassTurnChoice(c2);
|
||||||
|
REQUIRE_FALSE(side.AllChoicesSet());
|
||||||
|
side.SetChoice(choice1);
|
||||||
|
REQUIRE_FALSE(side.AllChoicesSet());
|
||||||
|
side.SetChoice(choice2);
|
||||||
|
REQUIRE(side.AllChoicesSet());
|
||||||
|
delete choice1;
|
||||||
|
delete choice2;
|
||||||
|
delete c1;
|
||||||
|
delete c2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,11 +1,6 @@
|
||||||
#ifdef TESTS_BUILD
|
#ifdef TESTS_BUILD
|
||||||
#include "../../extern/catch.hpp"
|
#include "../../extern/catch.hpp"
|
||||||
#include "../../src/Battling/Library/BattleLibrary.hpp"
|
#include "../../src/Battling/Library/BattleLibrary.hpp"
|
||||||
#include "../../src/Library/SpeciesLibrary.hpp"
|
|
||||||
#include "../../src/Library/AttackLibrary.hpp"
|
|
||||||
#include "../../src/Library/ItemLibrary.hpp"
|
|
||||||
#include "../../src/Library/GrowthRates/GrowthRateLibrary.hpp"
|
|
||||||
#include "../../src/Library/DataLibrary.hpp"
|
|
||||||
|
|
||||||
using namespace CreatureLib::Core;
|
using namespace CreatureLib::Core;
|
||||||
using namespace CreatureLib::Library;
|
using namespace CreatureLib::Library;
|
||||||
|
|
Loading…
Reference in New Issue