wave-survival/modules/wave_survival/weapons/revolver.cpp
2025-07-31 23:03:31 +02:00

74 lines
1.8 KiB
C++

#include "revolver.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/player_camera.h"
#include "wave_survival/player_input.h"
void Revolver::_bind_methods() {
}
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()) {
this->muzzle->shoot();
if (this->alt_active) {
get_anim()->queue("fire_single");
get_anim()->queue("idle_single");
} else {
get_anim()->queue("fire_double");
get_anim()->queue("idle_double");
}
}
}
void Revolver::on_primary_fire(bool pressed) {
if (pressed) {
shoot();
}
}
void Revolver::on_alt_mode(bool pressed) {
this->alt_requested = pressed;
}
void Revolver::ready() {
this->muzzle = cast_to<HitscanMuzzle>(get_node(NodePath("%HitscanMuzzle")));
get_input()->connect(PlayerInput::sig_primary_fire, callable_mp(this, &self_type::on_primary_fire));
get_input()->connect(PlayerInput::sig_alt_mode, callable_mp(this, &self_type::on_alt_mode));
play_equip_anim();
}
void Revolver::process(double delta) {
String const current{ get_anim()->get_current_animation() };
double animation_time{ get_anim()->get_current_animation_position() };
if (current == "fire_single" || current == "fire_double") {
double t{ animation_time / this->recoil_time };
get_camera()->recoil(Math::lerp((double)this->recoil_force, 0.0, CLAMP(t, 0.0, 1.0)) * delta);
}
}
void Revolver::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
set_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
}
}
void Revolver::notify_selected() {
play_equip_anim();
}