]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added Lua update function that allows certain entities to update every loop
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 2 Sep 2019 05:05:57 +0000 (01:05 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 2 Sep 2019 05:05:57 +0000 (01:05 -0400)
Assets/cat_normal.png
Scripts/init.lua
Shaders/world.frag
src/components/Render.hpp
src/components/Script.hpp
src/engine.cpp
src/render.cpp
src/script.cpp

index ae10a495f2bcd9b12b4cd12c33c3665043e6eb26..5b5a74bd9030e06b5963339be542a90393b41aff 100644 (file)
Binary files a/Assets/cat_normal.png and b/Assets/cat_normal.png differ
index 9b8912f5e68ed799b31397956af53bf4988112b9..26b2aa7113afa5a82ff04436cfc091ef61461240 100644 (file)
@@ -26,6 +26,13 @@ bird = {
         --end
         --self.visibleTick = self.visibleTick + 1
     end,
+    Update = function(self)
+        if self.Velocity.x < 0 then
+            self.Render.flipx = true
+        elseif self.Velocity.x > 0 then
+            self.Render.flipx = false
+        end
+    end,
     visibleTick = 0
 }
 
@@ -48,6 +55,13 @@ cat = {
         self.Velocity.x = -100 * math.sin(math.rad(self.counter));
         self.Velocity.y =  100 * math.cos(math.rad(self.counter));
         self.counter = self.counter + 5;
+    end,
+    Update = function(self)
+        if self.Velocity.x < 0 then
+            self.Render.flipx = true
+        elseif self.Velocity.x > 0 then
+            self.Render.flipx = false
+        end
     end
 }
 
@@ -65,7 +79,7 @@ animal = {
         r = 0.0,
         b = 1.0,
         g = 0.2,
-        strength = 2.0
+        strength = 1.0
     },
     Idle = function(self)
         self.Velocity.x = -200 * math.sin(math.rad(self.counter));
index 4a17a937260f04b1423d70afc5b4d12082f6a642..85941f2106fae6c2829b71cca851242f285edee6 100644 (file)
@@ -16,9 +16,11 @@ out vec4 FragColor;
 uniform vec3 LightPos[32];
 uniform vec4 LightColor[32];
 uniform int LightNum;
+uniform vec4 AmbientLight;
 
 void main()
 {
+    // Quadratic light falloff
     vec3 Falloff = vec3(0.1, 0.2, 0.0);
 
     vec4 DiffuseColor = texture2D(textu, texCoord);
@@ -37,9 +39,10 @@ void main()
         vec3 N = normalize(NormalMap * 2.0 - 1.0);
         vec3 L = normalize(LightDir);
 
-        vec3 Diffuse = (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
+        vec3 Diffuse = 
+            (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
 
-        vec3 Ambient = vec3(0.0);
+        vec3 Ambient = AmbientLight.rgb * AmbientLight.a;
 
         float Attenuation = 
             1.0 / (Falloff.x + (Falloff.y * D) + (Falloff.z * D * D));
@@ -52,47 +55,3 @@ void main()
 
     FragColor = vec4(SumLight, DiffuseColor.a);
 };
-
-/*
-    vec4 normalMap = texture2D(normu, texCoord);
-    vec3 normal = normalMap.xyz * 2.0 - 1.0;
-
-    if (pixTex.a < 0.1f) 
-               discard;
-
-       vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
-
-    float dist = length(light.xy - fragCoord.xy);
-    if (dist < light.w) {
-        float attenuation = clamp(1.0f - dist*dist/(light.w*light.w), 0.0f, 1.0f);
-        attenuation *= attenuation;
-        shadeColor += vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(normal.xyz, 1);
-        //shadeColor = vec4(1.0) + 0.1*normal;
-        //shadeColor = vec4(1.0);
-    }
-
-    //FragColor = pixTex * shadeColor;
-    FragColor = vec4(normal.xyz, 1);
-}
-*/
-
-/*
-    vec2 texLoc = vec2(texCoord.x, 1-texCoord.y);
-       vec4 pixTex = texture2D(texture, texLoc);
-    if (pixTex.a < 0.1f) 
-               discard;
-   
-       vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
-       if (lightImpact > 0.0f) {
-               for (int i = 0; i < lightSize; i++) {
-                       vec2 loc = light[i].xy;
-                       float dist = length(loc - fragCoord.xy);
-                       if (dist < light[i].w) {
-                               float attenuation = clamp(1.0f - dist*dist/(light[i].w*light[i].w), 0.0f, 1.0f);
-                               attenuation *= attenuation;
-                               shadeColor += (vec4(attenuation, attenuation, attenuation, 0.0f) * vec4(lightColor[i])) * lightImpact;
-                       }
-               }
-       }
-       shadeColor += ambientLight;
-*/
index f5936ea6c8f3a65527d51362c1c332670fd61877..3f1750fe786d462e574c6a3027dcedcbdedcfdf2 100644 (file)
@@ -27,7 +27,7 @@ public:
     Texture texture;
     Texture normal;
     bool visible;
-    bool hasNormal = false;
+    bool flipX = false;
 
     Render(std::string _file) :
         texture(_file), visible(true) {}
@@ -42,10 +42,10 @@ public:
                 this->visible = tab["visible"];
             if (tab["texture"].get_type() == sol::type::string)
                 this->texture = Texture(static_cast<std::string>(tab["texture"]));
-            if (tab["normal"].get_type() == sol::type::string) {
+            if (tab["normal"].get_type() == sol::type::string)
                 this->normal = Texture(static_cast<std::string>(tab["normal"]));
-                hasNormal = true;
-            }
+            if (tab["flipx"].get_type() == sol::type::boolean)
+                this->flipX = tab["flipx"];
         } else {
             throw std::string(
                 "Render component table formatted incorrectly"
index 66addc87e192c9f350dc56f583672d6ee072cd2d..069fea9952e065fc3041574296cd29cbd63f5979 100644 (file)
@@ -47,6 +47,12 @@ public:
             caller["Idle"](caller); // Call idle function and pass itself
                                     //  in or to fulfill the 'self' param
     }
+
+    void update(void)
+    {
+        if (caller["Update"] == sol::type::function)
+            caller["Update"](caller);
+    }
 };
 
 #endif // COMPONENT_SCRIPT_HPP_
index f23565152fdd99a4368a35569dd684c8d0ba458d..aa3ccc5babd5c2f23ccc4758240fb67d5152f921 100644 (file)
@@ -67,6 +67,7 @@ void Engine::logicLoop(void)
         });
 
         systems.update<InputSystem>(dt);
+        systems.update<ScriptSystem>(dt);
 
         /*******************
         *  LOGIC UPDATES  *
index d9cc054638dc354ababe5306d956c19797448822..4990955e3a38781bb4dc7f2206faab2bef00ed74 100644 (file)
@@ -33,6 +33,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                           [[maybe_unused]] entityx::EventManager& events,
                           [[maybe_unused]] entityx::TimeDelta dt)
 {
+    // TODO move these to only happen once to speed up rendering
     GLuint s = worldShader.getProgram();
     GLuint v = worldShader.getUniform("view");
     GLuint p = worldShader.getUniform("projection");
@@ -42,6 +43,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
 
     GLuint q = worldShader.getUniform("textu");
     GLuint n = worldShader.getUniform("normu");
+    GLuint b = worldShader.getUniform("AmbientLight");
 
     /***********
     *  SETUP  *
@@ -82,9 +84,14 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     glEnableVertexAttribArray(a);
     glEnableVertexAttribArray(t);
 
-    /*************
-    *  DRAWING  *
-    *************/
+    GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f};
+
+    glUniform4fv(b, 1, amb);
+
+    /**************
+    *  LIGHTING  *
+    **************/
+    
 
     std::vector<glm::vec3> lightPos;
     std::vector<glm::vec4> lightColor;
@@ -107,6 +114,11 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                  lightColor.size(),
                  reinterpret_cast<GLfloat*>(lightColor.data()));
 
+    /*************
+    *  DRAWING  *
+    *************/
+
+
     entities.each<Render, Position>(
         [this, a, q, t, n](entityx::Entity, Render &r, Position &p) {
 
@@ -127,6 +139,15 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                 (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f,
         };
 
+        // TODO flip nicely (aka model transformations)
+        if (r.flipX) {
+            std::swap(tri_data[3], tri_data[8]);
+            tri_data[13] = tri_data[3];
+
+            std::swap(tri_data[23], tri_data[28]);
+            tri_data[18] = tri_data[23];
+        }
+
         glActiveTexture(GL_TEXTURE0);
         glBindTexture(GL_TEXTURE_2D, r.texture.tex);
         glUniform1i(q, 0);
@@ -139,8 +160,10 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
         glBufferData(GL_ARRAY_BUFFER, sizeof(tri_data), tri_data, GL_STREAM_DRAW);
 
-        glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
-        glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
+        glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE,
+                              5*sizeof(float), 0);
+        glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 
+                              5*sizeof(float), (void*)(3*sizeof(float)));
         glDrawArrays(GL_TRIANGLES, 0, 6);
     });
     
@@ -215,6 +238,7 @@ int RenderSystem::init(void)
     worldShader.addUniform("LightPos");
     worldShader.addUniform("LightColor");
     worldShader.addUniform("LightNum");
+    worldShader.addUniform("AmbientLight");
 
     glEnableVertexAttribArray(worldShader.getAttribute("vertex"));
     glUseProgram(worldShader.getProgram());
index 30328ab623ee4c65fcd54a8e261734e5d356956a..a886ca3e955579d9c07e3cbd184d6f0ef1de6a07 100644 (file)
@@ -36,10 +36,14 @@ void ScriptSystem::configure(entityx::EntityManager& entities,
     //init();
 }
 
-void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites,
+#include <components/Script.hpp>
+void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                           [[maybe_unused]] entityx::EventManager& events,
                           [[maybe_unused]] entityx::TimeDelta dt)
 {
+    entities.each<Scripted>([](entityx::Entity, Scripted &s){
+        s.update();
+    });
 }
 
 
@@ -99,7 +103,8 @@ void ScriptSystem::scriptExport(void)
     lua.new_usertype<Render>("Render",
             sol::constructors<Render(std::string), Render()>(),
             "visible", &Render::visible,
-            "texture", &Render::texture);
+            "texture", &Render::texture,
+            "flipx", &Render::flipX);
 
     lua.new_usertype<Velocity>("Velocity",
             sol::constructors<Velocity(double, double), Velocity()>(),