From b08b390c6eab5c0f38b72668a900b2ab0f942d9b Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 29 Aug 2025 18:13:50 +0200 Subject: [PATCH] feat: replaced running animation with procedural motion --- modules/wave_survival/player_camera.cpp | 22 +++++++++++++++++++++- modules/wave_survival/player_camera.h | 5 +++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/wave_survival/player_camera.cpp b/modules/wave_survival/player_camera.cpp index 776e0104..cda59c2e 100644 --- a/modules/wave_survival/player_camera.cpp +++ b/modules/wave_survival/player_camera.cpp @@ -1,5 +1,6 @@ #include "player_camera.h" #include "macros.h" +#include "player_body.h" #include "player_input.h" void PlayerCamera::_bind_methods() { @@ -7,7 +8,7 @@ void PlayerCamera::_bind_methods() { void PlayerCamera::on_look_input(Vector2 input) { GETSET(rotation, { - rotation.x = CLAMP(rotation.x + input.y, -Math::PI / 2.0, Math::PI / 2.0); + rotation.x = CLAMP(rotation.x + input.y, -Math::PI / 2.1, Math::PI / 2.1); }); global_rotate(Vector3{ 0.f, 1.f, 0.f }, input.x); } @@ -18,13 +19,32 @@ void PlayerCamera::update_fov() { } } +void PlayerCamera::update_offset() { + Basis const basis{ get_basis() }; + double wave{ Math::sin(this->time) }; + Vector3 const offset{ (this->body->get_is_running() ? Vector3{ float(wave), float(Math::abs(wave)), 0 } * 0.025 : Vector3{ 0, 0, 0 }) }; + GETSET(position, { + position = this->home + basis.get_column(0) * offset.x + basis.get_column(1) * offset.y; + }); + for (Variant child : get_children()) { + Node3D *child_3d{ cast_to(child) }; + GETSETM(child_3d, position, { + position = -offset; + }); + } +} + void PlayerCamera::ready() { PlayerInput *input{ cast_to(get_node(NodePath("%PlayerInput"))) }; input->connect(PlayerInput::sig_look_input, callable_mp(this, &self_type::on_look_input)); this->base_fov = get_fov(); + this->home = get_position(); + this->body = cast_to(get_parent()); } void PlayerCamera::process(double delta) { + this->time += delta * 10.; + update_offset(); this->fov_factor = 1.0; update_fov(); } diff --git a/modules/wave_survival/player_camera.h b/modules/wave_survival/player_camera.h index 7e686ac6..1489d5d1 100644 --- a/modules/wave_survival/player_camera.h +++ b/modules/wave_survival/player_camera.h @@ -2,12 +2,14 @@ #define PLAYER_CAMERA_H #include "scene/3d/camera_3d.h" +class PlayerBody; class PlayerCamera : public Camera3D { GDCLASS(PlayerCamera, Camera3D); static void _bind_methods(); void on_look_input(Vector2 input); void update_fov(); + void update_offset(); void ready(); void process(double delta); @@ -23,6 +25,9 @@ public: private: float base_fov{ 60.f }; float fov_factor{ 1.0f }; + Vector3 home{ 0, 0, 0 }; + double time{ 0.0 }; + PlayerBody *body{ nullptr }; }; #endif // !PLAYER_CAMERA_H