diff options
-rw-r--r-- | include/common.hpp | 1 | ||||
-rw-r--r-- | include/components.hpp | 38 | ||||
-rw-r--r-- | include/engine.hpp | 9 | ||||
-rw-r--r-- | include/entities.hpp | 1 | ||||
-rw-r--r-- | include/texture.hpp | 36 | ||||
-rw-r--r-- | main.cpp | 3 | ||||
-rw-r--r-- | src/components.cpp | 230 | ||||
-rw-r--r-- | src/engine.cpp | 11 | ||||
-rw-r--r-- | src/entities.cpp | 19 | ||||
-rw-r--r-- | xml/!town.xml | 4 |
10 files changed, 346 insertions, 6 deletions
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 <algorithm> #include <list> #include <iterator> +#include <unordered_map> // 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 <entityx/entityx.h> +#include <common.hpp> /** * @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<MovementSystem> { +private: +public: + void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; +}; + +class RenderSystem : public entityx::System<RenderSystem> { +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 <entityx/entityx.h> +#include "entityx/deps/Dependencies.h" +#include <texture.hpp> +#include <components.hpp> #include <events.hpp> +//game::engine::Systems->add<entityx::deps::Dependency<Visible, Sprite>>(); + class Engine : public entityx::Receiver<Engine> { 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<GameEndEvent>(); } + + 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<uint64_t, GLuint> sprites; + std::unordered_map<std::string, uint64_t> 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? */ @@ -221,6 +221,9 @@ int main(int argc, char *argv[]) game::engine.getSystem<WorldSystem>()->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 <components.hpp> + +#include <entityx/entityx.h> +#include <events.hpp> + +#include <render.hpp> +#include <engine.hpp> + +void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)ev; + en.each<Position, Direction>([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<Visible, Sprite, Position>([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 <inventory.hpp> #include <entities.hpp> #include <window.hpp> +#include <components.hpp> extern World *currentWorld; @@ -19,9 +20,11 @@ void Engine::init(void) { game::events.subscribe<GameEndEvent>(*this); systems.add<WindowSystem>(); - systems.add<InputSystem>(); + systems.add<RenderSystem>(); + systems.add<InputSystem>(); systems.add<InventorySystem>(); systems.add<WorldSystem>(); + systems.add<MovementSystem>(); systems.add<PlayerSystem>(&player); systems.configure(); @@ -31,7 +34,9 @@ void Engine::init(void) { void Engine::render(entityx::TimeDelta dt) { - systems.update<WindowSystem>(dt); + systems.update<RenderSystem>(dt); + systems.update<WindowSystem>(dt); + } void Engine::update(entityx::TimeDelta dt) @@ -39,6 +44,7 @@ void Engine::update(entityx::TimeDelta dt) systems.update<InputSystem>(dt); systems.update<InventorySystem>(dt); systems.update<PlayerSystem>(dt); + systems.update<MovementSystem>(dt); systems.update<WorldSystem>(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<Position>(100.0f, 100.0f); + e.assign<Direction>(0.0f, 0.0f); + + e = game::entities.create(); + e.assign<Position>(0.0f, 100.0f); + e.assign<Direction>(-0.01f, 0.0f); + e.assign<Visible>(-.2f); + auto sprite_h = e.assign<Sprite>(); + 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 @@ <generation type="Random" width="1600"/> <time>6000</time> <spawnx>-300</spawnx> - <npc name="Sanc" hasDialog="true" health="1" x="23.971899" y="59.998001" dindex="9999"/> - <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="308.18121" y="60.597992" dindex="0"/> + <npc name="Sanc" hasDialog="true" health="1" x="-208.37956" y="65.399033" dindex="9999"/> + <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="283.33429" y="67.798996" dindex="0"/> <structure type="1" spawnx="300" alive="1"/> <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/> <chest alive="1"/> |