From ed10ef9ede3d397672239c3b3dbe42cc6fbe56b4 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 28 Oct 2016 09:18:53 -0400 Subject: npc dialog --- include/components.hpp | 21 +++++++++++++++---- include/events.hpp | 9 ++++++++ include/world.hpp | 3 +++ shaders/new.frag | 4 ++-- src/components.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/engine.cpp | 3 +++ src/ui.cpp | 5 +++-- src/world.cpp | 47 +++++------------------------------------- xml/!town.xml | 4 ++-- xml/entities.xml | 1 + 10 files changed, 101 insertions(+), 52 deletions(-) diff --git a/include/components.hpp b/include/components.hpp index 1195337..474a0eb 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -12,6 +12,7 @@ #include #include #include +#include /** * @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 { -private: public: void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; }; class PhysicsSystem : public entityx::System { -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 { -private: public: void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; }; +class DialogSystem : public entityx::System, public entityx::Receiver { +public: + void configure(entityx::EventManager&); + void receive(const MouseClickEvent&); + void update(entityx::EntityManager&, entityx::EventManager&, entityx::TimeDelta) override; +}; + #endif //COMPONENTS_HPP diff --git a/include/events.hpp b/include/events.hpp index 6a9bd8c..8a09638 100644 --- a/include/events.hpp +++ b/include/events.hpp @@ -8,6 +8,7 @@ #include #include +#include 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) {} diff --git a/include/world.hpp b/include/world.hpp index 421031c..8d88e5a 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -144,6 +144,9 @@ public: ev.subscribe(*this); } + inline XMLDocument* getXML(void) + { return &xmlDoc; } + inline float getWidth(void) const { return world.startX * -2.0f; } diff --git a/shaders/new.frag b/shaders/new.frag index 3537e26..0e4ed05 100644 --- a/shaders/new.frag +++ b/shaders/new.frag @@ -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; } diff --git a/src/components.cpp b/src/components.cpp index e00d474..220ecab 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -6,6 +6,7 @@ #include #include #include +#include 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(*this); +} + +void DialogSystem::receive(const MouseClickEvent &mce) +{ + game::entities.each( + [&](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()->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; +} + diff --git a/src/engine.cpp b/src/engine.cpp index 04ea912..c50e7e7 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -25,8 +25,10 @@ void Engine::init(void) { systems.add(); systems.add(); systems.add(); + systems.add(); systems.add(); + systems.add(); systems.configure(); @@ -48,6 +50,7 @@ void Engine::update(entityx::TimeDelta dt) systems.update(dt); //systems.update(dt); systems.update(dt); + //systems.update(dt); systems.update(dt); systems.update(dt); } diff --git a/src/ui.cpp b/src/ui.cpp index 3c568d1..3a4a22e 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -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(mouse, e.button.button); // run actions? //if ((action::make = e.button.button & SDL_BUTTON_RIGHT)) diff --git a/src/world.cpp b/src/world.cpp index 3ff819b..0b0defc 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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(g); } else if (tname == "Name") { entity.assign(coalesce(wxml->Attribute("name"), abcd->Attribute("value"))); + } else if (tname == "Dialog") { + entity.assign(); } 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()->setX(world.startX + HLINES(15)); + //game::engine.getSystem()->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()->setX(world.startX * -1 - HLINES(15)); + //game::engine.getSystem()->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); diff --git a/xml/!town.xml b/xml/!town.xml index 4a64ee0..20b5ca4 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -18,11 +18,11 @@ - + Hey there! The name's Bob. Good to see you've finally woken up from your nap by the cliff there... lol - + 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. diff --git a/xml/entities.xml b/xml/entities.xml index 431c067..a74e912 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -8,6 +8,7 @@ + -- cgit v1.2.3