Added startup scripts
This commit is contained in:
1 parent
a479fa4ac1
commit
13ef92160d
26 files changed
+117164
-150
No files matched your search
@@ -2,6 +2,7 @@
|
||||
#include "Game.h"
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include "Location.h"
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
@@ -11,6 +12,8 @@ namespace ZL {
|
||||
|
||||
struct ScriptEngine::Impl {
|
||||
sol::state lua;
|
||||
std::unordered_map<std::string, sol::protected_function> triggerEnterCallbacks;
|
||||
std::unordered_map<std::string, sol::protected_function> triggerExitCallbacks;
|
||||
};
|
||||
|
||||
ScriptEngine::ScriptEngine() = default;
|
||||
@@ -133,6 +136,24 @@ namespace ZL {
|
||||
}
|
||||
});
|
||||
|
||||
api.set_function("switch_navigation",
|
||||
[game](int index) {
|
||||
if (!game->switchNavigation(index)) {
|
||||
std::cerr << "[script] switch_navigation: index " << index << " out of range\n";
|
||||
}
|
||||
});
|
||||
|
||||
// set_trigger_zone_callbacks(zone_id, on_enter, on_exit)
|
||||
// on_enter and on_exit are optional Lua functions (pass nil to omit).
|
||||
// Called when the player enters or exits the named trigger zone.
|
||||
api.set_function("set_trigger_zone_callbacks",
|
||||
[this_impl = impl.get()](const std::string& zoneId, sol::object onEnter, sol::object onExit) {
|
||||
if (onEnter.is<sol::protected_function>())
|
||||
this_impl->triggerEnterCallbacks[zoneId] = onEnter.as<sol::protected_function>();
|
||||
if (onExit.is<sol::protected_function>())
|
||||
this_impl->triggerExitCallbacks[zoneId] = onExit.as<sol::protected_function>();
|
||||
});
|
||||
|
||||
// receive_npc_gift(npc_index)
|
||||
api.set_function("receive_npc_gift", [game, inventory](int npcIndex) {
|
||||
std::cout << "[script] receive_npc_gift: npc index " << npcIndex << std::endl;
|
||||
@@ -254,4 +275,26 @@ namespace ZL {
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::callTriggerEnterCallback(const std::string& zoneId) {
|
||||
if (!impl) return;
|
||||
auto it = impl->triggerEnterCallbacks.find(zoneId);
|
||||
if (it == impl->triggerEnterCallbacks.end()) return;
|
||||
auto result = it->second();
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cerr << "[SCRIPT] trigger enter callback error for '" << zoneId << "': " << err.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::callTriggerExitCallback(const std::string& zoneId) {
|
||||
if (!impl) return;
|
||||
auto it = impl->triggerExitCallbacks.find(zoneId);
|
||||
if (it == impl->triggerExitCallbacks.end()) return;
|
||||
auto result = it->second();
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cerr << "[SCRIPT] trigger exit callback error for '" << zoneId << "': " << err.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
Reference in new issue
Block a user