diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-08-28 23:45:35 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-08-28 23:45:35 -0400 |
commit | 41addc2d6b600e9cade63884de1c13dba662dc31 (patch) | |
tree | e7aa56cfc2f043cf99f25a337900bf17ab30e537 | |
parent | 85fb2bff38b2ef6cb17e86c5f602ee09a365b117 (diff) |
Can now create entities using Lua scripting
-rw-r--r-- | Scripts/init.lua | 13 | ||||
-rw-r--r-- | lib/entityx/entityx/Entity.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 63 |
3 files changed, 55 insertions, 23 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua index 4dfb3b9..20ea044 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -13,3 +13,16 @@ local q = comp.Position(6, 3) print(q.x .. "," .. q.y) print("HEY") + +bird = { + Position = { + x = 1.2, + y = 3.4 + }, + init = function(self) + print(self.Position.x .. "," .. self.Position.y) + end +} + +birdSpawn = game.spawn(bird); +birdSpawn:init() diff --git a/lib/entityx/entityx/Entity.h b/lib/entityx/entityx/Entity.h index f217372..dfa4062 100644 --- a/lib/entityx/entityx/Entity.h +++ b/lib/entityx/entityx/Entity.h @@ -437,7 +437,7 @@ class EntityManager : entityx::help::NonCopyable { ViewIterator<Iterator, All>::next(); } - void next_entity(Entity &entity) {} + void next_entity([[maybe_unused]]Entity &entity) {} }; Iterator begin() { return Iterator(manager_, mask_, 0); } diff --git a/src/main.cpp b/src/main.cpp index 266519c..57a4efb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -148,27 +148,45 @@ struct Position : entityx::Component<Position> float x,y; }; +using namespace entityx; +using namespace entityx::lua; +namespace lb = luabridge; -void LuaTest(void) +EventManager events; +EntityManager entities(events); + +lua_State* L; + +lb::LuaRef spawn(lb::LuaRef ref) { - using namespace entityx; - using namespace entityx::lua; - namespace lb = luabridge; + lb::LuaRef entity(L); + entity = lb::newTable(L); - std::function<Position(float, float)> ctor = [](float x, float y) { - return Position(x, y); - }; + if (ref.isTable()) { + + Entity e = entities.create(); + + for (auto &&comp : lb::pairs(ref)) { + if (comp.first.cast<std::string>() == "Position") { + float x = comp.second["x"]; + float y = comp.second["y"]; + entity["Position"] = e.assign<Position>(x, y).get(); + } else if (comp.first.cast<std::string>() == "init") { + entity["init"] = comp.second; + } + } + } else { + std::cerr << "Parameter to spawn() must be a table!" << std::endl; + } + + return entity; +} - ctor(6,7); - //export_component<Position>("Position", ctor, - // [](MemberRegister<Position>& m) { - // m.add("x", &Position::x); - // m.add("y", &Position::y); - // } - //); +void LuaTest(void) +{ - lua_State* L = luaL_newstate(); + L = luaL_newstate(); luaL_openlibs(L); lb::getGlobalNamespace(L). @@ -180,16 +198,17 @@ void LuaTest(void) .endClass() .endNamespace(); - setup_entityx_api(L); - - std::shared_ptr<EventManager> events(new EventManager()); - std::shared_ptr<EntityManager> entities(new EntityManager(*events)); - new_entity_manager(L, entities, "manager"); + lb::getGlobalNamespace(L) + .beginNamespace("game") + .addFunction("spawn", spawn) + .endNamespace(); - if (luaL_dofile(L, "Scripts/init.lua")) { - std::cout << "Lua Error: " << lua_tostring(L, -1) << std::endl; + if (luaL_dofile(L, "Scripts/init.lua")) + { + std::cout << "Lua error: " << lua_tostring(L, -1) << std::endl; } + entities.each<Position>([&](Entity e, Position& p){ (void)e;std::cout << p.x << std::endl;}); lua_close(L); } |