]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
wandering
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 26 Nov 2016 00:29:33 +0000 (19:29 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 26 Nov 2016 00:29:33 +0000 (19:29 -0500)
include/components.hpp
include/world.hpp
main.cpp
src/components.cpp
src/player.cpp
src/world.cpp
xml/entities.xml

index 1c83a344b35f0acf21ec52c9aa2565ebde459f63..58f5aea568640c21f0856d926f4de5fe5d4fe892 100644 (file)
@@ -111,7 +111,7 @@ struct Solid {
         */
        Solid(float w = 0.0f, float h = 0.0f): width(w), height(h) {offset = 0.0f; passable = true; }
        //Solid(float w = 0.0f, float h = 0.0f, vec2 offset = 0.0f): width(w), height(h), offset(offset) {passable = true; }
-       
+
        void Passable(bool v) {passable = v;}
        bool Passable(void)   {return passable;}
 
@@ -122,13 +122,13 @@ struct Solid {
 };
 
 struct SpriteData {
-       
-       SpriteData(std::string path, vec2 offset): 
+
+       SpriteData(std::string path, vec2 offset):
                offset(offset) {
                        pic = Texture::loadTexture(path);
                        size = Texture::imageDim(path);
                }
-       
+
        GLuint pic;
        vec2 offset;
        vec2 size;
@@ -177,7 +177,7 @@ struct Sprite {
        vec2 getSpriteSize() {
                vec2 st; /** the start location of the sprite */
                vec2 dim; /** how wide the sprite is */
-               
+
                if (sprite.size()) {
                        st.x = sprite[0].second.x;
                        st.y = sprite[0].second.y;
@@ -238,6 +238,28 @@ struct Dialog {
        int rindex;
 };
 
+
+
+// movement styles
+
+/**
+ * Causes the entity to hop around.
+ */
+struct Hop {}; // TODO require wander, for range?
+
+/**
+ * Causes the entity to wander about.
+ */
+struct Wander {
+       Wander(float ix = 0, float r = 0)
+               : initialX(ix), range(r), countdown(0) {}
+
+       float initialX;
+       float range;
+       int countdown;
+};
+
+
 /**
  * SYSTEMS
  */
index 308222d497238a623a34a0e29b9843b9e5ad37b9..cb8c30d40939a1436d1b592648e5a037cfaf5f07 100644 (file)
@@ -175,7 +175,7 @@ public:
                ev.subscribe<BGMToggleEvent>(*this);
        }
 
-       inline XMLDocument* getXML(void) 
+       inline XMLDocument* getXML(void)
        { return &xmlDoc; }
 
        inline float getWidth(void) const
@@ -210,7 +210,7 @@ public:
        void addHole(const unsigned int& start, const unsigned int& end);
        void addHill(const ivec2& peak, const unsigned int& width);
 
-       bool save(const std::string& file);
+       bool save(void);
        void load(const std::string& file);
 };
 
index ca65dd3eb40869cc7e76d4f87a21cf3ea49ba8c2..2205897daf5dda8a25c4fe536140944e3a4dd3aa 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -235,7 +235,7 @@ EXIT_ROUTINE:
     Texture::freeTextures();
 
        // close up the game stuff
-//     currentWorld->save();
+       game::engine.getSystem<WorldSystem>()->save();
 
        game::engine.getSystem<WindowSystem>()->die();
 
index 7c6091ed0299b81105d23e3098a038d1fb7a85bb..5b85f51a68c9cb4a8a0fc74d7e77dcd6b91da726 100644 (file)
@@ -15,9 +15,25 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e
 {
        (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;
+
+               if (entity.has_component<Sprite>()) {
+                       auto& fl = entity.component<Sprite>()->faceLeft;
+                       if (direction.x != 0)
+                               fl = (direction.x < 0);
+               }
+
+               if (entity.has_component<Wander>()) {
+                       auto& countdown = entity.component<Wander>()->countdown;
+
+                       if (countdown > 0) {
+                               countdown--;
+                       } else {
+                               countdown = 5000 + randGet() % 10 * 100;
+                               direction.x = (randGet() % 3 - 1) * 0.02f;
+                       }
+               }
        });
 }
 
@@ -26,7 +42,7 @@ void PhysicsSystem::update(entityx::EntityManager &en, entityx::EventManager &ev
        (void)ev;
        en.each<Direction, Physics>([dt](entityx::Entity entity, Direction &direction, Physics &physics) {
                (void)entity;
-               // TODO GET GRAVITY FROM WOLRD
+               // TODO GET GRAVITY FROM WORLD
                direction.y += physics.g * dt;
        });
 }
@@ -168,4 +184,3 @@ void DialogSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
        (void)ev;
        (void)dt;
 }
-
index 1bbfaeaab9ead5479b0b2dca0f726bf3024b2fc8..2694a549ba3bd3bca25ccf0eed64686fe5eb256a 100644 (file)
@@ -85,7 +85,6 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
        auto kc = kde.keycode;
        auto& loc = *player.component<Position>().get();
        auto& vel = *player.component<Direction>().get();
-    auto& faceLeft = player.component<Sprite>().get()->faceLeft;
 
        if ((kc == SDLK_SPACE) && game::canJump && ((vel.y > -0.01) & (vel.y < 0.01))) {
                loc.y += HLINES(2);
@@ -97,14 +96,14 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
 
                } else if (kc == getControl(1)) {
                        if (!ui::fadeEnable) {
-                moveLeft = faceLeft = true;
+                moveLeft = true;
                                moveRight = false;
 
                                worldSystem.goWorldLeft(loc);
                        }
                } else if (kc == getControl(2)) {
                        if (!ui::fadeEnable) {
-                               moveLeft = faceLeft = false;
+                               moveLeft = false;
                 moveRight = true;
 
                                worldSystem.goWorldRight(loc, *player.component<Solid>().get());
index a50a04d90df646869a036a69f8596bd0899008fe..5f7f0a6688ae8491bc58247f8407e5c62cc17e86 100644 (file)
@@ -151,13 +151,23 @@ void WorldSystem::generate(unsigned int width)
 
 static Color ambient;
 
-bool WorldSystem::save(const std::string& s)
+bool WorldSystem::save(void)
 {
-       (void)s;
-       /*for (const auto &e : entity)
-               e->saveToXML();
+       std::ofstream save (xmlFolder + currentXMLFile + ".dat");
 
-       currentXMLDoc.SaveFile((s.empty() ? currentXML : xmlFolder + s).c_str(), false);*/
+       // signature?
+       save << "831998 ";
+
+       game::entities.each<Position>([&](entityx::Entity entity, Position& pos) {
+               // save position
+               save << "p " << pos.x << ' ' << pos.y << ' ';
+
+               // save dialog, if exists
+               if (entity.has_component<Dialog>())
+                       save << "d " << entity.component<Dialog>()->index << ' ';
+       });
+
+       save.close();
        return false;
 }
 
@@ -356,6 +366,8 @@ void WorldSystem::load(const std::string& file)
                                                entity.assign<Dialog>((wxml->BoolAttribute("hasDialog") ? 0 : 9999));
                                        } else if (tname == "Grounded") {
                                                entity.assign<Grounded>();
+                                       } else if (tname == "Wander") {
+                                               entity.assign<Wander>();
                                        }
 
                                        abcd = abcd->NextSiblingElement();
@@ -374,6 +386,37 @@ void WorldSystem::load(const std::string& file)
                wxml = wxml->NextSiblingElement();
        }
 
+       // attempt to load data
+       std::ifstream save (xmlFolder + currentXMLFile + ".dat");
+       if (save.good()) {
+               // check signature
+               int signature;
+               save >> signature;
+               if (signature != 831998)
+                       UserError("Save file signature is invalid... (delete it)");
+
+               char id;
+               save >> id;
+
+               entityx::ComponentHandle<Position> pos;
+               for (entityx::Entity entity : game::entities.entities_with_components(pos)) {
+                       save >> pos->x >> pos->y;
+                       save >> id;
+
+                       while (id != 'p') {
+                               switch (id) {
+                               case 'd':
+                                       save >> entity.component<Dialog>()->index;
+                                       break;
+                               }
+
+                               save >> id;
+                       }
+               }
+
+               save.close();
+       }
+
        game::events.emit<BGMToggleEvent>();
 }
 
@@ -1195,12 +1238,12 @@ void WorldSystem::detect(entityx::TimeDelta dt)
 
 void WorldSystem::goWorldRight(Position& p, Solid &d)
 {
-       if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(15))) {
+       if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(5))) {
                ui::toggleBlack();
                ui::waitForCover();
                auto file = world.toRight;
                load(file);
-               //game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(15));
+               game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(10));
                ui::toggleBlack();
        }
 }
@@ -1211,7 +1254,7 @@ void WorldSystem::goWorldLeft(Position& p)
                ui::toggleBlack();
                ui::waitForCover();
                load(world.toLeft);
-               //game::engine.getSystem<PlayerSystem>()->setX(world.startX * -1 - HLINES(15));
+               game::engine.getSystem<PlayerSystem>()->setX(world.startX * -1 - HLINES(15));
                ui::toggleBlack();
        }
 }
index ad5009c3c7194b7502d0c8771549610beb1f9bbc..60b4424ed0609afa64ad00b32090657f7175c683 100644 (file)
@@ -9,6 +9,7 @@
        <Physics />
        <Name value="Daddy" />
        <Dialog />
+       <Wander />
 </npc>
 
 <structure>