aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:14:27 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:14:27 -0400
commit2662ac356ce14dacfbc91689fd37244facff4989 (patch)
tree8943534ef3c74290a6a79885abbfcfc5eacba785
parent70c4239993cbd7fe5604de8f39a3783e9c7a7471 (diff)
Added Name and Render components to Lua
-rw-r--r--Scripts/init.lua16
-rw-r--r--src/components/Position.hpp5
-rw-r--r--src/script.cpp33
-rw-r--r--src/script.hpp5
4 files changed, 52 insertions, 7 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 82c2486..3ec4618 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -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"
diff --git a/src/components/Position.hpp b/src/components/Position.hpp
index 8684a7f..1c6cf71 100644
--- a/src/components/Position.hpp
+++ b/src/components/Position.hpp
@@ -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"];
diff --git a/src/script.cpp b/src/script.cpp
index 4630440..86917b9 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -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;
}
diff --git a/src/script.hpp b/src/script.hpp
index baad0b1..bd8c620 100644
--- a/src/script.hpp
+++ b/src/script.hpp
@@ -24,11 +24,6 @@
#include <entityx/entityx.h>
#include <sol/sol.hpp>
-/****************
-* COMPONENTS *
-****************/
-#include <components/Position.hpp>
-
struct EntitySpawnEvent {
EntitySpawnEvent (sol::object ref)
: ref(ref) {}