Working on scripts

This commit is contained in:
Vladislav Khorev committed 2026-05-17 14:39:02 +03:00
1 parent c086fe1ef0
commit 4044acefb2
35 files changed
+107190 -185

No files matched your search

+102
View File
@@ -16,6 +16,11 @@ namespace ZL {
std::unordered_map<std::string, sol::protected_function> triggerEnterCallbacks;
std::unordered_map<std::string, sol::protected_function> triggerExitCallbacks;
std::unordered_map<std::string, sol::protected_function> cutsceneCompleteCallbacks;
std::unordered_map<std::string, int>* globalInts = nullptr;
sol::protected_function locationEnterCallback;
sol::protected_function locationExitCallback;
sol::protected_function darklandsEnterCallback;
sol::protected_function darklandsExitCallback;
};
ScriptEngine::ScriptEngine() = default;
@@ -163,6 +168,63 @@ namespace ZL {
this_impl->triggerExitCallbacks[zoneId] = onExit.as<sol::protected_function>();
});
// start_darklands_transition()
// Triggers the white-flash transition to toggle darklands mode.
// Does nothing if a transition is already in progress.
api.set_function("start_darklands_transition",
[game]() {
if (game->requestDarklandsTransition)
game->requestDarklandsTransition();
});
// set_location_callbacks(on_enter, on_exit)
// on_enter() called once when the player arrives at this location.
// on_exit() called once just before the player leaves this location.
api.set_function("set_location_callbacks",
[this_impl = impl.get()](sol::object onEnter, sol::object onExit) {
if (onEnter.is<sol::protected_function>())
this_impl->locationEnterCallback = onEnter.as<sol::protected_function>();
if (onExit.is<sol::protected_function>())
this_impl->locationExitCallback = onExit.as<sol::protected_function>();
});
// is_darklands() → bool
api.set_function("is_darklands",
[game]() { return game->isDarklands; });
// set_player_hp(value) — sets the player's current HP directly.
api.set_function("set_player_hp",
[game](float value) {
if (game->player)
game->player->hp = value;
});
// Global integer store — shared across all location scripts.
// setIntValue(key, value) / getIntValue(key) → int (0 if not set)
api.set_function("setIntValue",
[this_impl = impl.get()](const std::string& key, int value) {
if (this_impl->globalInts)
(*this_impl->globalInts)[key] = value;
});
api.set_function("getIntValue",
[this_impl = impl.get()](const std::string& key) -> int {
if (!this_impl->globalInts) return 0;
auto it = this_impl->globalInts->find(key);
return it != this_impl->globalInts->end() ? it->second : 0;
});
// set_darklands_callbacks(on_enter, on_exit)
// on_enter() called when the player switches into Darklands mode.
// on_exit() called when the player switches back to normal mode.
// Pass nil to omit either callback.
api.set_function("set_darklands_callbacks",
[this_impl = impl.get()](sol::object onEnter, sol::object onExit) {
if (onEnter.is<sol::protected_function>())
this_impl->darklandsEnterCallback = onEnter.as<sol::protected_function>();
if (onExit.is<sol::protected_function>())
this_impl->darklandsExitCallback = onExit.as<sol::protected_function>();
});
api.set_function("set_trigger_zone_enabled",
[game](int index, bool value) {
auto& triggerZones = game->triggerZones;
@@ -405,6 +467,46 @@ namespace ZL {
}
}
void ScriptEngine::setGlobalStore(std::unordered_map<std::string, int>* store) {
if (impl) impl->globalInts = store;
}
void ScriptEngine::callLocationEnterCallback() {
if (!impl || !impl->locationEnterCallback.valid()) return;
auto result = impl->locationEnterCallback();
if (!result.valid()) {
sol::error err = result;
std::cerr << "[SCRIPT] location enter callback error: " << err.what() << "\n";
}
}
void ScriptEngine::callLocationExitCallback() {
if (!impl || !impl->locationExitCallback.valid()) return;
auto result = impl->locationExitCallback();
if (!result.valid()) {
sol::error err = result;
std::cerr << "[SCRIPT] location exit callback error: " << err.what() << "\n";
}
}
void ScriptEngine::callDarklandsEnterCallback() {
if (!impl || !impl->darklandsEnterCallback.valid()) return;
auto result = impl->darklandsEnterCallback();
if (!result.valid()) {
sol::error err = result;
std::cerr << "[SCRIPT] darklands enter callback error: " << err.what() << "\n";
}
}
void ScriptEngine::callDarklandsExitCallback() {
if (!impl || !impl->darklandsExitCallback.valid()) return;
auto result = impl->darklandsExitCallback();
if (!result.valid()) {
sol::error err = result;
std::cerr << "[SCRIPT] darklands exit callback error: " << err.what() << "\n";
}
}
void ScriptEngine::callCutsceneCompleteCallback(const std::string& cutsceneId) {
if (!impl) return;
auto it = impl->cutsceneCompleteCallbacks.find(cutsceneId);