]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Can now create entities using Lua scripting
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Thu, 29 Aug 2019 03:45:35 +0000 (23:45 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Thu, 29 Aug 2019 03:45:35 +0000 (23:45 -0400)
Scripts/init.lua
lib/entityx/entityx/Entity.h
src/main.cpp

index 4dfb3b93ca06605456d24bd598000a30a6560ea5..20ea044b9f0679d67824d063525fc702f5d3a55f 100644 (file)
@@ -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()
index f21737260c89977c6ccf7faecb4ddfda49a10faa..dfa40624b68a018d069b0e6bf449eaaee2b6483c 100644 (file)
@@ -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); }
index 266519ca94b1d655db4c1ec5d5f7c8b62ad5e959..57a4efbc5ec99cf04f740ce691438b032b1e34b3 100644 (file)
@@ -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);
 }