]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Game loop now updates position every tick, and added circular movement to dog
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sun, 1 Sep 2019 05:25:14 +0000 (01:25 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sun, 1 Sep 2019 05:25:14 +0000 (01:25 -0400)
Scripts/init.lua
src/components/Position.hpp
src/components/Render.hpp
src/components/Velocity.hpp
src/engine.cpp
src/render.cpp
src/render.hpp
src/script.cpp

index 596fe38541bd7c8b18a74530f8d92c0357a4cd32..ad69ae631035dc369a19c5da4bdadbdfeffa5841 100644 (file)
@@ -12,12 +12,12 @@ bird = {
 
 dog = {
     Velocity = {
-        x = 0.01,
-        y = 10.5
+        x = 0.0,
+        y = 0.0
     },
     Position = {
-        x = 6.5,
-        y = 1.3
+        x = 50,
+        y = 0
     },
     Render = {
         texture = "assets/tex.png",
@@ -26,8 +26,11 @@ dog = {
     Init = function(self)
         print(self.Position.x .. "," .. self.Position.y)
     end,
+    counter = 0;
     Idle = function(self)
-        self.Position.x = self.Position.x + 0.01;
+        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
 }
 
@@ -41,7 +44,6 @@ animal = {
 birdSpawn = game.spawn(bird);
 
 dogSpawn = game.spawn(dog);
-dogSpawn.Position.x = 37.5
 
 animalSpawn = game.spawn(animal);
 print("Animal pos: " .. animalSpawn.Position.x)
index 0462d1ad29855743d2090e207aafae0b2f08d7ea..8a6bd74c0408ccea4da893fe7204c78a934401e2 100644 (file)
@@ -24,8 +24,8 @@
 struct Position : Component<Position>, entityx::Component<Position>
 {
     public:
-        float x, y;
-        Position(float _x, float _y): x(_x), y(_y) {}
+        double x, y;
+        Position(double _x, double _y): x(_x), y(_y) {}
         Position(void): x(0), y(0) {}
 
         Position FromLua(sol::object ref)
index 4444fef859fb44ee84cb0655d758404cc60a982f..5a9b1a48c0a92a95f7e76e043b0b1a8f7716d84b 100644 (file)
@@ -15,8 +15,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef RENDER_HPP_
-#define RENDER_HPP_
+#ifndef RENDERC_HPP_
+#define RENDERC_HPP_
 
 #include <components/Component.hpp>
 
@@ -49,4 +49,4 @@ struct Render : Component<Render>, entityx::Component<Render>
 
 };
 
-#endif//RENDER_HPP_
+#endif//RENDERC_HPP_
index eacadf24e5c3f94fb6c102291c360f4d2323878a..c2b85e8ed9e19a78772feda4f94e8cd838ad36c4 100644 (file)
@@ -25,9 +25,9 @@
 struct Velocity : Component<Velocity>, entityx::Component<Velocity>
 {
     public:
-        float x, y;
+        double x, y;
         Velocity(): x(0), y(0) {}
-        Velocity(float _x, float _y): x(_x), y(_y) {}
+        Velocity(double _x, double _y): x(_x), y(_y) {}
 
         Velocity FromLua(sol::object ref)
         {
index a89a876dfbb5346369056632ede9aadc2a521f6d..4579eb7e8fe8c75c9f678e9643e13e0972ab4ace 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "components/Script.hpp"
 #include "components/Position.hpp"
+#include "components/Velocity.hpp"
 
 int Engine::init(void)
 {
@@ -42,17 +43,48 @@ int Engine::init(void)
 void Engine::logicLoop(void)
 {
        using namespace std::chrono_literals;
+    typedef std::chrono::high_resolution_clock mc;
 
-       entityx::TimeDelta dt = 0;
+    entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */
+    double elapsed = 0;
 
        while (shouldRun()) {
-               systems.update<InputSystem>(dt);
 
-        // All entities with an idle function should be run here
-        entities.each<Scripted>([](entityx::Entity, Scripted &f){
-            f.exec();
+        auto start = mc::now();
+
+        /***********************
+        *  UPDATE FREQUENTLY  *
+        ***********************/
+        
+        entities.each<Position, Velocity>
+            ([&](entityx::Entity, Position &p, Velocity &v){
+            p.x += (v.x * dt/1000.0);
+            p.y += (v.y * dt/1000.0);
         });
-               std::this_thread::sleep_for(100ms);
+
+               systems.update<InputSystem>(dt);
+
+        /*******************
+        *  LOGIC UPDATES  *
+        *******************/
+        
+        // Update 20 times a second
+        if (elapsed > 50) {
+            elapsed = 0;
+            
+            // All entities with an idle function should be run here
+            entities.each<Scripted>([](entityx::Entity, Scripted &f){
+                f.exec();
+            });
+        }
+
+        auto end = mc::now();
+        auto diff = end - start;
+        auto micros = cr::duration_cast<cr::microseconds>(diff);
+        auto msc = micros.count();
+        dt = static_cast<double>(msc)/1000.0;
+        elapsed += dt;
+               std::this_thread::yield();
        }
 
     // Remove all Lua references from entities
index 30fd39646030b2b3407b7e607727cc95000b4627..5b3a1f1821e34cfc30fcf52948ee984077728b32 100644 (file)
@@ -19,6 +19,8 @@
  */
 
 #include <render.hpp>
+#include <components/Render.hpp>
+#include <components/Position.hpp>
 
 void RenderSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
                              [[maybe_unused]]entityx::EventManager& events)
@@ -26,16 +28,10 @@ void RenderSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
     init();
 }
 
-void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entites,
+void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                           [[maybe_unused]] entityx::EventManager& events,
                           [[maybe_unused]] entityx::TimeDelta dt)
 {
-    static GLfloat tri_data[] = {
-            -1.0, -1.0, 0.0,
-            1.0, -1.0, 0.0,
-            0.0, 1.0, 0.0,
-    };
-    
     GLuint s = worldShader.getProgram();
     GLuint v = worldShader.getUniform("view");
     GLuint p = worldShader.getUniform("projection");
@@ -50,10 +46,18 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entites,
                                  glm::vec3(0.0f, 0.0f, 0.0f),  // Facing
                                  glm::vec3(0.0f, 1.0f, 0.0f)); // Up
 
-    glm::mat4 projection = glm::perspective(45.0f, 
-                                            ((float)width/(float)height), 
-                                            0.01f, 
-                                            2048.0f);
+    //glm::mat4 projection = glm::perspective(45.0f, 
+    //                                        ((float)width/(float)height), 
+    //                                        0.01f, 
+    //                                        2048.0f);
+
+    glm::mat4 projection = glm::ortho(-((float)width/2),    // Left
+                                       ((float)width/2),    // Right
+                                      -((float)height/2),   // Bottom
+                                       ((float)height/2),   // Top
+                                      -10.0f,               // zNear
+                                       10.0f                // zFar
+                                     );
 
     glm::mat4 model = glm::mat4(1.0f);
 
@@ -75,14 +79,25 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entites,
     *  DRAWING  *
     *************/
 
-    GLuint tri_vbo;
-    
-    glGenBuffers(1, &tri_vbo);
-    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, 0, 0);
-    glDrawArrays(GL_TRIANGLES, 0, 3);
+    entities.each<Render, Position>(
+            [this, a](entityx::Entity, Render, Position &p){
+
+        GLuint tri_vbo;
+        GLfloat tri_data[] = {
+                (float)p.x-10.0f, (float)p.y-10.0f, 00.0f,
+                (float)p.x+10.0f, (float)p.y-10.0f, 00.0f,
+                (float)p.x+00.0f, (float)p.y+10.0f, 00.0f,
+        };
+
+        glGenBuffers(1, &tri_vbo);
+        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, 0, 0);
+        glDrawArrays(GL_TRIANGLES, 0, 3);
+    
+    });
     
 
     /*************
index fa609a37b4be743241e4c305c5c05be9ca44a0dc..21869a3fb4c37df91b46695c649616d1885fa520 100644 (file)
@@ -65,7 +65,7 @@ public:
     /**
      * Updates the render system.
      */
-    void update([[maybe_unused]] entityx::EntityManager& entites,
+    void update([[maybe_unused]] entityx::EntityManager& entities,
                 [[maybe_unused]] entityx::EventManager& events,
                 [[maybe_unused]] entityx::TimeDelta dt) final;
 
index 89ae9e0f19b0ffbdfc9b1a40866dcebafe7b34fd..260df133c28c32a97f941c737c0bbe10757a478a 100644 (file)
@@ -53,7 +53,7 @@ void ScriptSystem::receive([[maybe_unused]] const EntitySpawnEvent &toSpawn)
 
 int ScriptSystem::init(void)
 {
-    lua.open_libraries(sol::lib::base);
+    lua.open_libraries(sol::lib::base, sol::lib::math);
 
     scriptExport();
     doFile();
@@ -87,7 +87,7 @@ void ScriptSystem::scriptExport()
         [this](sol::table t){ return spawn(t);};
 
     lua.new_usertype<Position>("Position",
-            sol::constructors<Position(float x, float y), Position()>(),
+            sol::constructors<Position(double x, double y), Position()>(),
             "x", &Position::x,
             "y", &Position::y);
 
@@ -101,7 +101,7 @@ void ScriptSystem::scriptExport()
             "texture", &Render::texture);
 
     lua.new_usertype<Velocity>("Velocity",
-            sol::constructors<Velocity(float, float), Velocity()>(),
+            sol::constructors<Velocity(double, double), Velocity()>(),
             "x", &Velocity::x,
             "y", &Velocity::y);