Browse Source

Add support for culling objects that are out-of-bounds using the polygon-polygon intersection check.

Sam Jaffe 6 years ago
parent
commit
bdcc17ccb9
2 changed files with 27 additions and 0 deletions
  1. 3 0
      include/danmaku/level.hpp
  2. 24 0
      src/level.cxx

+ 3 - 0
include/danmaku/level.hpp

@@ -32,6 +32,9 @@ namespace danmaku {
     void add_bullet(bullet b);
 
   private:
+    template <typename T>
+    void perform_oob_culling_impl(std::vector<std::unique_ptr<T>> &);
+    void perform_oob_culling();
     void update_waves(float delta);
     void update_object(float delta);
     void check_collisions();

+ 24 - 0
src/level.cxx

@@ -72,7 +72,31 @@ void level::add_bullet(bullet b) {
   collidables[0].emplace_back(bullets_.back().get());
 }
 
+template <typename T>
+void level::perform_oob_culling_impl(std::vector<std::unique_ptr<T>> & data) {
+  // TODO: Aquire screen size correctly...?
+  math::dim2::rectangle bound{{{0.f, 0.f}}, {{3840.f, 2160.f}}};
+  auto is_oob = [&bound](std::unique_ptr<T> & ptr) {
+    return !math::intersects(ptr->render_info().points, bound);
+  };
+  auto it = std::remove_if(data.begin(), data.end(), is_oob);
+  auto remove_collider = [this](std::unique_ptr<T> & ptr) {
+    // TODO: colliders, use the collision enums.
+    collidables[0].erase(
+        std::remove(collidables[0].begin(), collidables[0].end(), ptr.get()),
+        collidables[0].end());
+  };
+  std::for_each(it, data.end(), remove_collider);
+  data.erase(it, data.end());
+}
+
+void level::perform_oob_culling() {
+  //  perform_oob_culling_impl(actors_);
+  perform_oob_culling_impl(bullets_);
+}
+
 void level::update(float delta) {
+  perform_oob_culling();
   update_waves(delta);
   update_object(delta);
   check_collisions();