From: Daniel Karbach Date: Thu, 26 Nov 2015 10:04:32 +0000 (+0100) Subject: make collisions reference their entity X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=dc9e991ce52da5d89722e1b5f3862988afaa41cc;p=blank.git make collisions reference their entity so it doesn't accidentally get deleted --- diff --git a/src/client/client.cpp b/src/client/client.cpp index 77b7fa6..b79c435 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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(); } diff --git a/src/standalone/standalone.cpp b/src/standalone/standalone.cpp index ad6450d..2ffe6bc 100644 --- a/src/standalone/standalone.cpp +++ b/src/standalone/standalone.cpp @@ -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(); } diff --git a/src/world/EntityCollision.hpp b/src/world/EntityCollision.hpp index be1a82d..ec7cd60 100644 --- a/src/world/EntityCollision.hpp +++ b/src/world/EntityCollision.hpp @@ -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; + }; } diff --git a/src/world/world.cpp b/src/world/world.cpp index 893285e..4f6d908 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -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::infinity(); + coll = EntityCollision(nullptr, std::numeric_limits::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 &col) {