]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
CATATATATATATATTTTT (basic entities)
authorAndy <drumsetmonkey@gmail.com>
Wed, 19 Oct 2016 14:46:12 +0000 (10:46 -0400)
committerAndy <drumsetmonkey@gmail.com>
Wed, 19 Oct 2016 14:46:12 +0000 (10:46 -0400)
include/common.hpp
include/components.hpp
include/engine.hpp
include/entities.hpp
include/texture.hpp
main.cpp
src/components.cpp [new file with mode: 0644]
src/engine.cpp
src/entities.cpp
xml/!town.xml

index 69e6a11f81678d7f020d4161164cee02e71ea5a9..e7ed8e1d197f769dc21bebc6a5ba1931ab3060b8 100644 (file)
@@ -14,6 +14,7 @@
 #include <algorithm>
 #include <list>
 #include <iterator>
+#include <unordered_map>
 
 // alternative windows thread library
 #ifndef __WIN32__
index 5a638de8a9d8d9df2ff8819ca2d976f5cc4a647f..31ff2838b94abf24e14c6e56773449f54788714b 100644 (file)
@@ -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
index 2bde6de2b95129dfef90151f773da9d43cfe8c83..e5646916ab10ba2b7411b408eaea59a2818d7ad7 100644 (file)
@@ -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;
 }
 
 
index 49598bdd3b292bfb804c86e01b14a252ae3a1bb0..95cbff78ae922d1b7c77a2500e96c339cad1f89c 100644 (file)
@@ -517,6 +517,7 @@ public:
        void receive(const KeyUpEvent &kue);
 };
 
+void entityxTest();
 
 #endif // ENTITIES_H
 
index 7f22a790ade742c492006820c01628b3367f40fb..7426a46bf5755572fa4328217984ee986b67614f 100644 (file)
@@ -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?
  */
index 91a2759e835e1726aa11328bd1ff691b39652a4a..85af92531cb930cda3e2651be56132f5245411fb 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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 (file)
index 0000000..a1e3e45
--- /dev/null
@@ -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();
+}*/
index 56dee480715cdb43afa0083e7fbff58126aae52a..3705d4f857015dd8b47f70992c5fb4e472dba6cd 100644 (file)
@@ -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;
 }
index 6b9c3fe0b4a4431d8b62a7cc2bccfcd5847c840f..a03b4ff995bfbad87ab4e8e9e6dba8772ce3f1de 100644 (file)
@@ -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));
+}
index 12cc438aa10ed32bb3d788d5db76c5905abc2a44..bd0264e2d6e87983e0f582a3e143ac9e329de022 100644 (file)
@@ -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"/>