]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
particles are a lot better
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 10 Jan 2017 13:42:54 +0000 (08:42 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 10 Jan 2017 13:42:54 +0000 (08:42 -0500)
include/components.hpp
include/particle.hpp
src/particle.cpp
src/player.cpp
src/world.cpp
xml/!town.xml

index 1deaf69d7a4ba81a1cceec0b6c5d2d4dc2bf8345..8e245a74088058a42d01bcec0b1af4a364574e64 100644 (file)
@@ -44,10 +44,11 @@ struct Direction {
         * @param x The velocity of the object on the x axis.
         * @param y The velocity of the object on the y axis.
         */
-       Direction(float x = 0.0f, float y = 0.0f): x(x), y(y) {}
+       Direction(float x = 0.0f, float y = 0.0f): x(x), y(y), grounded(false) {}
 
        float x; /**< Velocity the object is moving in the x direction, this is added to the position */
        float y; /**< Velocity the object is moving in the y direction, this is added to the position */
+       bool grounded;
 };
 
 /**
index 3fcd409236564d96ed87bb6650a219c94c529d55..92ab7e4bd99175bdd8fd0b214adf187012be52ce 100644 (file)
@@ -11,7 +11,8 @@
 enum class ParticleType : char {
        Drop,
        Confetti,
-       SmallBlast
+       SmallBlast,
+       SmallPoof
 };
 
 struct Particle {
@@ -28,10 +29,10 @@ struct Particle {
 class ParticleSystem : public entityx::System<ParticleSystem> {
 private:
        std::vector<Particle> parts;
-       bool max;
+       unsigned int maximum;
 
 public:
-       ParticleSystem(int count = 2048, bool m = false);
+       ParticleSystem(unsigned int max = 2048);
 
        void add(const vec2& pos, const ParticleType& type, const int& timeleft = 3000,
                const unsigned char& color = 0);
index b0b98aeafc9c1d4f2022e5cb45ad9595a71e2407..3a286c4a2f3646b8b8ef9cb42e39f477071e632c 100644 (file)
@@ -4,19 +4,17 @@
 #include <render.hpp>
 #include <world.hpp>
 
-ParticleSystem::ParticleSystem(int count, bool m)
-       : max(m)
+ParticleSystem::ParticleSystem(unsigned int max)
+       : maximum(max)
 {
-       parts.reserve(count);
+       parts.reserve(maximum);
 }
 
-void ParticleSystem::add(const vec2& pos, const ParticleType& type, const int& timeleft, const unsigned char& color)
+void ParticleSystem::add(const vec2& pos, const ParticleType& type, const int& timeleft,
+       const unsigned char& color)
 {
-       // TODO not enforce max
-       if (/*max &&*/ parts.size() + 1 >= parts.capacity())
-               return;
-
-       parts.emplace_back(pos, type, timeleft, vec2(color / 8 * 0.25f, color % 8 * 0.125f + 0.0625f));
+       if (parts.size() < maximum)
+               parts.emplace_back(pos, type, timeleft, vec2(color / 8 * 0.25f, color % 8 * 0.125f + 0.0625f));
 }
 
 void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f,
@@ -24,22 +22,21 @@ void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std
 {
        int togo = count;
        while (togo-- > 0)
-               parts.emplace_back(f(), type, timeleft, vec2(color / 8 * 0.25f, color % 8 * 0.125f + 0.0625f));
+               add(f(), type, timeleft, color);
 }
 
 void ParticleSystem::render(void)
 {
        static GLuint particleVBO = 9999;
+       static const Texture palette ("assets/colorIndex.png");
        // six vertices, 3d coord + 2d tex coord = 5
        constexpr auto entrySize = (6 * 5) * sizeof(GLfloat);
-       static const Texture palette ("assets/colorIndex.png");
 
        if (particleVBO == 9999) {
                // generate VBO
                glGenBuffers(1, &particleVBO);
                glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
-               glBufferData(GL_ARRAY_BUFFER, parts.capacity() * entrySize, nullptr,
-                       GL_STREAM_DRAW);
+               glBufferData(GL_ARRAY_BUFFER, maximum * entrySize, nullptr,     GL_STREAM_DRAW);
        }
 
        // clear dead particles
@@ -51,7 +48,7 @@ void ParticleSystem::render(void)
 
        int offset = 0;
        for (const auto& p : parts) {
-               static const auto hl = game::HLINE;
+               static const auto& hl = game::HLINE;
                GLfloat coords[30] = {
                        p.location.x,      p.location.y,      -1, p.color.x, p.color.y,
                        p.location.x,      p.location.y + hl, -1, p.color.x, p.color.y,
@@ -70,7 +67,6 @@ void ParticleSystem::render(void)
        Render::worldShader.enable();
 
        // set coords
-       glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
        glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE,
                5 * sizeof(GLfloat), 0);
        // set tex coords
@@ -78,12 +74,11 @@ void ParticleSystem::render(void)
                5 * sizeof(GLfloat), reinterpret_cast<void*>(3 * sizeof(GLfloat)));
 
        palette.use();
-       //if(glGetError() != GL_NO_ERROR)abort();
        glDrawArrays(GL_TRIANGLES, 0, parts.size() * 6);
 
-       glBindBuffer(GL_ARRAY_BUFFER, 0);
        Render::worldShader.disable();
        Render::worldShader.unuse();
+       glBindBuffer(GL_ARRAY_BUFFER, 0);
 }
 
 void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
@@ -113,6 +108,7 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
                                p.velocity.x += (p.velocity.x > 0) ? -0.002f : 0.002f;
                        }
                        p.velocity.y = -0.15f;
+                       p.timeLeft = 1000;
                        break;
                case ParticleType::SmallBlast:
                        if (p.velocity.x == 0) {
@@ -121,13 +117,22 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
                                p.velocity.y = sin(degree) / 4.0f;
                        } else {
                                p.velocity.x += (p.velocity.x > 0) ? -0.001f : 0.001f;
-                               p.velocity.x += (p.velocity.y > 0) ? -0.001f : 0.001f;
+                               p.velocity.y += (p.velocity.y > 0) ? -0.001f : 0.001f;
                                if ((p.velocity.x > -0.01 && p.velocity.x < 0.01) &&
                                        (p.velocity.y > -0.01 && p.velocity.y < 0.01)) {
                                        p.timeLeft = 0;
                                }
                        }
 
+                       break;
+               case ParticleType::SmallPoof:
+                       if (p.velocity.x == 0) {
+                               p.velocity.y = 0.1f;
+                               p.velocity.x = randGet() % 12 / 30.0f - 0.2f;
+                       } else {
+                               p.velocity.x += (p.velocity.x > 0) ? -0.001f : 0.001f;
+                               p.velocity.y -= 0.0015f;
+                       }
                        break;
                }
 
@@ -137,8 +142,12 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
 
                // world collision
                auto height = worldSystem.isAboveGround(p.location);
-               if (height != 0)
-                       p.location.y = height + 5, p.velocity.y = randGet() % 10 / 40.0f;
+               if (height != 0) {
+                       if (p.type == ParticleType::Drop || p.type == ParticleType::SmallPoof)
+                               p.location.y = height + 5, p.velocity.y = randGet() % 10 / 40.0f;
+                       else 
+                               p.timeLeft = 0;
+               }
        }
 }
 
index bdfc9fc4e1eb54bae076e5c60dc67decd37f1b8e..62ff6fea982d8bbca332cb570950580d612b0802 100644 (file)
@@ -86,6 +86,7 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
        if ((kc == SDLK_SPACE) && game::canJump && ((vel.y > -0.01) & (vel.y < 0.01))) {
                loc.y += HLINES(2);
                vel.y = .4;
+               vel.grounded = false;
        } else if (!ui::dialogBoxExists || ui::dialogPassive) {
                if (kc == getControl(0)) {
                        if (!ui::fadeIntensity)
index f54af3ec22d301280f1a024bec60c623a02752c1..b6149e881eb631042ba277544bf6243178f355c5 100644 (file)
@@ -22,6 +22,7 @@ using namespace std::literals::chrono_literals;
 #include <components.hpp>
 #include <player.hpp>
 #include <weather.hpp>
+#include <particle.hpp>
 
 // local library headers
 #include <tinyxml2.h>
@@ -1156,7 +1157,11 @@ void WorldSystem::detect(entityx::TimeDelta dt)
                        } else {
                                loc.y = data[line].groundHeight - 0.001f * dt;
                                vel.y = 0;
-                               // TODO ground flag
+                               if (!vel.grounded) {
+                                       vel.grounded = true;
+                                       game::engine.getSystem<ParticleSystem>()->addMultiple(20, ParticleType::SmallPoof,
+                                               [&](){ return vec2(loc.x + randGet() % static_cast<int>(dim.width * game::HLINE), loc.y);}, 500, 30);
+                               }
                        }
                }
 
index c197d8df6f627c7b59460cc1713a0b2a70cc8428..0a281240eed22257c18a2bd4126b5b57ef054942 100644 (file)
@@ -4,7 +4,7 @@
 <World>
     <style background="0" bgm="assets/music/embark.wav" folder="assets/style/classic/"/>
     <generation width="320"/>
-       <weather>Rainy</weather>
+       <weather>Snowy</weather>
     <time>6000</time>
     <link right="!town2.xml"/>
     <spawnx>-300</spawnx>