Browse Source

Extract out 'collide-able' object definition from entity.

Sam Jaffe 6 years ago
parent
commit
4d1e9c6c8a

+ 1 - 0
engine/include/game/engine/engine_fwd.hpp

@@ -19,6 +19,7 @@ namespace engine {
 
   using key_binding = std::unordered_map<raw_key_t, key_enum_t>;
 
+  class collidable;
   class entity;
   class fps_counter;
   class game_dispatch;

+ 14 - 6
engine/include/game/engine/entity.hpp

@@ -16,14 +16,24 @@
 #include "game/util/identity.hpp"
 
 namespace engine {
-  class entity : identity<entity> {
+  class collidable : identity<collidable> {
+  public:
+    collidable(graphics::object const & obj);
+    collidable(Json::Value const & json, graphics::manager const & mgr);
+
+    virtual void collide(collidable const &) {}
+    graphics::object const & render_info() const { return render_info_; }
+
+  protected:
+    // A mix of position and graphics info
+    graphics::object render_info_;
+  };
+
+  class entity : public collidable {
   public:
     // TODO: Extract this out?
     entity(Json::Value const & json, graphics::manager const & mgr);
     void update(float delta);
-    void collide(entity const &);
-
-    graphics::object const & render_info() const { return render_info_; }
 
   private:
     // The scene that owns this object
@@ -34,8 +44,6 @@ namespace engine {
     math::vec2 velocity;
     math::vec2 acceleration{{0, 0}};
     float angular_velocity{0.f};
-    // A mix of position and graphics info
-    graphics::object render_info_;
     // Graphics info
     std::size_t frame_index{0};
     std::vector<math::vec2> frame_texture_coords;

+ 4 - 4
engine/include/game/engine/scene.hpp

@@ -36,16 +36,16 @@ namespace engine {
     graphics::manager const & graphics_manager() const;
     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);
+    static void check_collisions(std::vector<collidable *> const & from,
+                                 std::vector<collidable *> const & to);
 
   protected:
     std::vector<entity> entities;
 
     // Map from entity::collides_with -> [entity*]
-    std::unordered_map<collision_t, std::vector<entity *>> colliders;
+    std::unordered_map<collision_t, std::vector<collidable *>> colliders;
     // Map from entity::collides_as -> [entity*]
-    std::unordered_map<collision_t, std::vector<entity *>> collidables;
+    std::unordered_map<collision_t, std::vector<collidable *>> collidables;
 
   private:
     math::vec2 local_scene_dimension_;

+ 7 - 4
engine/src/entity.cxx

@@ -21,9 +21,14 @@ static unsigned int next_id() {
   return ++id;
 }
 
+collidable::collidable(graphics::object const & obj)
+    : identity<collidable>(next_id()), render_info_(obj) {}
+
+collidable::collidable(Json::Value const & json, graphics::manager const & mgr)
+    : identity<collidable>(next_id()), render_info_(to_object(json, mgr)) {}
+
 entity::entity(Json::Value const & json, graphics::manager const & mgr)
-    : identity<entity>(next_id()), velocity(to_vec2(json["velocity"])),
-      render_info_(to_object(json, mgr)),
+    : collidable(json, mgr), velocity(to_vec2(json["velocity"])),
       frame_texture_coords({make_vector(0.f, 0.f)}),
       scale(json["size"].asFloat()), collides_with(0), collides_as(0) {
   render_info_.location.size *= scale;
@@ -41,5 +46,3 @@ void entity::update(float delta) {
   velocity += acceleration * delta;
   //  render_info.angle += angular_velocity * delta;
 }
-
-void entity::collide(entity const &) {}

+ 2 - 2
engine/src/scene.cpp

@@ -20,8 +20,8 @@ scene::scene(std::string const & str, std::shared_ptr<game_dispatch> dispatch)
 
 scene::~scene() {}
 
-void scene::check_collisions(std::vector<entity *> const & from,
-                             std::vector<entity *> const & to) {
+void scene::check_collisions(std::vector<collidable *> const & from,
+                             std::vector<collidable *> const & to) {
   for (auto & ent : from) {
     for (auto & hit : to) {
       if (math::intersects(ent->render_info().points,