EntityWrapper to uniquely identify entities and make using them a little easier
This commit is contained in:
@@ -15,7 +15,7 @@ public:
|
||||
{ }
|
||||
|
||||
virtual void Update(World* world, double dt) override;
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& component, double dt) override;
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override;
|
||||
|
||||
private:
|
||||
Octree* m_Octree;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "../Rendering/RawModel.h"
|
||||
#include "../Rendering/RenderQueueFactory.h"
|
||||
#include "../Core/Entity.h"
|
||||
#include "../Core/EntityWrapper.h"
|
||||
|
||||
class World;
|
||||
struct ComponentWrapper;
|
||||
@@ -59,7 +60,7 @@ bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation);
|
||||
bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon = 0.0001f);
|
||||
|
||||
// Calculates an absolute AABB from an entity AABB component
|
||||
boost::optional<AABB> EntityAbsoluteAABB(World* world, EntityID entity);
|
||||
boost::optional<AABB> EntityAbsoluteAABB(EntityWrapper& entity);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &CollisionSystem::OnKeyUp);
|
||||
}
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) override;
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override;
|
||||
|
||||
private:
|
||||
Octree* m_Octree;
|
||||
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
, m_Octree(octree)
|
||||
{ }
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& collision, double dt) override;
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override;
|
||||
|
||||
private:
|
||||
Octree* m_Octree;
|
||||
|
||||
@@ -4,22 +4,5 @@
|
||||
typedef unsigned int EntityID;
|
||||
const static unsigned int EntityID_Invalid = -1;
|
||||
|
||||
class World;
|
||||
struct EntityWrapper
|
||||
{
|
||||
EntityWrapper(::World* world, EntityID id)
|
||||
: World(world)
|
||||
, ID(id)
|
||||
{ }
|
||||
|
||||
::World* World;
|
||||
EntityID ID;
|
||||
|
||||
bool operator==(const EntityWrapper& e)
|
||||
{
|
||||
return (this->World == e.World) && (this->ID == e.ID);
|
||||
}
|
||||
operator EntityID() { return this->ID; }
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef EntityWrapper_h__
|
||||
#define EntityWrapper_h__
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
#include "ComponentWrapper.h"
|
||||
|
||||
class World;
|
||||
struct EntityWrapper
|
||||
{
|
||||
EntityWrapper(::World* world, EntityID id)
|
||||
: World(world)
|
||||
, ID(id)
|
||||
{ }
|
||||
|
||||
::World* World;
|
||||
EntityID ID;
|
||||
|
||||
bool HasComponent(const std::string& componentName);
|
||||
|
||||
ComponentWrapper operator[](const std::string& componentName);
|
||||
bool operator==(const EntityWrapper& e);
|
||||
explicit operator EntityID();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "World.h"
|
||||
#include "EntityWrapper.h"
|
||||
#include "ComponentWrapper.h"
|
||||
|
||||
class System
|
||||
@@ -33,7 +34,7 @@ protected:
|
||||
|
||||
const std::string m_ComponentType;
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& component, double dt) = 0;
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) = 0;
|
||||
};
|
||||
|
||||
class ImpureSystem : public virtual System
|
||||
|
||||
@@ -68,7 +68,7 @@ public:
|
||||
}
|
||||
for (auto& component : *pool) {
|
||||
for (auto& system : systems) {
|
||||
system->UpdateComponent(world, component, dt);
|
||||
system->UpdateComponent(world, EntityWrapper(world, component.EntityID), component, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
HealthSystem(EventBroker* eventBroker);
|
||||
|
||||
//updatecomponent
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override;
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override;
|
||||
|
||||
private:
|
||||
//methods which will take care of specific events
|
||||
|
||||
@@ -8,5 +8,5 @@ public:
|
||||
: PureSystem(eventBroker, "Player")
|
||||
{ }
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt);
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt);
|
||||
};
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELeave, &PlayerSystem::OnLeave);
|
||||
}
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override;
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override;
|
||||
private:
|
||||
float m_Speed = 5;
|
||||
EventRelay<PlayerSystem, Events::TriggerEnter> m_EEnter;
|
||||
|
||||
@@ -9,9 +9,9 @@ public:
|
||||
, PureSystem("RaptorCopter")
|
||||
{ }
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& raptorCopter, double dt) override
|
||||
virtual void UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt) override
|
||||
{
|
||||
ComponentWrapper& transform = world->GetComponent(raptorCopter.EntityID, "Transform");
|
||||
(glm::vec3&)transform["Orientation"] += (float)(double)raptorCopter["Speed"] * (float)dt * (glm::vec3)raptorCopter["Axis"];
|
||||
ComponentWrapper& transform = world->GetComponent(component.EntityID, "Transform");
|
||||
(glm::vec3&)transform["Orientation"] += (float)(double)component["Speed"] * (float)dt * (glm::vec3)component["Axis"];
|
||||
}
|
||||
};
|
||||
@@ -5,16 +5,14 @@ void CollidableOctreeSystem::Update(World* world, double dt)
|
||||
m_Octree->ClearDynamicObjects();
|
||||
}
|
||||
|
||||
void CollidableOctreeSystem::UpdateComponent(World* world, ComponentWrapper& cCollidable, double dt)
|
||||
void CollidableOctreeSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
EntityID entity = cCollidable.EntityID;
|
||||
|
||||
if (world->HasComponent(entity, "AABB")) {
|
||||
boost::optional<AABB> absoluteAABB = Collision::EntityAbsoluteAABB(world, entity);
|
||||
if (entity.HasComponent("AABB")) {
|
||||
boost::optional<AABB> absoluteAABB = Collision::EntityAbsoluteAABB(entity);
|
||||
if (absoluteAABB) {
|
||||
m_Octree->AddDynamicObject(*absoluteAABB);
|
||||
}
|
||||
} else if (world->HasComponent(entity, "Model")) {
|
||||
} else if (entity.HasComponent("Model")) {
|
||||
// TODO: Derive AABB from model
|
||||
}
|
||||
}
|
||||
@@ -283,15 +283,15 @@ bool attachAABBComponentFromModel(World* world, EntityID id)
|
||||
return true;
|
||||
}
|
||||
|
||||
boost::optional<AABB> EntityAbsoluteAABB(World* world, EntityID entity)
|
||||
boost::optional<AABB> EntityAbsoluteAABB(EntityWrapper& entity)
|
||||
{
|
||||
if (!world->HasComponent(entity, "AABB")) {
|
||||
if (!entity.HasComponent("AABB")) {
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
ComponentWrapper& cAABB = world->GetComponent(entity, "AABB");
|
||||
glm::vec3 absPosition = RenderQueueFactory::AbsolutePosition(world, entity);
|
||||
glm::vec3 absScale = RenderQueueFactory::AbsoluteScale(world, entity);
|
||||
ComponentWrapper& cAABB = entity["AABB"];
|
||||
glm::vec3 absPosition = RenderQueueFactory::AbsolutePosition(entity.World, entity.ID);
|
||||
glm::vec3 absScale = RenderQueueFactory::AbsoluteScale(entity.World, entity.ID);
|
||||
glm::vec3 origin = absPosition + (glm::vec3)cAABB["Origin"];
|
||||
glm::vec3 size = (glm::vec3)cAABB["Size"] * absScale;
|
||||
return AABB::FromOriginSize(origin, size);
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
#include "Collision/CollisionSystem.h"
|
||||
#include "Core/AABB.h"
|
||||
|
||||
void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, double dt)
|
||||
void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
EntityID entity = cAABB.EntityID;
|
||||
boost::optional<AABB> boundingBox = Collision::EntityAbsoluteAABB(world, entity);
|
||||
boost::optional<AABB> boundingBox = Collision::EntityAbsoluteAABB(entity);
|
||||
if (!boundingBox) {
|
||||
return;
|
||||
}
|
||||
ComponentWrapper& cTransform = world->GetComponent(entity, "Transform");
|
||||
ComponentWrapper& cTransform = entity["Transform"];
|
||||
AABB& boxA = *boundingBox;
|
||||
|
||||
//Press 'Z' to enable/disable collision.
|
||||
|
||||
@@ -3,22 +3,22 @@
|
||||
#include "Core/AABB.h"
|
||||
#include "Rendering/Model.h"
|
||||
|
||||
void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& cTrigger, double dt)
|
||||
void TriggerSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
//Currently only players can trigger things.
|
||||
auto players = world->GetComponents("Player");
|
||||
if (players == nullptr) {
|
||||
return;
|
||||
}
|
||||
EntityID tId = cTrigger.EntityID;
|
||||
boost::optional<AABB> triggerBox = Collision::EntityAbsoluteAABB(world, tId);
|
||||
EntityID tId = component.EntityID;
|
||||
boost::optional<AABB> triggerBox = Collision::EntityAbsoluteAABB(entity);
|
||||
//The trigger *should* have a bounding box, or something, to test against so it can be triggered.
|
||||
if (!triggerBox) {
|
||||
return;
|
||||
}
|
||||
for (auto& pc : *players) {
|
||||
EntityID pId = pc.EntityID;
|
||||
boost::optional<AABB> playerBox = Collision::EntityAbsoluteAABB(world, pId);
|
||||
boost::optional<AABB> playerBox = Collision::EntityAbsoluteAABB(EntityWrapper(world, pId));
|
||||
//The player can't trigger anything without an AABB.
|
||||
if (!playerBox) {
|
||||
continue;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "Core/EntityWrapper.h"
|
||||
#include "Core/World.h"
|
||||
|
||||
bool EntityWrapper::operator==(const EntityWrapper& e)
|
||||
{
|
||||
return (this->World == e.World) && (this->ID == e.ID);
|
||||
}
|
||||
|
||||
bool EntityWrapper::HasComponent(const std::string& componentName)
|
||||
{
|
||||
return World->HasComponent(ID, componentName);
|
||||
}
|
||||
|
||||
ComponentWrapper EntityWrapper::operator[](const std::string& componentName)
|
||||
{
|
||||
if (World->HasComponent(ID, componentName)) {
|
||||
return World->GetComponent(ID, componentName);
|
||||
} else {
|
||||
LOG_WARNING("EntityWrapper implicitly attached \"%s\" component to #%i as a result of a fetch request!", componentName.c_str(), ID);
|
||||
return World->AttachComponent(ID, componentName);
|
||||
}
|
||||
}
|
||||
|
||||
EntityWrapper::operator EntityID()
|
||||
{
|
||||
return this->ID;
|
||||
}
|
||||
|
||||
@@ -10,24 +10,24 @@ HealthSystem::HealthSystem(EventBroker* eventBroker)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup);
|
||||
}
|
||||
|
||||
void HealthSystem::UpdateComponent(World *world, ComponentWrapper &health, double dt)
|
||||
void HealthSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
//if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity)
|
||||
ComponentWrapper player = world->GetComponent(health.EntityID, "Player");
|
||||
double maxHealth = (double)health["MaxHealth"];
|
||||
ComponentWrapper player = world->GetComponent(component.EntityID, "Player");
|
||||
double maxHealth = (double)component["MaxHealth"];
|
||||
|
||||
//process the DeltaHealthVector and change the entitys health accordingly
|
||||
for (size_t i = m_DeltaHealthVector.size(); i > 0; i--)
|
||||
{
|
||||
auto deltaHP = m_DeltaHealthVector[i - 1];
|
||||
//if we have a healthchange for the current player and health is greater than 0, then apply it
|
||||
if (std::get<0>(deltaHP) == player.EntityID && (double)health["Health"] > 0.0f) {
|
||||
if (std::get<0>(deltaHP) == player.EntityID && (double)component["Health"] > 0.0f) {
|
||||
//get the deltaHP value from the tuple and make sure you dont get more than maxHealth
|
||||
double newHealth = std::min((double)health["Health"] + (double)std::get<1>(deltaHP), maxHealth);
|
||||
health["Health"] = newHealth;
|
||||
double newHealth = std::min((double)component["Health"] + (double)std::get<1>(deltaHP), maxHealth);
|
||||
component["Health"] = newHealth;
|
||||
m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + i - 1);
|
||||
//check if health is <= 0
|
||||
if ((double)health["Health"] <= 0.0f) {
|
||||
if ((double)component["Health"] <= 0.0f) {
|
||||
//publish death event
|
||||
Events::PlayerDeath e;
|
||||
e.PlayerID = player.EntityID;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "RaptorCopterSystem.h"
|
||||
|
||||
void PlayerMovementSystem::UpdateComponent(World* world, ComponentWrapper& player, double dt)
|
||||
void PlayerMovementSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
|
||||
ComponentWrapper& transform = world->GetComponent(component.EntityID);
|
||||
}
|
||||
|
||||
|
||||
+13
-13
@@ -1,25 +1,25 @@
|
||||
#include "PlayerSystem.h"
|
||||
|
||||
void PlayerSystem::UpdateComponent(World * world, ComponentWrapper & player, double dt)
|
||||
void PlayerSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
player["Velocity"] = glm::vec3(0.f, 0.f, 0.f);
|
||||
if ((bool&)player["Forward"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt) * -1;
|
||||
component["Velocity"] = glm::vec3(0.f, 0.f, 0.f);
|
||||
if ((bool&)component["Forward"] == true) {
|
||||
((glm::vec3&)component["Velocity"]).z = m_Speed * float(dt) * -1;
|
||||
|
||||
}
|
||||
if ((bool&)player["Left"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt) * -1;
|
||||
if ((bool&)component["Left"] == true) {
|
||||
((glm::vec3&)component["Velocity"]).x = m_Speed * float(dt) * -1;
|
||||
}
|
||||
if ((bool&)player["Back"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt);
|
||||
if ((bool&)component["Back"] == true) {
|
||||
((glm::vec3&)component["Velocity"]).z = m_Speed * float(dt);
|
||||
}
|
||||
if ((bool&)player["Right"] == true) {
|
||||
((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt);
|
||||
if ((bool&)component["Right"] == true) {
|
||||
((glm::vec3&)component["Velocity"]).x = m_Speed * float(dt);
|
||||
}
|
||||
|
||||
if ((glm::vec3)player["Velocity"] != glm::vec3(0.f)) {
|
||||
ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform");
|
||||
(glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"];
|
||||
if ((glm::vec3)component["Velocity"] != glm::vec3(0.f)) {
|
||||
ComponentWrapper& transform = world->GetComponent(component.EntityID, "Transform");
|
||||
(glm::vec3&)transform["Position"] += (glm::vec3)component["Velocity"];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user