feat: updated rifle texture, turning animation

This commit is contained in:
Sara 2024-12-01 21:38:30 +01:00
parent 92f31aa85b
commit efc8006c66
28 changed files with 7376 additions and 114 deletions

View file

@ -6,24 +6,30 @@
void Player::_bind_methods() {
#define CLASSNAME Player
GDFUNCTION(get_input_directions);
GDFUNCTION(get_input_fire);
}
void Player::_ready() {
if(gd::Engine::get_singleton()->is_editor_hint())
return;
this->anim_tree = this->get_node<gd::AnimationTree>("%AnimationTree");
this->sfm = gd::Object::cast_to<gd::AnimationNodeStateMachinePlayback>(this->anim_tree->get("parameters/Actions/playback"));
this->fsm = gd::Object::cast_to<gd::AnimationNodeStateMachinePlayback>(this->anim_tree->get("parameters/Actions/playback"));
this->input = this->get_node<utils::PlayerInput>("%PlayerInput");
this->input->listen_to(utils::PlayerInput::Listener("dir_left", "dir_right", callable_mp(this, &Player::_on_dir_horizontal)));
this->input->listen_to(utils::PlayerInput::Listener("dir_backward", "dir_forward", callable_mp(this, &Player::_on_dir_vertical)));
this->input->listen_to(utils::PlayerInput::Listener("fire", callable_mp(this, &Player::_on_fire)));
this->model_node = this->get_node<gd::Node3D>("%CharacterModel");
this->camera_parent = this->get_node<gd::Node3D>("%CameraParent");
this->camera_parent->set_global_rotation(this->get_global_rotation());
}
void Player::_process(double delta) {
if(gd::Engine::get_singleton()->is_editor_hint())
return;
this->anim_tree->set("parameters/Actions/Walk/Forward/blend_amount", gd::Math::clamp(this->input_directions.y, 0.5f, 1.f));
this->rotate_y(-this->input_directions.x * delta);
if(this->input_fire >= 0.0)
this->input_fire -= delta;
this->process_rotate(delta);
this->process_transform_camera(delta);
}
void Player::_physics_process(double delta [[maybe_unused]]) {
@ -41,6 +47,29 @@ void Player::_physics_process(double delta [[maybe_unused]]) {
this->move_and_slide();
}
void Player::process_transform_camera(double delta) {
this->camera_parent->set_global_position(this->get_global_position());
float const camera_speed{float(delta) * (this->fsm->get_current_node().contains("[aim]") ? this->AIMING_CAMERA_ROTATION_SPEED : this->CAMERA_ROTATION_SPEED)};
this->camera_parent->rotate_y(this->input_directions.x * -camera_speed);
}
void Player::process_rotate(double delta) {
if(this->fsm->get_current_node().contains("[turn]") || this->fsm->get_current_node().contains("[turn_animated]")) {
//! 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});
//! the maximum rotation to allow for this frame
float const speed{float(delta) * this->ROTATION_SPEED};
float const actual_speed{speed < gd::Math::abs(diff) ? gd::Math::sign(diff) * speed : diff};
// rotate by max allowed or full difference, whichever has the smaller magnitude
this->rotate_y(actual_speed);
this->anim_tree->set("parameters/TurnDirection/blend_position", diff / (this->ROTATION_SPEED * .5f));
this->anim_tree->set("parameters/TurnAnimation/blend_amount", this->fsm->get_current_node().contains("[turn_animated]") ? 1.f : 0.f);
} else {
this->anim_tree->set("parameters/TurnAnimation/blend_amount", 0.f);
this->anim_tree->set("parameters/TurnDirection/blend_position", 0.f);
}
}
void Player::_on_dir_horizontal(gd::Ref<gd::InputEvent>, float value) {
this->input_directions.x = value;
}
@ -49,6 +78,18 @@ void Player::_on_dir_vertical(gd::Ref<gd::InputEvent>, float value) {
this->input_directions.y = value;
}
void Player::_on_fire(gd::Ref<gd::InputEvent>, float value) {
if(value > 0.f) {
this->input_fire = 0.5;
}
}
gd::Vector2 Player::get_input_directions() const {
return this->input_directions;
}
bool Player::get_input_fire() {
bool const val = this->input_fire > 0.0;
this->input_fire = 0.0;
return val;
}

View file

@ -15,16 +15,27 @@ public:
virtual void _process(double delta) override;
virtual void _physics_process(double delta) override;
void process_transform_camera(double delta);
void process_rotate(double delta);
void _on_dir_horizontal(gd::Ref<gd::InputEvent>, float value);
void _on_dir_vertical(gd::Ref<gd::InputEvent>, float value);
void _on_fire(gd::Ref<gd::InputEvent>, float value);
gd::Vector2 get_input_directions() const;
bool get_input_fire();
private:
gd::AnimationTree *anim_tree{nullptr};
gd::AnimationNodeStateMachinePlayback *sfm{nullptr};
gd::AnimationNodeStateMachinePlayback *fsm{nullptr};
gd::Node3D *camera_parent{nullptr};
utils::PlayerInput *input{nullptr};
gd::Node3D *model_node{nullptr};
gd::Vector2 input_directions{};
gd::Vector2 input_directions{0.f, 0.f};
double input_fire{0.0};
float const ROTATION_SPEED{1.5f};
float const CAMERA_ROTATION_SPEED{2.f};
float const AIMING_CAMERA_ROTATION_SPEED{1.f};
};
#endif // !TR_PLAYER_HPP