Some refactoring
This commit is contained in:
1 parent
f2f56125f0
commit
92ba3f2b60
7 files changed
+847
-485
No files matched your search
+22
-50
@@ -2,6 +2,7 @@
|
||||
#include "Game.h"
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include "Location.h"
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/sol.hpp>
|
||||
@@ -15,7 +16,7 @@ namespace ZL {
|
||||
ScriptEngine::ScriptEngine() = default;
|
||||
ScriptEngine::~ScriptEngine() = default;
|
||||
|
||||
void ScriptEngine::init(Game* game) {
|
||||
void ScriptEngine::init(Location* game, Inventory* inventory) {
|
||||
impl = std::make_unique<Impl>();
|
||||
sol::state& lua = impl->lua;
|
||||
|
||||
@@ -49,13 +50,15 @@ namespace ZL {
|
||||
});
|
||||
|
||||
// pickup_item(object_name)
|
||||
api.set_function("pickup_item", [game](const std::string& objectName) {
|
||||
api.set_function("pickup_item", [game, inventory](const std::string& objectName) {
|
||||
|
||||
|
||||
std::cout << "[script] pickup_item: " << objectName << std::endl;
|
||||
|
||||
for (auto& intObj : game->interactiveObjects) {
|
||||
if (intObj.id == objectName && intObj.isActive) {
|
||||
// Add item to inventory
|
||||
game->inventory.addItem(intObj.dropItem);
|
||||
inventory->addItem(intObj.dropItem);
|
||||
|
||||
// Deactivate object
|
||||
intObj.isActive = false;
|
||||
@@ -68,51 +71,18 @@ namespace ZL {
|
||||
});
|
||||
|
||||
|
||||
// show_inventory()
|
||||
api.set_function("show_inventory", [game]() {
|
||||
std::cout << "[script] show_inventory called" << std::endl;
|
||||
|
||||
game->menuManager.uiManager.setNodeVisible("inventory_items_panel", true);
|
||||
game->menuManager.uiManager.setNodeVisible("close_inventory_button", true);
|
||||
game->inventoryOpen = true;
|
||||
|
||||
// Update UI with current items
|
||||
const auto& items = game->inventory.getItems();
|
||||
std::string itemText;
|
||||
|
||||
if (items.empty()) {
|
||||
itemText = "Inventory (Empty)";
|
||||
}
|
||||
else {
|
||||
itemText = "Inventory (" + std::to_string(items.size()) + " items)\n\n";
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
itemText += std::to_string(i + 1) + ". " + items[i].name + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
game->menuManager.uiManager.setText("inventory_items_text", itemText);
|
||||
});
|
||||
|
||||
// hide_inventory()
|
||||
api.set_function("hide_inventory", [game]() {
|
||||
std::cout << "[script] hide_inventory called" << std::endl;
|
||||
game->menuManager.uiManager.setNodeVisible("inventory_items_panel", false);
|
||||
game->menuManager.uiManager.setNodeVisible("close_inventory_button", false);
|
||||
game->inventoryOpen = false;
|
||||
});
|
||||
|
||||
// add_item(item_id, name, description, icon)
|
||||
api.set_function("add_item", [game](const std::string& id, const std::string& name,
|
||||
api.set_function("add_item", [game, inventory](const std::string& id, const std::string& name,
|
||||
const std::string& description, const std::string& icon) {
|
||||
std::cout << "[script] add_item: " << name << std::endl;
|
||||
Item newItem(id, name, description, icon);
|
||||
game->inventory.addItem(newItem);
|
||||
inventory->addItem(newItem);
|
||||
});
|
||||
|
||||
// remove_item(item_id)
|
||||
api.set_function("remove_item", [game](const std::string& id) {
|
||||
api.set_function("remove_item", [game, inventory](const std::string& id) {
|
||||
std::cout << "[script] remove_item: " << id << std::endl;
|
||||
game->inventory.removeItem(id);
|
||||
inventory->removeItem(id);
|
||||
});
|
||||
|
||||
// deactivate_interactive_object(object_name)
|
||||
@@ -129,30 +99,31 @@ namespace ZL {
|
||||
});
|
||||
|
||||
// get_inventory_count()
|
||||
api.set_function("get_inventory_count", [game]() {
|
||||
return game->inventory.getCount();
|
||||
api.set_function("get_inventory_count", [inventory]() {
|
||||
return inventory->getCount();
|
||||
});
|
||||
|
||||
// has_item(item_id)
|
||||
api.set_function("has_item", [game](const std::string& id) {
|
||||
return game->inventory.hasItem(id);
|
||||
api.set_function("has_item", [inventory](const std::string& id) {
|
||||
return inventory->hasItem(id);
|
||||
});
|
||||
|
||||
api.set_function("start_dialogue",
|
||||
[game](const std::string& dialogueId) {
|
||||
/*----r
|
||||
if (!game->requestDialogueStart(dialogueId)) {
|
||||
std::cerr << "[script] start_dialogue failed for id: " << dialogueId << "\n";
|
||||
}
|
||||
}*/
|
||||
});
|
||||
|
||||
api.set_function("set_dialogue_flag",
|
||||
[game](const std::string& flag, int value) {
|
||||
game->setDialogueFlag(flag, value);
|
||||
//game->setDialogueFlag(flag, value); ---rrr
|
||||
});
|
||||
|
||||
api.set_function("get_dialogue_flag",
|
||||
[game](const std::string& flag) {
|
||||
return game->getDialogueFlag(flag);
|
||||
//return game->getDialogueFlag(flag); ---rrr
|
||||
});
|
||||
|
||||
api.set_function("set_navigation_area_available",
|
||||
@@ -164,7 +135,7 @@ namespace ZL {
|
||||
});
|
||||
|
||||
// receive_npc_gift(npc_index)
|
||||
api.set_function("receive_npc_gift", [game](int npcIndex) {
|
||||
api.set_function("receive_npc_gift", [game, inventory](int npcIndex) {
|
||||
std::cout << "[script] receive_npc_gift: npc index " << npcIndex << std::endl;
|
||||
|
||||
auto& npcs = game->npcs;
|
||||
@@ -188,7 +159,7 @@ namespace ZL {
|
||||
return;
|
||||
}
|
||||
|
||||
game->inventory.addItem(npc->giftItem);
|
||||
inventory->addItem(npc->giftItem);
|
||||
npc->giftReceived = true;
|
||||
|
||||
std::cout << "[script] Received gift from " << npc->npcName << ": "
|
||||
@@ -284,6 +255,7 @@ namespace ZL {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void ScriptEngine::showInventory(Game* game) {
|
||||
std::cout << "[script] toggle_inventory called" << std::endl;
|
||||
|
||||
@@ -309,6 +281,6 @@ namespace ZL {
|
||||
|
||||
game->menuManager.uiManager.setText("inventory_items_text", itemText);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
} // namespace ZL
|
||||
Reference in new issue
Block a user