]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
World can now draw properly, and camera follows player
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Tue, 17 Sep 2019 23:09:33 +0000 (19:09 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Tue, 17 Sep 2019 23:09:33 +0000 (19:09 -0400)
Assets/world_normal.png
Scripts/init.lua
Scripts/world.lua
Shaders/world.frag
src/physics.cpp
src/physics.hpp
src/render.cpp
src/render.hpp
src/script.cpp
src/world.cpp
src/world.hpp

index e5f013c2f40a4ef5c2470681892b53991c27e737..181889588e302d3a45e7a7604820e6a4c35839fd 100644 (file)
Binary files a/Assets/world_normal.png and b/Assets/world_normal.png differ
index 78b2a7655f634ff01152561667518c9eadd834d9..84d20731bf7ecc020c8b28f7eba97f00632ee0fe 100644 (file)
@@ -1,30 +1,30 @@
-bird = {
+player = {
     Player = 0,
     EventListeners = {
         MoveLeftPressed = function(self)
-            self.Velocity.x = self.Velocity.x - 100
+            self.Velocity.x = self.Velocity.x - 3
             self.Render.flipx = true;
         end,
         MoveLeftReleased = function(self)
-            self.Velocity.x = self.Velocity.x + 100
+            self.Velocity.x = self.Velocity.x + 3
         end,
         MoveRightPressed = function(self)
-            self.Velocity.x = self.Velocity.x + 100
+            self.Velocity.x = self.Velocity.x + 3
             self.Render.flipx = false;
         end,
         MoveRightReleased = function(self)
-            self.Velocity.x = self.Velocity.x - 100
+            self.Velocity.x = self.Velocity.x - 3
         end,
         JumpKeyPressed = function(self)
             if self.Physics.standing == true then
-                self.Velocity.y = self.Velocity.y + 210
+                self.Velocity.y = self.Velocity.y + 9
             end
         end,
         JumpKeyReleased = function(self)
         end
     },
     Position = {
-        x = 150,
+        x = 15,
         y = 75
     },
     Velocity = {
@@ -62,127 +62,35 @@ bird = {
     visibleTick = 0
 }
 
-cat = {
-    Velocity = {
-        x = 0.0,
-        y = 0.0
-    },
-    Position = {
-        x = 180,
-        y = -75
-    },
-    Render = {
-        texture = "Assets/cat.png",
-        normal = "Assets/cat_normal.png",
-        visible = true
-    },
-    counter = 0;
-    Idle = function(self)
-        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,
-    PhysicsIdle = function(self)
-        if self.Velocity.x < 0 then
-            self.Render.flipx = true
-        elseif self.Velocity.x > 0 then
-            self.Render.flipx = false
-        end
-    end
-}
-
-animal = {
-    Name = "animal",
-    Velocity = { -- This will automatically assign position to (0,0)
-        x = 0.0,
-        y = 0.0
-    },
-    Render = {
-        texture = "Assets/rabbit.png",
-        visible = true
-    },
-    Light = {
-        r = 0.0,
-        b = 1.0,
-        g = 0.2,
-        strength = 1.0
-    },
-    Idle = function(self)
-        self.Velocity.x = -200 * math.sin(math.rad(self.counter));
-        self.Velocity.y =  500 * math.cos(math.rad(self.counter*5));
-        self.counter = self.counter + 5;
-    end,
-    counter = 0,
-    TestMe = function(self)
-        print("Light: rgb("..self.Light.r..","..self.Light.g..","..self.Light.b..")")
-    end
-}
-
-wall = {
-    Position = {
-        y = -100
-    },
-    Render = {
-        texture = "Assets/rock.png",
-        normal = "Assets/rock_normal.png",
-        visible = true
-    }
-}
-
 -- Create the world
 dofile("Scripts/world.lua")
 
-birdSpawn = game.spawn(bird);
-
-dogSpawn = game.spawn(cat);
-
-animalSpawn = game.spawn(animal);
-
-wallSpawn = game.spawn(wall);
-
-game.spawn({
-    Velocity = {
-        x = 0,
-        y = 0
-    },
-    Light = {
-        r = 0.8,
-        g = 0.1,
-        b = 0.2,
-        strength = 0.75
-    },
-    counter = 0;
-    Idle = function(self)
-        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
-});
+playerSpawn = game.spawn(player);
 
 -------------------
 --  SERIALIZING  --
 -------------------
 
-function serial (before_ser)
-    binary = string.dump(before_ser)
-    formatted_binary = ""
-    for i = 1, string.len(binary) do
-        dec, _ = ("\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
-        formatted_binary = formatted_binary .. dec
-    end
-    return formatted_binary
-end
-
-function hello(herro)
-    print("Hello world ".. herro)
-end
-
---print(serial(hello))
-
-local ser = string.dump(hello)
-f2 = (loadstring or load)(ser)
-f2("shite")
-
-local testPut = string.dump(animalSpawn.TestMe)
-testRun = (loadstring or load)(testPut)
-testRun(animalSpawn)
+--function serial (before_ser)
+--    binary = string.dump(before_ser)
+--    formatted_binary = ""
+--    for i = 1, string.len(binary) do
+--        dec, _ = ("\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
+--        formatted_binary = formatted_binary .. dec
+--    end
+--    return formatted_binary
+--end
+--
+--function hello(herro)
+--    print("Hello world ".. herro)
+--end
+--
+----print(serial(hello))
+--
+--local ser = string.dump(hello)
+--f2 = (loadstring or load)(ser)
+--f2("shite")
+--
+--local testPut = string.dump(animalSpawn.TestMe)
+--testRun = (loadstring or load)(testPut)
+--testRun(animalSpawn)
index 8fb31367f1e1e9aff2d211d23e79bc69d4c1a94d..044559ad19205de2729766523f9c31108373a3f1 100644 (file)
@@ -63,21 +63,27 @@ world = {
     Generate = function(self)
         math.randomseed(self.Seed)
         xsize, ysize, zsize = self:setSize(250, 128, 3)
-        --self.data = {}
         for Z = 0,zsize-1 do
-            --self.data[Z] = {}
             for X = 0,xsize-1 do
-                --self.data[Z][X] = {}
-                YGen = math.floor(6*math.sin(X/20) + Z) + 64
-                YDepth = math.random(2,5)
+                if Z == 0 then
+                    YGen = math.floor(6*math.sin(X/20)) + 64
+                elseif Z == 1 then
+                    YGen = math.floor(9*math.sin(X/20)) + 64
+                else
+                    YGen = math.floor(15*math.sin(X/20)) + 64
+                end
+                YDepth = math.random(3,5)
                 for Y = 0,ysize-1 do
                     if Y == YGen then
                         self:setData(X, Y, Z, "grass");
                     elseif Y < YGen and Y > (YGen - YDepth) then
                         self:setData(X, Y, Z, "dirt");
                     elseif Y < YGen then
-                        --self:setData(X, Y, Z, "stone");
-                        self:setData(X, Y, Z, "grass");
+                        self:setData(X, Y, Z, "stone");
+                    elseif Y == YGen + 1 then
+                        if math.random(0, 100) == 53 then
+                            self:setData(X, Y, Z, "flower");
+                        end
                     end
                     --print(X..","..Y..","..Z);
                 end
index 2b5ab5e0201659108f7c63e556f607cc93399d9d..18945a836b551007c5a68f32eb8f52471b076c56 100644 (file)
@@ -22,7 +22,7 @@ uniform int Flipped;
 void main()
 {
     // Quadratic light falloff
-    vec3 Falloff = vec3(0.1, 0.2, 0.0);
+    vec3 Falloff = vec3(0.4, 0.1, 0.002);
 
     vec4 DiffuseColor = texture2D(textu, texCoord);
 
@@ -33,7 +33,8 @@ void main()
     vec3 SumLight = vec3(0.0);
 
     for (int i = 0; i < LightNum; i++) {
-        vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy, LightPos[i].z);
+        vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy, 
+                             (LightPos[i].z - fragCoord.z)*2);
 
         float D = length(LightDir);
 
index 31569ab7a199188b1ccfc641ad2ecc006614701e..1f70ecc6375b317834ba426f46d5d37c47a6762e 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include "physics.hpp"
+
 #include "components/Physics.hpp"
 #include "components/Position.hpp"
 #include "components/Velocity.hpp"
@@ -25,7 +26,7 @@
 void PhysicsSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
                               [[maybe_unused]]entityx::EventManager& events)
 {
-
+    events.subscribe<WorldChangeEvent>(*this);
 }
     
 void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
@@ -33,17 +34,18 @@ void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
                            [[maybe_unused]]entityx::TimeDelta dt)
 {
     entities.each<Position, Velocity>
-        ([dt](entityx::Entity e, Position &pos, Velocity &vel){
+        ([this, dt](entityx::Entity e, Position &pos, Velocity &vel){
 
         bool has_phys = e.has_component<Physics>();
 
         pos.x += (vel.x * dt/1000.0);
         pos.y += (vel.y * dt/1000.0);
 
-        double fallPosition = 0;
-
         // TODO make this intersect world instead of 0 y
         if (has_phys) {
+
+            double fallPosition = currentWorld->getHeight(pos.x, pos.y, 0.0);
+
             Physics *p = e.component<Physics>().get();
             // TODO only make this occur when the entity has a hitbox
             if (pos.y == fallPosition) {
@@ -57,8 +59,13 @@ void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
                 p->standing = true;
             } else {
                 p->standing = false;
-                vel.y -= 920 * (dt/1000.0f);
+                vel.y -= 32.2 * (dt/1000.0f);
             }
         }
     });
 }
+
+void PhysicsSystem::receive(const WorldChangeEvent& wce)
+{
+    currentWorld = wce.newWorld;
+}
index 8231d6d2452edef3fb5a63a9ee6f16db360f75ef..db93999fe8a6a64eb4ec26fe092b6e3b99a454f4 100644 (file)
 #include <entityx/entityx.h>
 #include <sol/sol.hpp>
 
+#include "world.hpp"
+#include "events/world.hpp"
+
 /**
  * @class PhysicsSystem
  * Handles the position and velocity updating of all entities
  */
-class PhysicsSystem : public entityx::System<PhysicsSystem>
+class PhysicsSystem : public entityx::System<PhysicsSystem>,
+                      public entityx::Receiver<PhysicsSystem>
 {
 private:
+    World* currentWorld;
 public:
     PhysicsSystem(void) {}
 
@@ -49,6 +54,8 @@ public:
     void update(entityx::EntityManager& entites,
                 entityx::EventManager& events,
                 entityx::TimeDelta dt) final;
+
+    void receive(const WorldChangeEvent& wce);
 };
 
 #endif // SYSTEM_PHYSICS_HPP_
index b991b2f882bee59985a49b31802926e7594dcd6a..2b49b2c6844e5d2abbb67ad71d8e136977c7b4e6 100644 (file)
@@ -28,6 +28,7 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
                              [[maybe_unused]] entityx::EventManager& events)
 {
     events.subscribe<WorldMeshUpdateEvent>(*this);
+    events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
     init();
 }
 
@@ -53,7 +54,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     ***********/
     
     glm::mat4 view = glm::lookAt(camPos,                       // Pos
-                                 glm::vec3(0.0f, 0.0f, 0.0f),  // Facing
+                                 glm::vec3(camPos.x, camPos.y, 0.0f),  // Facing
                                  glm::vec3(0.0f, 1.0f, 0.0f)); // Up
 
     //glm::mat4 projection = glm::perspective(45.0f, 
@@ -61,16 +62,19 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     //                                        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
+    float scale = 40.0f;
+    float scaleWidth = static_cast<float>(width) / scale;
+    float scaleHeight = static_cast<float>(height) / scale;
+
+    glm::mat4 projection = glm::ortho(-(scaleWidth/2),    // Left
+                                       (scaleWidth/2),    // Right
+                                      -(scaleHeight/2),   // Bottom
+                                       (scaleHeight/2),   // Top
+                                       10.0f,               // zFar
+                                      -10.0f                // zNear
                                      );
 
     glm::mat4 model = glm::mat4(1.0f);
-    model = glm::scale(model, glm::vec3(20.0f, 20.0f, 1.0f));
 
     glUseProgram(s);
 
@@ -91,10 +95,23 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f};
     glUniform4fv(b, 1, amb);
 
+    /************
+    *  CAMERA  *
+    ************/
+    try {
+        if (player.has_component<Position>()) {
+            Position *pos = player.component<Position>().get();
+            camPos.y = pos->y;
+            camPos.x = pos->x;
+        }
+    } catch (...) { // If the player doesn't exist or anything goes wrong
+        camPos.y = 0.0f;
+        camPos.x = 0.0f;
+    }
+
     /**************
     *  LIGHTING  *
     **************/
-    
 
     std::vector<glm::vec3> lightPos;
     std::vector<glm::vec4> lightColor;
@@ -103,7 +120,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     entities.each<Light, Position>([&]
         (entityx::Entity, Light &l, Position &p){
 
-        lightPos.push_back(glm::vec3(p.x, p.y,-10.0));
+        lightPos.push_back(glm::vec3(p.x, p.y, 1.0));
         lightColor.push_back(glm::vec4(l.r, l.g, l.b, l.strength));
         lightNum++;
     });
@@ -132,8 +149,8 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         //    e.component<Scripted>()->updateRender();
         //}
 
-        float w = r.texture.width/2.0f;
-        float h = r.texture.height;
+        float w = 0.5f;
+        float h = (float)r.texture.height/r.texture.width;
 
         GLuint tri_vbo;
         GLfloat tri_data[] = {
@@ -179,6 +196,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                               5*sizeof(float), (void*)(3*sizeof(float)));
         glDrawArrays(GL_TRIANGLES, 0, 6);
     });
+    glUniform1i(f, 0);
 
     // If we were given a world VBO render it
     if (worldVBO) {
@@ -299,3 +317,8 @@ void RenderSystem::receive(const WorldMeshUpdateEvent &wmu)
     worldTexture = wmu.worldTexture;
     worldNormal = wmu.worldNormal;
 }
+
+void RenderSystem::receive(const entityx::ComponentAddedEvent<Player> &cae)
+{
+    player = cae.entity;
+}
index 26e525b35e7cc99d7f2a3af54030458852372c2e..f3064d155c55b793db558863dfc02ee1fcdfae1f 100644 (file)
@@ -38,6 +38,8 @@
 #include "world.hpp"
 #include "events/world.hpp"
 
+#include "components/Player.hpp"
+
 class RenderSystem : public entityx::System<RenderSystem>,
                      public entityx::Receiver<RenderSystem>
 {
@@ -56,6 +58,7 @@ private:
     unsigned int worldVertex = 0;
     GLuint worldTexture = 0;
     GLuint worldNormal = 0;
+    entityx::Entity player; // Save the player so we can track the camera
 public:
     RenderSystem() :
         window(nullptr, SDL_DestroyWindow) {}
@@ -89,6 +92,7 @@ public:
     *  EVENTS  *
     ************/
     void receive(const WorldMeshUpdateEvent &wmu);
+    void receive(const entityx::ComponentAddedEvent<Player> &cae);
     
 };
 
index cf5b2ddee1c7e4868a2f886668e27e2296438278..fd99228ded8c0ff541be8cb3effdf11af2bc8fc3 100644 (file)
@@ -188,10 +188,6 @@ sol::table ScriptSystem::spawn(sol::object param)
                 e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
         }
 
-        if (tab["Player"] != nullptr) {
-            (*toRet)["Player"] = e.assign<Player>().get();
-        }
-
         if (tab["Physics"] != nullptr) {
             if (!e.has_component<Position>()) // Position must exist for phys.
                 (*toRet)["Position"] = e.assign<Position>().get();
@@ -214,6 +210,10 @@ sol::table ScriptSystem::spawn(sol::object param)
             e.assign<EventListener>(listeners);
         }
 
+        if (tab["Player"] != nullptr) {
+            (*toRet)["Player"] = e.assign<Player>().get();
+        }
+
     } else {
         // TODO better logging
         std::cerr << "Parameter to spawn() must be a table!" << std::endl;
index 7710ecc31f91601142cd45eee03eb5fbdaf39d6b..c20ce1711e1c4d628bf091e3c3f62badc7ce4869 100644 (file)
@@ -180,6 +180,31 @@ unsigned int World::setSeed(unsigned int s)
     return seed;
 }
 
+/* PHYSICS */
+double World::getHeight(double x, double y, double z)
+{
+    unsigned int X = static_cast<unsigned int>(x);
+    unsigned int Z = static_cast<unsigned int>(z);
+
+    double Y = 0.0;
+    try {
+        auto &d = data.at(Z).at(X);
+        for (int yi = d.size()-1; yi >= 0; yi--) {
+            if (d.at(yi) >= 0) {
+                if (!registry.at(d.at(yi)).passable) {
+                    Y = static_cast<double>(yi);
+                    Y += 1;
+                    break;
+                }
+            }
+        }
+    } catch (...) { // If we get any errors, just let the character 
+        return y;
+    }
+
+    return Y;
+}
+
 
 /******************
 *  WORLD SYSTEM  *
@@ -188,8 +213,6 @@ unsigned int World::setSeed(unsigned int s)
 World* WorldSystem::addWorld(sol::object t)
 {
     worlds.push_back(World(t));
-    if (currentWorld == nullptr)
-        currentWorld = &(worlds.back());
     return &(worlds.back());
 }
 
index 696f0aa0e65f9d0b314d5a3886b8a0b8f8efcf8b..9314fa6d1bdb145ff5868c4f9e2637adb82930a4 100644 (file)
@@ -112,6 +112,9 @@ public:
     /* SEED */
     unsigned int getSeed();
     unsigned int setSeed(unsigned int);
+
+    /* PHYSICS */
+    double getHeight(double x, double y, double z);
 };
 
 /**