Browse Source

Link bullet_pattern_factory to actor construction

Sam Jaffe 6 years ago
parent
commit
1079868e88

+ 4 - 0
danmaku.xcodeproj/project.pbxproj

@@ -27,6 +27,7 @@
 		CD49F783229B194C00EB8926 /* burstshot_pattern.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD49F782229B194C00EB8926 /* burstshot_pattern.cxx */; };
 		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 */; };
 		CD7E87A52295FCED00D877FE /* danmakuUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD7E87A42295FCED00D877FE /* danmakuUITests.m */; };
 /* End PBXBuildFile section */
 
@@ -89,6 +90,7 @@
 		CD49F777229B0FCD00EB8926 /* actor.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = actor.cxx; sourceTree = "<group>"; };
 		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>"; };
 		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>"; };
@@ -133,6 +135,7 @@
 			isa = PBXGroup;
 			children = (
 				CD49F75E229B09F800EB8926 /* level.cxx */,
+				CD49F793229C22A800EB8926 /* serial.cxx */,
 				CD49F781229B104900EB8926 /* entity */,
 				CD49F77F229B103200EB8926 /* pattern */,
 			);
@@ -422,6 +425,7 @@
 				CD49F778229B0FCD00EB8926 /* actor.cxx in Sources */,
 				CD1C833C2298A98700825C4E /* main.m in Sources */,
 				CD49F767229B0A3000EB8926 /* player.cxx in Sources */,
+				CD49F794229C22A800EB8926 /* serial.cxx in Sources */,
 				CD49F783229B194C00EB8926 /* burstshot_pattern.cxx in Sources */,
 				CD49F762229B0A0500EB8926 /* enemy.cxx in Sources */,
 				CD49F76A229B0A6C00EB8926 /* bullet.cxx in Sources */,

+ 4 - 1
include/danmaku/actor.hpp

@@ -28,9 +28,12 @@ namespace danmaku {
     virtual class level * level() const = 0;
   };
 
+  struct bullet_pattern;
+  using attack_factory = std::function<std::shared_ptr<bullet_pattern>(
+      Json::Value const &, actor *)>;
   using actor_factory =
       objects::prototype::factory<std::unique_ptr<actor>, Json::Value const &,
-                                  graphics::object const &>;
+                                  graphics::object const &, attack_factory>;
 }
 
 #define BIND_ACTOR(name, function)                                             \

+ 3 - 6
include/danmaku/enemy.hpp

@@ -10,10 +10,6 @@
 
 #include "actor.hpp"
 
-namespace danmaku {
-  struct bullet_patern;
-}
-
 namespace danmaku {
   struct points_map {
     int on_damage;
@@ -22,10 +18,11 @@ namespace danmaku {
 
   class enemy : public actor {
   public:
-    enemy(Json::Value const & json, graphics::object const & obj);
+    enemy(Json::Value const & json, graphics::object const & obj,
+          attack_factory get_attack);
 
   private:
     points_map points_;
-    std::vector<std::shared_ptr<bullet_patern>> attack_;
+    std::vector<std::shared_ptr<bullet_pattern>> attack_;
   };
 }

+ 27 - 0
include/danmaku/serial.hpp

@@ -0,0 +1,27 @@
+//
+//  serial.hpp
+//  danmaku
+//
+//  Created by Sam Jaffe on 5/27/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <memory>
+
+#include <json/forwards.h>
+
+#include "game/engine/serial.hpp"
+
+namespace danmaku {
+  class actor;
+}
+namespace graphics {
+  class manager;
+}
+
+namespace danmaku {
+  std::unique_ptr<actor> to_actor(Json::Value const & json,
+                                  graphics::manager const & mgr);
+}

+ 16 - 2
src/entity/enemy.cxx

@@ -19,5 +19,19 @@ points_map to_points_map(Json::Value const & json) {
   return {json["damage"].asInt(), json["kill"].asInt()};
 }
 
-enemy::enemy(Json::Value const & json, graphics::object const & obj)
-    : actor(json, obj), points_(to_points_map(json["points"])), attack_() {}
+template <typename F, typename... Args>
+auto to_vector(Json::Value const & json, F && func, Args &&... args)
+    -> std::vector<decltype(func(json, args...))> {
+  std::vector<decltype(func(json, args...))> out;
+  out.reserve(json.size());
+  for (int i = 0; i < json.size(); ++i) {
+    out.emplace_back(func(json[i], args...));
+  }
+  return out;
+}
+
+enemy::enemy(Json::Value const & json, graphics::object const & obj,
+             attack_factory get_attack)
+    : actor(json, obj), points_(to_points_map(json["points"])) {
+  attack_ = to_vector(json["attack"], get_attack, this);
+}

+ 2 - 4
src/level.cxx

@@ -13,7 +13,7 @@
 #include "danmaku/actor.hpp"
 #include "danmaku/bullet.hpp"
 #include "danmaku/player.hpp"
-#include "game/engine/serial.hpp"
+#include "danmaku/serial.hpp"
 #include "game/graphics/renderer.hpp"
 #include "game/math/common.hpp"
 #include "resource_factory/prototype_factory.hpp"
@@ -24,9 +24,7 @@ std::vector<std::unique_ptr<actor>>
 get_wave(Json::Value const & json, graphics::manager const & manager) {
   std::vector<std::unique_ptr<actor>> out;
   for (int i = 0; i < json.size(); ++i) {
-    auto & factory = actor_factory::instance();
-    out.emplace_back(factory.get(json["type"].asString(), json,
-                                 engine::to_object(json, manager)));
+    out.emplace_back(to_actor(json, manager));
   }
   return out;
 }

+ 28 - 0
src/serial.cxx

@@ -0,0 +1,28 @@
+//
+//  serial.cxx
+//  danmaku
+//
+//  Created by Sam Jaffe on 5/27/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "danmaku/serial.hpp"
+
+#include <json/json.h>
+
+#include "danmaku/actor.hpp"
+#include "danmaku/bullet_pattern.hpp"
+#include "resource_factory/prototype_factory.hpp"
+
+namespace danmaku {
+  std::unique_ptr<actor> to_actor(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 actor_factory::instance().get(json["type"].asString(), json,
+                                         engine::to_object(json, mgr),
+                                         get_attack);
+  }
+}