diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-10-01 20:50:41 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-10-01 20:50:41 -0400 |
commit | 9b81db1fe44bf13d215cf2700495f2a8710a8ade (patch) | |
tree | 17a406810f13cf271fe34c41006454cb81b55c1d | |
parent | af39f2e08b0503db723ae707a5c7278d8c85f812 (diff) | |
parent | ad5a63db312d0029109e6ca0051feaa516419ad2 (diff) |
Merge branch 'master' of https://github.com/tcsullivan/gamedev2 into audio
-rw-r--r-- | src/engine.cpp | 10 | ||||
-rw-r--r-- | src/script.cpp | 11 | ||||
-rw-r--r-- | src/script.hpp | 15 |
3 files changed, 22 insertions, 14 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index 3fc4fdd..ed5ba07 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -61,13 +61,11 @@ int Engine::init(void) // Load game script and entity data auto* script = systems.system<ScriptSystem>().get(); script->addToGameNamespace("loadFont", - [this](std::string name, std::string file, int size) { - systems.system<TextSystem>().get()->loadFont(name, file, size); - }); + bindInstance(&TextSystem::loadFont, + systems.system<TextSystem>().get())); script->addToGameNamespace("puts", - [this](std::string name, float x, float y, std::string text) { - systems.system<TextSystem>().get()->put(name, x, y, text); - }); + bindInstance(&TextSystem::put, + systems.system<TextSystem>().get())); script->init(); diff --git a/src/script.cpp b/src/script.cpp index 20011b4..9fae1c9 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -88,12 +88,6 @@ void ScriptSystem::doFile(void) void ScriptSystem::scriptExport(void) { - std::function<sol::table(sol::table)> entitySpawn = - [this](sol::table t){ return spawn(t);}; - - std::function<World* (sol::object)> worldRegister = - [this](sol::object t){ return worldSystem.addWorld(t); }; - lua.new_usertype<Position>("Position", sol::constructors<Position(double x, double y), Position()>(), "x", &Position::x, @@ -142,8 +136,9 @@ void ScriptSystem::scriptExport(void) "getSize", &World::getSize); game = lua["game"].get_or_create<sol::table>(); - game.set_function("spawn", entitySpawn); - game.set_function("worldRegister", worldRegister); + game.set_function("spawn", bindInstance(&ScriptSystem::spawn, this)); + game.set_function("worldRegister", bindInstance(&WorldSystem::addWorld, + &worldSystem)); } sol::table ScriptSystem::spawn(sol::object param) diff --git a/src/script.hpp b/src/script.hpp index 0ac9e63..24cc142 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -26,6 +26,21 @@ #include "world.hpp" +/** + * Utility for pairing class instances to their member function calls. + * This is useful for adding functions to the Lua game namespace. + * + * @param func The member function to call + * @param instance The instance to bind to + * @return A function that calls the member function using the given instance + */ +template<class C, typename R, typename... Args> +auto bindInstance(R (C::* func)(Args...), C *instance) +{ + return [instance, func](Args... args) { (instance->*func)(args...); }; +} + + struct EntitySpawnEvent { sol::object ref; |