trenches/src/player_anim_tree.cpp

115 lines
3.4 KiB
C++
Raw Normal View History

2024-12-04 15:15:40 +00:00
#include "player_anim_tree.hpp"
#include "utils/godot_macros.hpp"
#include <godot_cpp/variant/utility_functions.hpp>
void PlayerAnimTree::_bind_methods() {
#define CLASSNAME PlayerAnimTree
GDPROPERTY(target_turn_speed, gd::Variant::FLOAT);
GDPROPERTY(is_walking, gd::Variant::FLOAT);
GDPROPERTY(walk_speed, gd::Variant::FLOAT);
GDFUNCTION(get_is_running);
GDPROPERTY(aim_weapon, gd::Variant::BOOL);
GDFUNCTION(get_fire_weapon);
}
void PlayerAnimTree::_ready() {
2024-12-06 16:12:06 +00:00
this->parent_3d = gd::Object::cast_to<gd::Node3D>(this->get_parent());
2024-12-04 15:15:40 +00:00
this->fsm = this->get("parameters/Actions/playback");
}
void PlayerAnimTree::_process(double delta) {
if(gd::Engine::get_singleton()->is_editor_hint())
return;
this->turn_speed = gd::Math::move_toward(this->turn_speed, this->target_turn_speed, float(delta * 30.));
this->commit_turn_speed();
this->update_tags(this->fsm->get_current_node());
this->fire_weapon -= delta;
this->running_time -= delta;
2024-12-06 16:12:06 +00:00
if(this->is_dead && this->death_blend < 1.f) {
this->death_blend = gd::Math::min(this->death_blend + float(delta * this->DEATH_BLEND_SPEED), 1.f);
this->set("parameters/DeathBlend/blend_amount", this->death_blend);
}
this->parent_3d->set_quaternion(this->get_root_motion_rotation_accumulator());
this->parent_3d->rotate_y(M_PIf);
2024-12-04 15:15:40 +00:00
}
void PlayerAnimTree::set_target_turn_speed(float value) {
this->target_turn_speed = value;
}
float PlayerAnimTree::get_target_turn_speed() const {
return this->target_turn_speed;
}
void PlayerAnimTree::set_is_walking(bool value) {
this->is_walking = value;
}
bool PlayerAnimTree::get_is_walking() const {
return this->is_walking;
}
void PlayerAnimTree::set_walk_speed(float value) {
this->walk_speed = value;
this->commit_walk_speed();
}
float PlayerAnimTree::get_walk_speed() const {
return this->walk_speed;
}
void PlayerAnimTree::set_is_running() {
2024-12-06 16:12:06 +00:00
this->running_time = this->RUN_PARAM_DECAY;
2024-12-04 15:15:40 +00:00
}
bool PlayerAnimTree::get_is_running() const {
return this->running_time > 0.0;
}
void PlayerAnimTree::set_aim_weapon(bool value) {
this->aim_weapon = value;
}
bool PlayerAnimTree::get_aim_weapon() const {
return this->aim_weapon;
}
void PlayerAnimTree::set_fire_weapon() {
2024-12-06 16:12:06 +00:00
this->fire_weapon = this->FIRE_PARAM_DECAY;
2024-12-04 15:15:40 +00:00
}
bool PlayerAnimTree::get_fire_weapon() {
bool const is_set = this->fire_weapon > 0.f;
this->fire_weapon = 0.f;
return is_set;
}
2024-12-06 16:12:06 +00:00
void PlayerAnimTree::death_animation() {
this->set("parameters/DeathSeek/request", 0.f);
this->is_dead = true;
}
2024-12-04 15:15:40 +00:00
bool PlayerAnimTree::match_tags(Tags tags) const {
return (this->current_tags & tags) != Tags::None;
}
void PlayerAnimTree::update_tags(gd::StringName const &anim) {
if(anim != this->last_known_anim && this->fsm->get_travel_path().size() <= 1) {
this->last_known_anim = anim;
this->current_tags = Tags::None;
if(anim.contains("[turn]"))
this->current_tags = Tags(this->current_tags | Tags::Turn);
if(anim.contains("[aim]"))
this->current_tags = Tags(this->current_tags | Tags::Aim);
}
}
void PlayerAnimTree::commit_turn_speed() {
this->set("parameters/Actions/Stationary [turn]/Turn/blend_position", this->turn_speed);
}
void PlayerAnimTree::commit_walk_speed() {
this->set("parameters/Actions/Walk [turn]/Speed/blend_amount", this->walk_speed);
this->set("parameters/Actions/Run/Speed/blend_amount", this->walk_speed);
}