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();
}
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();
}
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;
+
};
}
#include "Entity.hpp"
+#include "EntityCollision.hpp"
#include "EntityController.hpp"
#include "EntityDerivative.hpp"
#include "EntityState.hpp"
}
+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() {
}
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;
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) {