DeerEngine/Deer/Include/Deer/Components.h

91 lines
2.3 KiB
C++
Executable File

#pragma once
#include "Deer/Tools/Memory.h"
#include "Deer/Tools/SmallVector.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <stdint.h>
#include <array>
#include <string>
#include <vector>
#include "Deer/Log.h"
#include "glm/glm.hpp"
#include "glm/gtc/quaternion.hpp"
#define ENTITY_BUFFER_CHILDREN 8
// Here we define the core components of the ECS
namespace Deer {
class ComponentScriptInstance;
struct TagComponent {
std::string tag;
uint32_t entityUID;
TagComponent() = default;
TagComponent(const TagComponent&) = default;
TagComponent(std::string name, uint32_t _id = 0)
: tag(name), entityUID(_id) {}
};
struct RelationshipComponent {
uint16_t parent_id = 0;
// Use p-ranav's small_vector for children storage
// Inline capacity ENTITY_MAX_CHILDREN, fallback to heap if exceeded
ankerl::svector<uint16_t, ENTITY_BUFFER_CHILDREN> children;
inline uint16_t getChildId(size_t i) const {
DEER_CORE_ASSERT(i < children.size() && children[i] != 0, "Invalid child request {0}", i);
return children[i];
}
inline void addChildId(uint16_t childId) {
// Prevent duplicates
for (auto id : children)
if (id == childId)
return;
children.push_back(childId);
}
inline bool removeChild(uint16_t childId) {
for (size_t i = 0; i < children.size(); ++i) {
if (children[i] == childId) {
// Swap-remove for O(1)
std::swap(children[i], children.back());
children.pop_back();
return true;
}
}
return false;
}
inline size_t getChildCount() const {
return children.size();
}
RelationshipComponent() = default;
RelationshipComponent(const RelationshipComponent&) = default;
RelationshipComponent(uint16_t _parent) : parent_id(_parent) {}
};
struct TransformComponent {
glm::vec3 position = glm::vec3(0.0f);
glm::vec3 scale = glm::vec3(1.0f);
glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
TransformComponent() = default;
TransformComponent(glm::vec3 _position) : position(_position) {}
TransformComponent(const TransformComponent&) = default;
inline const glm::vec3 getEulerAngles() {
return glm::degrees(glm::eulerAngles(rotation));
}
inline void setEulerAngles(const glm::vec3& eulerAngles) {
rotation = glm::quat(glm::radians(eulerAngles));
}
glm::mat4 getMatrix() const;
};
} // namespace Deer