瀏覽代碼

Some reformatting to improve things a bit.

Sam Jaffe 6 年之前
父節點
當前提交
b8ba0bb5bf
共有 2 個文件被更改,包括 31 次插入18 次删除
  1. 6 3
      engine/include/game/engine/scene.hpp
  2. 25 15
      engine/src/scene.cpp

+ 6 - 3
engine/include/game/engine/scene.hpp

@@ -21,11 +21,11 @@
 namespace engine {
   class scene : public identity<scene, std::string> {
   public:
-    using identity<scene, std::string>::identity;
+    scene(std::string const &);
     virtual ~scene();
 
-    virtual void update(float delta);
-    virtual void render(graphics::renderer & renderer);
+    virtual void update(float delta) = 0;
+    virtual void render(graphics::renderer & renderer) = 0;
     virtual void handle_key_event(event::key_event evt);
     virtual void handle_mouse_event(event::mouse_event evt);
 
@@ -34,6 +34,9 @@ namespace engine {
 
   protected:
     void change_scene(std::string const & scene_id);
+    void check_collisions();
+    static void check_collisions(std::vector<entity *> const & from,
+                                 std::vector<entity *> const & to);
 
   protected:
     std::vector<entity> entities;

+ 25 - 15
engine/src/scene.cpp

@@ -15,31 +15,41 @@
 
 using namespace engine;
 
+scene::scene(std::string const & str) : identity<scene, std::string>(str) {}
+
 scene::~scene() {}
 
-void scene::update(float delta) {
-  for (auto & ent : entities) {
-    ent.update(delta);
-  }
-  for (auto & pair : colliders) {
-    auto & collidable = collidables[pair.first];
-    for (auto & ent : pair.second) {
-      for (auto & hit : collidable) {
-        if (math::intersects(ent->render_info().points,
-                             hit->render_info().points)) {
-          ent->collide(*hit);
-        }
+void scene::check_collisions(std::vector<entity *> const & from,
+                             std::vector<entity *> const & to) {
+  for (auto & ent : from) {
+    for (auto & hit : to) {
+      if (math::intersects(ent->render_info().points,
+                           hit->render_info().points)) {
+        ent->collide(*hit);
       }
     }
   }
 }
 
-void scene::render(graphics::renderer & renderer) {
-  for (auto & ent : entities) {
-    renderer.draw(ent.render_info());
+void scene::check_collisions() {
+  for (auto & pair : colliders) {
+    check_collisions(pair.second, collidables[pair.first]);
   }
 }
 
+// void scene::update(float delta) {
+//  for (auto & ent : entities) {
+//    ent.update(delta);
+//  }
+//  check_collisions();
+//}
+//
+// void scene::render(graphics::renderer & renderer) {
+//  for (auto & ent : entities) {
+//    renderer.draw(ent.render_info());
+//  }
+//}
+
 void scene::handle_key_event(event::key_event evt) {
   if (evt.type & event::PRESSED_MASK && evt.key == keys::QUIT) {
     dispatch_.lock()->quit();