Browse Source

Add name component and name-related entity commands

master
C. J. Howard 3 years ago
parent
commit
cd47f3a44c
6 changed files with 126 additions and 68 deletions
  1. +62
    -31
      src/entity/commands.cpp
  2. +14
    -9
      src/entity/commands.hpp
  3. +37
    -0
      src/entity/components/name.hpp
  4. +3
    -5
      src/game/states/forage.cpp
  5. +7
    -18
      src/game/states/loading.cpp
  6. +3
    -5
      src/game/states/nuptial-flight.cpp

+ 62
- 31
src/entity/commands.cpp View File

@ -25,61 +25,62 @@
#include "entity/components/parent.hpp"
#include "entity/components/celestial-body.hpp"
#include "entity/components/terrain.hpp"
#include "entity/components/name.hpp"
#include <limits>
namespace entity {
namespace command {
void translate(entity::registry& registry, entity::id entity_id, const float3& translation)
void translate(entity::registry& registry, entity::id eid, const float3& translation)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
component::transform& transform = registry.get<component::transform>(entity_id);
component::transform& transform = registry.get<component::transform>(eid);
transform.local.translation += translation;
}
}
void move_to(entity::registry& registry, entity::id entity_id, const float3& position)
void move_to(entity::registry& registry, entity::id eid, const float3& position)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
component::transform& transform = registry.get<component::transform>(entity_id);
component::transform& transform = registry.get<component::transform>(eid);
transform.local.translation = position;
}
}
void warp_to(entity::registry& registry, entity::id entity_id, const float3& position)
void warp_to(entity::registry& registry, entity::id eid, const float3& position)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
component::transform& transform = registry.get<component::transform>(entity_id);
component::transform& transform = registry.get<component::transform>(eid);
transform.local.translation = position;
transform.warp = true;
}
}
void set_scale(entity::registry& registry, entity::id entity_id, const float3& scale)
void set_scale(entity::registry& registry, entity::id eid, const float3& scale)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
component::transform& transform = registry.get<component::transform>(entity_id);
component::transform& transform = registry.get<component::transform>(eid);
transform.local.scale = scale;
}
}
void set_transform(entity::registry& registry, entity::id entity_id, const math::transform<float>& transform, bool warp)
void set_transform(entity::registry& registry, entity::id eid, const math::transform<float>& transform, bool warp)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
component::transform& component = registry.get<component::transform>(entity_id);
component::transform& component = registry.get<component::transform>(eid);
component.local = transform;
component.warp = warp;
}
}
void place(entity::registry& registry, entity::id entity_id, entity::id celestial_body_id, double altitude, double latitude, double longitude)
void place(entity::registry& registry, entity::id eid, entity::id celestial_body_id, double altitude, double latitude, double longitude)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
double x = 0.0;
double y = altitude;
@ -103,26 +104,26 @@ void place(entity::registry& registry, entity::id entity_id, entity::id celestia
}
}
component::transform& transform = registry.get<component::transform>(entity_id);
component::transform& transform = registry.get<component::transform>(eid);
transform.local.translation = math::type_cast<float>(double3{x, y, z});
transform.warp = true;
}
}
void assign_render_layers(entity::registry& registry, entity::id entity_id, unsigned int layers)
void assign_render_layers(entity::registry& registry, entity::id eid, unsigned int layers)
{
if (registry.has<component::model>(entity_id))
if (registry.has<component::model>(eid))
{
component::model model = registry.get<component::model>(entity_id);
component::model model = registry.get<component::model>(eid);
model.layers = layers;
registry.replace<component::model>(entity_id, model);
registry.replace<component::model>(eid, model);
// Apply to child layers
registry.view<component::parent>().each(
[&](entity::id entity_id, auto& component)
[&](entity::id eid, auto& component)
{
if (component.parent == entity_id)
assign_render_layers(registry, entity_id, layers);
if (component.parent == eid)
assign_render_layers(registry, eid, layers);
});
}
}
@ -133,22 +134,22 @@ void bind_transform(entity::registry& registry, entity::id source, entity::id ta
registry.assign_or_replace<component::copy_transform>(source, copy_transform);
}
math::transform<float> get_local_transform(entity::registry& registry, entity::id entity_id)
math::transform<float> get_local_transform(entity::registry& registry, entity::id eid)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
const component::transform& component = registry.get<component::transform>(entity_id);
const component::transform& component = registry.get<component::transform>(eid);
return component.local;
}
return math::identity_transform<float>;
}
math::transform<float> get_world_transform(entity::registry& registry, entity::id entity_id)
math::transform<float> get_world_transform(entity::registry& registry, entity::id eid)
{
if (registry.has<component::transform>(entity_id))
if (registry.has<component::transform>(eid))
{
const component::transform& component = registry.get<component::transform>(entity_id);
const component::transform& component = registry.get<component::transform>(eid);
return component.world;
}
@ -162,5 +163,35 @@ void parent(entity::registry& registry, entity::id child, entity::id parent)
registry.assign_or_replace<component::parent>(child, component);
}
void rename(entity::registry& registry, entity::id eid, const std::string& name)
{
registry.assign_or_replace<component::name>(eid, name);
}
entity::id find(entity::registry& registry, const std::string& name)
{
auto view = registry.view<component::name>();
for (auto eid: view)
{
if (view.get(eid).id == name)
return eid;
}
return entt::null;
}
entity::id create(entity::registry& registry)
{
return registry.create();
}
entity::id create(entity::registry& registry, const std::string& name)
{
auto eid = registry.create();
rename(registry, eid, name);
return eid;
}
} // namespace command
} // namespace entity

+ 14
- 9
src/entity/commands.hpp View File

@ -30,18 +30,23 @@ namespace entity {
/// Commands which operate on entity::id components
namespace command {
void translate(entity::registry& registry, entity::id entity_id, const float3& translation);
void move_to(entity::registry& registry, entity::id entity_id, const float3& position);
void warp_to(entity::registry& registry, entity::id entity_id, const float3& position);
void set_scale(entity::registry& registry, entity::id entity_id, const float3& scale);
void set_transform(entity::registry& registry, entity::id entity_id, const math::transform<float>& transform, bool warp = false);
void place(entity::registry& registry, entity::id entity_id, entity::id celestial_body_id, double altitude, double latitude, double longitude);
void assign_render_layers(entity::registry& registry, entity::id entity_id, unsigned int layers);
void translate(entity::registry& registry, entity::id eid, const float3& translation);
void move_to(entity::registry& registry, entity::id eid, const float3& position);
void warp_to(entity::registry& registry, entity::id eid, const float3& position);
void set_scale(entity::registry& registry, entity::id eid, const float3& scale);
void set_transform(entity::registry& registry, entity::id eid, const math::transform<float>& transform, bool warp = false);
void place(entity::registry& registry, entity::id eid, entity::id celestial_body_id, double altitude, double latitude, double longitude);
void assign_render_layers(entity::registry& registry, entity::id eid, unsigned int layers);
void bind_transform(entity::registry& registry, entity::id source_eid, entity::id target_eid);
math::transform<float> get_local_transform(entity::registry& registry, entity::id entity_id);
math::transform<float> get_world_transform(entity::registry& registry, entity::id entity_id);
math::transform<float> get_local_transform(entity::registry& registry, entity::id eid);
math::transform<float> get_world_transform(entity::registry& registry, entity::id eid);
void parent(entity::registry& registry, entity::id child, entity::id parent);
void rename(entity::registry& registry, entity::id eid, const std::string& name);
entity::id find(entity::registry& registry, const std::string& name);
entity::id create(entity::registry& registry);
entity::id create(entity::registry& registry, const std::string& name);
} // namespace command
} // namespace entity

+ 37
- 0
src/entity/components/name.hpp View File

@ -0,0 +1,37 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_ENTITY_COMPONENT_NAME_HPP
#define ANTKEEPER_ENTITY_COMPONENT_NAME_HPP
#include <string>
namespace entity {
namespace component {
struct name
{
std::string id;
};
} // namespace component
} // namespace entity
#endif // ANTKEEPER_ENTITY_COMPONENT_NAME_HPP

+ 3
- 5
src/game/states/forage.cpp View File

@ -42,11 +42,7 @@ void enter(game::context* ctx)
ctx->surface_camera->set_active(true);
// Find planet EID by name
entity::id planet_eid = entt::null;
if (auto it = ctx->named_entities.find("planet"); it != ctx->named_entities.end())
{
planet_eid = it->second;
}
auto planet_eid = entity::command::find(*ctx->entity_registry, "planet");
// Create biome terrain component
entity::component::terrain biome_terrain;
@ -101,6 +97,7 @@ void enter(game::context* ctx)
entity::archetype* larva_archetype = ctx->resource_manager->load<entity::archetype>("ant-larva.ent");
auto larva_eid = larva_archetype->create(*ctx->entity_registry);
entity::command::warp_to(*ctx->entity_registry, larva_eid, {50, 0.1935f, 0});
entity::command::assign_render_layers(*ctx->entity_registry, larva_eid, 0b10);
}
// Create cocoon
@ -108,6 +105,7 @@ void enter(game::context* ctx)
entity::archetype* cocoon_archetype = ctx->resource_manager->load<entity::archetype>("ant-cocoon.ent");
auto cocoon_eid = cocoon_archetype->create(*ctx->entity_registry);
entity::command::warp_to(*ctx->entity_registry, cocoon_eid, {-50, 0.1935f, 0});
entity::command::assign_render_layers(*ctx->entity_registry, cocoon_eid, 0b10);
}
ctx->surface_scene->update_tweens();

+ 7
- 18
src/game/states/loading.cpp View File

@ -29,6 +29,7 @@
#include "entity/components/transform.hpp"
#include "entity/systems/astronomy.hpp"
#include "entity/systems/orbit.hpp"
#include "entity/commands.hpp"
#include "game/states/nuptial-flight.hpp"
#include "game/states/splash.hpp"
#include "geom/spherical.hpp"
@ -181,10 +182,7 @@ void cosmogenesis(game::context* ctx)
void heliogenesis(game::context* ctx)
{
// Create solar entity
auto sun_eid = ctx->entity_registry->create();
// Name solar entity
ctx->named_entities["sun"] = sun_eid;
auto sun_eid = entity::command::create(*ctx->entity_registry, "sun");
// Assign solar celestial body component
entity::component::celestial_body body;
@ -236,10 +234,7 @@ void heliogenesis(game::context* ctx)
void planetogenesis(game::context* ctx)
{
// Create planetary entity
auto planet_eid = ctx->entity_registry->create();
// Name planetary entity
ctx->named_entities["planet"] = planet_eid;
auto planet_eid = entity::command::create(*ctx->entity_registry, "planet");
// Assign planetary celestial body component
entity::component::celestial_body body;
@ -298,10 +293,7 @@ void planetogenesis(game::context* ctx)
void selenogenesis(game::context* ctx)
{
// Create lunar entity
auto moon_eid = ctx->entity_registry->create();
// Name lunar entity
ctx->named_entities["moon"] = moon_eid;
auto moon_eid = entity::command::create(*ctx->entity_registry, "moon");
// Pass moon model to sky pass
ctx->surface_sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
@ -419,16 +411,13 @@ void extrasolar_heliogenesis(game::context* ctx)
void colonigenesis(game::context* ctx)
{
// Create queen entity
auto queen_eid = ctx->entity_registry->create();
ctx->named_entities["queen"] = queen_eid;
auto queen_eid = entity::command::create(*ctx->entity_registry, "queen");
// Create central shaft entity
auto shaft_eid = ctx->entity_registry->create();
ctx->named_entities["shaft"] = queen_eid;
auto shaft_eid = entity::command::create(*ctx->entity_registry, "shaft");
// Create entrance "lobby" chamber entity
auto lobby_eid = ctx->entity_registry->create();
ctx->named_entities["lobby"] = lobby_eid;
auto lobby_eid = entity::command::create(*ctx->entity_registry, "lobby");
}

+ 3
- 5
src/game/states/nuptial-flight.cpp View File

@ -27,6 +27,7 @@
#include "entity/components/camera-follow.hpp"
#include "entity/components/transform.hpp"
#include "entity/components/terrain.hpp"
#include "entity/commands.hpp"
#include "renderer/material-property.hpp"
#include "animation/screen-transition.hpp"
#include "animation/ease.hpp"
@ -44,11 +45,7 @@ void enter(game::context* ctx)
ctx->surface_camera->set_active(true);
// Find planet EID by name
entity::id planet_eid = entt::null;
if (auto it = ctx->named_entities.find("planet"); it != ctx->named_entities.end())
{
planet_eid = it->second;
}
auto planet_eid = entity::command::find(*ctx->entity_registry, "planet");
// Remove terrain component from planet (if any)
if (ctx->entity_registry->has<entity::component::terrain>(planet_eid))
@ -82,6 +79,7 @@ void enter(game::context* ctx)
entity::archetype* ant_round_eye_archetype = ctx->resource_manager->load<entity::archetype>("ant-round-eye.ent");
auto ant_round_eye_eid = ctx->entity_registry->create();
ant_round_eye_archetype->assign(*ctx->entity_registry, ant_round_eye_eid);
entity::command::assign_render_layers(*ctx->entity_registry, ant_round_eye_eid, 0b10);
// Create green orb ring
entity::archetype* orb_ring_archetype = ctx->resource_manager->load<entity::archetype>("orb-ring.ent");

Loading…
Cancel
Save