Created ComponentWrapperFactory to make test mocking easier and manual component registration a little less painful
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#include "../Common.h"
|
#include "../Common.h"
|
||||||
#include "EntityWrapper.h"
|
#include "EntityWrapper.h"
|
||||||
#include "ComponentInfo.h"
|
#include "ComponentInfo.h"
|
||||||
|
#include "Util/Any.h"
|
||||||
|
|
||||||
struct ComponentWrapper
|
struct ComponentWrapper
|
||||||
{
|
{
|
||||||
@@ -62,4 +63,43 @@ struct ComponentWrapper
|
|||||||
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
|
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: Move this to Tests once entity importing is finished
|
||||||
|
class ComponentWrapperFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ComponentWrapperFactory() = default;
|
||||||
|
ComponentWrapperFactory(std::string componentTypeName, std::size_t allocation = 0)
|
||||||
|
{
|
||||||
|
m_ComponentInfo.Name = componentTypeName;
|
||||||
|
m_ComponentInfo.Meta.Allocation = allocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void AddProperty(std::string fieldName, T defaultValue)
|
||||||
|
{
|
||||||
|
m_DefaultValues.push_back(defaultValue);
|
||||||
|
m_ComponentInfo.FieldTypes[fieldName] = typeid(T).name();
|
||||||
|
m_ComponentInfo.FieldOffsets[fieldName] = m_ComponentInfo.Meta.Stride;
|
||||||
|
m_ComponentInfo.Meta.Stride += sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentInfo& Finalize()
|
||||||
|
{
|
||||||
|
m_ComponentInfo.Defaults = std::shared_ptr<char>(new char[m_ComponentInfo.Meta.Stride]);
|
||||||
|
std::size_t offset = 0;
|
||||||
|
for (auto& val : m_DefaultValues) {
|
||||||
|
memcpy(m_ComponentInfo.Defaults.get() + offset, val.Data.get(), val.Size);
|
||||||
|
offset += val.Size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_ComponentInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator ComponentInfo&() { return Finalize(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
ComponentInfo m_ComponentInfo;
|
||||||
|
std::vector<Any> m_DefaultValues;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef Util_Any_h__
|
||||||
|
#define Util_Any_h__
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
struct Any
|
||||||
|
{
|
||||||
|
Any() { }
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Any(const T& value)
|
||||||
|
{
|
||||||
|
Data = std::shared_ptr<char>(new char[sizeof(T)]);
|
||||||
|
Size = sizeof(T);
|
||||||
|
memcpy(Data.get(), &value, Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Any(T&& value)
|
||||||
|
{
|
||||||
|
Data = std::shared_ptr<char>(new char[sizeof(T)]);
|
||||||
|
Size = sizeof(T);
|
||||||
|
memcpy(Data.get(), &value, Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Any& operator=(const T& value)
|
||||||
|
{
|
||||||
|
return Any(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Any& operator=(T&& value)
|
||||||
|
{
|
||||||
|
return Any(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<char> Data = nullptr;
|
||||||
|
std::size_t Size = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -3,39 +3,7 @@
|
|||||||
#include <boost/any.hpp>
|
#include <boost/any.hpp>
|
||||||
#include "GLM.h"
|
#include "GLM.h"
|
||||||
#include "Core/World.h"
|
#include "Core/World.h"
|
||||||
|
#include "Core/Util/Any.h"
|
||||||
struct any
|
|
||||||
{
|
|
||||||
any() { }
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
any(const T& value)
|
|
||||||
{
|
|
||||||
Buffer = std::shared_ptr<char>(new char[sizeof(T)]);
|
|
||||||
memcpy(Buffer.get(), &value, sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
any(T&& value)
|
|
||||||
{
|
|
||||||
Buffer = std::shared_ptr<char>(new char[sizeof(T)]);
|
|
||||||
memcpy(Buffer.get(), &value, sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
any& operator=(const T& value)
|
|
||||||
{
|
|
||||||
return any(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
any& operator=(T&& value)
|
|
||||||
{
|
|
||||||
return any(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<char> Buffer = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class HardcodedTestWorld : public World
|
class HardcodedTestWorld : public World
|
||||||
{
|
{
|
||||||
@@ -50,75 +18,66 @@ public:
|
|||||||
private:
|
private:
|
||||||
void registerTestComponents()
|
void registerTestComponents()
|
||||||
{
|
{
|
||||||
std::unordered_map<std::string, std::list<std::tuple<std::size_t, std::string, any>>> components
|
ComponentWrapperFactory f;
|
||||||
{
|
|
||||||
{
|
|
||||||
"Test",
|
|
||||||
{
|
|
||||||
std::make_tuple(sizeof(int), "TestInteger", 1337),
|
|
||||||
std::make_tuple(sizeof(float), "TestFloat", 13.37f)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Debug",
|
|
||||||
{
|
|
||||||
std::make_tuple(sizeof(std::string), "Name", std::string("Unnamed")),
|
|
||||||
std::make_tuple(sizeof(glm::vec3), "PickingColor", glm::vec3(0.f))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Transform",
|
|
||||||
{
|
|
||||||
std::make_tuple(sizeof(glm::vec3), "Position", glm::vec3(0.f, 0.f, 0.f)),
|
|
||||||
std::make_tuple(sizeof(glm::quat), "Orientation", glm::quat()),
|
|
||||||
std::make_tuple(sizeof(glm::vec3), "Scale", glm::vec3(1.f, 1.f, 1.f))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto& c : components) {
|
f = ComponentWrapperFactory("Test");
|
||||||
ComponentInfo ci;
|
f.AddProperty("TestInteger", 1337);
|
||||||
ci.Name = c.first;
|
f.AddProperty("TestFloat", 13.37f);
|
||||||
|
f.AddProperty("TestString", std::string("Carlito"));
|
||||||
|
RegisterComponent(f);
|
||||||
|
|
||||||
// Fields
|
f = ComponentWrapperFactory("Debug");
|
||||||
unsigned int stride = 0;
|
f.AddProperty("Name", std::string("Unnamed"));
|
||||||
for (auto& f : c.second) {
|
RegisterComponent(f);
|
||||||
stride += std::get<0>(f);
|
|
||||||
}
|
|
||||||
ci.Meta.Stride = stride;
|
|
||||||
ci.Defaults = std::shared_ptr<char>(new char[stride]);
|
|
||||||
unsigned int offset = 0;
|
|
||||||
for (auto& f : c.second) {
|
|
||||||
std::size_t size;
|
|
||||||
std::string fieldName;
|
|
||||||
any defaultValue;
|
|
||||||
std::tie(size, fieldName, defaultValue) = f;
|
|
||||||
|
|
||||||
ci.FieldOffsets[fieldName] = offset;
|
f = ComponentWrapperFactory("Transform");
|
||||||
ci.FieldTypes[fieldName] = "undefined";
|
f.AddProperty("Position", glm::vec3(0.f, 0.f, 0.f));
|
||||||
memcpy(ci.Defaults.get() + offset, defaultValue.Buffer.get(), size);
|
f.AddProperty("Orientation", glm::quat());
|
||||||
offset += size;
|
f.AddProperty("Scale", glm::vec3(1.f, 1.f, 1.f));
|
||||||
}
|
RegisterComponent(f);
|
||||||
|
|
||||||
RegisterComponent(ci);
|
f = ComponentWrapperFactory("Model");
|
||||||
}
|
f.AddProperty("Resource", std::string());
|
||||||
|
f.AddProperty("Color", glm::vec4(1.f, 1.f, 1.f, 1.f));
|
||||||
|
f.AddProperty("Visible", true);
|
||||||
|
RegisterComponent(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void createTestEntities()
|
void createTestEntities()
|
||||||
{
|
{
|
||||||
EntityID e = CreateEntity();
|
World& world = *this;
|
||||||
AttachComponent(e, "Test");
|
|
||||||
AttachComponent(e, "Debug");
|
// Create an entity
|
||||||
AttachComponent(e, "Transform");
|
EntityID e = world.CreateEntity();
|
||||||
ComponentWrapper testComponent = GetComponent(e, "Test");
|
|
||||||
int testValue = testComponent["TestInteger"];
|
// Attach a Debug component
|
||||||
float testFloat = testComponent["TestFloat"];
|
ComponentWrapper debug = world.AttachComponent(e, "Debug");
|
||||||
ComponentWrapper debugComponent = GetComponent(e, "Debug");
|
// Set the Name field of the Debug component using subscript operator
|
||||||
std::string name = debugComponent["Name"];
|
debug["Name"] = "Carlito";
|
||||||
glm::vec3 pickingColor = debugComponent["PickingColor"];
|
|
||||||
ComponentWrapper testTransform = GetComponent(e, "Transform");
|
// Attach a Transform component
|
||||||
glm::vec3 pos = testTransform["Position"];
|
world.AttachComponent(e, "Transform");
|
||||||
glm::quat ori = testTransform["Orientation"];
|
// Fetch the component based on EntityID and component type
|
||||||
glm::vec3 scale = testTransform["Scale"];
|
ComponentWrapper transform = world.GetComponent(e, "Transform");
|
||||||
|
// Set the fields of the Transform component
|
||||||
|
transform["Position"] = glm::vec3(0.f, 0.f, 0.f);
|
||||||
|
transform["Scale"] = glm::vec3(1.f, 1.f, 1.f);
|
||||||
|
|
||||||
|
// Move on the X axis by fetching field as reference
|
||||||
|
((glm::vec3&)transform["Position"]).x += 10.f;
|
||||||
|
// Shrink by a factor of 100
|
||||||
|
((glm::vec3&)transform["Scale"]) /= 100.f;
|
||||||
|
|
||||||
|
// Loop through all Transform components and print them
|
||||||
|
for (auto& transform : world.GetComponents("Transform")) {
|
||||||
|
glm::vec3 pos = transform["Position"];
|
||||||
|
std::cout << "Position: " << pos.x << " " << pos.y << " " << pos.z << std::endl;
|
||||||
|
glm::vec3 scale = transform["Scale"];
|
||||||
|
std::cout << "Scale: " << scale.x << " " << scale.y << " " << scale.z << std::endl;
|
||||||
|
|
||||||
|
// Fetch the Debug component also present in this entity
|
||||||
|
ComponentWrapper debug = world.GetComponent(transform.EntityID, "Debug");
|
||||||
|
std::cout << "Name: " << (std::string)debug["Name"] << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user