From 2cedd39a90fdb0387783b50446b16732517fb651 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Tue, 17 Sep 2019 19:09:33 -0400 Subject: [PATCH] World can now draw properly, and camera follows player --- Assets/world_normal.png | Bin 1204 -> 1877 bytes Scripts/init.lua | 154 ++++++++-------------------------------- Scripts/world.lua | 20 ++++-- Shaders/world.frag | 5 +- src/physics.cpp | 17 +++-- src/physics.hpp | 9 ++- src/render.cpp | 47 ++++++++---- src/render.hpp | 4 ++ src/script.cpp | 8 +-- src/world.cpp | 27 ++++++- src/world.hpp | 3 + 11 files changed, 138 insertions(+), 156 deletions(-) diff --git a/Assets/world_normal.png b/Assets/world_normal.png index e5f013c2f40a4ef5c2470681892b53991c27e737..181889588e302d3a45e7a7604820e6a4c35839fd 100644 GIT binary patch delta 1773 zcmVMlNcgFE6;1T zE1!^KHr*9+zw1=U_0vLS-Vg2Thk6&{d^foAkBx+$4dS>9)w_`J-8nsf@9pkvCHrR+ z?!36}_hV7MrX_JUHS&ZyadfwWe&(1Qej8g+@y9(M8@<1WkD&~mcnV28H9uMkOpqcK z?XC$s{*YwUN%Es@05Yh6e!0GVVfp^~EA7K)^)JCMSYN$2+$nyg9X==+-y!lw$zJWZ za?+25-mo5apLVu@o<1q{BOvepS|hJDAM0u|m~7Xm4--!hi_}<^9=Q1*_?CbB2L6w(u;`lzs%49MFP+iuQp_KJEfg2wN+ZApwDCBuWO2oY{%s z$P7Rgs|103Wq_Qo8iGnADdF%CmXK~zi}Y>8WeC?mhR?>);^+c|2_R$fJxpX(E(nnmTRz%(Kk85Nb|eoL^m^k5aUDu?w|%@2<*))W=e@S zxy>vVMph_8N!@S;ZDJspCb2GhvAg5~y!gLx3!V=*{;%Z1M)!;4e&+TawXv_~euzD3 zxSbjW*=Iw&Hd5y{dsW>oh%Spuab%Izp;SjO;Z~rLJ9ov5k!pvAv!qq(Oqix~Cj3m$ z1#22S!ehjL=4xmPOypdb^chmzU0lrXEvK4iu6qU-Z;7mnoq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>$iw*32!Mw9>TIX00`E z;p~EvRIhH{+&uz&MoFQ8*#VD;&Ru%x+H1Goy7w56&nQDj9X8tV5tFCPGIiEzvrV6| zXhBM8m8GjLTW$G@4Xth2W$UincH6$=MeRZLhxP~5=s}GyQpe9;)G%sx7P^+8bI<5u z24Y+Y#O)A(p!qO=%Z*CmL+&uk&Zr7y1gVG3q#TJV5SB%(n_leR$bFkzLax8!#@`{A z4!Zw=9H%9pxc!9MJU8LEh}~6qa+(FzSEpKZQsdOu2-TB=Y_i$iP)0STQp_sc9TA!F zP|TPm1|nQuw#O=%rE3*JrgkKcj*Re}xtmrP#}T>3ma$5IK0U_W@U=}J24tCD-M(s` z?Ha_fKHH6|xOW2?pl>y5!q^gCI0=NqUg2=i+PMshl0t{&^zs)r{o29C>Q~psfDYG2 za&XWoL}3+~X-Xfwz5R{I6=bbx&VjE%jEOb=x9KIx`0 zaM<+mwBOEu?GHBn&f!z;A*|?Kw>Wf2u8>Xmeq8Bo8qlqm~b=vC!2olKwDy75Rd)1*0epc0lzygF-)X%9kNA9^s)DF2NN%JcLuo+CH@Sed`olS&342o47r zBb4|lV3TqND1U}YL_t(I%f*v14#F@DMW0b8q|A{rS7PQM9E4l^JM~WG03I2_;AE&k z45&13vGp_f`&%*H*NPK3P+INUwKu;ZUF_TY1i 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) diff --git a/Scripts/world.lua b/Scripts/world.lua index 8fb3136..044559a 100644 --- a/Scripts/world.lua +++ b/Scripts/world.lua @@ -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 diff --git a/Shaders/world.frag b/Shaders/world.frag index 2b5ab5e..18945a8 100644 --- a/Shaders/world.frag +++ b/Shaders/world.frag @@ -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); diff --git a/src/physics.cpp b/src/physics.cpp index 31569ab..1f70ecc 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -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(*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 - ([dt](entityx::Entity e, Position &pos, Velocity &vel){ + ([this, dt](entityx::Entity e, Position &pos, Velocity &vel){ bool has_phys = e.has_component(); 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().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; +} diff --git a/src/physics.hpp b/src/physics.hpp index 8231d6d..db93999 100644 --- a/src/physics.hpp +++ b/src/physics.hpp @@ -24,13 +24,18 @@ #include #include +#include "world.hpp" +#include "events/world.hpp" + /** * @class PhysicsSystem * Handles the position and velocity updating of all entities */ -class PhysicsSystem : public entityx::System +class PhysicsSystem : public entityx::System, + public entityx::Receiver { 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_ diff --git a/src/render.cpp b/src/render.cpp index b991b2f..2b49b2c 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -28,6 +28,7 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { events.subscribe(*this); + events.subscribe>(*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(width) / scale; + float scaleHeight = static_cast(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 *pos = player.component().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 lightPos; std::vector lightColor; @@ -103,7 +120,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, entities.each([&] (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()->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 &cae) +{ + player = cae.entity; +} diff --git a/src/render.hpp b/src/render.hpp index 26e525b..f3064d1 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -38,6 +38,8 @@ #include "world.hpp" #include "events/world.hpp" +#include "components/Player.hpp" + class RenderSystem : public entityx::System, public entityx::Receiver { @@ -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 &cae); }; diff --git a/src/script.cpp b/src/script.cpp index cf5b2dd..fd99228 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -188,10 +188,6 @@ sol::table ScriptSystem::spawn(sol::object param) e.assign(Velocity().FromLua(tab["Velocity"])).get(); } - if (tab["Player"] != nullptr) { - (*toRet)["Player"] = e.assign().get(); - } - if (tab["Physics"] != nullptr) { if (!e.has_component()) // Position must exist for phys. (*toRet)["Position"] = e.assign().get(); @@ -214,6 +210,10 @@ sol::table ScriptSystem::spawn(sol::object param) e.assign(listeners); } + if (tab["Player"] != nullptr) { + (*toRet)["Player"] = e.assign().get(); + } + } else { // TODO better logging std::cerr << "Parameter to spawn() must be a table!" << std::endl; diff --git a/src/world.cpp b/src/world.cpp index 7710ecc..c20ce17 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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(x); + unsigned int Z = static_cast(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(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()); } diff --git a/src/world.hpp b/src/world.hpp index 696f0aa..9314fa6 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -112,6 +112,9 @@ public: /* SEED */ unsigned int getSeed(); unsigned int setSeed(unsigned int); + + /* PHYSICS */ + double getHeight(double x, double y, double z); }; /** -- 2.39.5