PkmnLib/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp

57 lines
2.0 KiB
C++
Raw Normal View History

#ifndef PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP
#define PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP
#include <CreatureLib/Battling/ScriptHandling/ItemUseScript.hpp>
#include "AngelScriptFunctionCall.hpp"
class AngelScriptResolver;
class AngelScriptItemUseScript final : public CreatureLib::Battling::ItemUseScript {
public:
AngelScriptItemUseScript(asIScriptObject* scriptObject, const AngelScriptResolver* resolver)
: _scriptObject(scriptObject), _resolver(resolver) {}
2021-03-07 17:20:42 +00:00
~AngelScriptItemUseScript(){
if (_scriptObject != nullptr){
_scriptObject->Release();
}
}
[[nodiscard]] bool IsItemUsable() const override;
[[nodiscard]] bool IsCreatureUseItem() const override;
bool IsUseValidForCreature(CreatureLib::Battling::Creature* creature) const override;
bool IsHoldable() const override;
void OnUse() const override;
void OnCreatureUse(CreatureLib::Battling::Creature* creature) const override;
private:
asIScriptObject* _scriptObject;
const AngelScriptResolver* _resolver;
struct FunctionInfo {
bool Exists = false;
asIScriptFunction* Function = nullptr;
};
FunctionInfo Initialize(const std::string& decl) {
auto val = _scriptObject->GetObjectType()->GetMethodByDecl(decl.c_str(), false);
if (val == nullptr) {
return FunctionInfo{.Exists = false, .Function = nullptr};
}
return FunctionInfo{.Exists = true, .Function = val};
}
#define ITEM_USE_SCRIPT_HOOK_FUNCTION(name, decl) FunctionInfo __##name = Initialize(decl);
ITEM_USE_SCRIPT_HOOK_FUNCTION(IsItemUsable, "bool IsItemUsable()");
ITEM_USE_SCRIPT_HOOK_FUNCTION(IsPokemonUseItem, "bool IsPokemonUseItem()");
ITEM_USE_SCRIPT_HOOK_FUNCTION(IsUseValidForPokemon, "bool IsUseValidForPokemon(Pokemon@ target)");
ITEM_USE_SCRIPT_HOOK_FUNCTION(IsHoldable, "bool IsHoldable()");
ITEM_USE_SCRIPT_HOOK_FUNCTION(OnUse, "void OnUse()");
ITEM_USE_SCRIPT_HOOK_FUNCTION(OnPokemonUse, "void OnPokemonUse(Pokemon@ target)");
};
#endif // PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP