added activeFunc for active obj
This commit is contained in:
parent
943b6b4d21
commit
bc2997d86f
@ -22,18 +22,7 @@ step1()
|
|||||||
|
|
||||||
function on_health_pickup()
|
function on_health_pickup()
|
||||||
game_api.pickup_item("health_potion")
|
game_api.pickup_item("health_potion")
|
||||||
|
print("[Lua] Health potion picked up!")
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_item_pickup(object_name)
|
|
||||||
print("========================================")
|
|
||||||
print("on_item_pickup CALLBACK CALLED!")
|
|
||||||
print("Player picked up item from: " .. tostring(object_name))
|
|
||||||
print("========================================")
|
|
||||||
|
|
||||||
game_api.pickup_item(object_name)
|
|
||||||
|
|
||||||
--game_api.show_inventory()
|
|
||||||
end
|
|
||||||
|
|
||||||
print("Lua script loaded successfully!")
|
|
||||||
19
src/Game.cpp
19
src/Game.cpp
@ -174,7 +174,6 @@ namespace ZL
|
|||||||
npcs = GameObjectLoader::loadAndCreateNpcs("resources/config2/npcs.json", CONST_ZIP_FILE);
|
npcs = GameObjectLoader::loadAndCreateNpcs("resources/config2/npcs.json", CONST_ZIP_FILE);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// === СТАРЫЙ КОД - ЗАКОММЕНТИРОВАНО, НПС ТЕПЕРЬ ГРУЗЯТСЯ ИЗ JSON ===
|
|
||||||
|
|
||||||
auto defaultTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/default_skin001.png", CONST_ZIP_FILE));
|
auto defaultTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/default_skin001.png", CONST_ZIP_FILE));
|
||||||
|
|
||||||
@ -214,8 +213,6 @@ namespace ZL
|
|||||||
npcs.push_back(std::move(npc02));
|
npcs.push_back(std::move(npc02));
|
||||||
|
|
||||||
std::cout << "Load resurces step 12" << std::endl;
|
std::cout << "Load resurces step 12" << std::endl;
|
||||||
|
|
||||||
// === КОНЕЦ СТАРОГО КОДА ===
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
loadingCompleted = true;
|
loadingCompleted = true;
|
||||||
@ -459,8 +456,20 @@ namespace ZL
|
|||||||
std::cout << "[PICKUP] Player reached object! Distance: " << distToObject << std::endl;
|
std::cout << "[PICKUP] Player reached object! Distance: " << distToObject << std::endl;
|
||||||
std::cout << "[PICKUP] Calling Lua callback for: " << targetInteractiveObject->id << std::endl;
|
std::cout << "[PICKUP] Calling Lua callback for: " << targetInteractiveObject->id << std::endl;
|
||||||
|
|
||||||
// Call Lua callback
|
// Call custom activate function if specified, otherwise use fallback
|
||||||
scriptEngine.callItemPickupCallback(targetInteractiveObject->id);
|
try {
|
||||||
|
if (!targetInteractiveObject->activateFunctionName.empty()) {
|
||||||
|
std::cout << "[PICKUP] Using custom function: " << targetInteractiveObject->activateFunctionName << std::endl;
|
||||||
|
scriptEngine.callActivateFunction(targetInteractiveObject->activateFunctionName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "[PICKUP] Using fallback callback" << std::endl;
|
||||||
|
scriptEngine.callItemPickupCallback(targetInteractiveObject->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
std::cerr << "[PICKUP] Error calling function: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
targetInteractiveObject = nullptr;
|
targetInteractiveObject = nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include "ScriptEngine.h"
|
#include "ScriptEngine.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#define SOL_ALL_SAFETIES_ON 1
|
#define SOL_ALL_SAFETIES_ON 1
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
@ -148,6 +149,62 @@ namespace ZL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptEngine::callActivateFunction(const std::string& functionName) {
|
||||||
|
if (!impl) {
|
||||||
|
throw std::runtime_error("[SCRIPT] Engine not initialized!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (functionName.empty()) {
|
||||||
|
throw std::runtime_error("[SCRIPT] Activate function name is empty!");
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::state& lua = impl->lua;
|
||||||
|
std::cout << "[SCRIPT] Looking for activate function: " << functionName << std::endl;
|
||||||
|
|
||||||
|
sol::function activateFunc = lua[functionName];
|
||||||
|
|
||||||
|
if (!activateFunc.valid()) {
|
||||||
|
throw std::runtime_error("[SCRIPT] Lua function not found: " + functionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[SCRIPT] Found function! Calling: " << functionName << std::endl;
|
||||||
|
auto result = activateFunc();
|
||||||
|
|
||||||
|
if (!result.valid()) {
|
||||||
|
sol::error err = result;
|
||||||
|
throw std::runtime_error("[SCRIPT] Error executing " + functionName + ": " + std::string(err.what()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[SCRIPT] Function executed successfully!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptEngine::callItemPickupCallback(const std::string& objectName) {
|
||||||
|
if (!impl) {
|
||||||
|
std::cerr << "[SCRIPT] impl is null!" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::state& lua = impl->lua;
|
||||||
|
|
||||||
|
// Try to find custom activate function first
|
||||||
|
sol::function activateFunc = lua["on_item_pickup"];
|
||||||
|
|
||||||
|
if (activateFunc.valid()) {
|
||||||
|
std::cout << "[SCRIPT] Callback found! Calling with argument: " << objectName << std::endl;
|
||||||
|
auto result = activateFunc(objectName);
|
||||||
|
if (!result.valid()) {
|
||||||
|
sol::error err = result;
|
||||||
|
std::cerr << "[SCRIPT] on_item_pickup callback error: " << err.what() << "\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "[SCRIPT] Callback executed successfully!" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "[SCRIPT] Fallback: on_item_pickup not found" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptEngine::showInventory(Game* game) {
|
void ScriptEngine::showInventory(Game* game) {
|
||||||
std::cout << "[script] toggle_inventory called" << std::endl;
|
std::cout << "[script] toggle_inventory called" << std::endl;
|
||||||
|
|
||||||
@ -157,7 +214,6 @@ namespace ZL {
|
|||||||
|
|
||||||
game->inventoryOpen = !isVisible;
|
game->inventoryOpen = !isVisible;
|
||||||
|
|
||||||
// Update UI with current items only if showing
|
|
||||||
if (!isVisible) {
|
if (!isVisible) {
|
||||||
const auto& items = game->inventory.getItems();
|
const auto& items = game->inventory.getItems();
|
||||||
std::string itemText;
|
std::string itemText;
|
||||||
@ -176,31 +232,4 @@ namespace ZL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEngine::callItemPickupCallback(const std::string& objectName) {
|
|
||||||
if (!impl) {
|
|
||||||
std::cerr << "[SCRIPT] impl is null!" << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sol::state& lua = impl->lua;
|
|
||||||
std::cout << "[SCRIPT] Looking for callback: on_item_pickup" << std::endl;
|
|
||||||
|
|
||||||
sol::function pickup = lua["on_item_pickup"];
|
|
||||||
|
|
||||||
if (pickup.valid()) {
|
|
||||||
std::cout << "[SCRIPT] Callback found! Calling with argument: " << objectName << std::endl;
|
|
||||||
auto result = pickup(objectName);
|
|
||||||
if (!result.valid()) {
|
|
||||||
sol::error err = result;
|
|
||||||
std::cerr << "[SCRIPT] on_item_pickup callback error: " << err.what() << "\n";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cout << "[SCRIPT] Callback executed successfully!" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cerr << "[SCRIPT] on_item_pickup callback not found!" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ZL
|
} // namespace ZL
|
||||||
|
|||||||
@ -18,6 +18,9 @@ public:
|
|||||||
void runScript(const std::string& path);
|
void runScript(const std::string& path);
|
||||||
|
|
||||||
void callItemPickupCallback(const std::string& objectName);
|
void callItemPickupCallback(const std::string& objectName);
|
||||||
|
|
||||||
|
void callActivateFunction(const std::string& functionName);
|
||||||
|
|
||||||
void showInventory(Game* game);
|
void showInventory(Game* game);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -61,6 +61,8 @@ namespace ZL {
|
|||||||
data.interactionRadius = itemData.value("radius", 2.0f);
|
data.interactionRadius = itemData.value("radius", 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data.activateFunctionName = item.value("activateFunction", "");
|
||||||
|
|
||||||
if (!data.meshPath.empty()) {
|
if (!data.meshPath.empty()) {
|
||||||
objects.push_back(data);
|
objects.push_back(data);
|
||||||
}
|
}
|
||||||
@ -158,9 +160,11 @@ namespace ZL {
|
|||||||
std::cout << "Loading interactive object: " << objData.name << std::endl;
|
std::cout << "Loading interactive object: " << objData.name << std::endl;
|
||||||
|
|
||||||
InteractiveObject intObj;
|
InteractiveObject intObj;
|
||||||
intObj.id = objData.name;
|
//intObj.id = objData.name;
|
||||||
|
intObj.id = !objData.itemId.empty() ? objData.itemId : objData.name;
|
||||||
intObj.name = objData.name;
|
intObj.name = objData.name;
|
||||||
intObj.interactionRadius = objData.interactionRadius;
|
intObj.interactionRadius = objData.interactionRadius;
|
||||||
|
intObj.activateFunctionName = objData.activateFunctionName;
|
||||||
|
|
||||||
// Load texture
|
// Load texture
|
||||||
try {
|
try {
|
||||||
@ -237,7 +241,11 @@ namespace ZL {
|
|||||||
|
|
||||||
interactiveObjects.push_back(intObj);
|
interactiveObjects.push_back(intObj);
|
||||||
|
|
||||||
std::cout << "Successfully loaded interactive: " << objData.name << " (item: " << objData.itemName << ")" << std::endl;
|
std::cout << "Successfully loaded interactive: " << objData.name << " (item: " << objData.itemName;
|
||||||
|
if (!objData.activateFunctionName.empty()) {
|
||||||
|
std::cout << ", function: " << objData.activateFunctionName;
|
||||||
|
}
|
||||||
|
std::cout << ")" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Total interactive objects loaded: " << interactiveObjects.size() << std::endl;
|
std::cout << "Total interactive objects loaded: " << interactiveObjects.size() << std::endl;
|
||||||
|
|||||||
@ -31,6 +31,7 @@ namespace ZL {
|
|||||||
std::string itemDescription;
|
std::string itemDescription;
|
||||||
std::string itemIcon;
|
std::string itemIcon;
|
||||||
float interactionRadius = 2.0f;
|
float interactionRadius = 2.0f;
|
||||||
|
std::string activateFunctionName;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct NpcData {
|
struct NpcData {
|
||||||
|
|||||||
@ -20,6 +20,17 @@ namespace ZL {
|
|||||||
VertexRenderStruct mesh;
|
VertexRenderStruct mesh;
|
||||||
Item dropItem;
|
Item dropItem;
|
||||||
bool isActive = true;
|
bool isActive = true;
|
||||||
|
|
||||||
|
bool isNpc = false;
|
||||||
|
std::string npcInteractCallback;
|
||||||
|
float walkSpeed = 1.5f;
|
||||||
|
float rotationSpeed = 8.0f;
|
||||||
|
float modelScale = 0.01f;
|
||||||
|
std::string animationIdlePath;
|
||||||
|
std::string animationWalkPath;
|
||||||
|
std::string texturePath;
|
||||||
|
|
||||||
|
std::string activateFunctionName;
|
||||||
|
|
||||||
InteractiveObject() : interactionRadius(2.0f) {}
|
InteractiveObject() : interactionRadius(2.0f) {}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user