Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graphite/engine/ecs/include/component.hpp
2024-01-03 16:05:02 -05:00

487 lines
No EOL
9.1 KiB
C++

#pragma once
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <interdata.hpp>
#include <utility.hpp>
struct DebugResult
{
struct DebugCube
{
DebugCube(glm::vec3 o, glm::vec3 s) : origin(o), size(s) {}
glm::vec3 origin, size;
};
void AddCube(glm::vec3 origin, glm::vec3 size)
{
debugCubes.push_back(DebugCube(origin, size));
}
std::vector<DebugCube> debugCubes;
};
class Entity;
class Component
{
friend class EntityPool;
public:
virtual void Load(IntermediateData&) {}
virtual void Save(IntermediateData&) {}
virtual DebugResult DrawDebug() { return DebugResult(); }
virtual std::vector<int> RequiredAssets()
{
return std::vector<int>();
}
Entity* GetEntity()
{
return entity_handle;
}
protected:
void SetEntity(Entity* entity)
{
entity_handle = entity;
}
private:
Entity* entity_handle = nullptr;
};
class Transform : public Component
{
public:
void Load(IntermediateData& data) override
{
position = data["position"];
orientation = data["orientation"];
scale = data["scale"];
parentID = data["parent"];
}
void Save(IntermediateData& data) override
{
data["position"] = position;
data["orientation"] = orientation;
data["scale"] = scale;
data["parent"] = parentID;
}
Transform* GetParent();
void SetParent(Transform* transform);
void SetEulerAngles(const glm::vec3& rot)
{
orientation = glm::quat(glm::vec3(glm::radians(rot.x), glm::radians(rot.y), glm::radians(rot.z)));
}
glm::vec3 GetEulerAngles()
{
glm::vec3 euler = glm::eulerAngles(orientation);
return glm::vec3(glm::degrees(euler.x), glm::degrees(euler.y), glm::degrees(euler.z));
}
glm::quat GetWorldOrientation()
{
glm::quat first = orientation;
glm::quat second;
if(GetParent() != nullptr)
second = GetParent()->orientation;
return second * first;
}
glm::vec3 GetWorldPosition()
{
return GetModel() * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
glm::vec3 GetForward()
{
return GetWorldOrientation() * glm::vec3(0.0f, 0.0f, 1.0f);
}
glm::vec3 GetUp()
{
return GetWorldOrientation() * glm::vec3(0.0f, 1.0f, 0.0f);
}
glm::vec3 GetRight()
{
return GetWorldOrientation() * glm::vec3(1.0f, 0.0f, 0.0f);
}
glm::mat4 GetModel()
{
glm::mat4 tmp(1.0f);
Transform* parent = GetParent();
if(parent != nullptr)
{
tmp = glm::translate(tmp, parent->position);
tmp *= glm::toMat4(parent->orientation);
tmp = glm::scale(tmp, parent->scale);
}
tmp = glm::translate(tmp, position);
tmp *= glm::toMat4(orientation);
tmp = glm::scale(tmp, scale);
return tmp;
}
std::vector<Transform*> GetChildren();
glm::vec3 position = glm::vec3(0);
glm::quat orientation = glm::quat();
glm::vec3 scale = glm::vec3(1);
int parentID = 0;
};
class MeshRenderer : public Component
{
public:
void Load(IntermediateData& data) override
{
meshID = data["meshid"];
materialID = data["matid"];
isStaticGeometry = data["static"];
}
void Save(IntermediateData& data) override
{
data["meshid"] = meshID;
data["matid"] = materialID;
data["static"] = isStaticGeometry;
}
std::vector<int> RequiredAssets() override
{
return { meshID, materialID };
}
int meshID = 0;
int materialID = 0;
bool isStaticGeometry = false;
};
class Light : public Component
{
public:
void Load(IntermediateData& data) override
{
color = data["color"];
radius = data["radius"];
type = data["type"];
shadowsEnabled = data["shadows"];
shadowFrustumSize = data["shadowsize"];
shadowZFar = data["shadowfar"];
}
void Save(IntermediateData& data) override
{
data["color"] = color;
data["radius"] = radius;
data["type"] = type;
data["shadows"] = shadowsEnabled;
data["shadowsize"] = shadowFrustumSize;
data["shadowfar"] = shadowZFar;
}
glm::mat4 GetProjection()
{
return glm::ortho(-shadowFrustumSize, shadowFrustumSize, -shadowFrustumSize, shadowFrustumSize, 0.1f, shadowZFar);
}
glm::vec3 color = glm::vec3(1);
float radius = 15.0f;
enum Type
{
Directional = 0,
Point = 1
};
int type = Point;
bool shadowsEnabled = false;
float shadowFrustumSize = 25.0f;
float shadowZFar = 50.0f;
};
class Camera : public Component
{
public:
void Load(IntermediateData& data) override
{
fieldOfView = static_cast<float>(data["fov"]);
}
void Save(IntermediateData& data) override
{
data["fov"] = static_cast<float>(fieldOfView);
}
glm::mat4 GetProjection();
glm::mat4 GetView();
float fieldOfView = 45.0f;
};
class ScriptActor : public Component
{
public:
void Load(IntermediateData& data) override
{
scriptPath = std::string(data["scriptpath"]);
}
void Save(IntermediateData& data) override
{
data["scriptpath"] = scriptPath;
}
std::string scriptPath;
bool intialized = false;
};
class btRigidBody;
class btCollisionShape;
class Rigidbody : public Component
{
friend class RigidbodyInspector;
public:
void Load(IntermediateData& data) override
{
m_mass = data["mass"];
m_friction = data["friction"];
m_enableRotation = data["enablerotation"];
m_kinematic = data["kinematic"];
m_enableDeactivation = data["enabledeactivation"];
}
void Save(IntermediateData& data) override
{
data["mass"] = m_mass;
data["friction"] = m_friction;
data["enablerotation"] = m_enableRotation;
data["kinematic"] = m_kinematic;
data["enabledeactivation"] = m_enableDeactivation;
}
void SetMass(float m)
{
m_mass = m;
isDirty = true;
}
float GetMass()
{
return m_mass;
}
void SetFriction(float f)
{
m_friction = f;
isDirty = true;
}
float GetFriction() {
return m_friction;
}
void EnableKinematic(bool k)
{
m_kinematic = k;
isDirty = true;
}
bool IsKinematic()
{
return m_kinematic;
}
void EnableDeactivation(bool d)
{
m_enableDeactivation = d;
isDirty = true;
}
bool IsDeactivationEnabled()
{
return m_enableDeactivation;
}
void SetRotation(bool r)
{
m_enableRotation = r;
isDirty = true;
}
bool IsRotationEnabled()
{
return m_enableRotation;
}
void AddForce(glm::vec3 dir)
{
m_force += dir;
}
void SetForce(glm::vec3 val)
{
m_force = val;
}
glm::vec3 GetForce()
{
return m_force;
}
bool isDirty = true;
btRigidBody* rigidbody = nullptr;
private:
bool m_kinematic = false;
bool m_enableDeactivation = true;
bool m_enableRotation = true;
float m_mass = 10;
float m_friction = 1.0f;
glm::vec3 m_force;
};
class Collider : public Component
{
public:
btCollisionShape* collisionShape;
bool isDirty = true;
};
class BoxCollider : public Collider
{
friend class BoxColliderInspector;
public:
void Load(IntermediateData& data) override
{
m_size = data["boxsize"];
m_origin = data["boxorigin"];
}
void Save(IntermediateData& data) override
{
data["boxsize"] = m_size;
data["boxorigin"] = m_origin;
}
DebugResult DrawDebug() override
{
DebugResult result;
result.AddCube(m_origin, m_size);
return result;
}
void SetOrigin(glm::vec3 origin)
{
m_origin = origin;
isDirty = true;
}
glm::vec3 GetOrigin()
{
return m_origin;
}
void SetSize(glm::vec3 size)
{
m_size = size;
isDirty = true;
}
glm::vec3 GetSize()
{
return m_size;
}
private:
glm::vec3 m_size = glm::vec3(1);
glm::vec3 m_origin = glm::vec3(0);
};
class CapsuleCollider : public Collider
{
public:
void Load(IntermediateData& data) override
{
m_height = data["height"];
}
void Save(IntermediateData& data) override
{
data["height"] = m_height;
}
void SetHeight(float val)
{
m_height = val;
isDirty = true;
}
float GetHeight()
{
return m_height;
}
private:
float m_height = 1.0f;
};
class PlayerStart : public Component
{
public:
};
class Player : public Component
{
public:
bool initialized = false;
glm::vec3 previousDirection;
};
class PlayerCamera : public Component
{
public:
float yaw = 0, pitch = 0;
float lastxoffset = 0, lastyoffset = 0;
float lastYaw = 0, lastPitch = 0;
};
/*
* Used for dynamic environment mapping (objects that are in the scene)
*/
class EnvironmentProbe : public Component
{
public:
};