Compare commits

..

2 Commits

13 changed files with 64 additions and 112 deletions
-12
View File
@@ -2,7 +2,6 @@
#define ComponentInfo_h__
#include "../Common.h"
#include "Entity.h"
#include <boost/shared_array.hpp>
struct ComponentInfo
@@ -22,7 +21,6 @@ struct ComponentInfo
{
std::string Name;
std::string Type;
unsigned char Index;
unsigned int Offset;
unsigned int Stride;
};
@@ -34,16 +32,6 @@ struct ComponentInfo
unsigned int Stride = 0;
boost::shared_array<char> Defaults = nullptr;
std::shared_ptr<Meta_t> Meta = nullptr;
std::size_t GetHeaderSize() const
{
std::size_t size = 0;
// A component block starts with an entity ID
size += sizeof(EntityID);
return size;
}
};
template<>
+8 -11
View File
@@ -5,15 +5,16 @@
#include "MemoryPool.h"
#include "ComponentInfo.h"
#include "ComponentWrapper.h"
#include "DirtySet.h"
class ComponentPool;
class ComponentPoolForwardIterator
: public std::iterator<std::forward_iterator_tag, ComponentWrapper>
{
public:
ComponentPoolForwardIterator(ComponentPool* pool);
ComponentPoolForwardIterator(const ComponentInfo& componentInfo, const MemoryPool<char>::iterator begin, const MemoryPool<char>::iterator end)
: m_ComponentInfo(componentInfo)
, m_MemoryPoolIterator(begin)
, m_MemoryPoolEnd(end)
{ }
ComponentPoolForwardIterator(const ComponentPoolForwardIterator& other) = default;
ComponentPoolForwardIterator(ComponentPoolForwardIterator&& other) = default;
@@ -26,7 +27,6 @@ public:
ComponentWrapper operator*() const;
private:
ComponentPool* m_ComponentPool;
const ComponentInfo& m_ComponentInfo;
MemoryPool<char>::iterator m_MemoryPoolIterator;
const MemoryPool<char>::iterator m_MemoryPoolEnd;
@@ -34,7 +34,6 @@ private:
class ComponentPool
{
friend class ComponentPoolForwardIterator;
public:
typedef ComponentPoolForwardIterator iterator;
typedef ptrdiff_t difference_type;
@@ -42,11 +41,10 @@ public:
typedef ComponentWrapper value_type;
typedef ComponentWrapper* pointer;
typedef ComponentWrapper& reference;
typedef std::unordered_map<EntityID, std::set<decltype(ComponentInfo::Field_t::Index)>> DirtySet_t;
ComponentPool(const ::ComponentInfo& ci)
: m_ComponentInfo(ci)
, m_Pool(ci.Meta->Allocation, ci.GetHeaderSize() + ci.Stride)
, m_Pool(ci.Meta->Allocation, sizeof(EntityID) + ci.Stride)
{ }
~ComponentPool();
ComponentPool(const ComponentPool& other);
@@ -63,8 +61,8 @@ public:
// Delete a component and free its memory
void Delete(ComponentWrapper& wrapper);
iterator begin();
iterator end();
iterator begin() const;
iterator end() const;
size_t size() const;
//Dumps information about what the pool memory looks like right now
@@ -82,7 +80,6 @@ private:
::ComponentInfo m_ComponentInfo;
MemoryPool<char> m_Pool;
std::unordered_map<EntityID, char*> m_EntityToComponent;
DirtySet m_DirtySet;
};
#endif
+31 -43
View File
@@ -6,54 +6,44 @@
#include "../Common.h"
#include "Entity.h"
#include "ComponentInfo.h"
#include "DirtySet.h"
#include "Util/Any.h"
template <typename T, typename Enable = void>
struct ComponentField { };
template <typename T>
struct ComponentField<T, typename std::enable_if<std::is_trivially_copyable<T>::value>::type>
{
static T& Get(const ComponentInfo::Field_t& info, char* data) { return *reinterpret_cast<T*>(data); }
static void Set(const ComponentInfo::Field_t& info, char* data, const T& value) { Get(data) = value; }
};
template <>
struct ComponentField<std::string, void>
{
static std::string& Get(const ComponentInfo::Field_t& info, char* data) { return **reinterpret_cast<std::string**>(data); }
static void Set(const ComponentInfo::Field_t& info, char* data, const std::string& value) { Get(info, data) = value; }
};
struct ComponentWrapper
{
ComponentWrapper(const ComponentInfo& componentInfo, char* data, ::DirtyBitField* dirtyBitField)
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
: Info(componentInfo)
, EntityID(*reinterpret_cast<::EntityID*>(data))
, Data(data + componentInfo.GetHeaderSize())
, DirtyBitField(dirtyBitField)
, Data(data + sizeof(::EntityID))
{ }
const ComponentInfo& Info;
const ::EntityID EntityID;
char* Data;
::DirtyBitField* DirtyBitField = nullptr;
ComponentInfo::EnumType Enum(const char* fieldName, const char* enumKey)
{
return Info.Meta->FieldEnumDefinitions.at(fieldName).at(enumKey);
}
bool Dirty(DirtySetType type, const std::string& fieldName)
{
if (DirtyBitField == nullptr) {
return true;
} else {
auto& field = Info.Fields.at(fieldName);
return DirtyBitField->operator[](type).count(field.Index) == 1;
}
}
void SetDirty(DirtySetType type, const std::string& fieldName, bool dirty = true)
{
if (DirtyBitField == nullptr) {
return;
}
auto& field = Info.Fields.at(fieldName);
if (dirty) {
DirtyBitField->operator[](type).insert(field.Index);
} else {
DirtyBitField->operator[](type).erase(field.Index);
}
}
template <typename T>
T& Field(const std::string& name)
T& Field(std::string name)
{
const ComponentInfo::Field_t& field = Info.Fields.at(name);
if (sizeof(T) > field.Stride) {
@@ -65,13 +55,13 @@ struct ComponentWrapper
}
template <typename T>
void SetField(const std::string& name, const T value) { Field<T>(name) = value; }
void SetField(std::string name, const T value) { Field<T>(name) = value; }
//template <typename T>
//void SetField(std::string name, T& value) { Field<T>(name) = value; }
// Specialization for string literals
template <std::size_t N>
void SetField(const std::string& name, const char(&value)[N]) { Field<std::string>(name) = std::string(value); }
void SetField(std::string name, const char(&value)[N]) { Field<std::string>(name) = std::string(value); }
void Copy(ComponentWrapper& destination)
{
@@ -106,41 +96,39 @@ struct ComponentWrapper
{
friend struct ComponentWrapper;
private:
SubscriptProxy(ComponentWrapper* component, std::string fieldName)
SubscriptProxy(ComponentWrapper* component, std::string propertyName)
: m_Component(component)
, m_FieldName(fieldName)
, m_PropertyName(propertyName)
{ }
ComponentWrapper* m_Component;
std::string m_FieldName;
std::string m_PropertyName;
public:
// Return the integer value of an enum type key for this field
ComponentInfo::EnumType Enum(const char* enumKey) { return m_Component->Enum(m_FieldName.c_str(), enumKey); }
bool Dirty(DirtySetType type) { return m_Component->Dirty(type, m_FieldName); }
void SetDirty(DirtySetType type, bool dirty = true) { m_Component->SetDirty(type, m_FieldName, dirty); }
ComponentInfo::EnumType Enum(const char* enumKey) { return m_Component->Enum(m_PropertyName.c_str(), enumKey); }
template <typename T>
operator T&() { return m_Component->Field<T>(m_FieldName); }
operator T&() { return m_Component->Field<T>(m_PropertyName); }
template <typename T>
void operator=(const T val) { m_Component->SetField<T>(m_FieldName, val); }
void operator=(const T val) { m_Component->SetField<T>(m_PropertyName, val); }
// TODO: Pass by reference and rvalue (universal reference?)
//template <typename T>
//void operator=(T& val) { m_Component->SetField<T>(m_PropertyName, val); }
// Specialization for string literals
template <std::size_t N>
void operator=(const char(&val)[N]) { m_Component->SetField<N>(m_FieldName, val); }
void operator=(const char(&val)[N]) { m_Component->SetField<N>(m_PropertyName, val); }
};
SubscriptProxy operator[](const std::string& propertyName) { return SubscriptProxy(this, propertyName); }
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
};
// A component wrapper that "owns" its data through a shared pointer
struct SharedComponentWrapper : ComponentWrapper
{
SharedComponentWrapper(const ComponentInfo& componentInfo, boost::shared_array<char> data)
: ComponentWrapper(componentInfo, data.get(), nullptr)
: ComponentWrapper(componentInfo, data.get())
, m_DataReference(data)
{ }
-16
View File
@@ -1,16 +0,0 @@
#ifndef DirtySet_h__
#define DirtySet_h__
#include <set>
#include "ComponentInfo.h"
enum class DirtySetType
{
Transform,
Network
};
typedef std::unordered_map<DirtySetType, std::set<decltype(ComponentInfo::Field_t::Index)>> DirtyBitField;
typedef std::unordered_map<EntityID, DirtyBitField> DirtySet;
#endif
+1 -1
View File
@@ -81,7 +81,7 @@ public:
for (auto& pair : group.PureSystems) {
const std::string& componentName = pair.first;
auto& systems = pair.second;
ComponentPool* pool = m_World->GetComponents(componentName);
const ComponentPool* pool = m_World->GetComponents(componentName);
if (pool == nullptr) {
continue;
}
+1 -1
View File
@@ -35,7 +35,7 @@ public:
// Delete a component off an entity
void DeleteComponent(EntityID entity, const std::string& componentType);
// Get all components of the specified type
ComponentPool* GetComponents(const std::string& componentType);
const ComponentPool* GetComponents(const std::string& componentType);
// Get entity parent
EntityID GetParent(EntityID entity);
// Change the parent of an entity
@@ -15,6 +15,7 @@
#include "Rendering/Util/CommonFunctions.h"
//#define INDICATOR_TEST
#include "Core/ConfigFile.h"
class DamageIndicatorSystem : public ImpureSystem
{
@@ -40,6 +41,8 @@ private:
std::vector<DamageIndicatorStruct> updateDamageIndicatorVector;
float CalculateAngle(EntityWrapper player, glm::vec3 enemyPos);
bool m_NetworkEnabled;
//for tests
#ifdef INDICATOR_TEST
glm::vec3 DamageIndicatorTest(EntityWrapper player);
+7 -18
View File
@@ -1,17 +1,9 @@
#include "Core/ComponentPool.h"
ComponentPoolForwardIterator::ComponentPoolForwardIterator(ComponentPool* pool)
: m_ComponentPool(pool)
, m_ComponentInfo(pool->m_ComponentInfo)
, m_MemoryPoolIterator(pool->m_Pool.begin())
, m_MemoryPoolEnd(pool->m_Pool.end())
{ }
ComponentWrapper ComponentPoolForwardIterator::operator*() const
{
char* data = &(*m_MemoryPoolIterator);
EntityID entity = *reinterpret_cast<EntityID*>(data);
ComponentWrapper wrapper(m_ComponentInfo, data, &m_ComponentPool->m_DirtySet[entity]);
ComponentWrapper wrapper(m_ComponentInfo, data);
return wrapper;
}
@@ -79,7 +71,7 @@ ComponentWrapper ComponentPool::Allocate(EntityID entity)
memcpy(data, &entity, sizeof(EntityID));
m_EntityToComponent[entity] = data;
ComponentWrapper component(m_ComponentInfo, data, &m_DirtySet[entity]);
ComponentWrapper component(m_ComponentInfo, data);
// Copy defaults
memcpy(component.Data, m_ComponentInfo.Defaults.get(), m_ComponentInfo.Stride);
@@ -90,9 +82,7 @@ ComponentWrapper ComponentPool::Allocate(EntityID entity)
ComponentWrapper ComponentPool::GetByEntity(EntityID ent)
{
auto data = m_EntityToComponent.at(ent);
auto bitField = &m_DirtySet[ent];
return ComponentWrapper(m_ComponentInfo, data, bitField);
return ComponentWrapper(m_ComponentInfo, m_EntityToComponent.at(ent));
}
bool ComponentPool::KnowsEntity(EntityID ent)
@@ -105,17 +95,16 @@ void ComponentPool::Delete(ComponentWrapper& wrapper)
ComponentWrapper::Destroy(wrapper.Info, wrapper.Data);
m_EntityToComponent.erase(wrapper.EntityID);
m_Pool.Free(wrapper.Data - sizeof(EntityID));
m_DirtySet.erase(wrapper.EntityID);
}
ComponentPool::iterator ComponentPool::begin()
ComponentPool::iterator ComponentPool::begin() const
{
return iterator(this);
return iterator(m_ComponentInfo, m_Pool.begin(), m_Pool.end());
}
ComponentPool::iterator ComponentPool::end()
ComponentPool::iterator ComponentPool::end() const
{
return iterator(this);
return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end());
}
size_t ComponentPool::size() const
@@ -125,7 +125,6 @@ void EntityXMLFilePreprocessor::parseComponentInfo()
auto modelGroup = modelGroupParticle->getModelGroupTerm();
// <xs:element...
unsigned char fieldIndex = 0;
unsigned int fieldOffset = 0;
auto particles = modelGroup->getParticles();
for (unsigned int i = 0; i < particles->size(); ++i) {
@@ -183,14 +182,12 @@ void EntityXMLFilePreprocessor::parseComponentInfo()
auto& field = compInfo.Fields[name];
field.Name = name;
field.Type = effectiveType;
field.Index = fieldIndex;
field.Offset = fieldOffset;
field.Stride = stride;
compInfo.FieldsInOrder.push_back(name);
if (field.Type == "string") {
compInfo.StringFields.push_back(name);
}
fieldIndex += 1;
fieldOffset += stride;
}
+1 -1
View File
@@ -95,7 +95,7 @@ void World::DeleteComponent(EntityID entity, const std::string& componentType)
}
}
ComponentPool* World::GetComponents(const std::string& componentType)
const ComponentPool* World::GetComponents(const std::string& componentType)
{
auto it = m_ComponentPools.find(componentType);
return (it != m_ComponentPools.end()) ? it->second : nullptr;
+10 -4
View File
@@ -10,10 +10,11 @@ DamageIndicatorSystem::DamageIndicatorSystem(SystemParams params)
//load texture to cache
auto texture = CommonFunctions::LoadTexture("Textures/DamageIndicator.png", false);
auto entityFile = ResourceManager::Load<EntityXMLFile>("Schema/Entities/DamageIndicator.xml");
m_NetworkEnabled = ResourceManager::Load<ConfigFile>("Config.ini")->Get("Networking.StartNetwork", false);
}
void DamageIndicatorSystem::Update(double dt) {
if (!IsServer && LocalPlayer.Valid()) {
if ((!IsServer || !m_NetworkEnabled) && LocalPlayer.Valid()) {
for (auto& iter = updateDamageIndicatorVector.begin(); iter != updateDamageIndicatorVector.end(); iter++) {
if (!iter->spriteEntity.Valid()) {
updateDamageIndicatorVector.erase(iter);
@@ -40,10 +41,15 @@ bool DamageIndicatorSystem::OnPlayerDamage(Events::PlayerDamage& e)
return false;
}
//friendly fire - return
if (e.Damage < 0.1f) {
return false;
}
glm::vec3 inflictorPos = e.Inflictor["Transform"]["Position"];
//if testing
#ifdef INDICATOR_TEST
inflictorPos = DamageIndicatorTest(e.Victim);
inflictorPos = DamageIndicatorTest(e.Victim);
#endif
float angleBetweenVectors = CalculateAngle(e.Victim, inflictorPos);
@@ -55,7 +61,7 @@ bool DamageIndicatorSystem::OnPlayerDamage(Events::PlayerDamage& e)
//simply set the rotation z-wise to the angleBetweenVectors
sprite["Transform"]["Orientation"] = glm::vec3(0, 0, angleBetweenVectors);
if (!IsServer) {
if (!IsServer || !m_NetworkEnabled) {
updateDamageIndicatorVector.emplace_back(sprite, inflictorPos);
}
@@ -122,7 +128,7 @@ glm::vec3 DamageIndicatorSystem::DamageIndicatorTest(EntityWrapper player) {
}
m_TestVar++;
auto inflictorPos = glm::vec3(currentPos.x + testVar*6.0f, currentPos.y, currentPos.z + testVar2*6.0f);
auto inflictorPos = glm::vec3(currentPos.x + testVar*6.0f, currentPos.y, currentPos.z + testVar2*6.0f);
//load the explosioneffect XML
auto deathEffect = ResourceManager::Load<EntityXMLFile>("Schema/Entities/PlayerDeathExplosionWithCamera.xml");
+1 -1
View File
@@ -29,7 +29,7 @@ void PlayerMovementSystem::Update(double dt)
return;
}
m_SprintEffectTimer = 0.f;
auto pool = m_World->GetComponents("SprintAbility");
const ComponentPool* pool = m_World->GetComponents("SprintAbility");
if (pool == nullptr) {
return;
}
+1 -1
View File
@@ -86,7 +86,7 @@ bool SpawnerSystem::spawnedEntityIsColliding(EntityWrapper spawnedEntity, Entity
transformEntityToSpawnPoint(spawnedEntity, spawnPoint);
//Check if the spawned entity collides with anything, and if so, continue to the next spawnpoint.
EntityAABB spawnedBox = *Collision::EntityAbsoluteAABB(spawnedEntity);
auto otherSpawnedEntities = spawnPoint.World->GetComponents(dontCollideComponent);
const ComponentPool* otherSpawnedEntities = spawnPoint.World->GetComponents(dontCollideComponent);
for (const auto& obj : *otherSpawnedEntities) {
if (spawnedEntity.ID == obj.EntityID) {
continue;