mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-14 16:09:06 +07:00
add loading behaviour script asset
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#include "../core/_all.hpp"
|
||||
|
||||
#include <ecs.hpp/ecs.hpp>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <3rdparty/sol/sol.hpp>
|
||||
|
||||
namespace e2d
|
||||
|
||||
@@ -9,12 +9,19 @@
|
||||
#include "../_high.hpp"
|
||||
|
||||
#include "../factory.hpp"
|
||||
#include "../assets/script_asset.hpp"
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
class behaviour final {
|
||||
public:
|
||||
behaviour() = default;
|
||||
behaviour(const script_asset::ptr& script);
|
||||
|
||||
behaviour& script(const script_asset::ptr& value) noexcept;
|
||||
[[nodiscard]] const script_asset::ptr& script() const noexcept;
|
||||
private:
|
||||
script_asset::ptr script_;
|
||||
};
|
||||
|
||||
template <>
|
||||
@@ -31,3 +38,18 @@ namespace e2d
|
||||
const collect_context& ctx) const;
|
||||
};
|
||||
}
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
inline behaviour::behaviour(const script_asset::ptr& value)
|
||||
: script_(value) {}
|
||||
|
||||
inline behaviour& behaviour::script(const script_asset::ptr& value) noexcept {
|
||||
script_ = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const script_asset::ptr& behaviour::script() const noexcept {
|
||||
return script_;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,16 +8,38 @@
|
||||
|
||||
#include "_high.hpp"
|
||||
|
||||
#include "script.hpp"
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
class luasol final : public module<luasol> {
|
||||
public:
|
||||
luasol();
|
||||
~luasol() noexcept final;
|
||||
~luasol() noexcept final = default;
|
||||
|
||||
sol::state& lua() noexcept;
|
||||
const sol::state& lua() const noexcept;
|
||||
template < typename F >
|
||||
decltype(auto) with_state(F&& f);
|
||||
template < typename F >
|
||||
decltype(auto) with_state(F&& f) const;
|
||||
|
||||
std::optional<script> load_script(buffer_view src);
|
||||
std::optional<script> load_script(const input_stream_uptr& src);
|
||||
private:
|
||||
sol::state lua_;
|
||||
sol::state state_;
|
||||
};
|
||||
}
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
template < typename F >
|
||||
decltype(auto) luasol::with_state(F&& f) {
|
||||
E2D_ASSERT(is_in_main_thread());
|
||||
return std::invoke(std::forward<F>(f), state_);
|
||||
}
|
||||
|
||||
template < typename F >
|
||||
decltype(auto) luasol::with_state(F&& f) const {
|
||||
E2D_ASSERT(is_in_main_thread());
|
||||
return std::invoke(std::forward<F>(f), state_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,13 @@
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
class bad_script_access final : public exception {
|
||||
public:
|
||||
const char* what() const noexcept final {
|
||||
return "bad script access";
|
||||
}
|
||||
};
|
||||
|
||||
class script final {
|
||||
public:
|
||||
script() = default;
|
||||
@@ -21,14 +28,35 @@ namespace e2d
|
||||
script(const script& other);
|
||||
script& operator=(const script& other);
|
||||
|
||||
void clear() noexcept;
|
||||
void swap(script& other) noexcept;
|
||||
script(sol::protected_function&& func) noexcept;
|
||||
script(const sol::protected_function& func);
|
||||
|
||||
script& assign(script&& other) noexcept;
|
||||
script& assign(const script& other);
|
||||
|
||||
script& assign(sol::protected_function&& func) noexcept;
|
||||
script& assign(const sol::protected_function& func);
|
||||
|
||||
void clear() noexcept;
|
||||
void swap(script& other) noexcept;
|
||||
bool empty() const noexcept;
|
||||
|
||||
template < typename... Ret, typename... Args >
|
||||
decltype(auto) call(Args&&... args) const;
|
||||
private:
|
||||
std::optional<sol::protected_function> func_;
|
||||
};
|
||||
|
||||
void swap(script& l, script& r) noexcept;
|
||||
bool operator==(const script& l, const script& r) noexcept;
|
||||
bool operator!=(const script& l, const script& r) noexcept;
|
||||
}
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
template < typename... Ret, typename... Args >
|
||||
decltype(auto) script::call(Args&&... args) const {
|
||||
if ( !func_ ) {
|
||||
throw bad_script_access();
|
||||
}
|
||||
return func_->call<Ret...>(std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user