diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components.cpp | 21 | ||||
-rw-r--r-- | src/player.cpp | 5 | ||||
-rw-r--r-- | src/world.cpp | 59 |
3 files changed, 71 insertions, 14 deletions
diff --git a/src/components.cpp b/src/components.cpp index 7c6091e..5b85f51 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -15,9 +15,25 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e { (void)ev; en.each<Position, Direction>([dt](entityx::Entity entity, Position &position, Direction &direction) { - (void)entity; position.x += direction.x * dt; position.y += direction.y * dt; + + if (entity.has_component<Sprite>()) { + auto& fl = entity.component<Sprite>()->faceLeft; + if (direction.x != 0) + fl = (direction.x < 0); + } + + if (entity.has_component<Wander>()) { + auto& countdown = entity.component<Wander>()->countdown; + + if (countdown > 0) { + countdown--; + } else { + countdown = 5000 + randGet() % 10 * 100; + direction.x = (randGet() % 3 - 1) * 0.02f; + } + } }); } @@ -26,7 +42,7 @@ void PhysicsSystem::update(entityx::EntityManager &en, entityx::EventManager &ev (void)ev; en.each<Direction, Physics>([dt](entityx::Entity entity, Direction &direction, Physics &physics) { (void)entity; - // TODO GET GRAVITY FROM WOLRD + // TODO GET GRAVITY FROM WORLD direction.y += physics.g * dt; }); } @@ -168,4 +184,3 @@ void DialogSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, (void)ev; (void)dt; } - diff --git a/src/player.cpp b/src/player.cpp index 1bbfaea..2694a54 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -85,7 +85,6 @@ void PlayerSystem::receive(const KeyDownEvent &kde) auto kc = kde.keycode; auto& loc = *player.component<Position>().get(); auto& vel = *player.component<Direction>().get(); - auto& faceLeft = player.component<Sprite>().get()->faceLeft; if ((kc == SDLK_SPACE) && game::canJump && ((vel.y > -0.01) & (vel.y < 0.01))) { loc.y += HLINES(2); @@ -97,14 +96,14 @@ void PlayerSystem::receive(const KeyDownEvent &kde) } else if (kc == getControl(1)) { if (!ui::fadeEnable) { - moveLeft = faceLeft = true; + moveLeft = true; moveRight = false; worldSystem.goWorldLeft(loc); } } else if (kc == getControl(2)) { if (!ui::fadeEnable) { - moveLeft = faceLeft = false; + moveLeft = false; moveRight = true; worldSystem.goWorldRight(loc, *player.component<Solid>().get()); diff --git a/src/world.cpp b/src/world.cpp index a50a04d..5f7f0a6 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -151,13 +151,23 @@ void WorldSystem::generate(unsigned int width) static Color ambient; -bool WorldSystem::save(const std::string& s) +bool WorldSystem::save(void) { - (void)s; - /*for (const auto &e : entity) - e->saveToXML(); + std::ofstream save (xmlFolder + currentXMLFile + ".dat"); - currentXMLDoc.SaveFile((s.empty() ? currentXML : xmlFolder + s).c_str(), false);*/ + // signature? + save << "831998 "; + + game::entities.each<Position>([&](entityx::Entity entity, Position& pos) { + // save position + save << "p " << pos.x << ' ' << pos.y << ' '; + + // save dialog, if exists + if (entity.has_component<Dialog>()) + save << "d " << entity.component<Dialog>()->index << ' '; + }); + + save.close(); return false; } @@ -356,6 +366,8 @@ void WorldSystem::load(const std::string& file) entity.assign<Dialog>((wxml->BoolAttribute("hasDialog") ? 0 : 9999)); } else if (tname == "Grounded") { entity.assign<Grounded>(); + } else if (tname == "Wander") { + entity.assign<Wander>(); } abcd = abcd->NextSiblingElement(); @@ -374,6 +386,37 @@ void WorldSystem::load(const std::string& file) wxml = wxml->NextSiblingElement(); } + // attempt to load data + std::ifstream save (xmlFolder + currentXMLFile + ".dat"); + if (save.good()) { + // check signature + int signature; + save >> signature; + if (signature != 831998) + UserError("Save file signature is invalid... (delete it)"); + + char id; + save >> id; + + entityx::ComponentHandle<Position> pos; + for (entityx::Entity entity : game::entities.entities_with_components(pos)) { + save >> pos->x >> pos->y; + save >> id; + + while (id != 'p') { + switch (id) { + case 'd': + save >> entity.component<Dialog>()->index; + break; + } + + save >> id; + } + } + + save.close(); + } + game::events.emit<BGMToggleEvent>(); } @@ -1195,12 +1238,12 @@ void WorldSystem::detect(entityx::TimeDelta dt) void WorldSystem::goWorldRight(Position& p, Solid &d) { - if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(15))) { + if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(5))) { ui::toggleBlack(); ui::waitForCover(); auto file = world.toRight; load(file); - //game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(15)); + game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(10)); ui::toggleBlack(); } } @@ -1211,7 +1254,7 @@ void WorldSystem::goWorldLeft(Position& p) ui::toggleBlack(); ui::waitForCover(); load(world.toLeft); - //game::engine.getSystem<PlayerSystem>()->setX(world.startX * -1 - HLINES(15)); + game::engine.getSystem<PlayerSystem>()->setX(world.startX * -1 - HLINES(15)); ui::toggleBlack(); } } |