diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-10-07 17:39:52 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-10-07 17:39:52 -0400 |
commit | 1b63e00b6b08133f5ee37ed90043eee0f67942fd (patch) | |
tree | 686443bdea7368eeaa98c1ea8347a491200ff8d7 | |
parent | 9cef5e64ddd1ef20369b25cc19b1980084ebaefe (diff) |
Added script conversion code, can now convert table to vec2, vec3 and vec4
-rw-r--r-- | Assets/world/world1/layers/0/hitbox.png | bin | 4795 -> 4782 bytes | |||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | Scripts/init.lua | 28 | ||||
-rw-r--r-- | src/components/Component.hpp | 2 | ||||
-rw-r--r-- | src/components/Position.hpp | 13 | ||||
-rw-r--r-- | src/script.cpp | 2 | ||||
-rw-r--r-- | src/script/vectors.cpp | 108 | ||||
-rw-r--r-- | src/script/vectors.hpp | 31 | ||||
-rw-r--r-- | src/world.cpp | 1 | ||||
-rw-r--r-- | src/world.hpp | 12 |
10 files changed, 170 insertions, 29 deletions
diff --git a/Assets/world/world1/layers/0/hitbox.png b/Assets/world/world1/layers/0/hitbox.png Binary files differindex 881b13e..9ae9622 100644 --- a/Assets/world/world1/layers/0/hitbox.png +++ b/Assets/world/world1/layers/0/hitbox.png @@ -40,7 +40,7 @@ LIBS = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -lluajit -ldl -lGLEW -lGL \ -lSDL2_image -lSOIL -lfreetype CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic \ - -Wno-class-memaccess -Wno-implicit-fallthrough -m64 + -Wno-class-memaccess -Wno-implicit-fallthrough -m64 -O1 CXXINCS = -Isrc -I$(LIBDIR)/LuaJIT/src -I$(LIBDIR)/entityx \ -I$(LIBDIR)/LuaBridge/Source -I$(LIBDIR)/sol2/include \ diff --git a/Scripts/init.lua b/Scripts/init.lua index 1693d0c..34e464b 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -28,20 +28,31 @@ player = { end }, Position = { - x = 15, - y = 75 + 15.0, 10.0 }, Velocity = { x = 0.0, y = 0.0 }, + Hitbox = { + bounds = { + {x = -0.5, y = -0.8}, + {x = 0.5, y = -0.8}, + {x = -0.5, y = 0.8}, + {x = 0.5, y = 0.8}, + } + }, Physics = 0, Name = "bord", - hellotrue = true, - hellofalse = false, Render = { texture = "Assets/player.png", - visible = true + visible = true, + bounds = { + {x = -0.5, y = -0.8}, + {x = 0.5, y = -0.8}, + {x = -0.5, y = 0.8}, + {x = 0.5, y = 0.8}, + } }, Light = { r = 1.0, @@ -50,11 +61,6 @@ player = { strength = 1.0 }, Idle = function(self) - --if (self.visibleTick >= 20) then - -- self.Render.visible = not self.Render.visible - -- self.visibleTick = 0 - --end - --self.visibleTick = self.visibleTick + 1 end, visibleTick = 0 } @@ -86,7 +92,7 @@ ball = { dofile("Scripts/world.lua") playerSpawn = game.spawn(player); ---game.spawn(ball); +game.spawn(ball); ------------------- -- SERIALIZING -- diff --git a/src/components/Component.hpp b/src/components/Component.hpp index 2928366..538d42b 100644 --- a/src/components/Component.hpp +++ b/src/components/Component.hpp @@ -24,6 +24,8 @@ #include <entityx/entityx.h> #include <sol/sol.hpp> +#include <script/vectors.hpp> + template<typename T> class Component : public entityx::Component<T> { diff --git a/src/components/Position.hpp b/src/components/Position.hpp index 56e8707..07009f9 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -31,15 +31,10 @@ public: Position FromLua(sol::object ref) { - if (ref.get_type() == sol::type::table) { - sol::table tab = ref; - if (tab["x"] != nullptr) - this->x = tab["x"]; - if (tab["y"] != nullptr) - this->y = tab["y"]; - } else { - throw std::string("Position table not formatted properly"); - } + glm::vec2 vec = Script::to<glm::vec2>(ref); + this->x = vec.x; + this->y = vec.y; + return *this; } diff --git a/src/script.cpp b/src/script.cpp index 8109457..3c0d0b2 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -145,7 +145,7 @@ void ScriptSystem::scriptExport(void) sol::table ScriptSystem::spawn(sol::object param) { - sol::table* toRet; // "Entitiy" table to be returned + sol::table* toRet = NULL; // "Entitiy" table to be returned if (param.get_type() == sol::type::table) { sol::table tab = param; // Cast the generic parameter to a table diff --git a/src/script/vectors.cpp b/src/script/vectors.cpp new file mode 100644 index 0000000..4f8b344 --- /dev/null +++ b/src/script/vectors.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "vectors.hpp" + +#include <glm/glm.hpp> + +#include <iostream> + +namespace Script +{ + template<class T> + T to(sol::object obj) + { + (void)obj; + T fake; + return fake; + } + + template<> + glm::vec2 to<glm::vec2>(sol::object obj) + { + glm::vec2 toReturn; + + if (obj.get_type() == sol::type::table) { + sol::table table = obj; + // X + if (table["x"] == sol::type::number) { + toReturn.x = table["x"]; + } else if (table[1] == sol::type::number) { + toReturn.x = table[1]; + } + // Y + if (table["y"] == sol::type::number) { + toReturn.y = table["y"]; + } else if (table[2] == sol::type::number) { + toReturn.y = table[2]; + } + } else { + std::cerr << "Vectors must be in table form" << std::endl; + } + + return toReturn; + } + + template<> + glm::vec3 to<glm::vec3>(sol::object obj) + { + glm::vec3 toReturn; + + if (obj.get_type() == sol::type::table) { + sol::table table = obj; + glm::vec2 base = to<glm::vec2>(table); + toReturn.x = base.x; + toReturn.y = base.y; + + // Z + if (table["z"] == sol::type::number) { + toReturn.z = table["z"]; + } else if (table[3] == sol::type::number) { + toReturn.z = table[3]; + } + } else { + std::cerr << "Vectors must be in table form" << std::endl; + } + + return toReturn; + } + + template<> + glm::vec4 to<glm::vec4>(sol::object obj) + { + glm::vec4 toReturn; + + if (obj.get_type() == sol::type::table) { + sol::table table = obj; + glm::vec3 base = to<glm::vec3>(table); + toReturn.x = base.x; + toReturn.y = base.y; + toReturn.z = base.z; + + // W + if (table["w"] == sol::type::number) { + toReturn.w = table["w"]; + } else if (table[4] == sol::type::number) { + toReturn.w = table[4]; + } + } else { + std::cerr << "Vectors must be in table form" << std::endl; + } + + return toReturn; + } +} diff --git a/src/script/vectors.hpp b/src/script/vectors.hpp new file mode 100644 index 0000000..9ee0b31 --- /dev/null +++ b/src/script/vectors.hpp @@ -0,0 +1,31 @@ +/** + * @file vectors.hpp + * + * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SCRIPT_VECTORS_HPP_ +#define SCRIPT_VECTORS_HPP_ + +#include <sol/sol.hpp> + +namespace Script +{ + template<class T> + T to(sol::object obj); +} + +#endif//SCRIPT_VECTORS_HPP_ diff --git a/src/world.cpp b/src/world.cpp index a3923fa..fb846eb 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -132,7 +132,6 @@ double World::getHeight(double x, double y, double z) Y = h; h++; } - std::cout << l.drawLayer << "," << wx << "," << Y << std::endl; return (Y/unitSize); } } diff --git a/src/world.hpp b/src/world.hpp index 6776e2c..66d9e3f 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -94,16 +94,16 @@ public: &width, &height, &channels, SOIL_LOAD_RGBA); - for (int w = 0; w < width*4; w+=4) { + for (int w = 0; w < width*channels; w+=channels) { hitbox.push_back(std::vector<bool>(height)); for (int h = 0; h < height; h++) { - unsigned char* c = &box[(w) + (width*h*4)]; - // we want to read the alpha - if (c[3]) { - hitbox[w/4][height-h] = true; + unsigned char* c = &box[(w) + (width*h*channels)]; + // we want to read the last channel (alpha) + if (c[channels-1]) { + hitbox[w/channels][height-h] = true; } else - hitbox[w/4][height-h] = false; + hitbox[w/channels][height-h] = false; } } |