EntityWrapper to uniquely identify entities and make using them a little easier
This commit is contained in:
@@ -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