From cd47f3a44ca7b45874e1c1e0cc1f1ce84852ffde Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Mon, 13 Sep 2021 17:46:16 +0800 Subject: [PATCH] Add name component and name-related entity commands --- src/entity/commands.cpp | 93 ++++++++++++++++++++---------- src/entity/commands.hpp | 23 +++++--- src/entity/components/name.hpp | 37 ++++++++++++ src/game/states/forage.cpp | 8 +-- src/game/states/loading.cpp | 25 +++----- src/game/states/nuptial-flight.cpp | 8 +-- 6 files changed, 126 insertions(+), 68 deletions(-) create mode 100644 src/entity/components/name.hpp diff --git a/src/entity/commands.cpp b/src/entity/commands.cpp index a853976..efddcf9 100644 --- a/src/entity/commands.cpp +++ b/src/entity/commands.cpp @@ -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 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(entity_id)) + if (registry.has(eid)) { - component::transform& transform = registry.get(entity_id); + component::transform& transform = registry.get(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(entity_id)) + if (registry.has(eid)) { - component::transform& transform = registry.get(entity_id); + component::transform& transform = registry.get(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(entity_id)) + if (registry.has(eid)) { - component::transform& transform = registry.get(entity_id); + component::transform& transform = registry.get(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(entity_id)) + if (registry.has(eid)) { - component::transform& transform = registry.get(entity_id); + component::transform& transform = registry.get(eid); transform.local.scale = scale; } } -void set_transform(entity::registry& registry, entity::id entity_id, const math::transform& transform, bool warp) +void set_transform(entity::registry& registry, entity::id eid, const math::transform& transform, bool warp) { - if (registry.has(entity_id)) + if (registry.has(eid)) { - component::transform& component = registry.get(entity_id); + component::transform& component = registry.get(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(entity_id)) + if (registry.has(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(entity_id); + 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 entity_id, unsigned int layers) +void assign_render_layers(entity::registry& registry, entity::id eid, unsigned int layers) { - if (registry.has(entity_id)) + if (registry.has(eid)) { - component::model model = registry.get(entity_id); + component::model model = registry.get(eid); model.layers = layers; - registry.replace(entity_id, model); + registry.replace(eid, model); // Apply to child layers registry.view().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(source, copy_transform); } -math::transform get_local_transform(entity::registry& registry, entity::id entity_id) +math::transform get_local_transform(entity::registry& registry, entity::id eid) { - if (registry.has(entity_id)) + if (registry.has(eid)) { - const component::transform& component = registry.get(entity_id); + const component::transform& component = registry.get(eid); return component.local; } return math::identity_transform; } -math::transform get_world_transform(entity::registry& registry, entity::id entity_id) +math::transform get_world_transform(entity::registry& registry, entity::id eid) { - if (registry.has(entity_id)) + if (registry.has(eid)) { - const component::transform& component = registry.get(entity_id); + const component::transform& component = registry.get(eid); return component.world; } @@ -162,5 +163,35 @@ void parent(entity::registry& registry, entity::id child, entity::id parent) registry.assign_or_replace(child, component); } +void rename(entity::registry& registry, entity::id eid, const std::string& name) +{ + registry.assign_or_replace(eid, name); +} + +entity::id find(entity::registry& registry, const std::string& name) +{ + auto view = registry.view(); + + 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 diff --git a/src/entity/commands.hpp b/src/entity/commands.hpp index 4d1429b..913bb6f 100644 --- a/src/entity/commands.hpp +++ b/src/entity/commands.hpp @@ -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& 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& 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 get_local_transform(entity::registry& registry, entity::id entity_id); -math::transform get_world_transform(entity::registry& registry, entity::id entity_id); +math::transform get_local_transform(entity::registry& registry, entity::id eid); +math::transform 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 diff --git a/src/entity/components/name.hpp b/src/entity/components/name.hpp new file mode 100644 index 0000000..7abf5ab --- /dev/null +++ b/src/entity/components/name.hpp @@ -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 . + */ + +#ifndef ANTKEEPER_ENTITY_COMPONENT_NAME_HPP +#define ANTKEEPER_ENTITY_COMPONENT_NAME_HPP + +#include + +namespace entity { +namespace component { + +struct name +{ + std::string id; +}; + +} // namespace component +} // namespace entity + +#endif // ANTKEEPER_ENTITY_COMPONENT_NAME_HPP + diff --git a/src/game/states/forage.cpp b/src/game/states/forage.cpp index 472cf2f..a026ea6 100644 --- a/src/game/states/forage.cpp +++ b/src/game/states/forage.cpp @@ -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("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("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(); diff --git a/src/game/states/loading.cpp b/src/game/states/loading.cpp index 3da4048..b75998d 100644 --- a/src/game/states/loading.cpp +++ b/src/game/states/loading.cpp @@ -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("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"); } diff --git a/src/game/states/nuptial-flight.cpp b/src/game/states/nuptial-flight.cpp index 33b4ea5..05c13ca 100644 --- a/src/game/states/nuptial-flight.cpp +++ b/src/game/states/nuptial-flight.cpp @@ -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(planet_eid)) @@ -82,6 +79,7 @@ void enter(game::context* ctx) entity::archetype* ant_round_eye_archetype = ctx->resource_manager->load("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("orb-ring.ent");