Initial setup for script hooks (likely to be revamped)
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-11-08 18:45:43 +01:00
parent 19e1308f93
commit c3bfbb569e
5 changed files with 88 additions and 0 deletions

View File

@ -78,12 +78,15 @@ void TurnHandler::ExecuteAttackChoice(const AttackTurnChoice *choice) {
void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *target, ExecutingAttack::TargetData &targetData) {
//HOOK: Check if attack fails on target
target->ExecuteScripts(Hook::IncomingAttackFails, {std::any(attack), std::any(target)});
//HOOK: Check if target is invulnerable
target->ExecuteScripts(Hook::IsInvulnerable, {std::any(attack), std::any(target)});
if (!targetData.IsHit()){
//HOOK: On attack miss.
target->ExecuteScripts(Hook::AttackMiss, {std::any(attack), std::any(target)});
return;
}

View File

@ -6,6 +6,7 @@
#include "../../Library/Items/Item.hpp"
#include "LearnedAttack.hpp"
#include "DamageSource.hpp"
#include "../ScriptHandling/ScriptSet.hpp"
namespace CreatureLib::Battling{
// Forward declare battle class
@ -40,6 +41,9 @@ namespace CreatureLib::Battling{
int8_t _talentIndex;
std::vector<LearnedAttack*> _attacks;
Script* _status = nullptr;
ScriptSet _volatile = {};
public:
Creature(const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level,
uint32_t experience, Core::StatisticSet<uint8_t > statExp, Core::StatisticSet<uint8_t > statPotential,
@ -59,6 +63,11 @@ namespace CreatureLib::Battling{
[[nodiscard]] const std::vector<uint8_t>& GetTypes() const;
[[nodiscard]] bool HasType(uint8_t type) const;
void ExecuteScripts(Hook hook, const std::vector<std::any>& args){
_status->Execute(hook, args);
_volatile.Execute(hook, args);
}
//region Stat APIs
void SetBattle(Battle* battle);

View File

@ -0,0 +1,15 @@
#ifndef CREATURELIB_HOOKS_HPP
#define CREATURELIB_HOOKS_HPP
#include <cstdint>
namespace CreatureLib::Battling{
enum class Hook : uint8_t{
IncomingAttackFails,
IsInvulnerable,
AttackMiss,
ChangeMove,
};
}
#endif //CREATURELIB_HOOKS_HPP

View File

@ -0,0 +1,27 @@
#ifndef CREATURELIB_SCRIPT_HPP
#define CREATURELIB_SCRIPT_HPP
#include <any>
#include <string>
#include <utility>
#include <vector>
#include "Hooks.hpp"
namespace CreatureLib::Battling{
class Script{
const std::string _name;
public:
explicit Script(std::string name) :_name(std::move(name)){}
virtual ~Script() = default;
virtual void Execute(Hook hook, const std::vector<std::any>& args){};
virtual void Stack(){};
const std::string& GetName(){
return _name;
}
};
}
#endif //CREATURELIB_SCRIPT_HPP

View File

@ -0,0 +1,34 @@
#ifndef CREATURELIB_SCRIPTSET_HPP
#define CREATURELIB_SCRIPTSET_HPP
#include <any>
#include <unordered_map>
#include "Script.hpp"
namespace CreatureLib::Battling{
class ScriptSet{
std::unordered_map<std::string, Script*> _scripts;
public:
void Execute(Hook hook, const std::vector<std::any>& args){
for (auto s: _scripts){
s.second->Execute(hook, args);
}
}
void Add(Script* script){
auto f = _scripts.find(script->GetName());
if (f != _scripts.end()){
f->second->Stack();
}
else{
_scripts.insert({script->GetName(), script});
}
}
void Remove(const std::string& key){
_scripts.erase(key);
}
};
}
#endif //CREATURELIB_SCRIPTSET_HPP