added npc's gift
This commit is contained in:
1 parent
bc2997d86f
commit
34759326b9
9 files changed
+166
-15
No files matched your search
@@ -138,9 +138,64 @@ namespace ZL {
|
||||
return game->inventory.hasItem(id);
|
||||
});
|
||||
|
||||
// receive_npc_gift(npc_index)
|
||||
api.set_function("receive_npc_gift", [game](int npcIndex) {
|
||||
std::cout << "[script] receive_npc_gift: npc index " << npcIndex << std::endl;
|
||||
|
||||
auto& npcs = game->npcs;
|
||||
if (npcIndex < 0 || npcIndex >= static_cast<int>(npcs.size())) {
|
||||
std::cerr << "[script] receive_npc_gift: index out of range\n";
|
||||
return;
|
||||
}
|
||||
|
||||
auto& npc = npcs[npcIndex];
|
||||
if (!npc) {
|
||||
std::cerr << "[script] receive_npc_gift: npc is null\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (npc->giftReceived) {
|
||||
std::cout << "[script] NPC " << npc->npcName << " already gave gift\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (npc->giftItem.id.empty()) {
|
||||
std::cerr << "[script] receive_npc_gift: NPC has no gift\n";
|
||||
return;
|
||||
}
|
||||
|
||||
game->inventory.addItem(npc->giftItem);
|
||||
npc->giftReceived = true;
|
||||
|
||||
std::cout << "[script] Received gift from " << npc->npcName << ": "
|
||||
<< npc->giftItem.name << std::endl;
|
||||
});
|
||||
|
||||
lua.script_file("resources/start.lua");
|
||||
}
|
||||
|
||||
void ScriptEngine::callNpcInteractCallback(int npcIndex) {
|
||||
if (!impl) {
|
||||
std::cerr << "[SCRIPT] Engine not initialized!" << std::endl;
|
||||
return;
|
||||
}
|
||||
sol::state& lua = impl->lua;
|
||||
sol::function fn = lua["on_npc_interact"];
|
||||
if (fn.valid()) {
|
||||
auto result = fn(npcIndex);
|
||||
if (!result.valid()) {
|
||||
sol::error err = result;
|
||||
std::cerr << "[SCRIPT] on_npc_interact error: " << err.what() << "\n";
|
||||
}
|
||||
else {
|
||||
std::cout << "[SCRIPT] on_npc_interact called with index " << npcIndex << std::endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "[SCRIPT] Lua function 'on_npc_interact' not found!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::runScript(const std::string& path) {
|
||||
auto result = impl->lua.safe_script_file(path, sol::script_pass_on_error);
|
||||
if (!result.valid()) {
|
||||
|
||||
Reference in new issue
Block a user