]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
npc dialog
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 28 Oct 2016 13:18:53 +0000 (09:18 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 28 Oct 2016 13:18:53 +0000 (09:18 -0400)
include/components.hpp
include/events.hpp
include/world.hpp
shaders/new.frag
src/components.cpp
src/engine.cpp
src/ui.cpp
src/world.cpp
xml/!town.xml
xml/entities.xml

index 11953372e3236d4dd40293b10bfb4da2afa0e7c7..474a0ebb5f7895bd1949f59b8443746e00595295 100644 (file)
@@ -12,6 +12,7 @@
 #include <entityx/entityx.h>
 #include <common.hpp>
 #include <texture.hpp>
+#include <events.hpp>
 
 /**
  * @struct Position
@@ -220,25 +221,37 @@ struct Visible {
        float z; /**< The value along the z axis the entity will be drawn on */
 };
 
+struct Dialog {
+       Dialog(int idx = 0)
+               : index(idx) {}
+
+       int index;
+};
+
 /**
  * SYSTEMS
  */
 
 class MovementSystem : public entityx::System<MovementSystem> {
-private:
 public:
        void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
 };
 
 class PhysicsSystem : public entityx::System<PhysicsSystem> {
-private:
 public:
-       void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt);
+       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;
 };
 
+class DialogSystem : public entityx::System<DialogSystem>, public entityx::Receiver<DialogSystem> {
+public:
+       void configure(entityx::EventManager&);
+       void receive(const MouseClickEvent&);
+       void update(entityx::EntityManager&, entityx::EventManager&, entityx::TimeDelta) override;
+};
+
 #endif //COMPONENTS_HPP
index 6a9bd8caf83e953bdc57a04a40e86421cd33907f..8a096381d356590b3f58136bbb57950ed62f9755 100644 (file)
@@ -8,6 +8,7 @@
 #include <SDL2/SDL.h>
 
 #include <string>
+#include <common.hpp>
 
 class World;
 
@@ -18,6 +19,14 @@ struct MouseScrollEvent {
        int scrollDistance;
 };
 
+struct MouseClickEvent {
+       MouseClickEvent(vec2 pos, int b)
+               : position(pos), button(b) {}
+
+       vec2 position;
+       int button;
+};
+
 struct KeyDownEvent {
     KeyDownEvent(SDL_Keycode kc = 0)
         : keycode(kc) {}
index 421031c5619d1264e2ebe75d786acf2c172ab6a4..8d88e5a34b99b8946f8be5d60ab1f12c0e0f5698 100644 (file)
@@ -144,6 +144,9 @@ public:
                ev.subscribe<BGMToggleEvent>(*this);
        }
 
+       inline XMLDocument* getXML(void) 
+       { return &xmlDoc; }
+
        inline float getWidth(void) const
        { return world.startX * -2.0f; }
 
index 3537e265045bd024d77e6831dd9dbdea5ec6562d..0e4ed054433553512c89beca6f15598ff7f6e6d7 100644 (file)
@@ -6,7 +6,7 @@ varying vec4 color;
 void main(){
     vec4 pixelColor = texture2D(sampler, vec2(texCoord.x, texCoord.y));
     //TODO
-       if (pixelColor.w != 1.0f)
-               discard;
+       //if (pixelColor.w != 1.0f)
+       //      discard;
        gl_FragColor = pixelColor * color;
 }
index e00d474ad1f76eb42b87d683f35ac644cdff2f9a..220ecabaebd6a14f181a7e6172aedea0f703d513 100644 (file)
@@ -6,6 +6,7 @@
 #include <render.hpp>
 #include <ui.hpp>
 #include <engine.hpp>
+#include <world.hpp>
 
 void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
 {
@@ -245,3 +246,58 @@ if (health != maxHealth) {
 Render::worldShader.disable();
 Render::worldShader.unuse();
 }*/
+
+void DialogSystem::configure(entityx::EventManager &ev)
+{
+       ev.subscribe<MouseClickEvent>(*this);
+}
+
+void DialogSystem::receive(const MouseClickEvent &mce)
+{
+       game::entities.each<Position, Solid, Dialog, Name>(
+               [&](entityx::Entity e, Position &pos, Solid &dim, Dialog &d, Name &name) {
+                       (void)e;
+                       (void)d;
+
+                       if (((mce.position.x > pos.x) & (mce.position.x < pos.x + dim.width)) &&
+                           ((mce.position.y > pos.y) & (mce.position.y < pos.y + dim.height))) {
+
+                       std::thread([&] {
+                               auto exml = game::engine.getSystem<WorldSystem>()->getXML()->FirstChildElement("Dialog");
+                               int newIndex;
+
+                               if (exml != nullptr) {
+                                       while (exml->StrAttribute("name") != name.name)
+                                               exml = exml->NextSiblingElement();
+
+                                       exml = exml->FirstChildElement("text");
+                                       while (exml->IntAttribute("id") != d.index)
+                                               exml = exml->NextSiblingElement();
+
+                                       auto cxml = exml->FirstChildElement("content");
+                                       if (cxml == nullptr)
+                                               return;
+
+                                       auto content = cxml->GetText() - 1;
+                                       while (*++content && isspace(*content));
+
+                                       ui::dialogBox(name.name, "", false, content);
+                                       ui::waitForDialog();
+
+                                       if (exml->QueryIntAttribute("nextid", &newIndex) == XML_NO_ERROR)
+                                               d.index = newIndex;
+                               }
+                       }).detach();
+
+                       }
+               }
+       );
+}
+
+void DialogSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
+{
+       (void)en;
+       (void)ev;
+       (void)dt;
+}
+
index 04ea91272199ec9b35f7ebb879e8c97e6f4e7351..c50e7e7b4a4f14ee2c764b8bfcafcf495268e6a4 100644 (file)
@@ -25,8 +25,10 @@ void Engine::init(void) {
     systems.add<InventorySystem>();
     systems.add<WorldSystem>();
     systems.add<PlayerSystem>();
+
        systems.add<PhysicsSystem>();
        systems.add<MovementSystem>();
+       systems.add<DialogSystem>();
 
     systems.configure();
 
@@ -48,6 +50,7 @@ void Engine::update(entityx::TimeDelta dt)
     systems.update<InputSystem>(dt);
        //systems.update<PhysicsSystem>(dt);
        systems.update<MovementSystem>(dt);
+       //systems.update<DialogSystem>(dt);
        systems.update<WorldSystem>(dt);
     systems.update<PlayerSystem>(dt);
 }
index 3c568d1d86530118881b2ec724a9b8547c2f35d7..3a4a22e4fc65e869846860af03e896f79d20914d 100644 (file)
@@ -238,7 +238,7 @@ namespace ui {
 
        void setFontSize(unsigned int size) {
                (void)size;
-               /*if (size == 16) {
+               if (size == 16) {
                        if (!ft16loaded) {
                                loadFontSize(fontSize = size, ftex16, ftdat16);
                                ft16loaded = true;
@@ -254,7 +254,7 @@ namespace ui {
                        ftex = &ftex24;
                        ftdat = &ftdat24;
                        fontSize = 24;
-               }*/
+               }
        }
 
        /*
@@ -1270,6 +1270,7 @@ void InputSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                //case SDL_MOUSEBUTTONUP:
 
                case SDL_MOUSEBUTTONDOWN:
+                       ev.emit<MouseClickEvent>(mouse, e.button.button);
 
                        // run actions?
                        //if ((action::make = e.button.button & SDL_BUTTON_RIGHT))
index 3ff819b838e2d0b382794d806aba1393fdb35c5e..0b0defc6fab9cc22320cdfb63362753922e41c9d 100644 (file)
@@ -164,45 +164,6 @@ bool WorldSystem::save(const std::string& s)
        return false;
 }
 
-/*static bool loadedLeft = false;
-static bool loadedRight = false;
-
-World *loadWorldFromXML(std::string path) {
-       if (!currentXML.empty())
-               currentWorld->save();
-
-       return loadWorldFromXMLNoSave(path);
-}
-
-World *loadWorldFromPtr(World *ptr)
-{
-       currentWorld->save(); // save the current world to the current xml path
-
-       if (ptr->getToLeft() == currentXML) {
-               currentWorldToLeft = currentWorld;
-               loadedRight = true;
-               currentWorldToRight = loadWorldFromXMLNoSave(ptr->getToRight());
-               loadedRight = false;
-       } else if (ptr->getToRight() == currentXML) {
-               currentWorldToRight = currentWorld;
-               loadedLeft = true;
-               currentWorldToLeft = loadWorldFromXMLNoSave(ptr->getToLeft());
-               loadedLeft = false;
-       }
-
-    return ptr;
-}
-
-World *
-loadWorldFromXMLNoTakeover(std::string path)
-{
-       loadedLeft = true, loadedRight = true;
-       auto ret = loadWorldFromXMLNoSave(path);
-       loadedLeft = false, loadedRight = false;
-       return ret;
-}
-*/
-
 void WorldSystem::load(const std::string& file)
 {
        auto str2coord = [](std::string s) -> vec2 {
@@ -400,6 +361,8 @@ void WorldSystem::load(const std::string& file)
                                                entity.assign<Physics>(g);
                                        } else if (tname == "Name") {
                                                entity.assign<Name>(coalesce(wxml->Attribute("name"), abcd->Attribute("value"))); 
+                                       } else if (tname == "Dialog") {
+                                               entity.assign<Dialog>();
                                        }
 
                                        abcd = abcd->NextSiblingElement();
@@ -1097,7 +1060,7 @@ void WorldSystem::goWorldRight(Position& p, Solid &d)
                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(15));
                ui::toggleBlack();
        }
 }
@@ -1108,7 +1071,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();
        }
 }
@@ -1120,7 +1083,7 @@ void WorldSystem::goWorldPortal(Position& p)
                        (void)entity;
 
                        auto& size = sprite.sprite.front().first.size;
-                       if (p.x > loc.x && p.x < loc.x + size.x) {
+                       if (!(portal.toFile.empty()) && p.x > loc.x && p.x < loc.x + size.x)  {
                                ui::toggleBlack();
                                ui::waitForCover();
                                load(portal.toFile);
index 4a64ee0f063361221ca971fa9a17e27d135f57df..20b5ca479fad3713578e4524e0ec57bab49f880c 100644 (file)
 <Dialog name="Bob">
     <text id="0" nextid="1" pause="true">
         <give id="Dank MayMay" count="10"/>
-        <content alive="1">
+        <content
                        Hey there! The name's Bob. Good to see you've finally woken up from your nap by the cliff there... lol
                </content>
     </text>
-    <text id="1" pause="true" alive="1">
+    <text id="1" pause="true">
         <quest assign="Check out m&apos;swag, man!" desc="Go inside Bob&apos;s house and check out the stuff he has."/>
         <content>
                        Looks like you've got yourself pretty empty handed... you know, I have a simple solution for that. Come on inside, I have somethin' to show you.
index 431c0678cd308b0b704f6bdce55447fc653901f4..a74e91256586b046953102f628301fb7bd49762a 100644 (file)
@@ -8,6 +8,7 @@
        <Solid />
        <Physics />
        <Name value="Daddy" />
+       <Dialog />
 </npc>
 
 <structure>