|
|
|
|
/******************************************************************************
|
|
|
|
|
* File - entity.cpp
|
|
|
|
|
* Author - Joey Pollack
|
|
|
|
|
* Date - 2022/05/23 (y/m/d)
|
|
|
|
|
* Mod Date - 2022/05/23 (y/m/d)
|
|
|
|
|
* Description - Provides functionality to work with world entities
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "entity.h"
|
|
|
|
|
#include "world.h"
|
|
|
|
|
#include "components.h"
|
|
|
|
|
#include <utils/logger.h>
|
|
|
|
|
|
|
|
|
|
namespace lunarium
|
|
|
|
|
{
|
|
|
|
|
Entity::Entity(World& w)
|
|
|
|
|
: mHandle(entt::null), mWorld(w)
|
|
|
|
|
{
|
|
|
|
|
mUUID = UUID::GetNewID();
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entity::Entity(World& w, LUUID uuid, entt::entity handle)
|
|
|
|
|
: mHandle(handle), mWorld(w), mUUID(uuid)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Entity::Init()
|
|
|
|
|
{
|
|
|
|
|
auto prev = mHandle;
|
|
|
|
|
mHandle = mWorld.GetEntityRegistry()->create(mHandle);
|
|
|
|
|
|
|
|
|
|
if (prev != mHandle && prev != entt::null)
|
|
|
|
|
{
|
|
|
|
|
Logger::Warn(LogCategory::GAME_SYSTEM, "Requested entity handle was not used");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LUUID Entity::GetUUID() const
|
|
|
|
|
{
|
|
|
|
|
return mUUID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Entity::HasChildren() const
|
|
|
|
|
{
|
|
|
|
|
return mChildren.size() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Entity::AddChild(Entity* pChild)
|
|
|
|
|
{
|
|
|
|
|
mChildren.push_back(pChild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
// SERIALIZING
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
OpRes Entity::Serialize(nlohmann::ordered_json& node)
|
|
|
|
|
{
|
|
|
|
|
#if !BUILD_NO_EDITOR // Only does this when this is an editor build
|
|
|
|
|
|
|
|
|
|
node["UUID"] = mUUID;
|
|
|
|
|
|
|
|
|
|
auto& components = node["components"];
|
|
|
|
|
if (HasComponent<TagComponent>())
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json tag;
|
|
|
|
|
tag["type_name"] = "TagComponent";
|
|
|
|
|
tag["info"] = GetComponent<TagComponent>().Info;
|
|
|
|
|
components.emplace_back(tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (HasComponent<TransformComponent>())
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json transform;
|
|
|
|
|
TransformComponent& comp = GetComponent<TransformComponent>();
|
|
|
|
|
transform["type_name"] = "TransformComponent";
|
|
|
|
|
|
|
|
|
|
auto& pos = transform["position"];
|
|
|
|
|
pos["x"] = comp.Position.x;
|
|
|
|
|
pos["y"] = comp.Position.y;
|
|
|
|
|
pos["z"] = comp.Position.z;
|
|
|
|
|
|
|
|
|
|
auto& rot = transform["rotation"];
|
|
|
|
|
rot["x"] = comp.Rotation.x;
|
|
|
|
|
rot["y"] = comp.Rotation.y;
|
|
|
|
|
rot["z"] = comp.Rotation.z;
|
|
|
|
|
|
|
|
|
|
auto& scale = transform["scale"];
|
|
|
|
|
scale["x"] = comp.Scale.x;
|
|
|
|
|
scale["y"] = comp.Scale.y;
|
|
|
|
|
scale["z"] = comp.Scale.z;
|
|
|
|
|
|
|
|
|
|
components.emplace_back(transform);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (HasComponent<VelocityComponent>())
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json velocity;
|
|
|
|
|
VelocityComponent& comp = GetComponent<VelocityComponent>();
|
|
|
|
|
velocity["type_name"] = "VelocityComponent";
|
|
|
|
|
|
|
|
|
|
auto& pos = velocity["velocity"];
|
|
|
|
|
pos["x"] = comp.Velocity.x;
|
|
|
|
|
pos["y"] = comp.Velocity.y;
|
|
|
|
|
pos["z"] = comp.Velocity.z;
|
|
|
|
|
|
|
|
|
|
components.emplace_back(velocity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (HasComponent<CameraComponent>())
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json camera;
|
|
|
|
|
CameraComponent& comp = GetComponent<CameraComponent>();
|
|
|
|
|
camera["type_name"] = "CameraComponent";
|
|
|
|
|
|
|
|
|
|
auto& pos = camera["position"];
|
|
|
|
|
pos["x"] = comp.Camera.GetPosition().X;
|
|
|
|
|
pos["y"] = comp.Camera.GetPosition().Y;
|
|
|
|
|
pos["z"] = 0.0f;
|
|
|
|
|
|
|
|
|
|
auto& rot = camera["rotation"];
|
|
|
|
|
rot["degrees"] = comp.Camera.GetRotation();
|
|
|
|
|
|
|
|
|
|
auto& vpsize = camera["viewport_size"];
|
|
|
|
|
vpsize["width"] = comp.Camera.GetViewportSize().Width;
|
|
|
|
|
vpsize["height"] = comp.Camera.GetViewportSize().Height;
|
|
|
|
|
|
|
|
|
|
components.emplace_back(camera);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (HasComponent<BlockOutComponent>())
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json blockout;
|
|
|
|
|
BlockOutComponent& comp = GetComponent<BlockOutComponent>();
|
|
|
|
|
blockout["type_name"] = "BlockOutComponent";
|
|
|
|
|
|
|
|
|
|
auto& color = blockout["color"];
|
|
|
|
|
color["r"] = comp.Color.x;
|
|
|
|
|
color["g"] = comp.Color.y;
|
|
|
|
|
color["b"] = comp.Color.a;
|
|
|
|
|
color["a"] = comp.Color.w;
|
|
|
|
|
|
|
|
|
|
auto& size = blockout["size"];
|
|
|
|
|
size["width"] = comp.Size.x;
|
|
|
|
|
size["height"] = comp.Size.y;
|
|
|
|
|
|
|
|
|
|
components.emplace_back(blockout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: ADD CODE TO SERIALIZE ANY NEW COMPONENTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Children
|
|
|
|
|
auto& children = node["children"];
|
|
|
|
|
for (int i = 0; i < mChildren.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json child;
|
|
|
|
|
mChildren[i]->Serialize(child).LogIfFailed(LogCategory::GAME_SYSTEM);
|
|
|
|
|
children.emplace_back(child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
return OpRes::OK();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpRes Entity::Deserialize(nlohmann::ordered_json& node)
|
|
|
|
|
{
|
|
|
|
|
#if !BUILD_NO_EDITOR // Only does this when this is an editor build
|
|
|
|
|
|
|
|
|
|
mUUID = node["UUID"].get<u64>();
|
|
|
|
|
|
|
|
|
|
// TODO: Load components
|
|
|
|
|
auto& components = node["components"];
|
|
|
|
|
for (auto iter = components.begin(); iter != components.end(); iter++)
|
|
|
|
|
{
|
|
|
|
|
auto& comp = *iter;
|
|
|
|
|
|
|
|
|
|
std::string comp_type_name = comp["type_name"].get<std::string>();
|
|
|
|
|
|
|
|
|
|
if ("TagComponent" == comp_type_name)
|
|
|
|
|
{
|
|
|
|
|
std::string info = comp["info"].get<std::string>();
|
|
|
|
|
AddComponent<TagComponent>(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("TransformComponent" == comp_type_name)
|
|
|
|
|
{
|
|
|
|
|
auto& pos = comp["position"];
|
|
|
|
|
float x = pos["x"].get<f32>();
|
|
|
|
|
float y = pos["y"].get<f32>();
|
|
|
|
|
float z = pos["z"].get<f32>();
|
|
|
|
|
glm::vec3 position(x, y, z);
|
|
|
|
|
|
|
|
|
|
auto& rot = comp["rotation"];
|
|
|
|
|
x = rot["x"].get<f32>();
|
|
|
|
|
y = rot["y"].get<f32>();
|
|
|
|
|
z = rot["z"].get<f32>();
|
|
|
|
|
glm::vec3 rotation(x, y, z);
|
|
|
|
|
|
|
|
|
|
auto& sc = comp["scale"];
|
|
|
|
|
x = sc["x"].get<f32>();
|
|
|
|
|
y = sc["y"].get<f32>();
|
|
|
|
|
z = sc["z"].get<f32>();
|
|
|
|
|
glm::vec3 scale(x, y, z);
|
|
|
|
|
|
|
|
|
|
AddComponent<TransformComponent>(position, rotation, scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("VelocityComponent" == comp_type_name)
|
|
|
|
|
{
|
|
|
|
|
auto& vel = comp["velocity"];
|
|
|
|
|
float x = vel["x"].get<f32>();
|
|
|
|
|
float y = vel["y"].get<f32>();
|
|
|
|
|
float z = vel["z"].get<f32>();
|
|
|
|
|
glm::vec3 velocity(x, y, z);
|
|
|
|
|
|
|
|
|
|
AddComponent<VelocityComponent>(velocity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ("CameraComponent" == comp_type_name)
|
|
|
|
|
{
|
|
|
|
|
auto& pos = comp["position"];
|
|
|
|
|
float x = pos["x"].get<f32>();
|
|
|
|
|
float y = pos["y"].get<f32>();
|
|
|
|
|
float z = pos["z"].get<f32>();
|
|
|
|
|
glm::vec3 position(x, y, z);
|
|
|
|
|
|
|
|
|
|
auto& rot = comp["rotation"];
|
|
|
|
|
float deg = rot["degrees"];
|
|
|
|
|
|
|
|
|
|
auto& vps = comp["viewport_size"];
|
|
|
|
|
float width = vps["width"].get<f32>();
|
|
|
|
|
float height = vps["height"].get<f32>();
|
|
|
|
|
|
|
|
|
|
OrthographicCamera cam({x, y}, {width, height});
|
|
|
|
|
cam.SetRotation(deg);
|
|
|
|
|
|
|
|
|
|
AddComponent<CameraComponent>(cam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ("BlockOutComponent" == comp_type_name)
|
|
|
|
|
{
|
|
|
|
|
auto& color = comp["color"];
|
|
|
|
|
glm::vec4 Color(color["r"].get<f32>(), color["g"].get<f32>(), color["b"].get<f32>(), color["a"].get<f32>());
|
|
|
|
|
|
|
|
|
|
auto& size = comp["size"];
|
|
|
|
|
glm::vec2 Size(size["width"].get<f32>(), size["height"].get<f32>());
|
|
|
|
|
|
|
|
|
|
AddComponent<BlockOutComponent>(Color, Size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: ADD CODE TO DESERIALIZE ANY NEW COMPONENTS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Load children
|
|
|
|
|
auto& children = node["children"];
|
|
|
|
|
for (auto iter = children.begin(); iter != children.end(); iter++)
|
|
|
|
|
{
|
|
|
|
|
auto& child = *iter;
|
|
|
|
|
|
|
|
|
|
Entity* ne = new Entity(mWorld);
|
|
|
|
|
ne->Deserialize(child).LogIfFailed(LogCategory::GAME_SYSTEM);
|
|
|
|
|
mChildren.push_back(ne);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
return OpRes::OK();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Entity::IsValidNode(nlohmann::ordered_json& node)
|
|
|
|
|
{
|
|
|
|
|
if (node["UUID"].is_null()) { return false; }
|
|
|
|
|
if (!node["UUID"].is_number()) { return false; }
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nlohmann::ordered_json Entity::AsJSON()
|
|
|
|
|
{
|
|
|
|
|
#if !BUILD_NO_EDITOR // Only does this when this is an editor build
|
|
|
|
|
nlohmann::ordered_json node;
|
|
|
|
|
|
|
|
|
|
Serialize(node).LogIfFailed(LogCategory::GAME_SYSTEM);
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return nlohmann::ordered_json();
|
|
|
|
|
}
|
|
|
|
|
}
|