feat: final modifications for building

This commit is contained in:
Sara 2025-03-05 08:34:23 +01:00 committed by Sara
parent 75736b2528
commit 60fd92d827
29 changed files with 313 additions and 170 deletions

View file

@ -59,7 +59,7 @@ void ArtilleryTarget::timer_end() {
this->add_child(instance);
if(gd::Node3D *inst3d{gd::Object::cast_to<gd::Node3D>(instance)}) {
double const distance{gd::Math::sqrt(gd::UtilityFunctions::randf_range(0.f, 1.f) * this->spread)};
double const angle{gd::UtilityFunctions::randf_range(0., M_2_PI)};
double const angle{gd::UtilityFunctions::randf_range(0.f, 2.f/Math_PI)};
inst3d->set_global_position(this->get_global_position() + gd::Vector3{1.f, 0.f, 0.f}.rotated({0.f, 1.f, 0.f}, angle) * distance);
}
}

View file

@ -3,7 +3,6 @@
#include "utils/util_functions.hpp"
#include <godot_cpp/variant/utility_functions.hpp>
void CameraEffects::_bind_methods() {
#define CLASSNAME CameraEffects
}
@ -37,7 +36,7 @@ void CameraEffects::_process(double delta [[maybe_unused]]) {
}
float const current_y{this->spring->get_rotation().y};
float diff{gd::Math::wrapf(this->target_rotation - current_y, -M_2_PIf, M_2_PI)};
float diff{gd::Math::wrapf(this->target_rotation - current_y, -(2.f/Math_PI), 2.f/Math_PI)};
float const step(gd::Math::sign(diff) * delta * this->SHOULDER_SWITCH_SPEED);
this->spring->rotate_y(gd::Math::abs(step) < gd::Math::abs(diff) ? step : diff);

View file

@ -37,7 +37,7 @@ void Enemy::_process(double delta) {
// update debuging label
this->set_current_state_name(this->current_state_name);
// rotate towards target (defined by either aiming or navigation)
float const angle_left{gd::Math::wrapf(this->target_rotation - this->get_rotation().y, -M_PIf, M_PIf)};
float const angle_left{gd::Math::wrapf(this->target_rotation - this->get_rotation().y, -Math_PI, Math_PI)};
float const step(gd::Math::sign(angle_left) * delta * (this->anim_tree->get_current_state().begins_with("Run") ? this->TURN_SPEED : this->AIM_SPEED));
if(gd::Math::abs(angle_left) <= gd::Math::abs(step)) {
this->rotate_y(angle_left);
@ -109,11 +109,11 @@ Enemy::ActionFn Enemy::wait_end_of_shot() {
Enemy::ActionFn Enemy::stab() {
this->set_current_state_name("Stab (start)");
this->anim_tree->set_aim_weapon(false);
if(this->anim_tree->get_current_state().begins_with("Aim"))
return (ActionFn)&Enemy::stab;
this->anim_tree->set_stab();
float const target_diff{this->get_global_basis().get_column(2).signed_angle_to(this->last_known_player_position - this->aim_offset_position(), {0.f, 1.f, 0.f})};
this->target_rotation = this->get_global_rotation().y + target_diff;
if(this->anim_tree->get_current_state().begins_with("Aim") || !this->at_target_angle)
return (ActionFn)&Enemy::stab;
this->anim_tree->set_stab();
return (ActionFn)&Enemy::wait_end_of_stab;
}

17
src/enemy_state.cpp Normal file
View file

@ -0,0 +1,17 @@
#include "enemy_state.hpp"
EnemyState const *EnemyState::get_next() {
return this->next_state;
}
void EnemyState::set_next(EnemyState const *state) {
this->next_state = state;
}
EnemyState const *EnemyStateDB::get_state_by_name(gd::StringName name) {
return EnemyStateDB::states.has(name)
? &EnemyStateDB::states.get(name)
: nullptr;
}
gd::HashMap<gd::StringName, EnemyState> EnemyStateDB::states{};

47
src/enemy_state.hpp Normal file
View file

@ -0,0 +1,47 @@
#ifndef ENEMY_STATE_HPP
#define ENEMY_STATE_HPP
#include "enemy.hpp"
#include "godot_cpp/templates/hash_map.hpp"
#include <godot_cpp/classes/ref_counted.hpp>
namespace gd = godot;
class EnemyState {
public:
static gd::StringName get_static_state_name() {
return gd::StringName("EnemyState");
}
virtual gd::StringName get_state_name() const {
return gd::StringName("EnemyState");
}
virtual void process(Enemy *enemy) {}
EnemyState const *get_next();
protected:
void set_next(EnemyState const *state);
private:
EnemyState const *next_state{};
};
#define ENEMY_STATE(m_state_name)\
friend class EnemyStateDB;\
static godot::StringName get_static_state_name() { return godot::StringName(#m_state_name); }\
virtual godot::StringName get_state_name() const { return godot::StringName(#m_state_name); }
class EnemyStateDB {
public:
template<class TState> static void register_state() {
if(!EnemyStateDB::states.has(TState::get_static_state_name())) {
EnemyState state{new TState()};
EnemyStateDB::states.insert(state.get_state_name(), state);
}
}
static EnemyState const *get_state_by_name(gd::StringName name);
template<class TState> static EnemyState const *get_state() {
return EnemyStateDB::get_state_by_name(TState::get_static_state_name());
}
private:
static gd::HashMap<gd::StringName, EnemyState> states;
};
#endif // !ENEMY_STATE_HPP

View file

@ -69,17 +69,22 @@ void Player::process_transform_camera(double delta) {
this->camera_parent->set_global_position(this->get_global_position() + gd::Vector3{0.f, this->camera_height, 0.f});
float const camera_speed{float(delta) * (this->anim_tree->match_tags(PlayerAnimTree::Aim) ? this->AIMING_CAMERA_ROTATION_SPEED : this->CAMERA_ROTATION_SPEED)};
this->camera_parent->global_rotate({0.f, 1.f, 0.f}, this->input_directions.x * -camera_speed);
// collect the forward and left axes from the basis
gd::Basis const basis{this->camera_parent->get_global_basis()};
gd::Vector3 const camera_forward{basis.get_column(2)};
gd::Vector3 const left{basis.get_column(0)};
// get the angle between the camera and character forward around the character's left axis
float const v_angle{camera_forward.signed_angle_to(gd::Vector3{camera_forward.x, 0.f, camera_forward.z}.normalized(), left)};
// calculate a centering force to keep the camera point horizontally
float const max_frame_movement(gd::Math::abs(v_angle) / delta);
float const counter_speed(gd::Math::clamp((v_angle / this->CAMERA_VERTICAL_LIMIT) * this->CAMERA_VERTICAL_ROTATION_SPEED, -max_frame_movement, max_frame_movement));
// calculate force from input
float const input_speed{this->input_look_vertical * this->CAMERA_VERTICAL_ROTATION_SPEED};
// combine input and centering force to produce rotation this frame
this->camera_parent->global_rotate(left, (input_speed + counter_speed) * delta);
}
void Player::process_rotate(double delta) {
void Player::process_rotate(double delta) {
if(this->anim_tree->match_tags(PlayerAnimTree::Tags::Turn)) {
//! the signed angle difference between the left axes of the camera parent and Player
float const diff{-this->camera_parent->get_global_basis().get_column(0).signed_angle_to(this->get_global_basis().get_column(0), {0.f, 1.f, 0.f})};
@ -89,7 +94,7 @@ void Player::process_rotate(double delta) {
float const actual_speed{speed < gd::Math::abs(diff) ? dir * speed : diff};
// rotate by max allowed or full difference, whichever has the smaller magnitude
this->rotate_y(actual_speed);
this->anim_tree->set_target_turn_speed(gd::Math::clamp(diff * 2.f, -1.f, 1.f) * M_PI_2f *0.9f);
this->anim_tree->set_target_turn_speed(gd::Math::clamp(diff * 2.f, -1.f, 1.f) * Math_PI/2.f *0.9f);
}
}

View file

@ -38,7 +38,7 @@ void PlayerAnimTree::_process(double delta) {
}
// rotational root motion
this->parent_3d->set_quaternion(this->get_root_motion_rotation_accumulator());
this->parent_3d->rotate_y(M_PIf);
this->parent_3d->rotate_y(Math_PI);
this->update_tags(this->fsm->get_current_node());
}