From 0171ed56ef05e8a0a383bb084edf74ad2822bde7 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Sun, 3 Oct 2021 19:16:25 +0800 Subject: [PATCH] Compartmentalize tool entity generation functions --- CMakeLists.txt | 1 + src/game/states/forage.cpp | 107 ++--------------------------------- src/game/states/loading.cpp | 39 +------------ src/game/tools.cpp | 108 ++++++++++++++++++++++++++++++++++++ src/game/tools.hpp | 33 +++++++++++ 5 files changed, 148 insertions(+), 140 deletions(-) create mode 100644 src/game/tools.cpp create mode 100644 src/game/tools.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d4aa351..af368e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.7) + option(VERSION_STRING "Project version string" "0.0.0") project(antkeeper VERSION ${VERSION_STRING} LANGUAGES CXX) diff --git a/src/game/states/forage.cpp b/src/game/states/forage.cpp index 3fc1cba..c9ea1aa 100644 --- a/src/game/states/forage.cpp +++ b/src/game/states/forage.cpp @@ -34,8 +34,7 @@ #include "entity/components/constraints/three-dof.hpp" #include "entity/components/constraint-stack.hpp" #include "application.hpp" -#include "utility/timestamp.hpp" -#include "animation/animator.hpp" +#include "game/tools.hpp" namespace game { namespace state { @@ -204,109 +203,11 @@ void setup_camera(game::context* ctx) void setup_tools(game::context* ctx) { - if (!ctx->entities.count("move_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["move_tool"] = tool_eid; - } - - if (!ctx->entities.count("paint_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["paint_tool"] = tool_eid; - } - - if (!ctx->entities.count("flip_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["flip_tool"] = tool_eid; - } - - if (!ctx->entities.count("poke_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["poke_tool"] = tool_eid; - } - - if (!ctx->entities.count("inspect_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["inspect_tool"] = tool_eid; - } - - if (!ctx->entities.count("label_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["label_tool"] = tool_eid; - } - - if (!ctx->entities.count("time_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["time_tool"] = tool_eid; - - entity::component::tool tool; - tool.active = [ctx]() - { - auto [mouse_x, mouse_y] = ctx->app->get_mouse()->get_current_position(); - auto [window_w, window_h] = ctx->app->get_viewport_dimensions(); - - entity::id planet_eid = ctx->entities["planet"]; - entity::component::celestial_body body = ctx->entity_registry->get(planet_eid); - body.axial_rotation = math::radians(360.0f) * ((float)mouse_x / (float)window_w); - ctx->entity_registry->replace(planet_eid, body); - }; - - ctx->entity_registry->assign(tool_eid, tool); - } - - if (!ctx->entities.count("camera_tool")) - { - entity::id tool_eid = ctx->entity_registry->create(); - ctx->entities["camera_tool"] = tool_eid; - - entity::component::tool tool; - tool.activated = [ctx]() - { - if (!ctx->camera_flash_animation->is_stopped()) - return; - - std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png"; - ctx->app->save_frame(path); - - material_property* tint = static_cast*>(ctx->camera_flash_billboard->get_material()->get_property("tint")); - tint->set_value({1.0f, 1.0f, 1.0f, 1.0f}); - ctx->camera_flash_billboard->get_material()->update_tweens(); - ctx->ui_scene->add_object(ctx->camera_flash_billboard); - - ctx->camera_flash_animation->set_end_callback - ( - [ctx]() - { - ctx->ui_scene->remove_object(ctx->camera_flash_billboard); - } - ); - - ctx->camera_flash_animation->set_frame_callback - ( - [ctx, tint](int channel, const float& opacity) - { - - tint->set_value({1.0f, 1.0f, 1.0f, opacity}); - } - ); - - ctx->animator->remove_animation(ctx->camera_flash_animation); - ctx->animator->add_animation(ctx->camera_flash_animation); - ctx->camera_flash_animation->rewind(); - ctx->camera_flash_animation->play(); - }; - - ctx->entity_registry->assign(tool_eid, tool); - } + ctx->entities["camera_tool"] = build_camera_tool(ctx); + ctx->entities["time_tool"] = build_time_tool(ctx); // Set active tool - ctx->entities["active_tool"] = ctx->entities["camera_tool"]; + ctx->entities["active_tool"] = ctx->entities["time_tool"]; } void setup_controls(game::context* ctx) diff --git a/src/game/states/loading.cpp b/src/game/states/loading.cpp index 84ca63b..7449b6b 100644 --- a/src/game/states/loading.cpp +++ b/src/game/states/loading.cpp @@ -312,28 +312,10 @@ void heliogenesis(game::context* ctx) void planetogenesis(game::context* ctx) { // Create planetary entity - entity::id planet_eid = ctx->entity_registry->create(); + entity::archetype* planet_archetype = ctx->resource_manager->load("planet.ent"); + entity::id planet_eid = planet_archetype->create(*ctx->entity_registry); ctx->entities["planet"] = planet_eid; - // Assign planetary celestial body component - entity::component::celestial_body body; - body.radius = 6.3781e6; - body.axial_tilt = math::radians(23.4393); - body.axial_rotation = math::radians(280.46061837504); - body.angular_frequency = math::radians(360.9856122880876128); - ctx->entity_registry->assign(planet_eid, body); - - // Assign planetary orbit component - entity::component::orbit orbit; - orbit.elements.a = 1.496e+11; - orbit.elements.e = 0.01671123; - orbit.elements.i = math::radians(-0.00001531); - orbit.elements.raan = math::radians(0.0); - const double longitude_periapsis = math::radians(102.93768193); - orbit.elements.w = longitude_periapsis - orbit.elements.raan; - orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis; - ctx->entity_registry->assign(planet_eid, orbit); - // Assign planetary terrain component entity::component::terrain terrain; terrain.elevation = [](double, double) -> double @@ -345,23 +327,6 @@ void planetogenesis(game::context* ctx) terrain.patch_material = nullptr; ctx->entity_registry->assign(planet_eid, terrain); - // Assign planetary atmosphere component - entity::component::atmosphere atmosphere; - atmosphere.exosphere_altitude = 65e3; - atmosphere.index_of_refraction = 1.000293; - atmosphere.rayleigh_density = 2.545e25; - atmosphere.rayleigh_scale_height = 8000.0; - atmosphere.mie_density = 14.8875; - atmosphere.mie_scale_height = 1200.0; - atmosphere.mie_anisotropy = 0.8; - ctx->entity_registry->assign(planet_eid, atmosphere); - - // Assign planetary transform component - entity::component::transform transform; - transform.local = math::identity_transform; - transform.warp = true; - ctx->entity_registry->assign(planet_eid, transform); - // Pass planet to astronomy system as reference body ctx->astronomy_system->set_reference_body(planet_eid); diff --git a/src/game/tools.cpp b/src/game/tools.cpp new file mode 100644 index 0000000..c283051 --- /dev/null +++ b/src/game/tools.cpp @@ -0,0 +1,108 @@ +/* + * 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 "game/tools.hpp" +#include "application.hpp" +#include "animation/animator.hpp" +#include "animation/animation.hpp" +#include "entity/components/tool.hpp" +#include "entity/components/celestial-body.hpp" +#include "utility/timestamp.hpp" +#include "renderer/material.hpp" + +namespace game { + +entity::id build_camera_tool(game::context* ctx) +{ + // Create camera tool entity + entity::id tool_eid = ctx->entity_registry->create(); + + // Create tool component + entity::component::tool tool; + + // Setup tool activated callback + tool.activated = [ctx]() + { + if (!ctx->camera_flash_animation->is_stopped()) + return; + + std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png"; + ctx->app->save_frame(path); + + material_property* tint = static_cast*>(ctx->camera_flash_billboard->get_material()->get_property("tint")); + tint->set_value({1.0f, 1.0f, 1.0f, 1.0f}); + ctx->camera_flash_billboard->get_material()->update_tweens(); + ctx->ui_scene->add_object(ctx->camera_flash_billboard); + + ctx->camera_flash_animation->set_end_callback + ( + [ctx]() + { + ctx->ui_scene->remove_object(ctx->camera_flash_billboard); + } + ); + + ctx->camera_flash_animation->set_frame_callback + ( + [ctx, tint](int channel, const float& opacity) + { + + tint->set_value({1.0f, 1.0f, 1.0f, opacity}); + } + ); + + ctx->animator->remove_animation(ctx->camera_flash_animation); + ctx->animator->add_animation(ctx->camera_flash_animation); + ctx->camera_flash_animation->rewind(); + ctx->camera_flash_animation->play(); + }; + + // Add tool component to camera tool entity + ctx->entity_registry->assign(tool_eid, tool); + + return tool_eid; +} + +entity::id build_time_tool(game::context* ctx) +{ + // Create time tool entity + entity::id tool_eid = ctx->entity_registry->create(); + + // Create tool component + entity::component::tool tool; + + // Setup tool active calback + tool.active = [ctx]() + { + auto [mouse_x, mouse_y] = ctx->app->get_mouse()->get_current_position(); + auto [window_w, window_h] = ctx->app->get_viewport_dimensions(); + + entity::id planet_eid = ctx->entities["planet"]; + entity::component::celestial_body body = ctx->entity_registry->get(planet_eid); + body.axial_rotation = math::radians(360.0f) * ((float)mouse_x / (float)window_w); + ctx->entity_registry->replace(planet_eid, body); + }; + + // Add tool component to time tool entity + ctx->entity_registry->assign(tool_eid, tool); + + return tool_eid; +} + +} // namespace game diff --git a/src/game/tools.hpp b/src/game/tools.hpp new file mode 100644 index 0000000..335a393 --- /dev/null +++ b/src/game/tools.hpp @@ -0,0 +1,33 @@ +/* + * 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_GAME_TOOLS_HPP +#define ANTKEEPER_GAME_TOOLS_HPP + +#include "game/context.hpp" +#include "entity/id.hpp" + +namespace game { + +entity::id build_camera_tool(game::context* ctx); +entity::id build_time_tool(game::context* ctx); + +} // namespace game + +#endif // ANTKEEPER_GAME_TOOLS_HPP