Adding scripts
This commit is contained in:
1 parent
995e2c8e5b
commit
63c9d7b35b
9 files changed
+432480
-9
No files matched your search
@@ -0,0 +1,62 @@
|
||||
#include "ScriptEngine.h"
|
||||
#include "Game.h"
|
||||
#include <pybind11/embed.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
// Static pointer used by the embedded module (set before interpreter starts).
|
||||
static ZL::Game* s_game = nullptr;
|
||||
|
||||
// The embedded module must be defined at file scope before the interpreter
|
||||
// is started. It exposes a minimal surface: one function per scripting command.
|
||||
PYBIND11_EMBEDDED_MODULE(game_api, m) {
|
||||
m.doc() = "Game scripting API";
|
||||
|
||||
// npc_walk_to(index, x, y, z)
|
||||
// Tells the NPC at `index` in Game::npcs to walk to world position (x,y,z).
|
||||
m.def("npc_walk_to", [](int index, float x, float y, float z) {
|
||||
if (!s_game) {
|
||||
std::cerr << "[script] game_api.npc_walk_to: engine not ready\n";
|
||||
return;
|
||||
}
|
||||
auto& npcs = s_game->npcs;
|
||||
if (index < 0 || index >= static_cast<int>(npcs.size())) {
|
||||
std::cerr << "[script] game_api.npc_walk_to: index " << index
|
||||
<< " out of range (0.." << npcs.size() - 1 << ")\n";
|
||||
return;
|
||||
}
|
||||
npcs[index]->setTarget(Eigen::Vector3f(x, y, z));
|
||||
},
|
||||
py::arg("index"), py::arg("x"), py::arg("y"), py::arg("z"),
|
||||
"Command NPC[index] to walk to world position (x, y, z).");
|
||||
}
|
||||
|
||||
namespace ZL {
|
||||
|
||||
ScriptEngine::ScriptEngine() = default;
|
||||
ScriptEngine::~ScriptEngine() = default;
|
||||
|
||||
void ScriptEngine::init(Game* game) {
|
||||
s_game = game;
|
||||
interpreter = std::make_unique<py::scoped_interpreter>();
|
||||
runScript("resources/start.py");
|
||||
}
|
||||
|
||||
void ScriptEngine::runScript(const std::string& path) {
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "[script] Could not open script: " << path << "\n";
|
||||
return;
|
||||
}
|
||||
std::string source((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
try {
|
||||
py::exec(source);
|
||||
} catch (const py::error_already_set& e) {
|
||||
std::cerr << "[script] Error in " << path << ":\n" << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
Reference in new issue
Block a user