Browse Source

Start defining actors and things better.

Sam Jaffe 6 years ago
parent
commit
ce33be8a47

+ 3 - 3
include/danmaku/actor.hpp

@@ -8,13 +8,13 @@
 
 
 #pragma once
 #pragma once
 
 
-#include "game/engine/engine_fwd.hpp"
+#include "game/engine/entity.hpp"
 
 
 namespace danmaku {
 namespace danmaku {
   class level;
   class level;
-  struct actor {
+  struct actor : public engine::entity {
     virtual ~actor() = default;
     virtual ~actor() = default;
-    virtual void update(engine::entity & ent) = 0;
+    virtual void update(float delta) = 0;
     virtual std::shared_ptr<class level> level() const = 0;
     virtual std::shared_ptr<class level> level() const = 0;
   };
   };
 }
 }

+ 9 - 2
include/danmaku/bullet.hpp

@@ -13,10 +13,17 @@
 namespace danmaku {
 namespace danmaku {
   class bullet {
   class bullet {
   public:
   public:
-    bullet(int damage);
-    void update(engine::entity & ent);
+    bullet(int damage, math::vec2 const & vel, graphics::object const & obj);
+    void update(float delta);
+
+    graphics::object const & render_info() const { return render_info_; }
+
+    void set_position(math::vec2 const & ll, math::radian facing);
+    void set_velocity(math::vec2 const & v) { velocity_ = v; }
 
 
   private:
   private:
     int damage_;
     int damage_;
+    math::vec2 velocity_;
+    graphics::object render_info_;
   };
   };
 }
 }

+ 11 - 1
include/danmaku/level.hpp

@@ -11,11 +11,21 @@
 #include "game/engine/scene.hpp"
 #include "game/engine/scene.hpp"
 
 
 namespace danmaku {
 namespace danmaku {
+  class actor;
   class bullet;
   class bullet;
+  class player;
   class level : public engine::scene {
   class level : public engine::scene {
   public:
   public:
-    void add_bullet(bullet const &, engine::entity const &);
+    level();
+    ~level();
+    void update(float delta) override;
+    void render(graphics::renderer & renderer) override;
+
+    void add_bullet(bullet b);
 
 
   private:
   private:
+    player * player_;
+    std::vector<std::unique_ptr<actor>> actors_;
+    std::vector<std::unique_ptr<bullet>> bullets_;
   };
   };
 }
 }

+ 12 - 0
include/danmaku/player.hpp

@@ -7,3 +7,15 @@
 //
 //
 
 
 #pragma once
 #pragma once
+
+#include "actor.hpp"
+
+namespace danmaku {
+  class bullet;
+  class player : public actor {
+  public:
+    void hit(bullet const & b);
+    void update(float delta) override;
+    std::shared_ptr<class level> level() const override;
+  };
+}

+ 13 - 2
src/entity/bullet.cxx

@@ -8,8 +8,19 @@
 
 
 #include "danmaku/bullet.hpp"
 #include "danmaku/bullet.hpp"
 
 
+#include "game/math/angle.hpp"
+#include "game/math/common.hpp"
+
 using namespace danmaku;
 using namespace danmaku;
 
 
-bullet::bullet(int damage) : damage_(damage) {}
+bullet::bullet(int damage, math::vec2 const & vel, graphics::object const & obj)
+    : damage_(damage), velocity_(vel), render_info_(obj) {}
+
+void bullet::update(float delta) {}
 
 
-void bullet::update(engine::entity & ent) {}
+void bullet::set_position(math::vec2 const & ll, math::radian facing) {
+  auto & size = render_info_.location.size;
+  render_info_.location.origin = ll;
+  render_info_.points =
+      math::rotate(ll + size / 2.f, render_info_.location, facing);
+}

+ 4 - 0
src/entity/player.cxx

@@ -7,3 +7,7 @@
 //
 //
 
 
 #include "danmaku/player.hpp"
 #include "danmaku/player.hpp"
+
+using namespace danmaku;
+
+void player::hit(bullet const & b) {}

+ 39 - 1
src/level.cxx

@@ -8,6 +8,44 @@
 
 
 #include "danmaku/level.hpp"
 #include "danmaku/level.hpp"
 
 
+#include "danmaku/actor.hpp"
+#include "danmaku/bullet.hpp"
+#include "danmaku/player.hpp"
+#include "game/graphics/renderer.hpp"
+#include "game/math/common.hpp"
+
 using namespace danmaku;
 using namespace danmaku;
 
 
-void level::add_bullet(bullet const &, engine::entity const &) {}
+level::level() : engine::scene(""), player_() {}
+
+level::~level() {}
+
+void level::add_bullet(bullet b) {
+  bullets_.emplace_back(std::make_unique<bullet>(std::move(b)));
+  // TODO: Add to collision boxes...
+}
+
+void level::update(float delta) {
+  for (auto & a : actors_) {
+    a->update(delta);
+  }
+  for (auto & b : bullets_) {
+    b->update(delta);
+  }
+  check_collisions();
+  for (auto & b : bullets_) {
+    if (math::intersects(player_->render_info().points,
+                         b->render_info().points)) {
+      player_->hit(*b);
+    }
+  }
+}
+
+void level::render(graphics::renderer & renderer) {
+  for (auto & a : actors_) {
+    renderer.draw(a->render_info());
+  }
+  for (auto & b : bullets_) {
+    renderer.draw(b->render_info());
+  }
+}

+ 16 - 10
src/pattern/burstshot_pattern.cxx

@@ -12,7 +12,7 @@
 #include "danmaku/bullet.hpp"
 #include "danmaku/bullet.hpp"
 #include "danmaku/bullet_pattern.hpp"
 #include "danmaku/bullet_pattern.hpp"
 #include "danmaku/level.hpp"
 #include "danmaku/level.hpp"
-#include "game/engine/entity.hpp"
+#include "game/engine/serial.hpp"
 #include "game/math/angle.hpp"
 #include "game/math/angle.hpp"
 #include "game/math/common.hpp"
 #include "game/math/common.hpp"
 #include "game/math/math_fwd.hpp"
 #include "game/math/math_fwd.hpp"
@@ -28,7 +28,7 @@ struct burstshot : public danmaku::bullet_pattern {
   create(danmaku::actor *, Json::Value const &, graphics::manager &);
   create(danmaku::actor *, Json::Value const &, graphics::manager &);
 
 
   burstshot(danmaku::actor *, float shot_interval, std::vector<shot> shots,
   burstshot(danmaku::actor *, float shot_interval, std::vector<shot> shots,
-            bullet bullet, engine::entity ent);
+            bullet bullet);
   void update(float delta) override;
   void update(float delta) override;
 
 
   danmaku::actor * actor;
   danmaku::actor * actor;
@@ -37,7 +37,6 @@ struct burstshot : public danmaku::bullet_pattern {
   std::size_t idx{0};
   std::size_t idx{0};
   std::vector<shot> shots;
   std::vector<shot> shots;
   bullet bullet_proto;
   bullet bullet_proto;
-  engine::entity entity_proto;
 };
 };
 
 
 std::vector<shot> shot_vector(std::size_t count, float facing, float covered,
 std::vector<shot> shot_vector(std::size_t count, float facing, float covered,
@@ -61,9 +60,13 @@ std::vector<shot> shot_vector(std::size_t count, float facing, float covered,
 }
 }
 
 
 burstshot::burstshot(danmaku::actor * actor, float shot_interval,
 burstshot::burstshot(danmaku::actor * actor, float shot_interval,
-                     std::vector<shot> shots, bullet blt, engine::entity ent)
-    : actor(actor), shot_timer{shot_interval}, shots(shots), bullet_proto(blt),
-      entity_proto(ent) {}
+                     std::vector<shot> shots, bullet blt)
+    : actor(actor), shot_timer{shot_interval}, shots(shots), bullet_proto(blt) {
+}
+
+bullet make_bullet(Json::Value const & json, graphics::manager & manager) {
+  return bullet(json["damage"].asInt(), {}, engine::to_object(json, manager));
+}
 
 
 std::shared_ptr<burstshot> burstshot::create(danmaku::actor * actor,
 std::shared_ptr<burstshot> burstshot::create(danmaku::actor * actor,
                                              Json::Value const & json,
                                              Json::Value const & json,
@@ -73,8 +76,7 @@ std::shared_ptr<burstshot> burstshot::create(danmaku::actor * actor,
       shot_vector(json["bulletsPerWave"].asUInt(),
       shot_vector(json["bulletsPerWave"].asUInt(),
                   json["centerAngle"].asFloat(), json["burstAngle"].asFloat(),
                   json["centerAngle"].asFloat(), json["burstAngle"].asFloat(),
                   json["bulletSpeed"].asFloat(), json["clockwise"].asBool()),
                   json["bulletSpeed"].asFloat(), json["clockwise"].asBool()),
-      json["prototype"]["damage"].asInt(),
-      engine::entity(json["prototype"], manager));
+      make_bullet(json["prototype"], manager));
 }
 }
 
 
 void burstshot::update(float delta) {
 void burstshot::update(float delta) {
@@ -82,8 +84,12 @@ void burstshot::update(float delta) {
     shot_timer.remaining = shot_timer.interval;
     shot_timer.remaining = shot_timer.interval;
     std::size_t shots_fired = std::size_t(delta / shots.size());
     std::size_t shots_fired = std::size_t(delta / shots.size());
     while (shots_fired-- > 0) {
     while (shots_fired-- > 0) {
-      actor->level()->add_bullet(bullet_proto, entity_proto);
-      if (idx == shots.size()) { idx = 0; }
+      bullet new_bullet{bullet_proto};
+      new_bullet.set_velocity(shots[idx].first);
+      new_bullet.set_position(actor->render_info().location.origin,
+                              math::degree(shots[idx].second));
+      actor->level()->add_bullet(std::move(bullet_proto));
+      if (++idx == shots.size()) { idx = 0; }
     }
     }
   }
   }
 }
 }