]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added Name and Render components to Lua
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Fri, 30 Aug 2019 04:14:27 +0000 (00:14 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Fri, 30 Aug 2019 04:14:27 +0000 (00:14 -0400)
Scripts/init.lua
src/components/Position.hpp
src/script.cpp
src/script.hpp

index 82c2486379e59088aaf086c90f54305660522e4f..3ec46185a08144d31b33d3f20dcf00e88a1d4b7d 100644 (file)
@@ -15,6 +15,7 @@ bird = {
         x = 1.2,
         y = 3.4
     },
+    Name = "bord",
     init = function(self)
         print(self.Position.x .. "," .. self.Position.y)
     end
@@ -25,14 +26,29 @@ dog = {
         x = 6.5,
         y = 1.3
     },
+    Render = {
+        texture = "assets/tex.png",
+        visible = true
+    },
     init = function(self)
         print(self.Position.x .. "," .. self.Position.y)
     end
 }
 
+animal = {
+    Render = {
+        texture = "assets/anim.png",
+        visible = false
+    }
+}
+
 birdSpawn = game.spawn(bird);
 birdSpawn:init()
+print(birdSpawn.Name.value)
 
 dogSpawn = game.spawn(dog);
 dogSpawn:init()
 dogSpawn.Position.x = 37.5
+
+animalSpawn = game.spawn(animal);
+animalSpawn.Render.texture = "assets/newText.png"
index 8684a7f15edce4058674d9455474100c7f6ddbe5..1c6cf714db7700a6b48cc66a9bb792631d2dae4b 100644 (file)
@@ -1,4 +1,5 @@
 /**
+
  * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -26,11 +27,11 @@ struct Position : Component<Position>, entityx::Component<Position>
     public:
         float x,y;
         Position(float _x, float _y): x(_x), y(_y) {}
-        Position(void){x = y = 0.0;}
+        Position(void): x(0), y(0) {}
 
         Position FromLua(sol::object ref)
         {
-            if (ref.get_type() == sol::type::table){
+            if (ref.get_type() == sol::type::table) {
                 sol::table tab = ref;
                 if (tab["x"] != nullptr)
                     this->x = tab["x"];
index 46304408a84ec50155c3bb87371722fecd88a49f..86917b9494126bee6967ff19831699238c693ec6 100644 (file)
@@ -62,6 +62,11 @@ int ScriptSystem::init(void)
     return 0;
 }
 
+// TODO move all of these below once the test printouts are gone
+#include <components/Position.hpp>
+#include <components/Name.hpp>
+#include <components/Render.hpp>
+
 void ScriptSystem::doFile(void)
 {
     auto result = lua.script_file("Scripts/init.lua");
@@ -72,11 +77,20 @@ void ScriptSystem::doFile(void)
     manager->each<Position>(
             [&](entityx::Entity, Position& p)
             {std::cout << p.x << "," << p.y << std::endl;});
+
+    manager->each<Name>(
+            [&](entityx::Entity, Name& n)
+            {std::cout << n.name << std::endl;});
+
+    manager->each<Render>(
+            [&](entityx::Entity, Render& r)
+            {std::cout << r.texture << ": " << r.visible << std::endl;});
 }
 
 /********************
 *  SCRIPT PARSING  *
 ********************/
+// TODO here
 
 void ScriptSystem::scriptExport()
 {
@@ -89,6 +103,15 @@ void ScriptSystem::scriptExport()
             "x", &Position::x,
             "y", &Position::y);
 
+    lua.new_usertype<Name>("Name",
+            sol::constructors<Name(std::string), Name()>(),
+            "value", &Name::name);
+
+    lua.new_usertype<Render>("Render",
+            sol::constructors<Render(std::string), Render()>(),
+            "visible", &Render::visible,
+            "texture", &Render::texture);
+
     auto gamespace = lua["game"].get_or_create<sol::table>();
     gamespace.set_function("spawn", func);
 }
@@ -111,6 +134,16 @@ sol::table ScriptSystem::spawn(sol::object param)
             toRet["init"] = tab["init"];
         }
 
+        if (tab["Name"] != nullptr) {
+            toRet["Name"] =
+                e.assign<Name>(Name().FromLua(tab["Name"])).get();
+        }
+
+        if (tab["Render"] != nullptr) {
+            toRet["Render"] =
+                e.assign<Render>(Render().FromLua(tab["Render"])).get();
+        }
+
     } else {
         std::cerr << "Parameter to spawn() must be a table!" << std::endl;
     }
index baad0b183ea792d1719ece71d10124d134aa06cf..bd8c6209825fbdd07db4ec1c72fdec21d8969786 100644 (file)
 #include <entityx/entityx.h>
 #include <sol/sol.hpp>
 
-/****************
-*  COMPONENTS  *
-****************/
-#include <components/Position.hpp>
-
 struct EntitySpawnEvent {
     EntitySpawnEvent (sol::object ref)
         : ref(ref) {}