#include #include #include #include #include #include #include static const char *spriteXML = " \ \ assets/player/player.png \ assets/player/player.png\ assets/player/player.png\ assets/player/player.png\ \ "; static const char *animationXML = "\ \ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ \ \ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ assets/player/player.png\ \ \ \ "; void PlayerSystem::create(void) { player = game::entities.create(); player.assign(); player.assign(0.0f, 100.0f); player.assign(0.0f, 0.0f); //player.assign(-0.001f); player.assign(1); player.assign(-0.2f); player.assign(100); auto sprite = player.assign(); XMLDocument xmld; xmld.Parse(spriteXML); auto frame = developFrame(xmld.FirstChildElement("Sprite")); if (frame.size() > 0) sprite->sprite = frame.at(0); vec2 dim = player.component().get()->getSpriteSize(); float cdat[2] = {dim.x, dim.y}; player.assign(cdat[0], cdat[1]); // handle player animation xmld.Parse(animationXML); player.assign(nullptr, xmld.FirstChildElement()); } void PlayerSystem::configure(entityx::EventManager &ev) { ev.subscribe(*this); ev.subscribe(*this); ev.subscribe(*this); } void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { (void)en; (void)ev; (void)dt; auto& vel = *player.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; }*/ } } void PlayerSystem::receive(const KeyDownEvent &kde) { static auto& worldSystem = *game::engine.getSystem(); auto kc = kde.keycode; auto& loc = *player.component().get(); auto& vel = *player.component().get(); if ((kc == SDLK_SPACE) && game::canJump && ((vel.y > -0.01) & (vel.y < 0.01))) { loc.y += HLINES(2); vel.y = .4; vel.grounded = false; } else if (!ui::dialogBoxExists || ui::dialogPassive) { if (kc == getControl(0)) { if (!ui::fadeIntensity) worldSystem.goWorldPortal(loc); } else if (kc == getControl(1)) { if (!ui::fadeEnable) { moveLeft = true; moveRight = false; worldSystem.goWorldLeft(loc); } } else if (kc == getControl(2)) { if (!ui::fadeEnable) { moveLeft = false; moveRight = true; worldSystem.goWorldRight(loc, *player.component().get()); } } else if (kc == getControl(3)) { if (game::canSprint) speed = 2.0f; game::engine.getSystem()->addMultiple(10, ParticleType::SmallBlast, [&](){ return vec2(loc.x, loc.y); }, 500, 7); } 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; } } else if (kc == SDLK_DELETE) { game::endGame(); } else if (kc == SDLK_t) { game::time::tick(50); } } vec2 PlayerSystem::getPosition(void) const { auto& loc = *game::entities.component(player.id()).get(); return vec2(loc.x, loc.y); } void PlayerSystem::receive(const UseItemEvent& uie) { static std::atomic_bool cool (true); if (cool.load()) { if (uie.item->sound != nullptr) Mix_PlayChannel(0, uie.item->sound, 0); if (uie.item->type == "Sword") { auto loc = getPosition(); auto &solid = *player.component().get(); loc.x += solid.width / 2, loc.y += solid.height / 2; game::events.emit(loc, AttackType::ShortSlash); } else if (uie.item->type == "Bow") { if (game::engine.getSystem()->take("Arrow", 1)) { auto e = game::entities.create(); auto pos = getPosition(); e.assign(pos.x, pos.y + 10); auto angle = std::atan2(uie.curs.y - pos.y, uie.curs.x - pos.x); e.assign(1 * std::cos(angle), 1 * std::sin(angle)); e.assign(-5); e.assign(); auto sprite = e.assign(); auto tex = game::engine.getSystem()->getItem("Arrow"); sprite->addSpriteSegment(SpriteData(tex.sprite), 0); auto dim = HLINES(sprite->getSpriteSize()); e.assign(dim.x, dim.y); } } /*cool.store(false); std::thread([&](void) { std::this_thread::sleep_for(std::chrono::milliseconds(uie.item->cooldown)); cool.store(true); }).detach();*/ } }