feat: implemented run animation for revolver

This commit is contained in:
Sara 2025-08-05 13:38:14 +02:00
parent 66aede32bd
commit 2d720a983a
7 changed files with 109 additions and 23 deletions

View file

@ -1,6 +1,7 @@
#include "revolver.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/macros.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_camera.h"
#include "wave_survival/player_input.h"
@ -10,33 +11,67 @@ void Revolver::_bind_methods() {
BIND_PROPERTY(Variant::FLOAT, double_action_spread);
}
void Revolver::play_equip_anim() {
get_anim()->play("equip", 0.0);
get_anim()->queue("idle_double");
get_anim()->advance(0.0);
}
void Revolver::shoot() {
if (!is_animating()) {
if (this->alt_active) {
get_anim()->queue("fire_single");
this->alt_active = false;
this->muzzle->set_spread(this->single_action_spread);
this->muzzle->shoot();
start_recoil();
} else {
this->muzzle->set_spread(this->double_action_spread);
get_anim()->queue("fire_double");
}
void Revolver::queue_idle_anim() {
if (get_anim()->get_current_animation() == "run") {
stop_running();
}
// set a delay between the end of an action and (re)starting the run animation
this->run_animation_timer = this->run_animation_delay;
if (this->alt_active == true) {
get_anim()->queue("idle_single");
} else {
get_anim()->queue("idle_double");
}
}
void Revolver::start_running() {
if (this->run_animation_timer <= 0.0) {
get_anim()->queue("run");
}
}
void Revolver::stop_running() {
String const current{ get_anim()->get_current_animation() };
if (current == "run") {
get_anim()->stop();
}
if (current.begins_with("run")) {
if (this->alt_active) {
get_anim()->queue("run_to_single");
} else {
get_anim()->queue("run_to_double");
}
}
}
void Revolver::play_equip_anim() {
get_anim()->play("equip", 0.0);
queue_idle_anim();
get_anim()->advance(0.0);
}
void Revolver::shoot() {
if (animation_is_idle()) {
stop_running();
if (this->alt_active) {
this->alt_active = false;
this->muzzle->set_spread(this->single_action_spread);
get_anim()->queue("fire_single");
} else {
this->muzzle->set_spread(this->double_action_spread);
get_anim()->queue("fire_double");
}
queue_idle_anim();
}
}
void Revolver::cock_hammer() {
if (!this->alt_active && !is_animating()) {
if (!this->alt_active && animation_is_idle()) {
stop_running();
this->alt_active = true;
get_anim()->queue("to_single");
get_anim()->queue("idle_single");
queue_idle_anim();
}
}
@ -67,6 +102,18 @@ void Revolver::process(double delta) {
double t{ 1.0 - CLAMP(this->recoil_timer / this->recoil_time, 0.0, 1.0) };
get_camera()->recoil(Math::lerp((double)this->recoil_force, 0.0, t) * delta);
}
if (this->run_animation_timer > 0.0) {
this->run_animation_timer -= delta;
}
if (!is_animating() && get_body()->get_is_running()) {
start_running();
} else if (!get_body()->get_is_running() && get_anim()->get_current_animation() == "run") {
queue_idle_anim();
}
}
bool Revolver::animation_is_idle() const {
return !is_animating() || get_anim()->get_current_animation() == "run";
}
void Revolver::_notification(int what) {