From ed10ef9ede3d397672239c3b3dbe42cc6fbe56b4 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 28 Oct 2016 09:18:53 -0400 Subject: npc dialog --- src/components.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/engine.cpp | 3 +++ src/ui.cpp | 5 +++-- src/world.cpp | 47 +++++---------------------------------------- 4 files changed, 67 insertions(+), 44 deletions(-) (limited to 'src') 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); -- cgit v1.2.3