Browse Source

Compartmentalize tool entity generation functions

master
C. J. Howard 3 years ago
parent
commit
0171ed56ef
5 changed files with 148 additions and 140 deletions
  1. +1
    -0
      CMakeLists.txt
  2. +4
    -103
      src/game/states/forage.cpp
  3. +2
    -37
      src/game/states/loading.cpp
  4. +108
    -0
      src/game/tools.cpp
  5. +33
    -0
      src/game/tools.hpp

+ 1
- 0
CMakeLists.txt View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
option(VERSION_STRING "Project version string" "0.0.0") option(VERSION_STRING "Project version string" "0.0.0")
project(antkeeper VERSION ${VERSION_STRING} LANGUAGES CXX) project(antkeeper VERSION ${VERSION_STRING} LANGUAGES CXX)

+ 4
- 103
src/game/states/forage.cpp View File

@ -34,8 +34,7 @@
#include "entity/components/constraints/three-dof.hpp" #include "entity/components/constraints/three-dof.hpp"
#include "entity/components/constraint-stack.hpp" #include "entity/components/constraint-stack.hpp"
#include "application.hpp" #include "application.hpp"
#include "utility/timestamp.hpp"
#include "animation/animator.hpp"
#include "game/tools.hpp"
namespace game { namespace game {
namespace state { namespace state {
@ -204,109 +203,11 @@ void setup_camera(game::context* ctx)
void setup_tools(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<entity::component::celestial_body>(planet_eid);
body.axial_rotation = math::radians(360.0f) * ((float)mouse_x / (float)window_w);
ctx->entity_registry->replace<entity::component::celestial_body>(planet_eid, body);
};
ctx->entity_registry->assign<entity::component::tool>(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<float4>* tint = static_cast<material_property<float4>*>(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<entity::component::tool>(tool_eid, tool);
}
ctx->entities["camera_tool"] = build_camera_tool(ctx);
ctx->entities["time_tool"] = build_time_tool(ctx);
// Set active tool // 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) void setup_controls(game::context* ctx)

+ 2
- 37
src/game/states/loading.cpp View File

@ -312,28 +312,10 @@ void heliogenesis(game::context* ctx)
void planetogenesis(game::context* ctx) void planetogenesis(game::context* ctx)
{ {
// Create planetary entity // Create planetary entity
entity::id planet_eid = ctx->entity_registry->create();
entity::archetype* planet_archetype = ctx->resource_manager->load<entity::archetype>("planet.ent");
entity::id planet_eid = planet_archetype->create(*ctx->entity_registry);
ctx->entities["planet"] = planet_eid; 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<entity::component::celestial_body>(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<entity::component::orbit>(planet_eid, orbit);
// Assign planetary terrain component // Assign planetary terrain component
entity::component::terrain terrain; entity::component::terrain terrain;
terrain.elevation = [](double, double) -> double terrain.elevation = [](double, double) -> double
@ -345,23 +327,6 @@ void planetogenesis(game::context* ctx)
terrain.patch_material = nullptr; terrain.patch_material = nullptr;
ctx->entity_registry->assign<entity::component::terrain>(planet_eid, terrain); ctx->entity_registry->assign<entity::component::terrain>(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<entity::component::atmosphere>(planet_eid, atmosphere);
// Assign planetary transform component
entity::component::transform transform;
transform.local = math::identity_transform<float>;
transform.warp = true;
ctx->entity_registry->assign<entity::component::transform>(planet_eid, transform);
// Pass planet to astronomy system as reference body // Pass planet to astronomy system as reference body
ctx->astronomy_system->set_reference_body(planet_eid); ctx->astronomy_system->set_reference_body(planet_eid);

+ 108
- 0
src/game/tools.cpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<float4>* tint = static_cast<material_property<float4>*>(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<entity::component::tool>(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<entity::component::celestial_body>(planet_eid);
body.axial_rotation = math::radians(360.0f) * ((float)mouse_x / (float)window_w);
ctx->entity_registry->replace<entity::component::celestial_body>(planet_eid, body);
};
// Add tool component to time tool entity
ctx->entity_registry->assign<entity::component::tool>(tool_eid, tool);
return tool_eid;
}
} // namespace game

+ 33
- 0
src/game/tools.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

Loading…
Cancel
Save