trenches/src/player_anim_tree.cpp

103 lines
2.8 KiB
C++

#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() {
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;
}
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() {
this->running_time = 0.25;
}
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() {
this->fire_weapon = 0.5f;
}
bool PlayerAnimTree::get_fire_weapon() {
bool const is_set = this->fire_weapon > 0.f;
this->fire_weapon = 0.f;
return is_set;
}
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);
}