]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
rip skirl
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 13 Feb 2017 21:57:19 +0000 (16:57 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 13 Feb 2017 21:57:19 +0000 (16:57 -0500)
entityx/Entity.cc
entityx/Entity.h
include/components.hpp
src/components.cpp
src/player.cpp
src/window.cpp
src/world.cpp
xml/entities.xml

index adac3de6c5a7596e076f0942fc76e73183f889af..476def7d682d0be725b962b25de36a78c9d08b99 100644 (file)
@@ -27,6 +27,11 @@ void Entity::destroy() {
   invalidate();
 }
 
+void Entity::kill(void) {
+  assign<Killed>();
+}
+
+
 std::bitset<entityx::MAX_COMPONENTS> Entity::component_mask() const {
   return manager_->component_mask(id_);
 }
index a423f8ffc36b6b00ad3d6f86bda3b2af0aae2b9e..e7d2e8daa4f1d64f5230606669375a959bad8efc 100644 (file)
@@ -46,7 +46,7 @@ class EntityManager;
 template <typename C, typename EM = EntityManager>
 class ComponentHandle;
 
-
+struct Killed {};
 
 /** A convenience handle around an Entity::Id.
  *
@@ -166,6 +166,8 @@ public:
 
   std::bitset<entityx::MAX_COMPONENTS> component_mask() const;
 
+  void kill();
+
  private:
   EntityManager *manager_ = nullptr;
   Entity::Id id_ = INVALID;
@@ -462,11 +464,13 @@ class EntityManager : entityx::help::NonCopyable {
   public:
     template <typename T> struct identity { typedef T type; };
 
-    void each(typename identity<std::function<void(Entity entity, Components&...)>>::type f) {
+    void each(typename identity<std::function<void(Entity entity, Components&...)>>::type f, bool dead = false) {
       static std::mutex locked;
       locked.lock();
-      for (auto it : *this)
-        f(it, *(it.template component<Components>().get())...);
+      for (auto it : *this) {
+        if (dead || !it.has_component<Killed>())
+          f(it, *(it.template component<Components>().get())...);
+      }
       locked.unlock();
     }
 
@@ -766,8 +770,8 @@ class EntityManager : entityx::help::NonCopyable {
   template <typename T> struct identity { typedef T type; };
 
   template <typename ... Components>
-  void each(typename identity<std::function<void(Entity entity, Components&...)>>::type f) {
-    return entities_with_components<Components...>().each(f);
+  void each(typename identity<std::function<void(Entity entity, Components&...)>>::type f, bool dead = false) {
+    return entities_with_components<Components...>().each(f, dead);
   }
 
   /**
index ebc256dd3fa493a3b78f0450c7d960d10f3d118d..3169a6d50b3ac00674b15092145f9e759fb87bfa 100644 (file)
@@ -88,10 +88,11 @@ struct Health {
        /**
         * Constructor that sets the variables, with 0 health as default.
         */
-       Health(int h = 0, int m = 0) : health(h), maxHealth(m) {}
+       Health(int m = 1, int h = 0)
+               : health(h != 0 ? h : m), maxHealth(m) {}
 
-       int health;
-       int maxHealth;
+       int health; /**< The current amount of health */
+       int maxHealth; /**< The maximum amount of health */
 };
 
 struct Portal {
index 3b38a53427f71b77798078872f2e5ff8a8bb5ee9..a66a1db017f9f5ec1387b8ae4f2c7824b3d48a4b 100644 (file)
@@ -51,9 +51,13 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e
                        // TODO initialX and range?
                        if (entity.has_component<Aggro>()) {
                                auto ppos = game::engine.getSystem<PlayerSystem>()->getPosition();
-                               if (ppos.x > position.x && ppos.x < position.x + entity.component<Solid>()->width)
-                                       arena = entity.component<Aggro>()->arena;
-                               else
+                               if (ppos.x > position.x && ppos.x < position.x + entity.component<Solid>()->width) {
+                                       auto& h = entity.component<Health>()->health;
+                                       if (h > 0) {
+                                               arena = entity.component<Aggro>()->arena;
+                                               h = 0;
+                                       }
+                               } else
                                        direction.x = (ppos.x > position.x) ? .05 : -.05;
                        } else if (entity.has_component<Wander>()) {
                                auto& countdown = entity.component<Wander>()->countdown;
@@ -164,7 +168,7 @@ void RenderSystem::render(void)
                        glUniform1i(Render::worldShader.uniform[WU_texture], 0);
 
                        glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
-                       glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord);
+                       glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0tex_coord);
                        glDrawArrays(GL_TRIANGLES, 0, 6);
 
                        //glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0);
@@ -172,6 +176,26 @@ void RenderSystem::render(void)
                        its-=.01;
                }
                glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f)));
+
+               if (entity.has_component<Health>()) {
+                       float width = entity.component<Solid>()->width;
+                       auto& health = *entity.component<Health>();
+                       width /= health.health / health.maxHealth;
+
+                       GLfloat health_coord[] = {
+                               pos.x, pos.y, -9, 0, 0,
+                               pos.x + width, pos.y, -9, 0, 0,
+                               pos.x + width, pos.y - 5, -9, 0, 0,
+                               pos.x + width, pos.y - 5, -9, 0, 0,
+                               pos.x, pos.y - 5, -9, 0, 0,
+                               pos.x, pos.y, -9, 0, 0,
+                       };
+
+                       Colors::red.use();
+                       glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), health_coord);
+                       glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), health_coord + 3);
+                       glDrawArrays(GL_TRIANGLES, 0, 6);
+               }
        });
 
        Render::worldShader.disable();
index da719148daf9e28dc45dd5147d263e242ac0874a..cd939bbe2b7d04a51b40fa46643c55b653ee0e09 100644 (file)
@@ -72,10 +72,10 @@ void PlayerSystem::create(void)
        player = game::entities.create();
        player.assign<Position>(0.0f, 100.0f);
        player.assign<Direction>(0.0f, 0.0f);
-       // The new g value is a multiplier for the gravity constant. This allows for air resistance simulation.
        //player.assign<Physics>(-0.001f);
        player.assign<Physics>(1);
        player.assign<Visible>(-0.2f);
+       player.assign<Health>(100);
        auto sprite = player.assign<Sprite>();
        XMLDocument xmld;
        xmld.Parse(spriteXML);
index f2c250b4e213541b516f040114d18b425013be53..049ee69f530043239fed66dd24d16c9e1744b473 100644 (file)
@@ -82,21 +82,30 @@ void WindowSystem::receive(const WindowResizeEvent &wre)
 
 #include <ui.hpp>
 
+#include <atomic>
+
+static std::atomic_bool doScreenshot;
+
 void WindowSystem::receive(const ScreenshotEvent &scr)
 {
-       // Make the BYTE array, factor of 3 because it's RBG.
-       static GLubyte* pixels;
-       pixels = new GLubyte[ 3 * scr.w * scr.h];
-       //glReadPixels(0, 0, scr.w, scr.h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
-       for(int i = 0; i < (3 * scr.w * scr.h); i++) {
-               pixels[i] = 255;
-       }
-
-       ui::takeScreenshot(pixels);
-       std::cout << "Triggered\n";
+       (void)scr;
+       doScreenshot.store(true);
 }
 
 void WindowSystem::render(void)
 {
+       if (doScreenshot.load()) {
+               doScreenshot.store(false);
+               // Make the BYTE array, factor of 3 because it's RBG.
+               static GLubyte* pixels;
+               int count = 3 * game::SCREEN_WIDTH * game::SCREEN_HEIGHT;
+               pixels = new GLubyte[count];
+               glReadPixels(0, 0, game::SCREEN_WIDTH, game::SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+               //for(int i = 0; i < count; i++)
+               //      pixels[i] = 255;
+               ui::takeScreenshot(pixels);
+               std::cout << "Triggered\n";
+       }
+
     SDL_GL_SwapWindow(window);
 }
index 09d277fb778cae6be632420ffc52a45135054f57..b0e28a39c87a1734a73d4fc352a873ff7abe1a0d 100644 (file)
@@ -144,7 +144,10 @@ bool WorldSystem::save(void)
                // save dialog, if exists
                if (entity.has_component<Dialog>())
                        save << "d " << entity.component<Dialog>()->index << ' ';
-       });
+
+               if (entity.has_component<Health>())
+                       save << "h " << entity.component<Health>()->health << ' ';
+       }, true);
 
        save.close();
        return true;
@@ -160,6 +163,9 @@ void WorldSystem::load(const std::string& file)
        if (file.empty())
                return;
 
+       // insert smiley face showing teeth as if we're doing something bad
+       save();
+
        // load file data to string
        auto xmlPath = xmlFolder + file;
        auto xmlRaw = readFile(xmlPath);
@@ -355,6 +361,8 @@ void WorldSystem::load(const std::string& file)
                                                entity.assign<Wander>();
                                        } else if (tname == "Hop" ) {
                                                entity.assign<Hop>();
+                                       } else if (tname == "Health") {
+                                               entity.assign<Health>();
                                        } else if (tname == "Aggro" ) {
                                                entity.assign<Aggro>(abcd->Attribute("arena"));
                                        } else if (tname == "Animation") {
@@ -435,11 +443,14 @@ void WorldSystem::load(const std::string& file)
                        save >> pos->x >> pos->y;
                        save >> id;
 
-                       while (id != 'p') {
+                       while (!save.eof() && id != 'p') {
                                switch (id) {
                                case 'd':
                                        save >> entity.component<Dialog>()->index;
                                        break;
+                               case 'h':
+                                       save >> entity.component<Health>()->health;
+                                       break;
                                }
 
                                save >> id;
@@ -1115,6 +1126,13 @@ void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
 
 void WorldSystem::detect(entityx::TimeDelta dt)
 {
+       game::entities.each<Health>(
+               [](entityx::Entity e, Health& h) {
+               if (h.health <= 0)
+                       e.kill();
+                       //e.destroy();
+       });
+
        game::entities.each<Grounded, Position, Solid>(
                [&](entityx::Entity e, Grounded &g, Position &loc, Solid &dim) {
                (void)e;
index 7195b518954f5cf9d50e9acf05e7ddbce0db2efc..0d3c80b3752462de8d11c48ad562d6d9aa1dbb9e 100644 (file)
@@ -62,6 +62,7 @@
                </frame>
        </Sprite>
        <Direction />
+       <Health />
        <Solid />
        <Physics />
        <Name value="SKIRL" />