Browse Source

Add world code.

Sam Jaffe 6 years ago
parent
commit
f9a06b9046

+ 4 - 0
danmaku.xcodeproj/project.pbxproj

@@ -28,6 +28,7 @@
 		CD49F784229B25DE00EB8926 /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7E883022960DBC00D877FE /* libmath.dylib */; };
 		CD49F786229B291D00EB8926 /* libjsoncpp.1.8.4.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD49F785229B291D00EB8926 /* libjsoncpp.1.8.4.dylib */; };
 		CD49F794229C22A800EB8926 /* serial.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD49F793229C22A800EB8926 /* serial.cxx */; };
+		CD49F7B3229C530A00EB8926 /* world.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD49F7B2229C530A00EB8926 /* world.cxx */; };
 		CD7E87A52295FCED00D877FE /* danmakuUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD7E87A42295FCED00D877FE /* danmakuUITests.m */; };
 /* End PBXBuildFile section */
 
@@ -91,6 +92,7 @@
 		CD49F782229B194C00EB8926 /* burstshot_pattern.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = burstshot_pattern.cxx; sourceTree = "<group>"; };
 		CD49F785229B291D00EB8926 /* libjsoncpp.1.8.4.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libjsoncpp.1.8.4.dylib; path = ../../../../../../opt/local/lib/libjsoncpp.1.8.4.dylib; sourceTree = "<group>"; };
 		CD49F793229C22A800EB8926 /* serial.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = serial.cxx; sourceTree = "<group>"; };
+		CD49F7B2229C530A00EB8926 /* world.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = world.cxx; sourceTree = "<group>"; };
 		CD7E87862295FCEA00D877FE /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
 		CD7E87872295FCEA00D877FE /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
 		CD7E878D2295FCEA00D877FE /* GameView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GameView.h; sourceTree = "<group>"; };
@@ -136,6 +138,7 @@
 			children = (
 				CD49F75E229B09F800EB8926 /* level.cxx */,
 				CD49F793229C22A800EB8926 /* serial.cxx */,
+				CD49F7B2229C530A00EB8926 /* world.cxx */,
 				CD49F781229B104900EB8926 /* entity */,
 				CD49F77F229B103200EB8926 /* pattern */,
 			);
@@ -430,6 +433,7 @@
 				CD49F762229B0A0500EB8926 /* enemy.cxx in Sources */,
 				CD49F76A229B0A6C00EB8926 /* bullet.cxx in Sources */,
 				CD49F75F229B09F800EB8926 /* level.cxx in Sources */,
+				CD49F7B3229C530A00EB8926 /* world.cxx in Sources */,
 				CD1C833A2298A97F00825C4E /* AppDelegate.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 2 - 0
include/danmaku/level.hpp

@@ -21,6 +21,8 @@ namespace danmaku {
   class player;
   class level : public engine::scene {
   public:
+    level(std::string const & id, Json::Value const & json,
+          std::shared_ptr<engine::game_dispatch>);
     level(Json::Value const & json, std::shared_ptr<engine::game_dispatch>);
     ~level();
     void update(float delta) override;

+ 3 - 0
include/danmaku/serial.hpp

@@ -16,6 +16,7 @@
 
 namespace danmaku {
   class actor;
+  class player;
   struct points_map;
 }
 namespace graphics {
@@ -26,6 +27,8 @@ namespace danmaku {
   points_map to_points_map(Json::Value const & json);
   std::unique_ptr<actor> to_actor(Json::Value const & json,
                                   graphics::manager const & mgr);
+  std::unique_ptr<player> to_player(Json::Value const & json,
+                                    graphics::manager const & mgr);
 
   template <typename F, typename... Args>
   auto to_vector(Json::Value const & json, F && func, Args &&... args)

+ 34 - 0
include/danmaku/world.hpp

@@ -0,0 +1,34 @@
+//
+//  world.hpp
+//  danmaku
+//
+//  Created by Sam Jaffe on 5/27/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <json/forwards.h>
+#include <memory>
+#include <vector>
+
+#include "game/engine/scene.hpp"
+
+namespace danmaku {
+  class level;
+  class player;
+
+  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();
+
+    void update(float delta) override {}
+    void render(graphics::renderer & renderer) override {}
+
+  private:
+    std::unique_ptr<player> player_;
+    std::vector<std::shared_ptr<level>> levels_;
+  };
+}

+ 6 - 0
src/level.cxx

@@ -44,6 +44,12 @@ level::level(Json::Value const & json,
       player_(), wave_timer_{json["timeBetweenWaves"].asFloat()},
       waves_(get_waves(json["waves"], graphics_manager())) {}
 
+level::level(std::string const & id, Json::Value const & json,
+             std::shared_ptr<engine::game_dispatch> dispatch)
+    : engine::scene(id, dispatch),
+      player_(), wave_timer_{json["timeBetweenWaves"].asFloat()},
+      waves_(get_waves(json["waves"], graphics_manager())) {}
+
 level::~level() {}
 
 void level::add_bullet(bullet b) {

+ 11 - 0
src/serial.cxx

@@ -13,6 +13,7 @@
 #include "danmaku/actor.hpp"
 #include "danmaku/bullet_pattern.hpp"
 #include "danmaku/enemy.hpp"
+#include "danmaku/player.hpp"
 #include "resource_factory/prototype_factory.hpp"
 
 namespace danmaku {
@@ -27,6 +28,16 @@ namespace danmaku {
                                          get_attack);
   }
 
+  std::unique_ptr<player> to_player(Json::Value const & json,
+                                    graphics::manager const & mgr) {
+    auto & bp_factory = bullet_pattern_factory::instance();
+    auto get_attack = [&](Json::Value const & j, actor * a) {
+      return bp_factory.get(j["id"].asString(), std::move(a), j, mgr);
+    };
+    return std::make_unique<player>(json, engine::to_object(json, mgr),
+                                    get_attack);
+  }
+
   points_map to_points_map(Json::Value const & json) {
     return {json["damage"].asInt(), json["kill"].asInt()};
   }

+ 42 - 0
src/world.cxx

@@ -0,0 +1,42 @@
+//
+//  world.cxx
+//  danmaku
+//
+//  Created by Sam Jaffe on 5/27/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "danmaku/world.hpp"
+
+#include <json/json.h>
+
+#include "danmaku/level.hpp"
+#include "danmaku/player.hpp"
+#include "danmaku/serial.hpp"
+#include "game/util/env.hpp"
+
+using namespace danmaku;
+
+Json::Value player_json(std::string const & player_file) {
+  return engine::read_file(env::resource_file("scripts/player/" + player_file));
+}
+
+std::shared_ptr<level>
+level_from_path(Json::Value const & json, std::string const & path,
+                std::shared_ptr<engine::game_dispatch> game) {
+  Json::Value level_json =
+      engine::read_file(path + "/" + json["path"].asString() + "/level.json");
+  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() {}
+
+// to_vector(Json::Value const &, ...)
+#include "danmaku/serial.tpp"