Skip to content

Commit

Permalink
onRemoveTrait() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ElementW committed Oct 11, 2017
1 parent 92e9220 commit a9be0f8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
12 changes: 11 additions & 1 deletion include/radix/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <vector>

#include <RadixEntity/Entity.hpp>

Expand All @@ -20,6 +21,10 @@ class World;

using EntityId = uint32_t;

namespace entities {
class Trait;
}

/** \class Entity
* It is created like this:
* @snippet source/data/map/XmlMapLoader.cpp Creating an Entity
Expand All @@ -41,6 +46,11 @@ class Entity : public entity::Entity, protected Transform {
std::string m_name;

protected:
/**
* List of traits contained within this entity instance.
*/
std::vector<entities::Trait*> m_traits;

Entity() :
world(util::NullReference<World>) {
throw std::invalid_argument("You forgot to properly instantiate the Entity base");
Expand Down Expand Up @@ -135,7 +145,7 @@ class Entity : public entity::Entity, protected Transform {
virtual void onRemove() {}

virtual std::string fullClassName() const = 0;
virtual std::string className() const = 0;
virtual std::string className() const = 0;

virtual void tick(TDelta) {}
};
Expand Down
1 change: 1 addition & 0 deletions include/radix/entities/traits/RigidBodyTrait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RigidBodyTrait : public Trait {
btDefaultMotionState motionState;
btRigidBody *body;

virtual void onRemoveTrait() override;
~RigidBodyTrait();

virtual int getCollisionFlags() const;
Expand Down
20 changes: 18 additions & 2 deletions include/radix/entities/traits/Trait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@
#include <radix/Entity.hpp>

namespace radix {

class EntityManager;

namespace entities {

class Trait : public virtual Entity {
class Trait : protected virtual Entity {
protected:
Trait() = default;
friend EntityManager;
friend Entity;

Trait();

virtual void onRemoveTrait() {}

public:
inline Entity& entity() {
return *this;
}
inline const Entity& entity() const {
return *this;
}
};

} /* namespace entities */
Expand Down
3 changes: 2 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ set(RADIX_SOURCES
${D}/EntityManager.cpp
${D}/entities/Player.cpp
${D}/entities/StaticModel.cpp
${D}/entities/traits/HealthTrait.cpp
${D}/entities/traits/RigidBodyTrait.cpp
${D}/entities/traits/SoundSourceTrait.cpp
${D}/entities/traits/HealthTrait.cpp
${D}/entities/traits/Trait.cpp
${D}/entities/Trigger.cpp
${D}/env/ArgumentsParser.cpp
${D}/env/GameConsole.cpp
Expand Down
3 changes: 3 additions & 0 deletions source/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void Entity::setName(const std::string &newName) {
}

void Entity::remove() {
for (auto trit = m_traits.rbegin(); trit != m_traits.rend(); ++trit) {
(*trit)->onRemoveTrait();
}
onRemove();
world.entityManager.queueDeleteEntity(this);
}
Expand Down
7 changes: 5 additions & 2 deletions source/entities/traits/RigidBodyTrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ void RigidBodyTrait::setRigidBody(float mass,
phys.getPhysicsWorld().addRigidBody(body, getCollisionGroup(), getCollisionMask());
}

RigidBodyTrait::~RigidBodyTrait() {
void RigidBodyTrait::onRemoveTrait() {
if (body) {
auto &phys = world.simulations.findFirstOfType<simulation::Physics>();
Util::Log(Verbose, Tag) << "Removing body from phys world (" << id << ')';
phys.getPhysicsWorld().removeRigidBody(body);
delete body;
}
}

RigidBodyTrait::~RigidBodyTrait() {
delete body;
}

void RigidBodyTrait::setPosition(const Vector3f &val) {
position = val;
if (body) {
Expand Down
11 changes: 11 additions & 0 deletions source/entities/traits/Trait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <radix/entities/traits/Trait.hpp>

namespace radix {
namespace entities {

Trait::Trait() {
m_traits.emplace_back(this);
}

} /* namespace entities */
} /* namespace radix */
2 changes: 1 addition & 1 deletion source/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void Renderer::updateLights(Shader& shader) {
std::string distance = "lights[" + index + "].distance";
std::string energy = "lights[" + index + "].energy";
std::string specular = "lights[" + index + "].specular";
const Vector3f &tposition = ls.getPosition();
const Vector3f &tposition = e.getPosition();
glUniform3f(shader.uni(position.c_str()), tposition.x, tposition.y, tposition.z);
glUniform3f(shader.uni(color.c_str()), ls.color.x, ls.color.y, ls.color.z);
glUniform1f(shader.uni(distance.c_str()), ls.distance);
Expand Down

0 comments on commit a9be0f8

Please sign in to comment.