remove old transform and velocity components and replace with new decomposed components

focus on 2D for now
This commit is contained in:
2022-01-04 22:33:59 +01:00
parent c36fa30cbc
commit 12b0a90ad0
71 changed files with 1114 additions and 528 deletions

View File

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.8)
project(systems CXX)
add_subdirectory(transform)
add_subdirectory(simple_velocity)
if(NOT MM_HEADLESS)

View File

@ -6,22 +6,13 @@
namespace MM::Systems {
void player_velocity2d(entt::view<entt::get_t<MM::Input::PlayerID, MM::Components::Velocity2D>> view, const MM::Engine* engine) {
void player_velocity2d(entt::view<entt::get_t<const MM::Input::PlayerID, MM::Components::Velocity2DPositionIntent>> view, const MM::Engine* engine) {
ZoneScopedN("MM::Systems::PlayerVelocity2D");
auto& input_ss = engine->getService<MM::Services::InputService>();
view.each([&input_ss](const MM::Input::PlayerID p_id, MM::Components::Velocity2D& v) {
//const float movement_speed = 8.f; // apply via post processing
auto vec_force = input_ss.getMoveForce(p_id);
if (vec_force >= 0.01f) {
v.velocity = input_ss.getMoveVec(p_id);
//v.velocity *= vec_force;
} else {
v.velocity = {0.f, 0.f};
}
view.each([&input_ss](const MM::Input::PlayerID p_id, MM::Components::Velocity2DPositionIntent& v) {
v.intent = input_ss.getMoveVec(p_id);
});
}

View File

@ -1,14 +1,13 @@
#pragma once
#include <mm/engine_fwd.hpp>
#include <entt/entity/utility.hpp>
#include <mm/services/input_service.hpp>
#include <mm/components/velocity2d.hpp>
#include <mm/components/velocity2d_position_intent.hpp>
namespace MM::Systems {
// this system transforms the input from the input_service into velocity
void player_velocity2d(entt::view<entt::get_t<MM::Input::PlayerID, MM::Components::Velocity2D>> view, const MM::Engine* engine);
// this system transforms the input from the input_service into velocity intent
void player_velocity2d(entt::view<entt::get_t<const MM::Input::PlayerID, MM::Components::Velocity2DPositionIntent>> view, const MM::Engine* engine);
} // MM::Systems

View File

@ -1,21 +1,66 @@
#include "./simple_velocity_system2d.hpp"
#include "mm/components/rotation2d.hpp"
#include "mm/components/velocity2d_position.hpp"
#include "mm/components/velocity2d_rotation.hpp"
#include <entt/entity/registry.hpp>
namespace MM::Systems {
void simple_velocity(
void simple_positional_velocity(
entt::view<entt::get_t<
Components::Transform2D,
const Components::Velocity2D
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
) {
view.each([delta = td.tickDelta](auto, auto& t, auto& v) {
t.position += v.velocity * delta;
t.rotation += v.rotation * delta;
view.each([delta = td.tickDelta](auto& t, auto& v) {
t.pos += v.pos_vel * delta;
});
}
void simple_positional_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
) {
view.each([&scene, delta = td.tickDelta](const auto e, auto& t, auto& v) {
if (v.pos_vel.x != 0.f || v.pos_vel.y != 0.f) {
t.pos += v.pos_vel * delta;
scene.patch<MM::Components::Position2D>(e);
}
);
});
}
void simple_rotational_velocity(
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
) {
view.each([delta = td.tickDelta](auto& r, auto& v) {
r.rot += v.rot_vel * delta;
});
}
void simple_rotational_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
) {
view.each([&scene, delta = td.tickDelta](const auto e, auto& r, auto& v) {
if (v.rot_vel != 0.f) {
r.rot += v.rot_vel * delta;
scene.patch<MM::Components::Rotation2D>(e);
}
});
}
} // MM::Systems

View File

@ -1,17 +1,49 @@
#pragma once
#include <mm/components/velocity2d.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/velocity2d_position.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/time_delta.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <entt/fwd.hpp>
namespace MM::Systems {
void simple_velocity(
// non patching (on_update())
void simple_positional_velocity(
entt::view<entt::get_t<
Components::Transform2D,
const Components::Velocity2D
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
);
// patching (on_update())
void simple_positional_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
);
// non patching (on_update())
void simple_rotational_velocity(
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
);
// patching (on_update())
void simple_rotational_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
);

View File

@ -12,7 +12,8 @@ TEST(simple_velocity_2d, basic_run) {
// setup v system
auto& org = scene.set<entt::organizer>();
org.emplace<&MM::Systems::simple_velocity>("simple_velocity");
org.emplace<&MM::Systems::simple_positional_velocity>("simple_positional_velocity");
org.emplace<&MM::Systems::simple_rotational_velocity>("simple_rotational_velocity");
auto graph = org.graph();
// setup delta
@ -21,19 +22,21 @@ TEST(simple_velocity_2d, basic_run) {
// setup test entity
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
auto& v = scene.emplace<MM::Components::Velocity2D>(e);
t.position = { 0.f, 0.f };
t.rotation = 0.f;
auto& p = scene.emplace<MM::Components::Position2D>(e);
auto& r = scene.emplace<MM::Components::Rotation2D>(e);
auto& vp = scene.emplace<MM::Components::Velocity2DPosition>(e);
auto& vr = scene.emplace<MM::Components::Velocity2DRotation>(e);
p.pos = { 0.f, 0.f };
r.rot = 0.f;
v.velocity = { 1.f, 1.f };
v.rotation = 0.f;
vp.pos_vel = { 1.f, 1.f };
vr.rot_vel = 0.f;
// run all systems
for (auto&& vert : graph) {
vert.callback()(vert.data(), scene);
}
ASSERT_EQ(t.position.x, 1.f * 1.f/60.f);
ASSERT_EQ(p.pos.x, 1.f * 1.f/60.f);
}

View File

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.2)
project(transfrom_system CXX)
add_library(transform_system
src/mm/systems/transform.hpp
src/mm/systems/transform.cpp
)
target_include_directories(transform_system PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(transform_system
entt
glm
engine
common_components
tracy_client
)
#if (BUILD_TESTING)
#add_subdirectory(test)
#endif()

View File

@ -0,0 +1,43 @@
#include "./transform.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <tracy/Tracy.hpp>
namespace MM::Systems {
void position3d_from_2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position2D, const Components::Position2D_ZOffset, Components::Position3D>> view) {
ZoneScoped;
view.each([](const Components::Position2D& pos2, const Components::Position2D_ZOffset& zoff, Components::Position3D& pos3) {
pos3.pos = glm::vec3(pos2.pos, zoff.z_offset + pos2.pos.y/10.f);
});
}
void transform3d_translate(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position3D, Components::Transform4x4>> view) {
ZoneScoped;
view.each([](const Components::Position3D& pos, Components::Transform4x4& trans) {
trans.trans = glm::translate(glm::mat4x4{1.f}, pos.pos);
});
}
void transform3d_rotate2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Rotation2D, Components::Transform4x4>> view) {
ZoneScoped;
view.each([](const Components::Rotation2D& rot, Components::Transform4x4& trans) {
trans.trans = glm::rotate(trans.trans, rot.rot, glm::vec3(0.f, 0.f, 1.f));
});
}
void transform3d_scale2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Scale2D, Components::Transform4x4>> view) {
ZoneScoped;
view.each([](const Components::Scale2D& scale, Components::Transform4x4& trans) {
trans.trans = glm::scale(trans.trans, glm::vec3(scale.scale, 1.f));
});
}
void transform_clear_dirty(entt::registry& scene, entt::view<entt::get_t<Components::DirtyTransformTag>> view) {
ZoneScoped;
scene.remove<Components::DirtyTransformTag>(view.begin(), view.end());
}
} // MM::Systems

View File

@ -0,0 +1,53 @@
#pragma once
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <entt/entity/registry.hpp>
namespace MM::Systems {
// 2D usage:
//
// auto create dependencies, eg:
// scene.on_construct<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::Position3D>>();
// scene.on_construct<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::Transform4x4>>();
// scene.on_construct<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
//
// mark as dirty on change, eg:
// scene.on_update<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Position3D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Scale2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Rotation2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
//
// optinally cleanup on destruction, eg:
// scene.on_destroy<Components::Position2D>().connect<&entt::registry::remove<
// Components::DirtyTransformTag,
// Components::Position2D_ZOffset,
// Components::Position3D,
// Components::Scale2D,
// Components::Rotation2D,
// Components::Transform4x4
// >>();
// TODO: impl for no zoffset comp
void position3d_from_2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position2D, const Components::Position2D_ZOffset, Components::Position3D>> view);
void transform3d_translate(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position3D, Components::Transform4x4>> view);
void transform3d_rotate2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Rotation2D, Components::Transform4x4>> view);
void transform3d_scale2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Scale2D, Components::Transform4x4>> view);
void transform_clear_dirty(entt::registry& scene, entt::view<entt::get_t<Components::DirtyTransformTag>> view);
} // MM::Systems