]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
particle improvements, inventory start
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 14 Jan 2017 14:56:58 +0000 (09:56 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 14 Jan 2017 14:56:58 +0000 (09:56 -0500)
config/items.xml
include/inventory.hpp
include/particle.hpp
include/weather.hpp
src/engine.cpp
src/inventory.cpp
src/particle.cpp
src/render.cpp
src/world.cpp

index 0abd40d7a75b0acc831c4c43bf42432d0b918969..265dbf7973fa2dd812108eb1a3a46d70adc6673a 100644 (file)
@@ -22,5 +22,5 @@
 <item name="Crude Arrow"    type="Arrow"    damage="1"  maxStackSize="99"   sprite="assets/items/arrow_crude.png"/>
 
 <!-- UTILITIES -->
-<item name="Rusty Lantern"     type="Light"                     radius="150" color="255|255|255" sprite="assets/items/rusty_lantern.png"/>
+<!--<item name="Rusty Lantern" type="Light"                     radius="150" color="255|255|255" sprite="assets/items/rusty_lantern.png"/>-->
 <item name="Mossy Torch"       type="Light" fire="true" radius="150" color="245|220|200" sprite="assets/items/basic_torch.png"/>
index fa24de4905c710f3854cfe77b42368f83ce839c5..15877e93978975a4a15c31e8882e8b5bfc8a7e8d 100644 (file)
@@ -6,30 +6,36 @@
 #include <components.hpp>
 #include <events.hpp>
 
-struct Item {
-    GLuint icon;
+struct InventoryEntry {
+       GLuint icon;
+       std::string name;
+       std::string type;
+
+       int count;
+       int max;
+       vec2 loc;
 };
 
-using InventoryEntry = std::pair<Item, unsigned int>;
-
 class InventorySystem : public entityx::System<InventorySystem>, public entityx::Receiver<InventorySystem> {
 private:
     entityx::Entity currentItemEntity;
 
     std::vector<InventoryEntry> items;
-    unsigned int maxItemCount;
 
 public:
-    InventorySystem(unsigned int mic = 1)
-        : maxItemCount(mic) {}
+    InventorySystem(int size = 4) {
+               items.resize(size);
+               loadItems();
+       }
 
     void configure(entityx::EventManager &ev);
 
-    void loadIcons(void);
+    void loadItems(void);
 
     void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
 
     void receive(const KeyDownEvent &kde);
+       void render(void);
 };
 
 #endif // INVENTORY_HPP_
index 92ab7e4bd99175bdd8fd0b214adf187012be52ce..55228c035912fb56f4322d332d2c13d1d9dc9845 100644 (file)
@@ -9,22 +9,22 @@
 #include <entityx/entityx.h>
 
 enum class ParticleType : char {
-       Drop,
-       Confetti,
-       SmallBlast,
-       SmallPoof
+       Drop       = 1,
+       Confetti   = 2,
+       SmallBlast = 4,
+       SmallPoof  = 8
 };
 
 struct Particle {
-       vec2 location;
+       int timeLeft;
        vec2 velocity;
+       vec2 location;
        ParticleType type;
-       int timeLeft;
        vec2 color; // assets/colorIndex.png
 
        Particle(vec2 p, ParticleType t, int tl, vec2 c)
-               : location(p), type(t), timeLeft(tl), color(c) {}
-};// __attribute__ ((packed));
+               : timeLeft(tl), location(p), type(t), color(c) {}
+};
 
 class ParticleSystem : public entityx::System<ParticleSystem> {
 private:
index 08208715ecf24c1a7eac158e6fc39baf2d84221f..8e148ddcd0f31ead4addd06ef48637e76e330fcb 100644 (file)
@@ -56,7 +56,7 @@ public:
                                newPartDelay = 0;
                                partSystem.add(vec2(offset.x - game::SCREEN_WIDTH + randGet() % game::SCREEN_WIDTH * 2,
                                        offset.y + game::SCREEN_HEIGHT / 2 + 50),
-                                       ParticleType::Confetti, 6000, 0);
+                                       ParticleType::Confetti, 10000, 0);
                        }
                        break;
                default:
index c7999edbd243c2c02436c608f5bea8db1a6f0a28..d334aea33afe65d3b2a794f9983e3356bff4ab93 100644 (file)
@@ -23,7 +23,7 @@ void Engine::init(void) {
     systems.add<WindowSystem>();
     systems.add<RenderSystem>();
        systems.add<InputSystem>();
-    //systems.add<InventorySystem>();
+    systems.add<InventorySystem>();
     systems.add<WorldSystem>();
     systems.add<PlayerSystem>();
        systems.add<QuestSystem>();
index ed6028b1d4d572601d01a6a6da8bac1be1e365b8..de18104c204baca871acb815297b1f766de11f69 100644 (file)
@@ -2,23 +2,59 @@
 
 #include <common.hpp>
 #include <events.hpp>
-//#include <texture.hpp>
-//#include <render.hpp>
+#include <texture.hpp>
+#include <render.hpp>
 
-constexpr const char* iconTexturePath = "config/invIcons.txt";
+#include <deque>
 
-static std::vector<Texture> iconTextures;
+#include <tinyxml2.h>
+using namespace tinyxml2;
+
+constexpr const char* itemsPath = "config/items.xml";
+
+
+struct Item {
+       std::string name;
+       std::string type;
+       int value;
+       int stackSize;
+       Texture sprite;
+
+       Item(void)
+               : value(0), stackSize(1) {}
+
+       Item(XMLElement *e) {
+               name = e->StrAttribute("name");
+               type = e->StrAttribute("type");
+               
+               value = 0;
+               e->QueryIntAttribute("value", &value);
+               stackSize = 1;
+               e->QueryIntAttribute("maxStackSize", &stackSize);
+               
+               sprite = Texture(e->StrAttribute("sprite"));
+       }
+};
+
+static std::deque<Item> itemList;
 
 void InventorySystem::configure(entityx::EventManager &ev)
 {
     ev.subscribe<KeyDownEvent>(*this);
 }
 
-void InventorySystem::loadIcons(void) {
-    iconTextures.clear();
-    auto icons = readFileA(iconTexturePath);
-    for (const auto& s : icons)
-        iconTextures.push_back(s);
+void InventorySystem::loadItems(void) {
+       XMLDocument doc;
+       doc.LoadFile(itemsPath);
+
+       auto item = doc.FirstChildElement("item");
+       if (item == nullptr)
+               UserError("No items found");
+
+       do {
+               itemList.emplace_back(item);
+               item = item->NextSiblingElement("item");
+       } while (item != nullptr);
 }
 
 void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
@@ -26,20 +62,35 @@ void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager &
     (void)en;
     (void)ev;
     (void)dt;
+}
 
-       // TODO TODO TODO TODO until we do something
-       return;
+void InventorySystem::render(void)
+{
+       Render::textShader.use();
+       Render::textShader.enable();
+       Colors::black.use();
+       glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .8);
 
-    //vec2 start = vec2(offset.x, 100);// - game::SCREEN_WIDTH / 2 + 20, game::SCREEN_HEIGHT - 40);
+       // calculate positions
+       items.front().loc = vec2(offset.x - 35 * items.size(), offset.y - game::SCREEN_HEIGHT / 2);
+       for (unsigned int i = 1; i < items.size(); i++)
+               items[i].loc = vec2(items[i - 1].loc.x + 70, items[i - 1].loc.y);
 
-    //std::cout << start.x << ' ' << start.y << std::endl;
+       // draw items
+       for (const auto& i : items) {
+               glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, Colors::texCoord);
+               vec2 end = i.loc + 64;
+               GLfloat coords[18] = {
+                       i.loc.x, i.loc.y, -9, end.x, i.loc.y, -9, end.x, end.y, -9,
+                       end.x, end.y, -9, i.loc.x, end.y, -9, i.loc.x, i.loc.y, -9
+               };
+               glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
+               glDrawArrays(GL_TRIANGLES, 0, 6);
+       }
 
-    /*Render::textShader.use();
-        glActiveTexture(GL_TEXTURE0);
-        Colors::black.use();
-        Render::useShader(&Render::textShader);
-        Render::drawRect(start, start + 20, -9.9f);
-    Render::textShader.unuse();*/
+       glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1);
+       Render::textShader.disable();
+       Render::textShader.unuse();
 }
 
 void InventorySystem::receive(const KeyDownEvent &kde)
index 2d92409fc48c2eb14d09c471dc959f9889cc23c5..198387c2d5c585d8fee243857a07b4edcfa86374 100644 (file)
@@ -91,52 +91,54 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
                auto& p = parts[i];
 
                // update timers
-               p.timeLeft -= dt;
+               if (p.timeLeft > 0)
+                       p.timeLeft -= dt;
+               else
+                       continue;
 
                // update movement
+               auto& vel = p.velocity;
                switch (p.type) {
                case ParticleType::Drop:
-                       if (p.velocity.y > -.6)
-                               p.velocity.y -= 0.001f;
+                       if (vel.y > -.6)
+                               vel.y -= 0.001f;
                        break;
                case ParticleType::Confetti:
-                       if (p.velocity.x > -0.01 && p.velocity.x < 0.01) {
-                               p.velocity.x = randGet() % 12 / 30.0f - 0.2f;
+                       if (vel.x > -0.01 && vel.x < 0.01) {
+                               vel.x = randGet() % 12 / 30.0f - 0.2f;
+                               vel.y = -0.15f;
                        } else {
-                               p.velocity.x += (p.velocity.x > 0) ? -0.002f : 0.002f;
+                               vel.x += (vel.x > 0) ? -0.002f : 0.002f;
                        }
-                       p.velocity.y = -0.15f;
-                       p.timeLeft = 1000;
                        break;
                case ParticleType::SmallBlast:
-                       if (p.velocity.x == 0) {
+                       if (vel.x == 0) {
                                int degree = randGet() % 100;
-                               p.velocity.x = cos(degree) / 4.0f;
-                               p.velocity.y = sin(degree) / 4.0f;
+                               vel.x = cos(degree) / 4.0f;
+                               vel.y = sin(degree) / 4.0f;
                        } else {
-                               p.velocity.x += (p.velocity.x > 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)) {
+                               vel.x += (vel.x > 0) ? -0.001f : 0.001f;
+                               vel.y += (vel.y > 0) ? -0.001f : 0.001f;
+                               if ((vel.x > -0.01 && vel.x < 0.01) &&
+                                       (vel.y > -0.01 && vel.y < 0.01)) {
                                        p.timeLeft = 0;
                                }
                        }
-
                        break;
                case ParticleType::SmallPoof:
-                       if (p.velocity.x == 0) {
-                               p.velocity.y = 0.1f;
-                               p.velocity.x = randGet() % 10 / 20.0f - 0.25f;
+                       if (vel.x == 0) {
+                               vel.y = 0.1f;
+                               vel.x = randGet() % 10 / 20.0f - 0.25f;
                        } else {
-                               p.velocity.x += (p.velocity.x > 0) ? -0.001f : 0.001f;
-                               p.velocity.y -= 0.0015f;
+                               vel.x += (vel.x > 0) ? -0.001f : 0.001f;
+                               vel.y -= 0.0015f;
                        }
                        break;
                }
 
                // really update movement
-               p.location.x += p.velocity.x * dt;
-               p.location.y += p.velocity.y * dt;
+               p.location.x += vel.x * dt;
+               p.location.y += vel.y * dt;
 
                // world collision
                auto height = worldSystem.isAboveGround(p.location);
index 4e323119a8b7d84c5e1dcdde0ea89acdc928e284..a63d63a5b2fa403d16ec7944e94e925652836f94 100644 (file)
@@ -99,14 +99,16 @@ void render(const int& fps)
 
 } // namespace render
 
-#include <ui.hpp>
-#include <window.hpp>
-#include <world.hpp>
 #include <engine.hpp>
+#include <gametime.hpp>
+#include <inventory.hpp>
 #include <particle.hpp>
 #include <player.hpp>
+#include <ui.hpp>
+#include <window.hpp>
+#include <world.hpp>
+
 #include <entityx/entityx.h>
-#include <gametime.hpp>
 
 void preRender(void)
 {
@@ -172,6 +174,8 @@ void render(const int& fps)
 
        game::engine.getSystem<RenderSystem>()->render();
 
+       game::engine.getSystem<InventorySystem>()->render();
+
        // draw the debug overlay if desired
        if (ui::debug) {
                auto pos = game::engine.getSystem<PlayerSystem>()->getPosition();
index 8f4be8edfdb84ac57d057a6d4a7f03672e292a45..43b66cd563c90c20157683bc92c6a2edfcc870a7 100644 (file)
@@ -98,14 +98,6 @@ static const std::string buildPaths[] = {
        "brazzier.png"
 };
 
-// alpha-related values used for world drawing? nobody knows...
-static const float bgDraw[4][3]={
-       { 100, 240, 0.6  },
-       { 150, 250, 0.4  },
-       { 200, 255, 0.25 },
-       { 255, 255, 0.1  }
-};
-
 /* ----------------------------------------------------------------------------
 ** Functions section
 ** --------------------------------------------------------------------------*/
@@ -827,9 +819,12 @@ void WorldSystem::render(void)
 
     Render::worldShader.unuse();
        // draw the remaining layers
+       static const float alphas[4] = {
+               0.6, 0.4, 0.25, 0.1
+       };
        for (int i = 0; i < 4; i++) {
                bgTex++;
-               auto xcoord = offset.x * bgDraw[i][2];
+               auto xcoord = offset.x * alphas[i];
 
                bg_items.clear();
                bg_tex.clear();