From 17b4275fae1f6c5642156c73784622772d235c42 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 20 Oct 2016 20:16:21 -0500 Subject: playersystem, move cat --- src/engine.cpp | 9 ++-- src/entities.cpp | 5 +- src/player.cpp | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/world.cpp | 18 +++---- 4 files changed, 172 insertions(+), 14 deletions(-) create mode 100644 src/player.cpp (limited to 'src') diff --git a/src/engine.cpp b/src/engine.cpp index e53a688..f81312b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -6,6 +6,7 @@ //#include #include #include +#include extern World *currentWorld; @@ -23,7 +24,8 @@ void Engine::init(void) { systems.add(); // systems.add(); systems.add(); - systems.add(); + systems.add(); + systems.add(); // systems.add(&player); systems.configure(); @@ -35,7 +37,7 @@ void Engine::render(entityx::TimeDelta dt) { systems.update(dt); systems.update(dt); - + } void Engine::update(entityx::TimeDelta dt) @@ -45,13 +47,14 @@ void Engine::update(entityx::TimeDelta dt) // systems.update(dt); systems.update(dt); systems.update(dt); + systems.update(dt); } namespace game { entityx::EventManager events; entityx::EntityManager entities (events); - SpriteLoader sprite_l; + SpriteLoader sprite_l; Engine engine; } diff --git a/src/entities.cpp b/src/entities.cpp index d07de4b..1426eaf 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -1,6 +1,7 @@ #include #include +#include #include void entityxTest(void) @@ -8,7 +9,7 @@ void entityxTest(void) entityx::Entity e = game::entities.create(); e.assign(100.0f, 100.0f); e.assign(0.0f, 0.0f); - + e = game::entities.create(); e.assign(0.0f, 100.0f); e.assign(-0.01f, 0.0f); @@ -18,4 +19,6 @@ void entityxTest(void) vec2(0, 0), vec2(19, 15)), vec2(0, 0)); + + game::engine.getSystem()->setPlayer(e); } diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..912027f --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,154 @@ +#include + +#include +#include +#include +#include + +constexpr const float PLAYER_SPEED_CONSTANT = 0.15f; + +void PlayerSystem::configure(entityx::EventManager &ev) +{ + ev.subscribe(*this); + ev.subscribe(*this); +} + +void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { + (void)ev; + (void)dt; + + auto& vel = *en.get(pid).component().get(); + + if (moveLeft & !moveRight) + vel.x = -PLAYER_SPEED_CONSTANT; + else if (!moveLeft & moveRight) + vel.x = PLAYER_SPEED_CONSTANT; + else + vel.x = 0; + + vel.x *= speed; + + if (std::stoi(game::getValue("Slow")) == 1) + vel.x /= 2.0f; +} + +void PlayerSystem::receive(const KeyUpEvent &kue) +{ + auto kc = kue.keycode; + + if (kc == getControl(1)) { + moveLeft = false; + } else if (kc == getControl(2)) { + moveRight = false; + } else if (kc == getControl(3) || kc == getControl(4)) { + speed = 1.0f; + } else if (kc == getControl(5)) { + /*if (p->inv->invHover) { + p->inv->invHover = false; + } else { + if (!p->inv->selected) + p->inv->invOpening ^= true; + else + p->inv->selected = false; + + p->inv->mouseSel = false; + } + + // disable action ui + ui::action::disable();*/ + } +} + +void PlayerSystem::receive(const KeyDownEvent &kde) +{ + auto kc = kde.keycode; + + /*auto worldSwitch = [&](const WorldSwitchInfo& wsi){ + player->canMove = false; + ui::toggleBlackFast(); + ui::waitForCover(); + game::events.emit(wsi.first->bgm, wsi.first); + std::tie(currentWorld, player->loc) = wsi; // using p causes segfault + game::engine.getSystem()->setWorld(currentWorld); + ui::toggleBlackFast(); + ui::waitForUncover(); + player->canMove = true; // using p causes segfault + };*/ + + /*if ((kc == SDLK_SPACE) && (game::canJump & p->ground)) { + p->loc.y += HLINES(2); + p->vel.y = .4; + p->ground = false; + }*/ + + if (!ui::dialogBoxExists || ui::dialogPassive) { + if (kc == getControl(0)) { + /*if (inBattle) { + std::thread([&](void){ + auto thing = dynamic_cast(currentWorld)->exitArena(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + } else if (!ui::fadeIntensity) { + std::thread([&](void){ + auto thing = currentWorld->goInsideStructure(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + }*/ + } else if (kc == getControl(1)) { + if (!ui::fadeEnable) { + moveLeft = true; + moveRight = false; + + /*if (currentWorldToLeft) { + std::thread([&](void){ + auto thing = currentWorld->goWorldLeft(p); + if (thing.first != currentWorld) + worldSwitch(thing); + }).detach(); + }*/ + } + } 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(); + }*/ + } + } else if (kc == getControl(3)) { + if (game::canSprint) + speed = 2.0f; + } else if (kc == getControl(4)) { + speed = .5; + } else if (kc == getControl(5)) { + /*static int heyOhLetsGo = 0; + + //edown = true; + + // start hover counter? + if (!heyOhLetsGo) { + heyOhLetsGo = game::time::getTickCount(); + p->inv->mouseSel = false; + } + + // run hover thing + if (game::time::getTickCount() - heyOhLetsGo >= 2 && !(p->inv->invOpen) && !(p->inv->selected)) { + p->inv->invHover = true; + + // enable action ui + ui::action::enable(); + }*/ + } + } else if (kc == SDLK_DELETE) { + game::endGame(); + } else if (kc == SDLK_t) { + game::time::tick(50); + } +} diff --git a/src/world.cpp b/src/world.cpp index 6405382..4b5bb2d 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -71,7 +71,7 @@ std::string currentXML; using StyleList = std::array; static const std::vector bgPaths = { - { // Forest + { // Forest "bg.png", // daytime background "bgn.png", // nighttime background "bgFarMountain.png", // layer 1 (furthest) @@ -248,7 +248,7 @@ void WorldSystem::load(const std::string& file) if (wxml->QueryUnsignedAttribute("background", &styleNo) != XML_NO_ERROR) UserError("XML Error: No background given in