diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-02-13 16:57:19 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-02-13 16:57:19 -0500 |
commit | 183331ccf1aa3f4142697e9e37528f084fae7466 (patch) | |
tree | c3c2f2a26b4b85df9ec809f40443dce0d115b0b8 | |
parent | 3eccd38e989012ff35ee5376670aabc338c52008 (diff) |
rip skirl
-rw-r--r-- | entityx/Entity.cc | 5 | ||||
-rw-r--r-- | entityx/Entity.h | 16 | ||||
-rw-r--r-- | include/components.hpp | 7 | ||||
-rw-r--r-- | src/components.cpp | 32 | ||||
-rw-r--r-- | src/player.cpp | 2 | ||||
-rw-r--r-- | src/window.cpp | 29 | ||||
-rw-r--r-- | src/world.cpp | 22 | ||||
-rw-r--r-- | xml/entities.xml | 1 |
8 files changed, 88 insertions, 26 deletions
diff --git a/entityx/Entity.cc b/entityx/Entity.cc index adac3de..476def7 100644 --- a/entityx/Entity.cc +++ b/entityx/Entity.cc @@ -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_); } diff --git a/entityx/Entity.h b/entityx/Entity.h index a423f8f..e7d2e8d 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -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); } /** diff --git a/include/components.hpp b/include/components.hpp index ebc256d..3169a6d 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -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 { diff --git a/src/components.cpp b/src/components.cpp index 3b38a53..a66a1db 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -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, 0, tex_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(); diff --git a/src/player.cpp b/src/player.cpp index da71914..cd939bb 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -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); diff --git a/src/window.cpp b/src/window.cpp index f2c250b..049ee69 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -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); } diff --git a/src/world.cpp b/src/world.cpp index 09d277f..b0e28a3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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; diff --git a/xml/entities.xml b/xml/entities.xml index 7195b51..0d3c80b 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -62,6 +62,7 @@ </frame> </Sprite> <Direction /> + <Health /> <Solid /> <Physics /> <Name value="SKIRL" /> |