Browse Source

Make loading a world a little cleaner for now...

Sam Jaffe 6 years ago
parent
commit
2fa2f7112d
2 changed files with 23 additions and 9 deletions
  1. 7 3
      include/danmaku/world.hpp
  2. 16 6
      src/world.cxx

+ 7 - 3
include/danmaku/world.hpp

@@ -8,7 +8,6 @@
 
 #pragma once
 
-#include <json/forwards.h>
 #include <memory>
 #include <vector>
 
@@ -20,13 +19,18 @@ namespace danmaku {
 
   class world : public engine::scene {
   public:
-    world(std::string const & player_file, std::string const & path,
-          Json::Value const & content, std::shared_ptr<engine::game_dispatch>);
+    world(std::shared_ptr<engine::game_dispatch> game,
+          std::unique_ptr<player> player,
+          std::vector<std::shared_ptr<level>> levels);
     ~world();
 
     void update(float delta) override {}
     void render(graphics::renderer & renderer) override {}
 
+    static std::shared_ptr<world>
+    load_world(std::string const & path,
+               std::shared_ptr<engine::game_dispatch>);
+
   private:
     std::unique_ptr<player> player_;
     std::vector<std::shared_ptr<level>> levels_;

+ 16 - 6
src/world.cxx

@@ -13,6 +13,7 @@
 #include "danmaku/level.hpp"
 #include "danmaku/player.hpp"
 #include "danmaku/serial.hpp"
+#include "game/engine/game_dispatch.hpp"
 #include "game/util/env.hpp"
 
 using namespace danmaku;
@@ -29,14 +30,23 @@ level_from_path(Json::Value const & json, std::string const & path,
   return std::make_shared<level>(json["name"].asString(), level_json, game);
 }
 
-world::world(std::string const & player_file, std::string const & path,
-             Json::Value const & json,
-             std::shared_ptr<engine::game_dispatch> game)
-    : engine::scene("world", game),
-      player_(to_player(player_json(player_file), graphics_manager())),
-      levels_(to_vector(json["levels"], level_from_path, path, game)) {}
+world::world(std::shared_ptr<engine::game_dispatch> game,
+             std::unique_ptr<player> player,
+             std::vector<std::shared_ptr<level>> levels)
+    : engine::scene("world", game), player_(std::move(player)),
+      levels_(levels) {}
 
 world::~world() {}
 
+std::shared_ptr<world>
+world::load_world(std::string const & path,
+                  std::shared_ptr<engine::game_dispatch> game) {
+  std::string const directory = path.substr(0, path.find_last_of('/') + 1);
+  Json::Value const json = engine::read_file(env::resource_file(path));
+  return std::make_shared<world>(
+      game, to_player(player_json("player.json"), game->graphics_manager()),
+      to_vector(json["levels"], level_from_path, directory, game));
+}
+
 // to_vector(Json::Value const &, ...)
 #include "danmaku/serial.tpp"