From aff1681f6638f591e2c8281f63847550115a2a19 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 19 Oct 2016 10:46:12 -0400 Subject: CATATATATATATATTTTT (basic entities) --- include/common.hpp | 1 + include/components.hpp | 38 +++++++- include/engine.hpp | 9 +- include/entities.hpp | 1 + include/texture.hpp | 36 ++++++++ main.cpp | 3 + src/components.cpp | 230 +++++++++++++++++++++++++++++++++++++++++++++++++ src/engine.cpp | 11 ++- src/entities.cpp | 19 ++++ xml/!town.xml | 4 +- 10 files changed, 346 insertions(+), 6 deletions(-) create mode 100644 src/components.cpp diff --git a/include/common.hpp b/include/common.hpp index 69e6a11..e7ed8e1 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -14,6 +14,7 @@ #include #include #include +#include // alternative windows thread library #ifndef __WIN32__ diff --git a/include/components.hpp b/include/components.hpp index 5a638de..31ff283 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -10,6 +10,7 @@ #define COMPONENTS_HPP #include +#include /** * @struct Position @@ -91,7 +92,11 @@ struct Solid { }; struct SpriteData { - uint sheetID; + + SpriteData(uint64_t sid = 0, vec2 offset = 0.0f, vec2 size = 0.0f): + sheetID(sid), offset(offset), size(size) {} + + uint64_t sheetID; vec2 offset; vec2 size; }; @@ -148,4 +153,35 @@ struct Input { }; +/** + * @struct Visible + * @brief If an entity is visible we want to be able to draw it. + */ +struct Visible { + /** + * @brief Decide what layer to draw the entity on. + * When stuff is drawn, it is drawn on a "layer". This layer gives more of a 3D effect to the world. + * @param z The desired "layer" of the entity. + */ + Visible(float z = 0.0f): z(z) {} + + float z; /**< The value along the z axis the entity will be drawn on */ +}; + +/** + * SYSTEMS + */ + +class MovementSystem : public entityx::System { +private: +public: + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; +}; + +class RenderSystem : public entityx::System { +private: +public: + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; +}; + #endif //COMPONENTS_HPP diff --git a/include/engine.hpp b/include/engine.hpp index 2bde6de..e564691 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -2,9 +2,14 @@ #define ENGINE_HPP_ #include +#include "entityx/deps/Dependencies.h" +#include +#include #include +//game::engine::Systems->add>(); + class Engine : public entityx::Receiver { private: bool gameRunning; @@ -38,7 +43,7 @@ public: namespace game { - extern entityx::EventManager events; + extern entityx::EventManager events; extern entityx::EntityManager entities; extern Engine engine; @@ -46,6 +51,8 @@ namespace game { inline void endGame(void) { events.emit(); } + + extern SpriteLoader sprite_l; } diff --git a/include/entities.hpp b/include/entities.hpp index 49598bd..95cbff7 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -517,6 +517,7 @@ public: void receive(const KeyUpEvent &kue); }; +void entityxTest(); #endif // ENTITIES_H diff --git a/include/texture.hpp b/include/texture.hpp index 7f22a79..7426a46 100644 --- a/include/texture.hpp +++ b/include/texture.hpp @@ -35,6 +35,42 @@ namespace Texture { dim2 imageDim(std::string fileName); } +class SpriteLoader { +private: + std::unordered_map sprites; + std::unordered_map spritesLoc; + + uint64_t freeID = 0; + uint64_t increaseID() { + uint64_t id_t = 0; + while (1) { + try { + sprites.at(id_t); + } catch (const std::out_of_range& oor) { + freeID = id_t; + return freeID; + } + id_t++; + } + } +public: + uint64_t loadSprite(std::string s) { + uint64_t tex_e; + try { + tex_e = spritesLoc.at(s); + } catch (const std::out_of_range& oor) { + sprites.emplace(increaseID(), Texture::loadTexture (s)); + spritesLoc.emplace(s, freeID); + return freeID; + } + return tex_e; + } + + GLuint getSprite(uint64_t id) { + return sprites.at(id); + } +}; + /** * DRAFT texture iterator? */ diff --git a/main.cpp b/main.cpp index 91a2759..85af925 100644 --- a/main.cpp +++ b/main.cpp @@ -221,6 +221,9 @@ int main(int argc, char *argv[]) game::engine.getSystem()->setWorld(currentWorld); + //TODO + entityxTest(); + // spawn the arena arena = new Arena(); arena->setStyle(""); diff --git a/src/components.cpp b/src/components.cpp new file mode 100644 index 0000000..a1e3e45 --- /dev/null +++ b/src/components.cpp @@ -0,0 +1,230 @@ +#include + +#include +#include + +#include +#include + +void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)ev; + en.each([dt](entityx::Entity entity, Position &position, Direction &direction) { + (void)entity; + position.x += direction.x * dt; + position.y += direction.y * dt; + }); +} + +void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)ev; + Render::worldShader.use(); + + en.each([dt](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) { + (void)entity; + // Verticies and shit + GLfloat tex_coord[] = {0.0, 0.0, + 1.0, 0.0, + 1.0, 1.0, + + 1.0, 1.0, + 0.0, 1.0, + 0.0, 0.0}; + + GLfloat tex_coordL[] = {1.0, 0.0, + 0.0, 0.0, + 0.0, 1.0, + + 0.0, 1.0, + 1.0, 1.0, + 1.0, 0.0}; + + for (auto &S : sprite.sprite) { + float width = S.first.size.x; + float height = S.first.size.y; + + vec2 loc = vec2(pos.x + S.first.offset.x, pos.y + S.first.offset.y); + + GLfloat coords[] = {loc.x, loc.y, visible.z, + loc.x + width, loc.y, visible.z, + loc.x + width, loc.y + height, visible.z, + + loc.x + width, loc.y + height, visible.z, + loc.x, loc.y + height, visible.z, + loc.x, loc.y, visible.z}; + + + // make the entity hit flash red + // TODO + /*if (maxHitDuration-hitDuration) { + float flashAmt = 1-(hitDuration/maxHitDuration); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, flashAmt, flashAmt, 1.0); + }*/ + glBindTexture(GL_TEXTURE_2D, game::sprite_l.getSprite(S.first.sheetID)); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); + Render::worldShader.enable(); + + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + if (false) + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL); + else + 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); + } + }); + + Render::worldShader.disable(); + Render::worldShader.unuse(); +} + +/* +void Entity::draw(void) +{ + GLfloat tex_coord[] = {0.0, 0.0, + 1.0, 0.0, + 1.0, 1.0, + + 1.0, 1.0, + 0.0, 1.0, + 0.0, 0.0}; + + GLfloat tex_coordL[] = {1.0, 0.0, + 0.0, 0.0, + 0.0, 1.0, + + 0.0, 1.0, + 1.0, 1.0, + 1.0, 0.0}; + + GLfloat coords[] = {loc.x, loc.y, z, + loc.x + width, loc.y, z, + loc.x + width, loc.y + height, z, + + loc.x + width, loc.y + height, z, + loc.x, loc.y + height, z, + loc.x, loc.y, z}; + + + glActiveTexture(GL_TEXTURE0); + + if (!alive) + return; + + if (type == NPCT) { + NPCp(this)->drawThingy(); + + if (gender == MALE) + glColor3ub(255, 255, 255); + else if (gender == FEMALE) + glColor3ub(255, 105, 180); + } else if (type == MOBT) { + if (Mobp(this)->rider != nullptr) { + Mobp(this)->rider->loc.x = loc.x + width * 0.25f; + Mobp(this)->rider->loc.y = loc.y + height - HLINES(5); + Mobp(this)->rider->vel.y = .12; + Mobp(this)->rider->z = z + 0.01; + } + } + switch(type) { + case PLAYERT: + static int texState = 0; + if (speed && !(game::time::getTickCount() % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))) { + if (++texState == 9) + texState = 1; + glActiveTexture(GL_TEXTURE0); + tex(texState); + } + if (!ground) { + glActiveTexture(GL_TEXTURE0); + tex(0); + } else if (vel.x) { + glActiveTexture(GL_TEXTURE0); + tex(texState); + } else { + glActiveTexture(GL_TEXTURE0); + tex(0); + } + break; + case MOBT: + if (!Mobp(this)->bindTex()) + goto NOPE; + break; + case STRUCTURET: + default: + glActiveTexture(GL_TEXTURE0); + tex(0); + break; + } + + Render::worldShader.use(); + // make the entity hit flash red + if (maxHitDuration-hitDuration) { + float flashAmt = 1-(hitDuration/maxHitDuration); + glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, flashAmt, flashAmt, 1.0); + } + + glUniform1i(Render::worldShader.uniform[WU_texture], 0); + Render::worldShader.enable(); + + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + if (left) + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL); + else + 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); +NOPE: +if (near && type != MOBT) + ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-game::HLINE/2,name); +if (health != maxHealth) { + + static GLuint frontH = Texture::genColor(Color(255,0,0)); + static GLuint backH = Texture::genColor(Color(150,0,0)); + glUniform1i(Render::worldShader.uniform[WU_texture], 0); + + GLfloat coord_back[] = { + loc.x, loc.y + height, z + 0.1f, + loc.x + width, loc.y + height, z + 0.1f, + loc.x + width, loc.y + height + game::HLINE * 2, z + 0.1f, + + loc.x + width, loc.y + height + game::HLINE * 2, z + 0.1f, + loc.x, loc.y + height + game::HLINE * 2, z + 0.1f, + loc.x, loc.y + height, z + 0.1f, + }; + + GLfloat coord_front[] = { + loc.x, loc.y + height, z, + loc.x + health / maxHealth * width, loc.y + height, z, + loc.x + health / maxHealth * width, loc.y + height + game::HLINE * 2, z, + + loc.x + health / maxHealth * width, loc.y + height + game::HLINE * 2, z, + loc.x, loc.y + height + game::HLINE * 2, z, + loc.x, loc.y + height, z, + }; + + glBindTexture(GL_TEXTURE_2D, backH); + GLfloat tex[] = { 0.0, 0.0, + 1.0, 0.0, + 1.0, 1.0, + + 1.0, 1.0, + 0.0, 1.0, + 0.0, 0.0, + }; + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord_back); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glDrawArrays(GL_TRIANGLES, 0, 6); + + glBindTexture(GL_TEXTURE_2D, frontH); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord_front); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glDrawArrays(GL_TRIANGLES, 0, 6); +} + +Render::worldShader.disable(); +Render::worldShader.unuse(); +}*/ diff --git a/src/engine.cpp b/src/engine.cpp index 56dee48..3705d4f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -6,6 +6,7 @@ #include #include #include +#include extern World *currentWorld; @@ -19,9 +20,11 @@ void Engine::init(void) { game::events.subscribe(*this); systems.add(); - systems.add(); + systems.add(); + systems.add(); systems.add(); systems.add(); + systems.add(); systems.add(&player); systems.configure(); @@ -31,7 +34,9 @@ void Engine::init(void) { void Engine::render(entityx::TimeDelta dt) { - systems.update(dt); + systems.update(dt); + systems.update(dt); + } void Engine::update(entityx::TimeDelta dt) @@ -39,6 +44,7 @@ void Engine::update(entityx::TimeDelta dt) systems.update(dt); systems.update(dt); systems.update(dt); + systems.update(dt); systems.update(dt); } @@ -46,6 +52,7 @@ void Engine::update(entityx::TimeDelta dt) namespace game { entityx::EventManager events; entityx::EntityManager entities (events); + SpriteLoader sprite_l; Engine engine; } diff --git a/src/entities.cpp b/src/entities.cpp index 6b9c3fe..a03b4ff 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -1348,3 +1348,22 @@ void Player::sspawn(float x,float y) { in.close(); } } + + +//NEW +void entityxTest(void) +{ + entityx::Entity e = game::entities.create(); + e.assign(100.0f, 100.0f); + e.assign(0.0f, 0.0f); + + e = game::entities.create(); + e.assign(0.0f, 100.0f); + e.assign(-0.01f, 0.0f); + e.assign(-.2f); + auto sprite_h = e.assign(); + sprite_h->addSpriteSegment(SpriteData(game::sprite_l.loadSprite("assets/cat.png"), + vec2(0, 0), + vec2(19, 15)), + vec2(0, 0)); +} diff --git a/xml/!town.xml b/xml/!town.xml index 12cc438..bd0264e 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -4,8 +4,8 @@ -300 - - + + -- cgit v1.2.3