diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-18 12:10:25 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-18 12:10:25 -0400 |
commit | 0e7f7791fd592f0240a30168a9a570c57b0f5880 (patch) | |
tree | be4e0a598112748030412c452e0a7c24f45ef665 | |
parent | 145d74e433216f8c17475685c553321ca4cbedf3 (diff) | |
parent | 2cedd39a90fdb0387783b50446b16732517fb651 (diff) |
adjust for world changes
-rw-r--r-- | Assets/world_normal.png | bin | 1204 -> 1877 bytes | |||
-rw-r--r-- | Scripts/init.lua | 154 | ||||
-rw-r--r-- | Scripts/world.lua | 20 | ||||
-rw-r--r-- | Shaders/world.frag | 5 | ||||
-rw-r--r-- | src/physics.cpp | 17 | ||||
-rw-r--r-- | src/physics.hpp | 9 | ||||
-rw-r--r-- | src/render.cpp | 88 | ||||
-rw-r--r-- | src/render.hpp | 17 | ||||
-rw-r--r-- | src/script.cpp | 8 | ||||
-rw-r--r-- | src/world.cpp | 27 | ||||
-rw-r--r-- | src/world.hpp | 3 |
11 files changed, 183 insertions, 165 deletions
diff --git a/Assets/world_normal.png b/Assets/world_normal.png Binary files differindex e5f013c..1818895 100644 --- a/Assets/world_normal.png +++ b/Assets/world_normal.png diff --git a/Scripts/init.lua b/Scripts/init.lua index da9ba14..d2abb00 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -1,33 +1,33 @@ game.loadFont("default", "Assets/FreePixel.ttf", 16) -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 game.puts("default", self.Position.x, self.Position.y, "Hey.") + 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 = { @@ -65,127 +65,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) 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<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; +} 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 <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_ diff --git a/src/render.cpp b/src/render.cpp index 2a51b70..3eea57b 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -28,6 +28,9 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { events.subscribe<NewRenderEvent>(*this); + events.subscribe<WorldMeshUpdateEvent>(*this); + events.subscribe<entityx::ComponentAddedEvent<Player>>(*this); + init(); } @@ -53,7 +56,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 +64,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 +97,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 +122,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 +151,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,9 +198,33 @@ 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) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, worldTexture); + glUniform1i(q, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, worldNormal); + glUniform1i(n, 1); + + glBindBuffer(GL_ARRAY_BUFFER, worldVBO); + //glBufferData(GL_ARRAY_BUFFER, + // wm.size() * sizeof(WorldMeshData), + // &wm.front(), + // GL_STREAM_DRAW); - // Update all given VBOs - for (auto& r : renders) { + glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, + 6*sizeof(float), 0); + glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(3*sizeof(float))); + glDrawArrays(GL_TRIANGLES, 0, worldVertex); + } + + // Update all UI VBOs + for (auto& r : uiRenders) { auto& render = r.second; glActiveTexture(GL_TEXTURE0); @@ -293,7 +336,20 @@ int RenderSystem::init(void) void RenderSystem::receive(const NewRenderEvent &nre) { - renders.insert_or_assign(nre.vbo, - RenderData(nre.tex, nre.normal, nre.vertex)); + uiRenders.insert_or_assign(nre.vbo, + UIRenderData(nre.tex, nre.normal, nre.vertex)); +} + +void RenderSystem::receive(const WorldMeshUpdateEvent &wmu) +{ + worldVBO = wmu.worldVBO; + worldVertex = wmu.numVertex; + worldTexture = wmu.worldTexture; + worldNormal = wmu.worldNormal; +} + +void RenderSystem::receive(const entityx::ComponentAddedEvent<Player> &cae) +{ + player = cae.entity; } diff --git a/src/render.hpp b/src/render.hpp index 3f632f1..eabf4be 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -36,18 +36,19 @@ #include "shader.hpp" #include "world.hpp" +#include "components/Player.hpp" #include "events/render.hpp" #include "events/world.hpp" #include <map> -struct RenderData +struct UIRenderData { GLuint tex; GLuint normal; unsigned int vertex; - RenderData(GLuint _tex, GLuint _normal, unsigned int _vertex) : + UIRenderData(GLuint _tex, GLuint _normal, unsigned int _vertex) : tex(_tex), normal(_normal), vertex(_vertex) {} }; @@ -66,7 +67,13 @@ private: glm::vec3 camPos; // Map of VBOs and their render data - std::map<GLuint, RenderData> renders; + std::map<GLuint, UIRenderData> uiRenders; + + GLuint worldVBO = 0; + unsigned int worldVertex = 0; + GLuint worldTexture = 0; + GLuint worldNormal = 0; + entityx::Entity player; // Save the player so we can track the camera public: RenderSystem() : @@ -100,9 +107,9 @@ public: /************ * EVENTS * ************/ - //void receive(const WorldMeshUpdateEvent &wmu); - + void receive(const WorldMeshUpdateEvent &wmu); void receive(const NewRenderEvent &nre); + void receive(const entityx::ComponentAddedEvent<Player> &cae); }; #endif // SYSTEM_RENDER_HPP_ diff --git a/src/script.cpp b/src/script.cpp index ec8f5c7..6cda627 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -187,10 +187,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(); @@ -213,6 +209,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; diff --git a/src/world.cpp b/src/world.cpp index 9f69c45..93cf511 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -181,6 +181,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 * @@ -189,8 +214,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); }; /** |