Browse Source

Start defining player.

Sam Jaffe 6 years ago
parent
commit
1bebd9d7e7

+ 37 - 0
Resources/scripts/player/player.json

@@ -0,0 +1,37 @@
+{
+  "name":"Player",
+  "type":"player",
+  "hp":100,
+  "size":0.25,
+  "speed":400,
+  "solid":true,
+  "material":{
+    "texture":{
+      "file":"data/images/rock.png",
+      "uniform":"u_diffuseMap"
+    }
+  },
+  "attack":[
+    {
+      "id":"burstshot",
+      "timeBetweenBullets":0.05,
+      "burstAngle":120.0,
+      "centerAngle":90.0,
+      "bulletSpeed":900,
+      "prototype":{
+        "damage":2,
+        "size":10.0,
+        "material":{
+          "texture":{
+            "uniform":"u_diffuseMap"
+          }
+        }
+      }
+    }
+  ],
+  "shield":{
+    "max":100.0,
+    "regen":1.0
+  },
+  "level":1
+}

+ 12 - 3
include/danmaku/player.hpp

@@ -14,9 +14,18 @@ namespace danmaku {
   class bullet;
   class player : public actor {
   public:
+    player(Json::Value const & json, graphics::object const & obj,
+           attack_factory get_attack);
+
     void hit(bullet const & b);
-    void update(float delta) override;
-    void level(class level *) override;
-    class level * level() const override;
+    void update(float delta) override {}
+    void level(class level * l) override { level_ = l; }
+    class level * level() const override {
+      return level_;
+    }
+
+  private:
+    class level * level_;
+    std::vector<std::shared_ptr<bullet_pattern>> attack_;
   };
 }

+ 6 - 0
include/danmaku/serial.hpp

@@ -16,12 +16,18 @@
 
 namespace danmaku {
   class actor;
+  struct points_map;
 }
 namespace graphics {
   class manager;
 }
 
 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);
+
+  template <typename F, typename... Args>
+  auto to_vector(Json::Value const & json, F && func, Args &&... args)
+      -> std::vector<decltype(func(json, args...))>;
 }

+ 25 - 0
include/danmaku/serial.tpp

@@ -0,0 +1,25 @@
+//
+//  serial.tpp
+//  danmaku
+//
+//  Created by Sam Jaffe on 5/27/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <json/json.h>
+#include <vector>
+
+namespace danmaku {
+  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;
+  }
+}

+ 4 - 15
src/entity/enemy.cxx

@@ -11,25 +11,11 @@
 #include <json/json.h>
 
 #include "danmaku/bullet_pattern.hpp"
+#include "danmaku/serial.hpp"
 #include "resource_factory/prototype_factory.hpp"
 
 using namespace danmaku;
 
-points_map to_points_map(Json::Value const & json) {
-  return {json["damage"].asInt(), json["kill"].asInt()};
-}
-
-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"])) {
@@ -43,3 +29,6 @@ std::unique_ptr<enemy> make_enemy(Json::Value const & json,
 }
 
 BIND_ACTOR("enemy", &make_enemy);
+
+// to_vector(Json::Value, attack_factory, actor*)
+#include "danmaku/serial.tpp"

+ 11 - 0
src/entity/player.cxx

@@ -8,6 +8,17 @@
 
 #include "danmaku/player.hpp"
 
+#include <json/json.h>
+
+#include "danmaku/serial.hpp"
+
 using namespace danmaku;
 
+player::player(Json::Value const & json, graphics::object const & obj,
+               attack_factory get_attack)
+    : actor(json, obj), attack_(to_vector(json["attack"], get_attack, this)) {}
+
 void player::hit(bullet const & b) {}
+
+// to_vector(Json::Value, attack_factory, actor*)
+#include "danmaku/serial.tpp"

+ 5 - 0
src/serial.cxx

@@ -12,6 +12,7 @@
 
 #include "danmaku/actor.hpp"
 #include "danmaku/bullet_pattern.hpp"
+#include "danmaku/enemy.hpp"
 #include "resource_factory/prototype_factory.hpp"
 
 namespace danmaku {
@@ -25,4 +26,8 @@ namespace danmaku {
                                          engine::to_object(json, mgr),
                                          get_attack);
   }
+
+  points_map to_points_map(Json::Value const & json) {
+    return {json["damage"].asInt(), json["kill"].asInt()};
+  }
 }