]> git.localhorst.tv Git - blank.git/commitdiff
make collisions reference their entity
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 26 Nov 2015 10:04:32 +0000 (11:04 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 26 Nov 2015 10:04:32 +0000 (11:04 +0100)
so it doesn't accidentally get deleted

src/client/client.cpp
src/standalone/standalone.cpp
src/world/EntityCollision.hpp
src/world/world.cpp

index 77b7fa63a3ed5f774b5694a4fd53a8e435886738..b79c435473e946de841c7353cb2fb37870923c87 100644 (file)
@@ -158,7 +158,7 @@ void InteractiveState::Update(int dt) {
        if (input.BlockFocus()) {
                hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
        } else if (input.EntityFocus()) {
-               hud.FocusEntity(*input.EntityFocus().entity);
+               hud.FocusEntity(input.EntityFocus().GetEntity());
        } else {
                hud.FocusNone();
        }
index ad6450d5b05bda8a42d419f5969e32c977256a05..2ffe6bc862c85a72600cf5ce4512d4efbdecb323 100644 (file)
@@ -164,7 +164,7 @@ void MasterState::Update(int dt) {
        if (input.BlockFocus()) {
                hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
        } else if (input.EntityFocus()) {
-               hud.FocusEntity(*input.EntityFocus().entity);
+               hud.FocusEntity(input.EntityFocus().GetEntity());
        } else {
                hud.FocusNone();
        }
index be1a82d9085a52c632323c3746f0ba5f461f69da..ec7cd60748592dee12fc4afaa8fec0cabda3eb2f 100644 (file)
@@ -8,19 +8,26 @@ class Entity;
 
 struct EntityCollision {
 
-       Entity *entity;
-
        float depth;
        glm::vec3 normal;
 
        EntityCollision()
-       : entity(nullptr), depth(0.0f), normal(0.0f) { }
-       EntityCollision(Entity *e, float d, const glm::vec3 &n)
-       : entity(e), depth(d), normal(n) { }
+       : depth(0.0f), normal(0.0f), entity(nullptr) { }
+       EntityCollision(Entity *e, float d, const glm::vec3 &n);
+       ~EntityCollision();
+
+       EntityCollision(const EntityCollision &);
+       EntityCollision &operator =(const EntityCollision &);
 
        /// check if an actual collision
        operator bool() const noexcept { return entity; }
 
+       Entity &GetEntity() noexcept { return *entity; }
+       const Entity &GetEntity() const noexcept { return *entity; }
+
+private:
+       Entity *entity;
+
 };
 
 }
index 893285ec2ac90d6fedf369bb12ca4e9433abb48a..4f6d908720764b4de8fe3b4835150f5ac2889a6c 100644 (file)
@@ -1,4 +1,5 @@
 #include "Entity.hpp"
+#include "EntityCollision.hpp"
 #include "EntityController.hpp"
 #include "EntityDerivative.hpp"
 #include "EntityState.hpp"
@@ -287,6 +288,44 @@ void Entity::OrientHead(float dt) noexcept {
 }
 
 
+EntityCollision::EntityCollision(Entity *e, float d, const glm::vec3 &n)
+: depth(d)
+, normal(n)
+, entity(e) {
+       if (entity) {
+               entity->Ref();
+       }
+}
+
+EntityCollision::~EntityCollision() {
+       if (entity) {
+               entity->UnRef();
+       }
+}
+
+EntityCollision::EntityCollision(const EntityCollision &other)
+: depth(other.depth)
+, normal(other.normal)
+, entity(other.entity) {
+       if (entity) {
+               entity->Ref();
+       }
+}
+
+EntityCollision &EntityCollision::operator =(const EntityCollision &other) {
+       if (entity) {
+               entity->UnRef();
+       }
+       depth = other.depth;
+       normal = other.normal;
+       entity = other.entity;
+       if (entity) {
+               entity->Ref();
+       }
+       return *this;
+}
+
+
 EntityController::~EntityController() {
 
 }
@@ -557,8 +596,7 @@ bool World::Intersection(
        const Entity &reference,
        EntityCollision &coll
 ) {
-       coll.entity = nullptr;
-       coll.depth = std::numeric_limits<float>::infinity();
+       coll = EntityCollision(nullptr, std::numeric_limits<float>::infinity(), glm::vec3(0.0f));
        for (Entity &cur_entity : entities) {
                if (&cur_entity == &reference) {
                        continue;
@@ -568,14 +606,12 @@ bool World::Intersection(
                if (blank::Intersection(ray, cur_entity.Bounds(), cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
                        // TODO: fine grained check goes here? maybe?
                        if (cur_dist < coll.depth) {
-                               coll.entity = &cur_entity;
-                               coll.depth = cur_dist;
-                               coll.normal = cur_normal;
+                               coll = EntityCollision(&cur_entity, cur_dist, cur_normal);
                        }
                }
        }
 
-       return coll.entity;
+       return coll;
 }
 
 bool World::Intersection(const Entity &e, const EntityState &s, std::vector<WorldCollision> &col) {