/*
* Copyright (C) 2021 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see .
*/
#include "entity/commands.hpp"
#include "entity/components/model.hpp"
#include "entity/components/transform.hpp"
#include "entity/components/snap.hpp"
#include "entity/components/parent.hpp"
#include "entity/components/celestial-body.hpp"
#include "entity/components/terrain.hpp"
#include
namespace entity {
namespace command {
void translate(entity::registry& registry, entity::id eid, const float3& translation)
{
if (registry.has(eid))
{
component::transform& transform = registry.get(eid);
transform.local.translation += translation;
}
}
void rotate(entity::registry& registry, entity::id eid, float angle, const float3& axis)
{
if (registry.has(eid))
{
component::transform& transform = registry.get(eid);
transform.local.rotation = math::angle_axis(angle, axis) * transform.local.rotation;
}
}
void move_to(entity::registry& registry, entity::id eid, const float3& position)
{
if (registry.has(eid))
{
component::transform& transform = registry.get(eid);
transform.local.translation = position;
}
}
void warp_to(entity::registry& registry, entity::id eid, const float3& position)
{
if (registry.has(eid))
{
component::transform& transform = registry.get(eid);
transform.local.translation = position;
transform.warp = true;
}
}
void set_scale(entity::registry& registry, entity::id eid, const float3& scale)
{
if (registry.has(eid))
{
component::transform& transform = registry.get(eid);
transform.local.scale = scale;
}
}
void set_transform(entity::registry& registry, entity::id eid, const math::transform& transform, bool warp)
{
if (registry.has(eid))
{
component::transform& component = registry.get(eid);
component.local = transform;
component.warp = warp;
}
}
void place(entity::registry& registry, entity::id eid, entity::id celestial_body_id, double altitude, double latitude, double longitude)
{
if (registry.has(eid))
{
double x = 0.0;
double y = altitude;
double z = 0.0;
if (registry.has(celestial_body_id))
{
const component::celestial_body& celestial_body = registry.get(celestial_body_id);
x = longitude * math::two_pi * celestial_body.radius;
z = -latitude * math::two_pi * celestial_body.radius;
if (registry.has(celestial_body_id))
{
const component::terrain& terrain = registry.get(celestial_body_id);
if (terrain.elevation != nullptr)
{
y += terrain.elevation(latitude, longitude);
}
}
}
component::transform& transform = registry.get(eid);
transform.local.translation = math::type_cast(double3{x, y, z});
transform.warp = true;
}
}
void assign_render_layers(entity::registry& registry, entity::id eid, unsigned int layers)
{
if (registry.has(eid))
{
component::model model = registry.get(eid);
model.layers = layers;
registry.replace(eid, model);
// Apply to child layers
registry.view().each(
[&](entity::id eid, auto& component)
{
if (component.parent == eid)
assign_render_layers(registry, eid, layers);
});
}
}
math::transform get_local_transform(entity::registry& registry, entity::id eid)
{
if (registry.has(eid))
{
const component::transform& component = registry.get(eid);
return component.local;
}
return math::identity_transform;
}
math::transform get_world_transform(entity::registry& registry, entity::id eid)
{
if (registry.has(eid))
{
const component::transform& component = registry.get(eid);
return component.world;
}
return math::identity_transform;
}
void parent(entity::registry& registry, entity::id child, entity::id parent)
{
component::parent component;
component.parent = parent;
registry.assign_or_replace(child, component);
}
} // namespace command
} // namespace entity