From 149f255760e56447400d80532c99d481bc93644a Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 22 Oct 2016 18:40:40 -0400 Subject: working linking, left/right --- include/player.hpp | 2 ++ include/world.hpp | 11 +++-------- src/engine.cpp | 1 + src/player.cpp | 23 ++++++---------------- src/world.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++----- xml/!town.xml | 1 + xml/!town2.xml | 7 +++++++ 7 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 xml/!town2.xml diff --git a/include/player.hpp b/include/player.hpp index 430173f..1caccc8 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -5,6 +5,8 @@ #include +constexpr const float PLAYER_SPEED_CONSTANT = 0.15f; + class PlayerSystem : public entityx::System, public entityx::Receiver { private: entityx::Entity::Id pid; diff --git a/include/world.hpp b/include/world.hpp index 36ccdfa..1f54292 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -9,10 +9,10 @@ // local game includes #include #include - #include #include #include +#include using namespace tinyxml2; /** @@ -64,11 +64,6 @@ extern std::string currentXML; */ constexpr const unsigned int DAY_CYCLE = 10000; -/** - * Defines the velocity of player when moved by the keyboard - */ -constexpr const float PLAYER_SPEED_CONSTANT = 0.15f; - /** * Defines the strongest pull gravity can have on an entity. * This is the most that can be subtracted from an entity's velocity in one @@ -165,8 +160,8 @@ public: void detect(entityx::TimeDelta dt); - void goWorldLeft(void) {} - void goWorldRight(void) {} + void goWorldLeft(Position& p); + void goWorldRight(Position& p); // worlddata2 stuff WorldData2 worldData; diff --git a/src/engine.cpp b/src/engine.cpp index f81312b..54181c8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -38,6 +38,7 @@ void Engine::render(entityx::TimeDelta dt) systems.update(dt); systems.update(dt); + ui::fadeUpdate(); } void Engine::update(entityx::TimeDelta dt) diff --git a/src/player.cpp b/src/player.cpp index 912027f..fcbff1c 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -4,8 +4,8 @@ #include #include #include - -constexpr const float PLAYER_SPEED_CONSTANT = 0.15f; +#include +#include void PlayerSystem::configure(entityx::EventManager &ev) { @@ -62,6 +62,7 @@ void PlayerSystem::receive(const KeyUpEvent &kue) void PlayerSystem::receive(const KeyDownEvent &kde) { auto kc = kde.keycode; + auto& loc = *game::entities.get(pid).component().get(); /*auto worldSwitch = [&](const WorldSwitchInfo& wsi){ player->canMove = false; @@ -101,27 +102,15 @@ void PlayerSystem::receive(const KeyDownEvent &kde) moveLeft = true; moveRight = false; - /*if (currentWorldToLeft) { - std::thread([&](void){ - auto thing = currentWorld->goWorldLeft(p); - if (thing.first != currentWorld) - worldSwitch(thing); - }).detach(); - }*/ + game::engine.getSystem()->goWorldLeft(loc); } } else if (kc == getControl(2)) { if (!ui::fadeEnable) { moveLeft = false; moveRight = true; - /*if (currentWorldToRight) { - std::thread([&](void){ - auto thing = currentWorld->goWorldRight(p); - if (thing.first != currentWorld) - worldSwitch(thing); - }).detach(); - }*/ - } + game::engine.getSystem()->goWorldRight(loc); + } } else if (kc == getControl(3)) { if (game::canSprint) speed = 2.0f; diff --git a/src/world.cpp b/src/world.cpp index 4b5bb2d..508e2ac 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -18,6 +18,7 @@ #include #include #include +#include // local library headers #include @@ -236,6 +237,8 @@ void WorldSystem::load(const std::string& file) } } + world.toLeft = world.toRight = ""; + // iterate through tags while (wxml) { std::string tagName = wxml->Name(); @@ -299,6 +302,27 @@ void WorldSystem::load(const std::string& file) game::time::setTickCount(std::stoi(wxml->GetText())); } + else if (tagName == "entity") { + auto str2coord = [](std::string s) -> vec2 { + auto cpos = s.find(','); + s[cpos] = '\0'; + return vec2 (std::stof(s), std::stof(s.substr(cpos + 1))); + }; + + auto entity = game::entities.create(); + + auto loc = wxml->Attribute("loc"); + if (loc != nullptr) { + auto locVec = str2coord(loc); + float locDat[2] = {locVec.x, locVec.y}; + entity.assign(locDat[0], locDat[1]); + } + + unsigned int health; + if (wxml->QueryUnsignedAttribute("health", &health) != XML_NO_ERROR) + entity.assign(health, health); + } + // hill creation /*else if (tagName == "hill") { addHill(ivec2 { wxml->IntAttribute("peakx"), wxml->IntAttribute("peaky") }, wxml->UnsignedAttribute("width")); @@ -930,13 +954,12 @@ void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, void WorldSystem::detect(entityx::TimeDelta dt) { - game::entities.each( - [&](entityx::Entity e, Position &loc, Direction &vel, Health &health, Solid &dim) { - + game::entities.each( + [&](entityx::Entity e, Position &loc, Direction &vel, Solid &dim) { (void)e; - if (health.health <= 0) - UserError("die mofo"); + //if (health.health <= 0) + // UserError("die mofo"); // get the line the entity is on int line = std::clamp(static_cast((loc.x + dim.width / 2 - world.startX) / game::HLINE), @@ -973,3 +996,27 @@ void WorldSystem::detect(entityx::TimeDelta dt) } }); } + +void WorldSystem::goWorldRight(Position& p) +{ + if (!(world.toRight.empty()) && (p.x > world.startX * -1 - HLINES(10))) { + ui::toggleBlack(); + ui::waitForCover(); + auto file = world.toRight; + load(file); + p.x = world.startX + HLINES(15); + ui::toggleBlack(); + } +} + +void WorldSystem::goWorldLeft(Position& p) +{ + if (!(world.toLeft.empty()) && (p.x < world.startX + HLINES(10))) { + ui::toggleBlack(); + ui::waitForCover(); + auto file = world.toLeft; + load(file); + p.x = world.startX * -1 - HLINES(15); + ui::toggleBlack(); + } +} diff --git a/xml/!town.xml b/xml/!town.xml index 0530762..69441f3 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -3,6 +3,7 @@