PkmnLib/src/ScriptResolving/AngelScript/AngelScriptItemUseScript.hpp

63 lines
2.5 KiB
C++

#ifndef PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP
#define PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP
#include <CreatureLib/Battling/ScriptHandling/ItemUseScript.hpp>
#include <scriptarray/scriptarray.h>
#include "TypeRegistry/NativeArray.hpp"
class AngelScriptResolver;
class AngelScriptItemUseScript final : public CreatureLib::Battling::ItemUseScript {
public:
AngelScriptItemUseScript(asIScriptObject* scriptObject, AngelScriptResolver* resolver)
: _scriptObject(scriptObject), _resolver(resolver) {}
~AngelScriptItemUseScript() {
if (_scriptObject != nullptr) {
_scriptObject->Release();
}
}
void OnInitialize(const ArbUt::List<CreatureLib::Library::EffectParameter*>& parameters) override;
[[nodiscard]] bool IsItemUsable() const override;
[[nodiscard]] bool IsCreatureUseItem() const override;
bool IsUseValidForCreature(CreatureLib::Battling::Creature* creature) const override;
bool IsHoldable() const override;
void OnUse(CreatureLib::Battling::Battle* battle) const override;
void OnCreatureUse(CreatureLib::Battling::Creature* creature, bool isBattle) const override;
private:
asIScriptObject* _scriptObject;
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};
}
NativeArray<ArbUt::List<CreatureLib::Library::EffectParameter*>>*
GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls);
#define ITEM_USE_SCRIPT_HOOK_FUNCTION(name, decl) FunctionInfo __##name = Initialize(decl);
ITEM_USE_SCRIPT_HOOK_FUNCTION(OnInitialize, "void OnInitialize(const BattleLibrary@ library, const narray<EffectParameter@>@ parameters)");
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