diff options
| author | untodesu <kirill@untode.su> | 2025-12-11 15:14:26 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-12-11 15:14:26 +0500 |
| commit | f40d09cb8f712e87691af4912f3630d92d692779 (patch) | |
| tree | 7ac3a4168ff722689372fd489c6f94d0a2546e8f /src/game/shared/entity | |
| parent | 8bcbd2729388edc63c82d77d314b583af1447c49 (diff) | |
| download | voxelius-f40d09cb8f712e87691af4912f3630d92d692779.tar.bz2 voxelius-f40d09cb8f712e87691af4912f3630d92d692779.zip | |
Shuffle stuff around
- Use the new and improved hierarchy I figured out when making Prospero chat
- Re-add NSIS scripts, again from Prospero
- Update most build and utility scripts with their most recent versions
Diffstat (limited to 'src/game/shared/entity')
| -rw-r--r-- | src/game/shared/entity/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | src/game/shared/entity/collision.cc | 173 | ||||
| -rw-r--r-- | src/game/shared/entity/collision.hh | 21 | ||||
| -rw-r--r-- | src/game/shared/entity/factory.cc | 37 | ||||
| -rw-r--r-- | src/game/shared/entity/factory.hh | 11 | ||||
| -rw-r--r-- | src/game/shared/entity/gravity.cc | 20 | ||||
| -rw-r--r-- | src/game/shared/entity/gravity.hh | 14 | ||||
| -rw-r--r-- | src/game/shared/entity/grounded.hh | 12 | ||||
| -rw-r--r-- | src/game/shared/entity/head.hh | 16 | ||||
| -rw-r--r-- | src/game/shared/entity/player.hh | 6 | ||||
| -rw-r--r-- | src/game/shared/entity/stasis.cc | 21 | ||||
| -rw-r--r-- | src/game/shared/entity/stasis.hh | 16 | ||||
| -rw-r--r-- | src/game/shared/entity/transform.cc | 33 | ||||
| -rw-r--r-- | src/game/shared/entity/transform.hh | 30 | ||||
| -rw-r--r-- | src/game/shared/entity/velocity.cc | 19 | ||||
| -rw-r--r-- | src/game/shared/entity/velocity.hh | 19 |
16 files changed, 464 insertions, 0 deletions
diff --git a/src/game/shared/entity/CMakeLists.txt b/src/game/shared/entity/CMakeLists.txt new file mode 100644 index 0000000..36e45b6 --- /dev/null +++ b/src/game/shared/entity/CMakeLists.txt @@ -0,0 +1,16 @@ +target_sources(shared PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/collision.cc" + "${CMAKE_CURRENT_LIST_DIR}/collision.hh" + "${CMAKE_CURRENT_LIST_DIR}/factory.cc" + "${CMAKE_CURRENT_LIST_DIR}/factory.hh" + "${CMAKE_CURRENT_LIST_DIR}/gravity.cc" + "${CMAKE_CURRENT_LIST_DIR}/gravity.hh" + "${CMAKE_CURRENT_LIST_DIR}/grounded.hh" + "${CMAKE_CURRENT_LIST_DIR}/head.hh" + "${CMAKE_CURRENT_LIST_DIR}/player.hh" + "${CMAKE_CURRENT_LIST_DIR}/stasis.cc" + "${CMAKE_CURRENT_LIST_DIR}/stasis.hh" + "${CMAKE_CURRENT_LIST_DIR}/transform.cc" + "${CMAKE_CURRENT_LIST_DIR}/transform.hh" + "${CMAKE_CURRENT_LIST_DIR}/velocity.cc" + "${CMAKE_CURRENT_LIST_DIR}/velocity.hh") diff --git a/src/game/shared/entity/collision.cc b/src/game/shared/entity/collision.cc new file mode 100644 index 0000000..bc9209b --- /dev/null +++ b/src/game/shared/entity/collision.cc @@ -0,0 +1,173 @@ +#include "shared/pch.hh" + +#include "shared/entity/collision.hh" + +#include "core/math/constexpr.hh" + +#include "shared/entity/gravity.hh" +#include "shared/entity/grounded.hh" +#include "shared/entity/transform.hh" +#include "shared/entity/velocity.hh" + +#include "shared/world/dimension.hh" +#include "shared/world/voxel_registry.hh" + +#include "shared/coord.hh" +#include "shared/globals.hh" + +static int vgrid_collide(const world::Dimension* dimension, int d, entity::Collision& collision, entity::Transform& transform, + entity::Velocity& velocity, world::VoxelMaterial& touch_surface) +{ + const auto move = globals::fixed_frametime * velocity.value[d]; + const auto move_sign = math::sign<int>(move); + + const auto& ref_aabb = collision.aabb; + const auto current_aabb = ref_aabb.push(transform.local); + + auto next_aabb = math::AABBf(current_aabb); + next_aabb.min[d] += move; + next_aabb.max[d] += move; + + local_pos lpos_min; + lpos_min.x = static_cast<local_pos::value_type>(glm::floor(next_aabb.min.x)); + lpos_min.y = static_cast<local_pos::value_type>(glm::floor(next_aabb.min.y)); + lpos_min.z = static_cast<local_pos::value_type>(glm::floor(next_aabb.min.z)); + + local_pos lpos_max; + lpos_max.x = static_cast<local_pos::value_type>(glm::ceil(next_aabb.max.x)); + lpos_max.y = static_cast<local_pos::value_type>(glm::ceil(next_aabb.max.y)); + lpos_max.z = static_cast<local_pos::value_type>(glm::ceil(next_aabb.max.z)); + + // Other axes + const int u = (d + 1) % 3; + const int v = (d + 2) % 3; + + local_pos::value_type ddir; + local_pos::value_type dmin; + local_pos::value_type dmax; + + if(move < 0.0f) { + ddir = local_pos::value_type(+1); + dmin = lpos_min[d]; + dmax = lpos_max[d]; + } + else { + ddir = local_pos::value_type(-1); + dmin = lpos_max[d]; + dmax = lpos_min[d]; + } + + world::VoxelTouch latch_touch = world::VTOUCH_NONE; + glm::fvec3 latch_values = glm::fvec3(0.0f, 0.0f, 0.0f); + world::VoxelMaterial latch_surface = world::VMAT_UNKNOWN; + math::AABBf latch_vbox; + + for(auto i = dmin; i != dmax; i += ddir) { + for(auto j = lpos_min[u]; j < lpos_max[u]; ++j) + for(auto k = lpos_min[v]; k < lpos_max[v]; ++k) { + local_pos lpos; + lpos[d] = i; + lpos[u] = j; + lpos[v] = k; + + auto vpos = coord::to_voxel(transform.chunk, lpos); + auto voxel = dimension->get_voxel(vpos); + + if(voxel == nullptr) { + // Don't collide with something + // that we assume to be nothing + continue; + } + + math::AABBf vbox(voxel->get_collision().push(lpos)); + + if(!next_aabb.intersect(vbox)) { + // No intersection between the voxel + // and the entity's collision hull + continue; + } + + if(voxel->is_touch_type<world::VTOUCH_SOLID>()) { + // Solid touch type makes a collision + // response whenever it is encountered + velocity.value[d] = 0.0f; + touch_surface = voxel->get_surface_material(); + return move_sign; + } + + // In case of other touch types, they + // are latched and the last ever touch + // type is then responded to + if(voxel->get_touch_type() != world::VTOUCH_NONE) { + latch_touch = voxel->get_touch_type(); + latch_values = voxel->get_touch_values(); + latch_surface = voxel->get_surface_material(); + latch_vbox = vbox; + continue; + } + } + } + + if(latch_touch != world::VTOUCH_NONE) { + if(latch_touch == world::VTOUCH_BOUNCE) { + const auto move_distance = glm::abs(current_aabb.min[d] - next_aabb.min[d]); + const auto threshold = 2.0f * globals::fixed_frametime; + + if(move_distance > threshold) { + velocity.value[d] *= -latch_values[d]; + } + else { + velocity.value[d] = 0.0f; + } + + touch_surface = latch_surface; + + return move_sign; + } + + if(latch_touch == world::VTOUCH_SINK) { + velocity.value[d] *= latch_values[d]; + touch_surface = latch_surface; + return move_sign; + } + } + + return 0; +} + +void entity::Collision::fixed_update(world::Dimension* dimension) +{ + // FIXME: this isn't particularly accurate considering + // some voxels might be passable and some other voxels + // might apply some slowing factor; what I might do in the + // future is to add a specific value to the voxel registry + // entries that would specify the amount of force we apply + // to prevent player movement inside a specific voxel, plus + // we shouldn't treat all voxels as full cubes if we want + // to support slabs, stairs and non-full liquid voxels in the future + + auto group = dimension->entities.group<entity::Collision>(entt::get<entity::Transform, entity::Velocity>); + + for(auto [entity, collision, transform, velocity] : group.each()) { + auto surface = world::VMAT_UNKNOWN; + auto vertical_move = vgrid_collide(dimension, 1, collision, transform, velocity, surface); + + if(dimension->entities.any_of<entity::Gravity>(entity)) { + if(vertical_move == math::sign<int>(dimension->get_gravity())) { + dimension->entities.emplace_or_replace<entity::Grounded>(entity, entity::Grounded { surface }); + } + else { + dimension->entities.remove<entity::Grounded>(entity); + } + } + else { + // The entity cannot be grounded because the component + // setup of said entity should not let it comprehend the + // concept of resting on the ground (it flies around) + dimension->entities.remove<entity::Grounded>(entity); + } + + vgrid_collide(dimension, 0, collision, transform, velocity, surface); + vgrid_collide(dimension, 2, collision, transform, velocity, surface); + } +} diff --git a/src/game/shared/entity/collision.hh b/src/game/shared/entity/collision.hh new file mode 100644 index 0000000..b37f409 --- /dev/null +++ b/src/game/shared/entity/collision.hh @@ -0,0 +1,21 @@ +#pragma once + +#include "core/math/aabb.hh" + +namespace world +{ +class Dimension; +} // namespace world + +namespace entity +{ +struct Collision final { + math::AABBf aabb; + +public: + // NOTE: entity::Collision::fixed_update must be called + // before entity::Transform::fixed_update and entity::Velocity::fixed_update + // because both transform and velocity may be updated internally + static void fixed_update(world::Dimension* dimension); +}; +} // namespace entity diff --git a/src/game/shared/entity/factory.cc b/src/game/shared/entity/factory.cc new file mode 100644 index 0000000..619a418 --- /dev/null +++ b/src/game/shared/entity/factory.cc @@ -0,0 +1,37 @@ +#include "shared/pch.hh" + +#include "shared/entity/factory.hh" + +#include "shared/entity/collision.hh" +#include "shared/entity/gravity.hh" +#include "shared/entity/head.hh" +#include "shared/entity/player.hh" +#include "shared/entity/transform.hh" +#include "shared/entity/velocity.hh" + +#include "shared/world/dimension.hh" + +#include "shared/globals.hh" + +void entity::shared::create_player(world::Dimension* dimension, entt::entity entity) +{ + spdlog::debug("factory[{}]: assigning player components to {}", dimension->get_name(), static_cast<std::uint64_t>(entity)); + + auto& collision = dimension->entities.emplace_or_replace<entity::Collision>(entity); + collision.aabb.min = glm::fvec3(-0.4f, -1.6f, -0.4f); + collision.aabb.max = glm::fvec3(+0.4f, +0.2f, +0.4f); + + auto& head = dimension->entities.emplace_or_replace<entity::Head>(entity); + head.angles = glm::fvec3(0.0f, 0.0f, 0.0f); + head.offset = glm::fvec3(0.0f, 0.0f, 0.0f); + + dimension->entities.emplace_or_replace<entity::Player>(entity); + + auto& transform = dimension->entities.emplace_or_replace<entity::Transform>(entity); + transform.chunk = chunk_pos(0, 2, 0); + transform.local = glm::fvec3(0.0f, 0.0f, 0.0f); + transform.angles = glm::fvec3(0.0f, 0.0f, 0.0f); + + auto& velocity = dimension->entities.emplace_or_replace<entity::Velocity>(entity); + velocity.value = glm::fvec3(0.0f, 0.0f, 0.0f); +} diff --git a/src/game/shared/entity/factory.hh b/src/game/shared/entity/factory.hh new file mode 100644 index 0000000..e709060 --- /dev/null +++ b/src/game/shared/entity/factory.hh @@ -0,0 +1,11 @@ +#pragma once + +namespace world +{ +class Dimension; +} // namespace world + +namespace entity::shared +{ +void create_player(world::Dimension* dimension, entt::entity entity); +} // namespace entity::shared diff --git a/src/game/shared/entity/gravity.cc b/src/game/shared/entity/gravity.cc new file mode 100644 index 0000000..f1708aa --- /dev/null +++ b/src/game/shared/entity/gravity.cc @@ -0,0 +1,20 @@ +#include "shared/pch.hh" + +#include "shared/entity/gravity.hh" + +#include "shared/entity/stasis.hh" +#include "shared/entity/velocity.hh" + +#include "shared/world/dimension.hh" + +#include "shared/globals.hh" + +void entity::Gravity::fixed_update(world::Dimension* dimension) +{ + auto fixed_acceleration = globals::fixed_frametime * dimension->get_gravity(); + auto group = dimension->entities.group<entity::Gravity>(entt::get<entity::Velocity>, entt::exclude<entity::Stasis>); + + for(auto [entity, velocity] : group.each()) { + velocity.value.y += fixed_acceleration; + } +} diff --git a/src/game/shared/entity/gravity.hh b/src/game/shared/entity/gravity.hh new file mode 100644 index 0000000..21f4582 --- /dev/null +++ b/src/game/shared/entity/gravity.hh @@ -0,0 +1,14 @@ +#pragma once + +namespace world +{ +class Dimension; +} // namespace world + +namespace entity +{ +struct Gravity final { +public: + static void fixed_update(world::Dimension* dimension); +}; +} // namespace entity diff --git a/src/game/shared/entity/grounded.hh b/src/game/shared/entity/grounded.hh new file mode 100644 index 0000000..6c33d1d --- /dev/null +++ b/src/game/shared/entity/grounded.hh @@ -0,0 +1,12 @@ +#pragma once + +#include "shared/world/voxel.hh" + +namespace entity +{ +// Assigned to entities which are grounded +// according to the collision and gravity system +struct Grounded final { + world::VoxelMaterial surface; +}; +} // namespace entity diff --git a/src/game/shared/entity/head.hh b/src/game/shared/entity/head.hh new file mode 100644 index 0000000..3f5d6d1 --- /dev/null +++ b/src/game/shared/entity/head.hh @@ -0,0 +1,16 @@ +#pragma once + +namespace entity +{ +struct Head { + glm::fvec3 angles; + glm::fvec3 offset; +}; +} // namespace entity + +namespace entity::client +{ +// Client-side only - interpolated and previous head +struct HeadIntr final : public Head {}; +struct HeadPrev final : public Head {}; +} // namespace entity::client diff --git a/src/game/shared/entity/player.hh b/src/game/shared/entity/player.hh new file mode 100644 index 0000000..4bd0217 --- /dev/null +++ b/src/game/shared/entity/player.hh @@ -0,0 +1,6 @@ +#pragma once + +namespace entity +{ +struct Player final {}; +} // namespace entity diff --git a/src/game/shared/entity/stasis.cc b/src/game/shared/entity/stasis.cc new file mode 100644 index 0000000..3b86294 --- /dev/null +++ b/src/game/shared/entity/stasis.cc @@ -0,0 +1,21 @@ +#include "shared/pch.hh" + +#include "shared/entity/stasis.hh" + +#include "shared/entity/transform.hh" + +#include "shared/world/dimension.hh" + +void entity::Stasis::fixed_update(world::Dimension* dimension) +{ + auto view = dimension->entities.view<entity::Transform>(); + + for(auto [entity, transform] : view.each()) { + if(dimension->find_chunk(transform.chunk)) { + dimension->entities.remove<entity::Stasis>(entity); + } + else { + dimension->entities.emplace_or_replace<entity::Stasis>(entity); + } + } +} diff --git a/src/game/shared/entity/stasis.hh b/src/game/shared/entity/stasis.hh new file mode 100644 index 0000000..e6cb9e4 --- /dev/null +++ b/src/game/shared/entity/stasis.hh @@ -0,0 +1,16 @@ +#pragma once + +namespace world +{ +class Dimension; +} // namespace world + +namespace entity +{ +// Attached to entities with transform values +// out of bounds in a specific dimension +struct Stasis final { +public: + static void fixed_update(world::Dimension* dimension); +}; +} // namespace entity diff --git a/src/game/shared/entity/transform.cc b/src/game/shared/entity/transform.cc new file mode 100644 index 0000000..379ddd5 --- /dev/null +++ b/src/game/shared/entity/transform.cc @@ -0,0 +1,33 @@ +#include "shared/pch.hh" + +#include "shared/entity/transform.hh" + +#include "shared/world/dimension.hh" + +#include "shared/const.hh" + +constexpr inline static void update_component(unsigned int dim, entity::Transform& component) +{ + if(component.local[dim] >= CHUNK_SIZE) { + component.local[dim] -= CHUNK_SIZE; + component.chunk[dim] += 1; + return; + } + + if(component.local[dim] < 0.0f) { + component.local[dim] += CHUNK_SIZE; + component.chunk[dim] -= 1; + return; + } +} + +void entity::Transform::fixed_update(world::Dimension* dimension) +{ + auto view = dimension->entities.view<entity::Transform>(); + + for(auto [entity, transform] : view.each()) { + update_component(0U, transform); + update_component(1U, transform); + update_component(2U, transform); + } +} diff --git a/src/game/shared/entity/transform.hh b/src/game/shared/entity/transform.hh new file mode 100644 index 0000000..2b357f8 --- /dev/null +++ b/src/game/shared/entity/transform.hh @@ -0,0 +1,30 @@ +#pragma once + +#include "shared/types.hh" + +namespace world +{ +class Dimension; +} // namespace world + +namespace entity +{ +struct Transform { + chunk_pos chunk; + glm::fvec3 local; + glm::fvec3 angles; + +public: + // Updates entity::Transform values so that + // the local translation field is always within + // local coodrinates; [floating-point precision] + static void fixed_update(world::Dimension* dimension); +}; +} // namespace entity + +namespace entity::client +{ +// Client-side only - interpolated and previous transform +struct TransformIntr final : public Transform {}; +struct TransformPrev final : public Transform {}; +} // namespace entity::client diff --git a/src/game/shared/entity/velocity.cc b/src/game/shared/entity/velocity.cc new file mode 100644 index 0000000..86df445 --- /dev/null +++ b/src/game/shared/entity/velocity.cc @@ -0,0 +1,19 @@ +#include "shared/pch.hh" + +#include "shared/entity/velocity.hh" + +#include "shared/entity/stasis.hh" +#include "shared/entity/transform.hh" + +#include "shared/world/dimension.hh" + +#include "shared/globals.hh" + +void entity::Velocity::fixed_update(world::Dimension* dimension) +{ + auto group = dimension->entities.group<entity::Velocity>(entt::get<entity::Transform>, entt::exclude<entity::Stasis>); + + for(auto [entity, velocity, transform] : group.each()) { + transform.local += velocity.value * globals::fixed_frametime; + } +} diff --git a/src/game/shared/entity/velocity.hh b/src/game/shared/entity/velocity.hh new file mode 100644 index 0000000..c8a1c91 --- /dev/null +++ b/src/game/shared/entity/velocity.hh @@ -0,0 +1,19 @@ +#pragma once + +namespace world +{ +class Dimension; +} // namespace world + +namespace entity +{ +struct Velocity final { + glm::fvec3 value; + +public: + // Updates entities entity::Transform values + // according to velocities multiplied by fixed_frametime. + // NOTE: This system was previously called inertial + static void fixed_update(world::Dimension* dimension); +}; +} // namespace entity |
